Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,58 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import logging
|
3 |
+
import gradio as gr
|
4 |
+
from queue import Queue
|
5 |
+
import time
|
6 |
+
|
7 |
+
|
8 |
+
gr.load("models/Sevixdd/roberta-base-finetuned-ner").launch()
|
9 |
+
|
10 |
+
# --- Logging Setup ---
|
11 |
+
logging.basicConfig(filename="chat_log.txt", level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
12 |
+
|
13 |
+
# --- Queue and Metrics ---
|
14 |
+
chat_queue = Queue()
|
15 |
+
response_times = []
|
16 |
+
|
17 |
+
# --- Chat Function ---
|
18 |
+
def chat_function(message, history):
|
19 |
+
try:
|
20 |
+
start_time = time.time() # Record start time for metrics
|
21 |
+
chat_queue.put(message) # Add message to queue
|
22 |
+
logging.info(f"User: {message}")
|
23 |
+
|
24 |
+
# ... (Your chatbot processing logic here) ...
|
25 |
+
# Simulate processing delay
|
26 |
+
time.sleep(2) # Replace with actual processing
|
27 |
+
response = chat_queue.get()
|
28 |
+
logging.info(f"Bot: {response}")
|
29 |
+
|
30 |
+
# --- Metrics Calculation ---
|
31 |
+
end_time = time.time()
|
32 |
+
response_time = end_time - start_time
|
33 |
+
response_times.append(response_time)
|
34 |
+
avg_response_time = sum(response_times) / len(response_times) if response_times else 0
|
35 |
+
|
36 |
+
logging.info(f"Response Time: {response_time:.2f} seconds, Avg: {avg_response_time:.2f} seconds")
|
37 |
+
return response
|
38 |
+
except Exception as e:
|
39 |
+
logging.error(f"Error in chat processing: {e}")
|
40 |
+
return "An error occurred. Please try again."
|
41 |
+
|
42 |
+
# --- Gradio Interface ---
|
43 |
+
with gr.Blocks() as demo:
|
44 |
+
gr.Markdown("## Chat with the Bot")
|
45 |
+
chatbot = gr.ChatInterface(fn=chat_function)
|
46 |
+
|
47 |
+
demo.launch()
|
48 |
+
|
49 |
+
# --- Additional Monitoring (Optional) ---
|
50 |
+
def monitor_queue():
|
51 |
+
while True:
|
52 |
+
queue_size = chat_queue.qsize()
|
53 |
+
logging.info(f"Queue Size: {queue_size}")
|
54 |
+
time.sleep(60) # Check every 60 seconds
|
55 |
|
56 |
+
# Uncomment to enable queue monitoring
|
57 |
+
# import threading
|
58 |
+
# threading.Thread(target=monitor_queue, daemon=True).start()
|