lobrien001 commited on
Commit
82a31e6
·
verified ·
1 Parent(s): ea50176

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -6
app.py CHANGED
@@ -1,10 +1,9 @@
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')
@@ -36,16 +35,31 @@ def chat_function(message, history):
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()
 
 
 
1
  import logging
2
  import gradio as gr
3
  from queue import Queue
4
  import time
5
  from prometheus_client import start_http_server, Counter, Histogram
6
+ import threading
7
 
8
  # --- Prometheus Metrics Setup ---
9
  REQUEST_COUNT = Counter('gradio_request_count', 'Total number of requests')
 
35
  logging.error(f"Error in chat processing: {e}")
36
  return "An error occurred. Please try again."
37
 
38
+ # --- Gradio Interface with Metrics Display ---
39
  with gr.Blocks() as demo:
40
  gr.Markdown("## Chat with the Bot")
41
  chatbot = gr.ChatInterface(fn=chat_function)
42
 
43
+ with gr.Row():
44
+ request_count_display = gr.Textbox(label="Request Count")
45
+ avg_latency_display = gr.Textbox(label="Avg. Response Time (s)")
46
+
47
+ # --- Update Metrics Display ---
48
+ def update_metrics():
49
+ while True:
50
+ request_count_display.update(REQUEST_COUNT.collect()[0].samples[0].value)
51
+ avg_latency_display.update(round(REQUEST_LATENCY.collect()[0].samples[0].value, 2))
52
+ time.sleep(5) # Update every 5 seconds
53
+
54
+ # Start the metrics server in a separate thread
55
+ threading.Thread(target=start_http_server, args=(8000,), daemon=True).start()
56
+ demo.load(update_metrics, None, None, every=1) # Call update_metrics every second after initial page load
57
+
58
+ demo.launch(share=True) # For Hugging Face Spaces, set share=True to make the UI public
59
+
60
 
61
 
62
 
63
 
64
 
65
+ #gr.load("models/Sevixdd/roberta-base-finetuned-ner").launch()