lobrien001 Sevixdd commited on
Commit
69321aa
·
verified ·
1 Parent(s): 0a979ba

update app.py (#5)

Browse files

- update app.py (11610427275b75ca1ece7b61b6ea3182e26ead1c)


Co-authored-by: Sebastian Momir <[email protected]>

Files changed (1) hide show
  1. app.py +136 -79
app.py CHANGED
@@ -7,41 +7,79 @@ 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
- # --- 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,14 +90,13 @@ 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',
@@ -67,26 +104,10 @@ label_mapping = {
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 = []
@@ -99,42 +120,73 @@ def chat_function(index):
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):
@@ -146,20 +198,21 @@ def stress_test(num_requests, index, delay):
146
  for t in threads:
147
  t.join()
148
 
 
149
  # --- Gradio Interface with Background Image and Three Windows ---
150
- with gr.Blocks(css="""
151
- body {
152
- background-image: url("stag.jpeg");
153
- background-size: cover;
154
- background-repeat: no-repeat;
155
- }
156
- """, title="PLOD Filtered with Monitoring") as demo: # Load CSS for background image
157
- with gr.Tab("Chat"):
158
  gr.Markdown("## Chat with the Bot")
159
- index_input = gr.Textbox(label="Enter dataset index:", lines=1)
160
  output = gr.Markdown(label="Response")
161
  chat_interface = gr.Interface(fn=chat_function, inputs=[index_input], outputs=output)
162
- chat_interface.render()
 
 
 
 
 
163
 
164
  with gr.Tab("Model Parameters"):
165
  model_params_display = gr.Textbox(label="Model Parameters", lines=20, interactive=False) # Display model parameters
@@ -191,7 +244,9 @@ body {
191
  stress_test_status.value = f"Stress test failed: {e}"
192
 
193
  stress_test_button.click(run_stress_test, [num_requests_input, index_input_stress, delay_input], stress_test_status)
194
-
 
 
195
  # --- Update Functions ---
196
  def update_metrics(request_count_display, avg_latency_display):
197
  while True:
@@ -214,9 +269,11 @@ body {
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,7 +292,7 @@ body {
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
 
 
7
  import psutil
8
  import random
9
  from transformers import pipeline
10
+ from sklearn.metrics import precision_score, recall_score, f1_score, accuracy_score
11
  import requests
12
  from datasets import load_dataset
13
  import os
14
+ from logging import FileHandler
15
+ from typing import Iterable
16
 
 
 
 
 
 
 
 
 
 
17
 
18
+ # Ensure the log files exist
19
+ log_file_path = 'chat_log.log'
20
+ debug_log_file_path = 'debug.log'
21
+ if not os.path.exists(log_file_path):
22
+ with open(log_file_path, 'w') as f:
23
+ f.write(" ")
24
+ if not os.path.exists(debug_log_file_path):
25
+ with open(debug_log_file_path, 'w') as f:
26
+ f.write(" ")
27
+
28
+
29
+ # Create logger instance
30
+ logger = logging.getLogger()
31
+ logger.setLevel(logging.DEBUG) # Set logger level to the lowest level needed
32
+
33
+ #Create formatter
34
+ formatter = logging.Formatter(
35
+ '%(asctime)s - %(name)s - %(levelname)s - %(message)s', datefmt='%d-%b-%y %H:%M:%S')
36
+
37
+ # Create handlers
38
+ info_handler = FileHandler( filename=log_file_path, mode='w+')
39
+ info_handler.setLevel(logging.INFO)
40
+ info_handler.setFormatter(formatter)
41
+
42
+ debug_handler = FileHandler(filename=debug_log_file_path, mode='w+')
43
+ debug_handler.setLevel(logging.DEBUG)
44
+ debug_handler.setFormatter(formatter)
45
+
46
+
47
+ # Function to capture logs for Gradio display
48
+ class GradioHandler(logging.Handler):
49
+ def __init__(self, logs_queue):
50
+ super().__init__()
51
+ self.logs_queue = logs_queue
52
+
53
+ def emit(self, record):
54
+ log_entry = self.format(record)
55
+ self.logs_queue.put(log_entry)
56
+
57
+ # Create a logs queue
58
+ logs_queue = Queue()
59
+
60
+ # Create and configure Gradio handler
61
+ gradio_handler = GradioHandler(logs_queue)
62
+ gradio_handler.setLevel(logging.INFO)
63
+ gradio_handler.setFormatter(formatter)
64
+
65
+ # Add handlers to the logger
66
+ logger.addHandler(info_handler)
67
+ logger.addHandler(debug_handler)
68
+ logger.addHandler(gradio_handler)
69
 
70
  # Load the model
71
  try:
72
  ner_pipeline = pipeline("ner", model="Sevixdd/roberta-base-finetuned-ner")
73
+ logger.debug("NER pipeline loaded.")
74
  except Exception as e:
75
+ logger.debug(f"Error loading NER pipeline: {e}")
76
 
77
  # Load the dataset
78
  try:
79
  dataset = load_dataset("surrey-nlp/PLOD-filtered")
80
+ logger.debug("Dataset loaded.")
81
  except Exception as e:
82
+ logger.debug(f"Error loading dataset: {e}")
83
 
84
  # --- Prometheus Metrics Setup ---
85
  try:
 
90
  CPU_USAGE = Gauge('system_cpu_usage_percent', 'System CPU usage in percent')
91
  MEM_USAGE = Gauge('system_memory_usage_percent', 'System memory usage in percent')
92
  QUEUE_LENGTH = Gauge('chat_queue_length', 'Length of the chat queue')
93
+ logger.debug("Prometheus metrics setup complete.")
94
  except Exception as e:
95
+ logger.debug(f"Error setting up Prometheus metrics: {e}")
96
 
97
  # --- Queue and Metrics ---
98
  chat_queue = Queue() # Define chat_queue globally
99
 
 
100
  label_mapping = {
101
  0: 'B-O',
102
  1: 'B-AC',
 
104
  4: 'I-LF'
105
  }
106
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
 
108
+ def classification(message):
109
+ # Predict using the model
110
+ ner_results = ner_pipeline(" ".join(message))
111
 
112
  detailed_response = []
113
  model_predicted_labels = []
 
120
  detailed_response.append(f"Token: {token}, Entity: {label_mapping[label_id]}, Score: {score:.4f}")
121
 
122
  response = "\n".join(detailed_response)
 
 
123
  response_size = len(response.encode('utf-8'))
124
  RESPONSE_SIZE.observe(response_size)
125
 
126
  time.sleep(random.uniform(0.5, 2.5)) # Simulate processing time
127
 
128
+ return response, model_predicted_labels
 
 
 
 
 
 
 
 
 
 
 
129
 
130
+ # --- Chat Function with Monitoring ---
131
+ def chat_function(input, datasets):
132
+ logger.debug("Starting chat_function")
133
+ with REQUEST_LATENCY.time():
134
+ REQUEST_COUNT.inc()
135
+ try:
136
+ if input.isnumeric():
137
+ chat_queue.put(input)
138
+ # Get the example from the dataset
139
+ if datasets:
140
+ example = datasets[int(input)]
141
+ else:
142
+ example = dataset['train'][int(input)]
143
+ tokens = example['tokens']
144
+ ground_truth_labels = [label_mapping[label] for label in example['ner_tags']]
145
+
146
+ # Call the classification function
147
+ response, model_predicted_labels = classification(tokens)
148
+
149
+
150
+ # Ensure the model and ground truth labels are the same length for comparison
151
+ model_predicted_labels = model_predicted_labels[:len(ground_truth_labels)]
152
+
153
+ precision = precision_score(ground_truth_labels, model_predicted_labels, average='weighted', zero_division=0)
154
+ recall = recall_score(ground_truth_labels, model_predicted_labels, average='weighted', zero_division=0)
155
+ f1 = f1_score(ground_truth_labels, model_predicted_labels, average='weighted', zero_division=0)
156
+ accuracy = accuracy_score(ground_truth_labels, model_predicted_labels)
157
+
158
+ metrics_response = (f"Precision: {precision:.4f}\n"
159
+ f"Recall: {recall:.4f}\n"
160
+ f"F1 Score: {f1:.4f}\n"
161
+ f"Accuracy: {accuracy:.4f}")
162
+
163
+ full_response = f"**Record**:\nTokens: {tokens}\nGround Truth Labels: {ground_truth_labels}\n\n**Predictions**:\n{response}\n\n**Metrics**:\n{metrics_response}"
164
+ logger.info(f"\nInput details: \n Received index from user: {input} Sending response to user: {full_response}")
165
+ else:
166
+ chat_queue.put(input)
167
+ response, predicted_labels = classification([input])
168
+ full_response = f"Input details: \n**Input Sentence:** {input}\n\n**Predictions**:\n{response}\n\n"
169
+ logger.info(full_response)
170
+
171
  chat_queue.get()
 
172
  return full_response
173
  except Exception as e:
174
  ERROR_COUNT.inc()
175
+ logger.error(f"Error in chat processing: {e}", exc_info=True)
176
  return f"An error occurred. Please try again. Error: {e}"
177
 
178
  # Function to simulate stress test
179
+ def stress_test(num_requests, message, delay):
180
  def send_chat_message():
181
+ try:
182
+ response = requests.post("http://127.0.0.1:7860/api/predict/", json={
183
+ "data": [message],
184
+ "fn_index": 0 # This might need to be updated based on your Gradio app's function index
185
+ })
186
+ logger.debug(f"Request payload: {message}",exc_info=True)
187
+ logger.debug(f"Response: {response.json()}",exc_info=True)
188
+ except Exception as e:
189
+ logger.debug(f"Error during stress test request: {e}", exc_info=True)
190
 
191
  threads = []
192
  for _ in range(num_requests):
 
198
  for t in threads:
199
  t.join()
200
 
201
+
202
  # --- Gradio Interface with Background Image and Three Windows ---
203
+ with gr.Blocks(title="PLOD Filtered with Monitoring") as demo: # Load CSS for background image
204
+
205
+ with gr.Tab("Sentence input"):
 
 
 
 
 
206
  gr.Markdown("## Chat with the Bot")
207
+ index_input = gr.Textbox(label="Enter A sentence:", lines=1)
208
  output = gr.Markdown(label="Response")
209
  chat_interface = gr.Interface(fn=chat_function, inputs=[index_input], outputs=output)
210
+
211
+ with gr.Tab("Dataset and Index Input"):
212
+ gr.Markdown("## Chat with the Bot")
213
+ interface = gr.Interface(fn = chat_function,
214
+ inputs=[gr.Textbox(label="Enter dataset index:", lines=1), gr.UploadButton(label ="Upload Dataset", file_types=[".csv", ".tsv"])],
215
+ outputs = gr.Markdown(label="Response"))
216
 
217
  with gr.Tab("Model Parameters"):
218
  model_params_display = gr.Textbox(label="Model Parameters", lines=20, interactive=False) # Display model parameters
 
244
  stress_test_status.value = f"Stress test failed: {e}"
245
 
246
  stress_test_button.click(run_stress_test, [num_requests_input, index_input_stress, delay_input], stress_test_status)
247
+ img = gr.Image(
248
+ "stag.jpeg", label="Image"
249
+ )
250
  # --- Update Functions ---
251
  def update_metrics(request_count_display, avg_latency_display):
252
  while True:
 
269
 
270
  def update_logs(logs_display):
271
  while True:
272
+ info_log_vector = []
273
+ logs = []
274
+ while not logs_queue.empty():
275
+ logs.append(logs_queue.get())
276
+ logs_display.value = "\n".join(logs[-10:])
277
  time.sleep(1) # Update every 1 second
278
 
279
  def display_model_params(model_params_display):
 
292
  threading.Thread(target=start_http_server, args=(8000,), daemon=True).start()
293
  threading.Thread(target=update_metrics, args=(request_count_display, avg_latency_display), daemon=True).start()
294
  threading.Thread(target=update_usage, args=(cpu_usage_display, mem_usage_display), daemon=True).start()
295
+ threading.Thread(target=update_logs, args=(logs_display), daemon=True).start()
296
  threading.Thread(target=display_model_params, args=(model_params_display,), daemon=True).start()
297
  threading.Thread(target=update_queue_length, daemon=True).start()
298