File size: 1,511 Bytes
a27db0a
 
72871ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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()