Update app.py
Browse files
app.py
CHANGED
@@ -6,12 +6,10 @@ from prometheus_client import start_http_server, Counter, Histogram
|
|
6 |
import threading
|
7 |
import psutil
|
8 |
import random
|
9 |
-
from transformers import pipeline
|
10 |
|
11 |
-
# Load the model
|
12 |
-
|
13 |
-
ner_pipeline = pipeline("ner", model=model_name)
|
14 |
-
model = AutoModelForTokenClassification.from_pretrained(model_name) # Load full model
|
15 |
|
16 |
# --- Prometheus Metrics Setup ---
|
17 |
REQUEST_COUNT = Counter('gradio_request_count', 'Total number of requests')
|
@@ -21,35 +19,93 @@ REQUEST_LATENCY = Histogram('gradio_request_latency_seconds', 'Request latency i
|
|
21 |
logging.basicConfig(filename="chat_log.txt", level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
22 |
|
23 |
# --- Queue and Metrics ---
|
24 |
-
chat_queue = Queue()
|
25 |
|
26 |
# --- Chat Function with Monitoring ---
|
27 |
def chat_function(message, history):
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
demo.launch(share=True)
|
54 |
|
55 |
|
@@ -58,6 +114,19 @@ demo.launch(share=True)
|
|
58 |
|
59 |
|
60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
|
63 |
|
|
|
6 |
import threading
|
7 |
import psutil
|
8 |
import random
|
9 |
+
from transformers import pipeline
|
10 |
|
11 |
+
# Load the model
|
12 |
+
ner_pipeline = pipeline("ner", model="Sevixdd/roberta-base-finetuned-ner")
|
|
|
|
|
13 |
|
14 |
# --- Prometheus Metrics Setup ---
|
15 |
REQUEST_COUNT = Counter('gradio_request_count', 'Total number of requests')
|
|
|
19 |
logging.basicConfig(filename="chat_log.txt", level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
20 |
|
21 |
# --- Queue and Metrics ---
|
22 |
+
chat_queue = Queue() # Define chat_queue globally
|
23 |
|
24 |
# --- Chat Function with Monitoring ---
|
25 |
def chat_function(message, history):
|
26 |
+
with REQUEST_LATENCY.time():
|
27 |
+
REQUEST_COUNT.inc()
|
28 |
+
try:
|
29 |
+
start_time = time.time()
|
30 |
+
chat_queue.put(message)
|
31 |
+
logging.info(f"User: {message}")
|
32 |
+
|
33 |
+
ner_result = ner_pipeline(message)
|
34 |
+
response = f"Response from NER model: {ner_result}"
|
35 |
+
logging.info(f"Bot: {response}")
|
36 |
+
|
37 |
+
time.sleep(random.uniform(0.5, 2.5)) # Simulate processing time
|
38 |
+
|
39 |
+
chat_queue.get()
|
40 |
+
return response
|
41 |
+
except Exception as e:
|
42 |
+
logging.error(f"Error in chat processing: {e}")
|
43 |
+
return "An error occurred. Please try again."
|
44 |
+
|
45 |
+
# --- Gradio Interface with Background Image and Three Windows ---
|
46 |
+
with gr.Blocks(css="""
|
47 |
+
body {
|
48 |
+
background-image: url("stag.jpeg");
|
49 |
+
background-size: cover;
|
50 |
+
background-repeat: no-repeat;
|
51 |
+
}
|
52 |
+
""", title="PLOD Filtered with Monitoring") as demo: # Load CSS for background image
|
53 |
+
with gr.Tab("Chat"):
|
54 |
+
gr.Markdown("## Chat with the Bot")
|
55 |
+
chatbot = gr.ChatInterface(fn=chat_function)
|
56 |
+
|
57 |
+
with gr.Tab("Performance Metrics"):
|
58 |
+
request_count_display = gr.Number(label="Request Count", value=0)
|
59 |
+
avg_latency_display = gr.Number(label="Avg. Response Time (s)", value=0)
|
60 |
+
|
61 |
+
with gr.Tab("Infrastructure"):
|
62 |
+
cpu_usage_display = gr.Number(label="CPU Usage (%)", value=0)
|
63 |
+
mem_usage_display = gr.Number(label="Memory Usage (%)", value=0)
|
64 |
+
|
65 |
+
with gr.Tab("Logs"):
|
66 |
+
logs_display = gr.Textbox(label="Logs", lines=10) # Increased lines for better visibility
|
67 |
+
|
68 |
+
# --- Update Functions ---
|
69 |
+
def update_metrics(request_count_display, avg_latency_display):
|
70 |
+
while True:
|
71 |
+
request_count = REQUEST_COUNT._value.get()
|
72 |
+
latency_samples = REQUEST_LATENCY.collect()[0].samples
|
73 |
+
avg_latency = sum(s.value for s in latency_samples) / len(latency_samples) if latency_samples else 0
|
74 |
+
|
75 |
+
request_count_display.value = request_count
|
76 |
+
avg_latency_display.value = round(avg_latency, 2)
|
77 |
+
|
78 |
+
time.sleep(5) # Update every 5 seconds
|
79 |
+
|
80 |
+
def update_usage(cpu_usage_display, mem_usage_display):
|
81 |
+
while True:
|
82 |
+
cpu_usage_display.value = psutil.cpu_percent()
|
83 |
+
mem_usage_display.value = psutil.virtual_memory().percent
|
84 |
+
time.sleep(5)
|
85 |
+
|
86 |
+
def update_logs(logs_display):
|
87 |
+
while True:
|
88 |
+
with open("chat_log.txt", "r") as log_file:
|
89 |
+
logs = log_file.readlines()
|
90 |
+
logs_display.value = "".join(logs[-10:]) # Display last 10 lines
|
91 |
+
time.sleep(1) # Update every 1 second
|
92 |
+
|
93 |
+
# --- Start Threads ---
|
94 |
+
threading.Thread(target=start_http_server, args=(8000,), daemon=True).start()
|
95 |
+
threading.Thread(target=update_metrics, args=(request_count_display, avg_latency_display), daemon=True).start()
|
96 |
+
threading.Thread(target=update_usage, args=(cpu_usage_display, mem_usage_display), daemon=True).start()
|
97 |
+
threading.Thread(target=update_logs, args=(logs_display,), daemon=True).start()
|
98 |
+
|
99 |
+
# --- Simulate Chat Interactions ---
|
100 |
+
def simulate_interactions():
|
101 |
+
messages = ["Hello bot!", "What's your name?", "Tell me a joke.", "Who are you?"]
|
102 |
+
for msg in messages:
|
103 |
+
chat_function(msg, [])
|
104 |
+
time.sleep(random.uniform(1, 5)) # Random interval between messages
|
105 |
+
|
106 |
+
threading.Thread(target=simulate_interactions, daemon=True).start() # Start simulation
|
107 |
+
|
108 |
+
# Launch the app
|
109 |
demo.launch(share=True)
|
110 |
|
111 |
|
|
|
114 |
|
115 |
|
116 |
|
117 |
+
|
118 |
+
|
119 |
+
|
120 |
+
|
121 |
+
|
122 |
+
|
123 |
+
|
124 |
+
|
125 |
+
|
126 |
+
|
127 |
+
|
128 |
+
|
129 |
+
|
130 |
|
131 |
|
132 |
|