|
import logging |
|
import gradio as gr |
|
from queue import Queue |
|
import time |
|
from prometheus_client import start_http_server, Counter, Histogram |
|
import threading |
|
import psutil |
|
import random |
|
from transformers import pipeline, AutoConfig |
|
|
|
|
|
model_name = "Sevixdd/roberta-base-finetuned-ner" |
|
ner_pipeline = pipeline("ner", model=model_name) |
|
config = AutoConfig.from_pretrained(model_name) |
|
|
|
|
|
REQUEST_COUNT = Counter('gradio_request_count', 'Total requests') |
|
REQUEST_LATENCY = Histogram('gradio_request_latency_seconds', 'Request latency (s)') |
|
|
|
|
|
logging.basicConfig(filename="chat_log.txt", level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') |
|
|
|
|
|
chat_queue = Queue(maxsize=1) |
|
|
|
|
|
def chat_function(message, history): |
|
with REQUEST_LATENCY.time(): |
|
REQUEST_COUNT.inc() |
|
try: |
|
if chat_queue.full(): |
|
return "The model is busy. Please wait..." |
|
|
|
chat_queue.put(message) |
|
logging.info(f"User: {message}") |
|
|
|
ner_result = ner_pipeline(message) |
|
response = f"Response from NER model: {ner_result}" |
|
logging.info(f"Bot: {response}") |
|
|
|
time.sleep(random.uniform(0.5, 2.5)) |
|
|
|
chat_queue.get() |
|
return response |
|
except Exception as e: |
|
logging.error(f"Error: {e}") |
|
return "An error occurred. Please try again later." |
|
|
|
|
|
with gr.Blocks( |
|
css=""" |
|
body { |
|
background-image: url("stag.jpeg"); |
|
background-size: cover; |
|
background-repeat: no-repeat; |
|
} |
|
""", |
|
title="PLOD Filtered with Monitoring" |
|
) as demo: |
|
with gr.Tab("Chat"): |
|
gr.Markdown("## Chat with the Bot") |
|
chatbot = gr.ChatInterface(fn=chat_function) |
|
|
|
with gr.Tab("Model Details"): |
|
gr.Markdown("## Model Configuration") |
|
gr.JSON(value=config.to_dict(), interactive=False) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
threading.Thread(target=start_http_server, args=(8000,), daemon=True).start() |
|
|
|
|
|
|
|
demo.launch(share=True) |
|
|