Update app.py
Browse filesTrying to add some monitoring
app.py
CHANGED
@@ -1,3 +1,49 @@
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
+
|
4 |
+
import logging
|
5 |
+
import gradio as gr
|
6 |
+
from queue import Queue
|
7 |
+
import time
|
8 |
+
from prometheus_client import start_http_server, Counter, Histogram
|
9 |
+
|
10 |
+
# --- Prometheus Metrics Setup ---
|
11 |
+
REQUEST_COUNT = Counter('gradio_request_count', 'Total number of requests')
|
12 |
+
REQUEST_LATENCY = Histogram('gradio_request_latency_seconds', 'Request latency in seconds')
|
13 |
+
|
14 |
+
# --- Logging Setup ---
|
15 |
+
logging.basicConfig(filename="chat_log.txt", level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
16 |
+
|
17 |
+
# --- Queue and Metrics ---
|
18 |
+
chat_queue = Queue()
|
19 |
+
|
20 |
+
# --- Chat Function with Monitoring ---
|
21 |
+
def chat_function(message, history):
|
22 |
+
with REQUEST_LATENCY.time():
|
23 |
+
REQUEST_COUNT.inc()
|
24 |
+
|
25 |
+
try:
|
26 |
+
start_time = time.time()
|
27 |
+
chat_queue.put(message)
|
28 |
+
logging.info(f"User: {message}")
|
29 |
+
|
30 |
+
# ... (Your chatbot processing logic here) ...
|
31 |
+
time.sleep(2) # Simulate processing delay
|
32 |
+
response = chat_queue.get()
|
33 |
+
logging.info(f"Bot: {response}")
|
34 |
+
|
35 |
+
return response
|
36 |
+
except Exception as e:
|
37 |
+
logging.error(f"Error in chat processing: {e}")
|
38 |
+
return "An error occurred. Please try again."
|
39 |
+
|
40 |
+
# --- Gradio Interface ---
|
41 |
+
with gr.Blocks() as demo:
|
42 |
+
gr.Markdown("## Chat with the Bot")
|
43 |
+
chatbot = gr.ChatInterface(fn=chat_function)
|
44 |
+
|
45 |
+
# --- Start Prometheus Metrics Server ---
|
46 |
+
start_http_server(8000) # Expose metrics on port 8000
|
47 |
+
|
48 |
+
gr.load("models/Sevixdd/roberta-base-finetuned-ner").launch()
|
49 |
+
|