RichardForests commited on
Commit
883c2f8
1 Parent(s): 7079c07

Create testapp.py

Browse files
Files changed (1) hide show
  1. testapp.py +78 -0
testapp.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+ import gradio as gr
3
+ from queue import Queue
4
+ import time
5
+ from prometheus_client import start_http_server, Counter, Histogram
6
+ import threading
7
+ import psutil
8
+ import random
9
+ from transformers import pipeline, AutoConfig
10
+
11
+ # Load the model and its configuration
12
+ model_name = "Sevixdd/roberta-base-finetuned-ner" # Make sure this model is available
13
+ ner_pipeline = pipeline("ner", model=model_name)
14
+ config = AutoConfig.from_pretrained(model_name)
15
+
16
+ # --- Prometheus Metrics ---
17
+ REQUEST_COUNT = Counter('gradio_request_count', 'Total requests')
18
+ REQUEST_LATENCY = Histogram('gradio_request_latency_seconds', 'Request latency (s)')
19
+
20
+ # --- Logging ---
21
+ logging.basicConfig(filename="chat_log.txt", level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
22
+
23
+ # --- Chat Queue ---
24
+ chat_queue = Queue(maxsize=1) # Allow only one request at a time
25
+
26
+ # --- Chat Function ---
27
+ def chat_function(message, history):
28
+ with REQUEST_LATENCY.time():
29
+ REQUEST_COUNT.inc()
30
+ try:
31
+ if chat_queue.full():
32
+ return "The model is busy. Please wait..." # More user-friendly message
33
+
34
+ chat_queue.put(message)
35
+ logging.info(f"User: {message}")
36
+
37
+ ner_result = ner_pipeline(message)
38
+ response = f"Response from NER model: {ner_result}"
39
+ logging.info(f"Bot: {response}")
40
+
41
+ time.sleep(random.uniform(0.5, 2.5)) # Simulate processing (adjust as needed)
42
+
43
+ chat_queue.get()
44
+ return response
45
+ except Exception as e:
46
+ logging.error(f"Error: {e}")
47
+ return "An error occurred. Please try again later." # More helpful error message
48
+
49
+ # --- Gradio Interface ---
50
+ with gr.Blocks(
51
+ css="""
52
+ body {
53
+ background-image: url("stag.jpeg");
54
+ background-size: cover;
55
+ background-repeat: no-repeat;
56
+ }
57
+ """,
58
+ title="PLOD Filtered with Monitoring"
59
+ ) as demo:
60
+ with gr.Tab("Chat"):
61
+ gr.Markdown("## Chat with the Bot")
62
+ chatbot = gr.ChatInterface(fn=chat_function)
63
+
64
+ with gr.Tab("Model Details"):
65
+ gr.Markdown("## Model Configuration")
66
+ gr.JSON(value=config.to_dict(), interactive=False)
67
+
68
+ # ... other tabs (Performance Metrics, Infrastructure, Logs) ...
69
+
70
+ # --- Update Functions ---
71
+ # ... (Implement update functions for metrics, usage, and logs here)
72
+
73
+ # --- Background Threads ---
74
+ threading.Thread(target=start_http_server, args=(8000,), daemon=True).start()
75
+ # ... (Threads for metrics, usage, and logs update)
76
+
77
+ # Launch the app
78
+ demo.launch(share=True)