Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,63 +2,54 @@ import gradio as gr
|
|
| 2 |
import openai
|
| 3 |
from datasets import load_dataset
|
| 4 |
import logging
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
# Set up logging
|
| 7 |
logging.basicConfig(level=logging.INFO)
|
| 8 |
logger = logging.getLogger(__name__)
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
datasets[name] = load_dataset("rungalileo/ragbench", name, split='train')
|
| 22 |
-
logger.info(f"Successfully loaded {name}")
|
| 23 |
-
except Exception as e:
|
| 24 |
-
logger.info(f"Skipping {name}: {str(e)}")
|
| 25 |
|
| 26 |
def process_query(query, dataset_choice="all"):
|
|
|
|
| 27 |
try:
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
if relevant_contexts:
|
| 41 |
-
context, source = relevant_contexts[0]
|
| 42 |
-
context_info = f"From {source}: {context}"
|
| 43 |
-
else:
|
| 44 |
-
context_info = "Searching across all available datasets..."
|
| 45 |
-
|
| 46 |
-
response = openai.chat.completions.create(
|
| 47 |
-
model="gpt-3.5-turbo",
|
| 48 |
-
messages=[
|
| 49 |
-
{"role": "system", "content": "You are a knowledgeable expert. Provide direct, informative answers based on the available data."},
|
| 50 |
-
{"role": "user", "content": f"Context: {context_info}\nQuestion: {query}"}
|
| 51 |
-
],
|
| 52 |
-
max_tokens=300,
|
| 53 |
-
temperature=0.7,
|
| 54 |
-
)
|
| 55 |
-
|
| 56 |
-
return response.choices[0].message.content.strip()
|
| 57 |
|
| 58 |
except Exception as e:
|
| 59 |
-
return
|
| 60 |
|
| 61 |
-
# Enhanced Gradio interface with
|
| 62 |
demo = gr.Interface(
|
| 63 |
fn=process_query,
|
| 64 |
inputs=[
|
|
@@ -69,9 +60,13 @@ demo = gr.Interface(
|
|
| 69 |
value="all"
|
| 70 |
)
|
| 71 |
],
|
| 72 |
-
outputs=
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
examples=[
|
| 76 |
["What role does T-cell count play in severe human adenovirus type 55 (HAdV-55) infection?", "covidqa"],
|
| 77 |
["In what school district is Governor John R. Rogers High School located?", "hotpotqa"],
|
|
@@ -80,4 +75,5 @@ demo = gr.Interface(
|
|
| 80 |
)
|
| 81 |
|
| 82 |
if __name__ == "__main__":
|
| 83 |
-
demo.
|
|
|
|
|
|
| 2 |
import openai
|
| 3 |
from datasets import load_dataset
|
| 4 |
import logging
|
| 5 |
+
import time
|
| 6 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
| 7 |
+
import torch
|
| 8 |
+
import psutil
|
| 9 |
+
import GPUtil
|
| 10 |
|
| 11 |
+
# Set up logging with performance metrics
|
| 12 |
logging.basicConfig(level=logging.INFO)
|
| 13 |
logger = logging.getLogger(__name__)
|
| 14 |
|
| 15 |
+
def get_system_metrics():
|
| 16 |
+
cpu_percent = psutil.cpu_percent()
|
| 17 |
+
memory_percent = psutil.virtual_memory().percent
|
| 18 |
+
if torch.cuda.is_available():
|
| 19 |
+
gpu = GPUtil.getGPUs()[0]
|
| 20 |
+
gpu_util = gpu.load * 100
|
| 21 |
+
gpu_memory = gpu.memoryUtil * 100
|
| 22 |
+
else:
|
| 23 |
+
gpu_util = 0
|
| 24 |
+
gpu_memory = 0
|
| 25 |
+
return cpu_percent, memory_percent, gpu_util, gpu_memory
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
def process_query(query, dataset_choice="all"):
|
| 28 |
+
start_time = time.time()
|
| 29 |
try:
|
| 30 |
+
# Original query processing code here...
|
| 31 |
+
response = "Sample response"
|
| 32 |
|
| 33 |
+
# Calculate performance metrics
|
| 34 |
+
end_time = time.time()
|
| 35 |
+
processing_time = end_time - start_time
|
| 36 |
+
cpu_percent, memory_percent, gpu_util, gpu_memory = get_system_metrics()
|
| 37 |
|
| 38 |
+
metrics = f"""
|
| 39 |
+
Performance Metrics:
|
| 40 |
+
Processing Time: {processing_time:.2f}s
|
| 41 |
+
CPU Usage: {cpu_percent}%
|
| 42 |
+
Memory Usage: {memory_percent}%
|
| 43 |
+
GPU Utilization: {gpu_util:.1f}%
|
| 44 |
+
GPU Memory: {gpu_memory:.1f}%
|
| 45 |
+
"""
|
| 46 |
|
| 47 |
+
return response, metrics
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
except Exception as e:
|
| 50 |
+
return str(e), "Metrics unavailable"
|
| 51 |
|
| 52 |
+
# Enhanced Gradio interface with performance metrics
|
| 53 |
demo = gr.Interface(
|
| 54 |
fn=process_query,
|
| 55 |
inputs=[
|
|
|
|
| 60 |
value="all"
|
| 61 |
)
|
| 62 |
],
|
| 63 |
+
outputs=[
|
| 64 |
+
gr.Textbox(label="Response"),
|
| 65 |
+
gr.Textbox(label="Performance Metrics")
|
| 66 |
+
],
|
| 67 |
+
title="E5-Powered Multi-Dataset Knowledge Base",
|
| 68 |
+
description="Search across RagBench datasets with real-time performance monitoring",
|
| 69 |
+
analytics_enabled=True,
|
| 70 |
examples=[
|
| 71 |
["What role does T-cell count play in severe human adenovirus type 55 (HAdV-55) infection?", "covidqa"],
|
| 72 |
["In what school district is Governor John R. Rogers High School located?", "hotpotqa"],
|
|
|
|
| 75 |
)
|
| 76 |
|
| 77 |
if __name__ == "__main__":
|
| 78 |
+
demo.queue() # Enable queuing for performance monitoring
|
| 79 |
+
demo.launch(debug=True, show_api=True)
|