lobrien001 commited on
Commit
72871ca
·
verified ·
1 Parent(s): 99dde5b

Update app.py

Browse files

Trying to add some monitoring

Files changed (1) hide show
  1. app.py +47 -1
app.py CHANGED
@@ -1,3 +1,49 @@
1
  import gradio as gr
2
 
3
- gr.load("models/Sevixdd/roberta-base-finetuned-ner").launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+