Update app.py
Browse files
app.py
CHANGED
@@ -7,65 +7,41 @@ import threading
|
|
7 |
import psutil
|
8 |
import random
|
9 |
from transformers import pipeline
|
10 |
-
from sklearn.metrics import precision_score, recall_score, f1_score
|
11 |
import requests
|
12 |
from datasets import load_dataset
|
13 |
import os
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
with open(debug_log_file_path, 'w') as f:
|
25 |
-
f.write("")
|
26 |
-
|
27 |
-
|
28 |
|
29 |
# --- Logging Setup ---
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
logger.addHandler(file_handler)
|
36 |
-
|
37 |
-
|
38 |
-
debugger = logging.getLogger('debug')
|
39 |
-
debugger.setLevel(logging.DEBUG)
|
40 |
-
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
|
41 |
-
file_handler = logging.FileHandler(debug_log_file_path)
|
42 |
-
file_handler.setFormatter(formatter)
|
43 |
-
|
44 |
-
stream_handler = logging.StreamHandler()
|
45 |
-
stream_handler.setFormatter(formatter)
|
46 |
-
|
47 |
-
debugger.addHandler(file_handler)
|
48 |
-
debugger.addHandler(stream_handler)
|
49 |
-
|
50 |
-
# try:
|
51 |
-
# logging.basicConfig(filename=debug_log_file_path, level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
|
52 |
-
# logging.debug("Logging setup complete.")
|
53 |
-
# except Exception as e:
|
54 |
-
# print(f"Error setting up logging: {e}")
|
55 |
|
56 |
# Load the model
|
57 |
try:
|
58 |
ner_pipeline = pipeline("ner", model="Sevixdd/roberta-base-finetuned-ner")
|
59 |
-
|
60 |
except Exception as e:
|
61 |
-
|
62 |
|
63 |
# Load the dataset
|
64 |
try:
|
65 |
dataset = load_dataset("surrey-nlp/PLOD-filtered")
|
66 |
-
|
67 |
except Exception as e:
|
68 |
-
|
69 |
|
70 |
# --- Prometheus Metrics Setup ---
|
71 |
try:
|
@@ -76,16 +52,41 @@ try:
|
|
76 |
CPU_USAGE = Gauge('system_cpu_usage_percent', 'System CPU usage in percent')
|
77 |
MEM_USAGE = Gauge('system_memory_usage_percent', 'System memory usage in percent')
|
78 |
QUEUE_LENGTH = Gauge('chat_queue_length', 'Length of the chat queue')
|
79 |
-
|
80 |
except Exception as e:
|
81 |
-
|
82 |
|
83 |
# --- Queue and Metrics ---
|
84 |
chat_queue = Queue() # Define chat_queue globally
|
85 |
|
86 |
-
|
87 |
-
|
88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
|
90 |
detailed_response = []
|
91 |
model_predicted_labels = []
|
@@ -94,76 +95,46 @@ def classification(message):
|
|
94 |
score = result['score']
|
95 |
entity = result['entity']
|
96 |
label_id = int(entity.split('_')[-1]) # Extract numeric label from entity
|
97 |
-
model_predicted_labels.append(label_id)
|
98 |
-
detailed_response.append(f"Token: {token}, Entity: {
|
99 |
|
100 |
response = "\n".join(detailed_response)
|
|
|
101 |
|
102 |
response_size = len(response.encode('utf-8'))
|
103 |
RESPONSE_SIZE.observe(response_size)
|
104 |
|
105 |
time.sleep(random.uniform(0.5, 2.5)) # Simulate processing time
|
106 |
|
107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
|
109 |
-
# --- Chat Function with Monitoring ---
|
110 |
-
def chat_function(input):
|
111 |
-
debugger.debug("Starting chat_function")
|
112 |
-
with REQUEST_LATENCY.time():
|
113 |
-
REQUEST_COUNT.inc()
|
114 |
-
try:
|
115 |
-
if input.isnumeric():
|
116 |
-
chat_queue.put(input)
|
117 |
-
# Get the example from the dataset
|
118 |
-
example = dataset['train'][int(input)]
|
119 |
-
tokens = example['tokens']
|
120 |
-
ground_truth_labels = example['ner_tags']
|
121 |
-
|
122 |
-
# Call the classification function
|
123 |
-
response, model_predicted_labels = classification(tokens)
|
124 |
-
|
125 |
-
|
126 |
-
# Ensure the model and ground truth labels are the same length for comparison
|
127 |
-
model_predicted_labels = model_predicted_labels[:len(ground_truth_labels)]
|
128 |
-
|
129 |
-
precision = precision_score(ground_truth_labels, model_predicted_labels, average='weighted', zero_division=0)
|
130 |
-
recall = recall_score(ground_truth_labels, model_predicted_labels, average='weighted', zero_division=0)
|
131 |
-
f1 = f1_score(ground_truth_labels, model_predicted_labels, average='weighted', zero_division=0)
|
132 |
-
accuracy = accuracy_score(ground_truth_labels, model_predicted_labels)
|
133 |
-
|
134 |
-
metrics_response = (f"Precision: {precision:.4f}\n"
|
135 |
-
f"Recall: {recall:.4f}\n"
|
136 |
-
f"F1 Score: {f1:.4f}\n"
|
137 |
-
f"Accuracy: {accuracy:.4f}")
|
138 |
-
|
139 |
-
full_response = f"**Record**:\nTokens: {tokens}\nGround Truth Labels: {ground_truth_labels}\n\n**Predictions**:\n{response}\n\n**Metrics**:\n{metrics_response}"
|
140 |
-
logger.info(f"Input details: \n Received index from user: {input} Sending response to user: {full_response}")
|
141 |
-
else:
|
142 |
-
chat_queue.put(input)
|
143 |
-
full_response = classification([input])
|
144 |
-
debugger.debug(f"Full response: {full_response}")
|
145 |
-
logger.info(f"Input details: \nInput Sentence: {input}\n\n**Predictions**:\n{full_response}\n\n")
|
146 |
-
|
147 |
chat_queue.get()
|
148 |
-
|
149 |
return full_response
|
150 |
except Exception as e:
|
151 |
ERROR_COUNT.inc()
|
152 |
-
|
153 |
return f"An error occurred. Please try again. Error: {e}"
|
154 |
|
155 |
# Function to simulate stress test
|
156 |
-
def stress_test(num_requests,
|
157 |
def send_chat_message():
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
debugger.debug(f"Request payload: {message}")
|
164 |
-
debugger.debug(f"Response: {response.json()}")
|
165 |
-
except Exception as e:
|
166 |
-
debugger.error(f"Error during stress test request: {e}", exc_info=True)
|
167 |
|
168 |
threads = []
|
169 |
for _ in range(num_requests):
|
@@ -243,12 +214,9 @@ body {
|
|
243 |
|
244 |
def update_logs(logs_display):
|
245 |
while True:
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
info_log_vector.append(line)
|
250 |
-
debugger.debug(info_log_vector)
|
251 |
-
logs_display.value = info_log_vector # Display last 10 lines
|
252 |
time.sleep(1) # Update every 1 second
|
253 |
|
254 |
def display_model_params(model_params_display):
|
@@ -267,9 +235,9 @@ body {
|
|
267 |
threading.Thread(target=start_http_server, args=(8000,), daemon=True).start()
|
268 |
threading.Thread(target=update_metrics, args=(request_count_display, avg_latency_display), daemon=True).start()
|
269 |
threading.Thread(target=update_usage, args=(cpu_usage_display, mem_usage_display), daemon=True).start()
|
270 |
-
threading.Thread(target=update_logs, args=(logs_display), daemon=True).start()
|
271 |
threading.Thread(target=display_model_params, args=(model_params_display,), daemon=True).start()
|
272 |
threading.Thread(target=update_queue_length, daemon=True).start()
|
273 |
|
274 |
# Launch the app
|
275 |
-
demo.launch(share=True)
|
|
|
7 |
import psutil
|
8 |
import random
|
9 |
from transformers import pipeline
|
10 |
+
from sklearn.metrics import precision_score, recall_score, f1_score
|
11 |
import requests
|
12 |
from datasets import load_dataset
|
13 |
import os
|
14 |
|
15 |
+
# --- Ensure chat_log.txt exists ---
|
16 |
+
log_file = "chat_log.txt"
|
17 |
+
try:
|
18 |
+
if not os.path.exists(log_file):
|
19 |
+
with open(log_file, 'w') as f:
|
20 |
+
f.write("Log file created.\n") # Write a simple message to the log file
|
21 |
+
print(f"{log_file} is ready for logging.")
|
22 |
+
except Exception as e:
|
23 |
+
print(f"Error creating log file: {e}")
|
|
|
|
|
|
|
|
|
24 |
|
25 |
# --- Logging Setup ---
|
26 |
+
try:
|
27 |
+
logging.basicConfig(filename=log_file, level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
|
28 |
+
logging.debug("Logging setup complete.")
|
29 |
+
except Exception as e:
|
30 |
+
print(f"Error setting up logging: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
# Load the model
|
33 |
try:
|
34 |
ner_pipeline = pipeline("ner", model="Sevixdd/roberta-base-finetuned-ner")
|
35 |
+
logging.debug("NER pipeline loaded.")
|
36 |
except Exception as e:
|
37 |
+
logging.error(f"Error loading NER pipeline: {e}")
|
38 |
|
39 |
# Load the dataset
|
40 |
try:
|
41 |
dataset = load_dataset("surrey-nlp/PLOD-filtered")
|
42 |
+
logging.debug("Dataset loaded.")
|
43 |
except Exception as e:
|
44 |
+
logging.error(f"Error loading dataset: {e}")
|
45 |
|
46 |
# --- Prometheus Metrics Setup ---
|
47 |
try:
|
|
|
52 |
CPU_USAGE = Gauge('system_cpu_usage_percent', 'System CPU usage in percent')
|
53 |
MEM_USAGE = Gauge('system_memory_usage_percent', 'System memory usage in percent')
|
54 |
QUEUE_LENGTH = Gauge('chat_queue_length', 'Length of the chat queue')
|
55 |
+
logging.debug("Prometheus metrics setup complete.")
|
56 |
except Exception as e:
|
57 |
+
logging.error(f"Error setting up Prometheus metrics: {e}")
|
58 |
|
59 |
# --- Queue and Metrics ---
|
60 |
chat_queue = Queue() # Define chat_queue globally
|
61 |
|
62 |
+
# Label mapping
|
63 |
+
label_mapping = {
|
64 |
+
0: 'B-O',
|
65 |
+
1: 'B-AC',
|
66 |
+
3: 'B-LF',
|
67 |
+
4: 'I-LF'
|
68 |
+
}
|
69 |
+
|
70 |
+
# --- Chat Function with Monitoring ---
|
71 |
+
def chat_function(index):
|
72 |
+
logging.debug("Starting chat_function")
|
73 |
+
with REQUEST_LATENCY.time():
|
74 |
+
REQUEST_COUNT.inc()
|
75 |
+
try:
|
76 |
+
chat_queue.put(index)
|
77 |
+
logging.info(f"Received index from user: {index}")
|
78 |
+
|
79 |
+
# Get the example from the dataset
|
80 |
+
example = dataset['train'][int(index)]
|
81 |
+
tokens = example['tokens']
|
82 |
+
ground_truth_labels = [label_mapping[label] for label in example['ner_tags']]
|
83 |
+
|
84 |
+
logging.info(f"Tokens: {tokens}")
|
85 |
+
logging.info(f"Ground Truth Labels: {ground_truth_labels}")
|
86 |
+
|
87 |
+
# Predict using the model
|
88 |
+
ner_results = ner_pipeline(" ".join(tokens))
|
89 |
+
logging.debug(f"NER results: {ner_results}")
|
90 |
|
91 |
detailed_response = []
|
92 |
model_predicted_labels = []
|
|
|
95 |
score = result['score']
|
96 |
entity = result['entity']
|
97 |
label_id = int(entity.split('_')[-1]) # Extract numeric label from entity
|
98 |
+
model_predicted_labels.append(label_mapping[label_id])
|
99 |
+
detailed_response.append(f"Token: {token}, Entity: {label_mapping[label_id]}, Score: {score:.4f}")
|
100 |
|
101 |
response = "\n".join(detailed_response)
|
102 |
+
logging.info(f"Generated response: {response}")
|
103 |
|
104 |
response_size = len(response.encode('utf-8'))
|
105 |
RESPONSE_SIZE.observe(response_size)
|
106 |
|
107 |
time.sleep(random.uniform(0.5, 2.5)) # Simulate processing time
|
108 |
|
109 |
+
# Ensure the model and ground truth labels are the same length for comparison
|
110 |
+
model_predicted_labels = model_predicted_labels[:len(ground_truth_labels)]
|
111 |
+
|
112 |
+
precision = precision_score(ground_truth_labels, model_predicted_labels, average='weighted', zero_division=0)
|
113 |
+
recall = recall_score(ground_truth_labels, model_predicted_labels, average='weighted', zero_division=0)
|
114 |
+
f1 = f1_score(ground_truth_labels, model_predicted_labels, average='weighted', zero_division=0)
|
115 |
+
|
116 |
+
metrics_response = (f"Precision: {precision:.4f}\n"
|
117 |
+
f"Recall: {recall:.4f}\n"
|
118 |
+
f"F1 Score: {f1:.4f}")
|
119 |
+
|
120 |
+
full_response = f"**Record**:\nTokens: {tokens}\nGround Truth Labels: {ground_truth_labels}\n\n**Predictions**:\n{response}\n\n**Metrics**:\n{metrics_response}"
|
121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
chat_queue.get()
|
123 |
+
logging.debug("Finished processing message")
|
124 |
return full_response
|
125 |
except Exception as e:
|
126 |
ERROR_COUNT.inc()
|
127 |
+
logging.error(f"Error in chat processing: {e}", exc_info=True)
|
128 |
return f"An error occurred. Please try again. Error: {e}"
|
129 |
|
130 |
# Function to simulate stress test
|
131 |
+
def stress_test(num_requests, index, delay):
|
132 |
def send_chat_message():
|
133 |
+
response = requests.post("http://127.0.0.1:7860/api/predict/", json={
|
134 |
+
"data": [index],
|
135 |
+
"fn_index": 0 # This might need to be updated based on your Gradio app's function index
|
136 |
+
})
|
137 |
+
logging.debug(response.json())
|
|
|
|
|
|
|
|
|
138 |
|
139 |
threads = []
|
140 |
for _ in range(num_requests):
|
|
|
214 |
|
215 |
def update_logs(logs_display):
|
216 |
while True:
|
217 |
+
with open(log_file, "r") as log_file_handler:
|
218 |
+
logs = log_file_handler.readlines()
|
219 |
+
logs_display.value = "".join(logs[-10:]) # Display last 10 lines
|
|
|
|
|
|
|
220 |
time.sleep(1) # Update every 1 second
|
221 |
|
222 |
def display_model_params(model_params_display):
|
|
|
235 |
threading.Thread(target=start_http_server, args=(8000,), daemon=True).start()
|
236 |
threading.Thread(target=update_metrics, args=(request_count_display, avg_latency_display), daemon=True).start()
|
237 |
threading.Thread(target=update_usage, args=(cpu_usage_display, mem_usage_display), daemon=True).start()
|
238 |
+
threading.Thread(target=update_logs, args=(logs_display,), daemon=True).start()
|
239 |
threading.Thread(target=display_model_params, args=(model_params_display,), daemon=True).start()
|
240 |
threading.Thread(target=update_queue_length, daemon=True).start()
|
241 |
|
242 |
# Launch the app
|
243 |
+
demo.launch(share=True)
|