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

Update app.py

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