Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,9 @@ from queue import Queue
|
|
4 |
import time
|
5 |
from prometheus_client import start_http_server, Counter, Histogram
|
6 |
import threading
|
|
|
7 |
|
|
|
8 |
gr.load("models/Sevixdd/roberta-base-finetuned-ner").launch()
|
9 |
|
10 |
# --- Prometheus Metrics Setup ---
|
@@ -37,27 +39,42 @@ def chat_function(message, history):
|
|
37 |
logging.error(f"Error in chat processing: {e}")
|
38 |
return "An error occurred. Please try again."
|
39 |
|
40 |
-
# --- Gradio Interface with
|
41 |
-
with gr.Blocks() as demo:
|
42 |
-
gr.
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
threading.Thread(target=start_http_server, args=(8000,), daemon=True).start()
|
58 |
-
|
|
|
|
|
|
|
|
|
59 |
|
60 |
-
#demo.launch(share=True) # For Hugging Face Spaces, set share=True to make the UI public
|
61 |
|
62 |
|
63 |
|
|
|
4 |
import time
|
5 |
from prometheus_client import start_http_server, Counter, Histogram
|
6 |
import threading
|
7 |
+
import psutil
|
8 |
|
9 |
+
# Load the model
|
10 |
gr.load("models/Sevixdd/roberta-base-finetuned-ner").launch()
|
11 |
|
12 |
# --- Prometheus Metrics Setup ---
|
|
|
39 |
logging.error(f"Error in chat processing: {e}")
|
40 |
return "An error occurred. Please try again."
|
41 |
|
42 |
+
# --- Gradio Interface with Three Windows ---
|
43 |
+
with gr.Blocks(title="PLOD Filtered with Monitoring") as demo:
|
44 |
+
with gr.Tab("Chat"):
|
45 |
+
gr.Markdown("## Chat with the Bot")
|
46 |
+
chatbot = gr.ChatInterface(fn=chat_function)
|
47 |
+
|
48 |
+
with gr.Tab("Performance Metrics"):
|
49 |
+
with gr.Row():
|
50 |
+
request_count_display = gr.Textbox(label="Request Count")
|
51 |
+
avg_latency_display = gr.Textbox(label="Avg. Response Time (s)")
|
52 |
+
|
53 |
+
def update_metrics():
|
54 |
+
while True:
|
55 |
+
request_count_display.update(REQUEST_COUNT.collect()[0].samples[0].value)
|
56 |
+
avg_latency_display.update(round(REQUEST_LATENCY.collect()[0].samples[0].value, 2))
|
57 |
+
time.sleep(5) # Update every 5 seconds
|
58 |
+
|
59 |
+
with gr.Tab("Resource Usage"):
|
60 |
+
with gr.Row():
|
61 |
+
cpu_usage_display = gr.Textbox(label="CPU Usage (%)")
|
62 |
+
mem_usage_display = gr.Textbox(label="Memory Usage (%)")
|
63 |
+
|
64 |
+
def update_usage():
|
65 |
+
while True:
|
66 |
+
cpu_usage_display.update(psutil.cpu_percent())
|
67 |
+
mem_usage_display.update(psutil.virtual_memory().percent)
|
68 |
+
time.sleep(5)
|
69 |
+
|
70 |
+
# --- Start Threads ---
|
71 |
threading.Thread(target=start_http_server, args=(8000,), daemon=True).start()
|
72 |
+
threading.Thread(target=update_metrics, daemon=True).start()
|
73 |
+
threading.Thread(target=update_usage, daemon=True).start()
|
74 |
+
|
75 |
+
# Launch the app
|
76 |
+
demo.launch(share=True)
|
77 |
|
|
|
78 |
|
79 |
|
80 |
|