|
import gradio as gr |
|
|
|
|
|
import logging |
|
import gradio as gr |
|
from queue import Queue |
|
import time |
|
from prometheus_client import start_http_server, Counter, Histogram |
|
|
|
|
|
REQUEST_COUNT = Counter('gradio_request_count', 'Total number of requests') |
|
REQUEST_LATENCY = Histogram('gradio_request_latency_seconds', 'Request latency in seconds') |
|
|
|
|
|
logging.basicConfig(filename="chat_log.txt", level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') |
|
|
|
|
|
chat_queue = Queue() |
|
|
|
|
|
def chat_function(message, history): |
|
with REQUEST_LATENCY.time(): |
|
REQUEST_COUNT.inc() |
|
|
|
try: |
|
start_time = time.time() |
|
chat_queue.put(message) |
|
logging.info(f"User: {message}") |
|
|
|
|
|
time.sleep(2) |
|
response = chat_queue.get() |
|
logging.info(f"Bot: {response}") |
|
|
|
return response |
|
except Exception as e: |
|
logging.error(f"Error in chat processing: {e}") |
|
return "An error occurred. Please try again." |
|
|
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown("## Chat with the Bot") |
|
chatbot = gr.ChatInterface(fn=chat_function) |
|
|
|
|
|
start_http_server(8000) |
|
|
|
gr.load("models/Sevixdd/roberta-base-finetuned-ner").launch() |
|
|
|
|