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 # --- Prometheus Metrics Setup --- REQUEST_COUNT = Counter('gradio_request_count', 'Total number of requests') REQUEST_LATENCY = Histogram('gradio_request_latency_seconds', 'Request latency in seconds') # --- Logging Setup --- logging.basicConfig(filename="chat_log.txt", level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') # --- Queue and Metrics --- chat_queue = Queue() # --- Chat Function with Monitoring --- 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}") # ... (Your chatbot processing logic here) ... time.sleep(2) # Simulate processing delay 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." # --- Gradio Interface --- with gr.Blocks() as demo: gr.Markdown("## Chat with the Bot") chatbot = gr.ChatInterface(fn=chat_function) # --- Start Prometheus Metrics Server --- start_http_server(8000) # Expose metrics on port 8000 gr.load("models/Sevixdd/roberta-base-finetuned-ner").launch()