ajalisatgi commited on
Commit
2e77d5f
·
verified ·
1 Parent(s): a130567

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -38
app.py CHANGED
@@ -7,52 +7,83 @@ from langchain_community.embeddings import HuggingFaceEmbeddings
7
  import torch
8
  import psutil
9
  import GPUtil
10
- # Set up logging with performance metrics
 
11
  logging.basicConfig(level=logging.INFO)
12
  logger = logging.getLogger(__name__)
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  def get_system_metrics():
15
- cpu_percent = psutil.cpu_percent()
16
- memory_percent = psutil.virtual_memory().percent
17
- if torch.cuda.is_available():
18
- gpu = GPUtil.getGPUs()[0]
19
- gpu_util = gpu.load * 100
20
- gpu_memory = gpu.memoryUtil * 100
21
- else:
22
- gpu_util = 0
23
- gpu_memory = 0
24
- return cpu_percent, memory_percent, gpu_util, gpu_memory
25
 
26
  def process_query(query, dataset_choice="all"):
27
  start_time = time.time()
28
  try:
29
- # Original query processing code here...
30
- response = "Sample response"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
- # Calculate performance metrics
33
- end_time = time.time()
34
- processing_time = end_time - start_time
35
- cpu_percent, memory_percent, gpu_util, gpu_memory = get_system_metrics()
36
 
37
- metrics = f"""
38
- Performance Metrics:
39
- Processing Time: {processing_time:.2f}s
40
- CPU Usage: {cpu_percent}%
41
- Memory Usage: {memory_percent}%
42
- GPU Utilization: {gpu_util:.1f}%
43
- GPU Memory: {gpu_memory:.1f}%
44
  """
45
 
46
- return response, metrics
47
 
48
  except Exception as e:
49
- return str(e), "Metrics unavailable"
50
 
51
- # Enhanced Gradio interface with performance metrics
52
  demo = gr.Interface(
53
  fn=process_query,
54
  inputs=[
55
- gr.Textbox(label="Question", placeholder="Ask any question..."),
56
  gr.Dropdown(
57
  choices=["all"] + dataset_names,
58
  label="Select Dataset",
@@ -63,16 +94,10 @@ demo = gr.Interface(
63
  gr.Textbox(label="Response"),
64
  gr.Textbox(label="Performance Metrics")
65
  ],
66
- title="E5-Powered Multi-Dataset Knowledge Base",
67
- description="Search across RagBench datasets with real-time performance monitoring",
68
- analytics_enabled=True,
69
- examples=[
70
- ["What role does T-cell count play in severe human adenovirus type 55 (HAdV-55) infection?", "covidqa"],
71
- ["In what school district is Governor John R. Rogers High School located?", "hotpotqa"],
72
- ["What are the key financial metrics for Q3?", "finqa"]
73
- ]
74
  )
75
 
76
  if __name__ == "__main__":
77
- demo.queue() # Enable queuing for performance monitoring
78
- demo.launch(debug=True, show_api=True)
 
7
  import torch
8
  import psutil
9
  import GPUtil
10
+
11
+ # Set up logging
12
  logging.basicConfig(level=logging.INFO)
13
  logger = logging.getLogger(__name__)
14
 
15
+ # Initialize OpenAI API key
16
+ openai.api_key = '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'
17
+
18
+ # Initialize with E5 embedding model
19
+ model_name = 'intfloat/e5-base-v2'
20
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
21
+ embedding_model = HuggingFaceEmbeddings(model_name=model_name)
22
+ embedding_model.client.to(device)
23
+
24
+ # Load datasets
25
+ datasets = {}
26
+ dataset_names = ['covidqa', 'hotpotqa', 'pubmedqa'] # Starting with key datasets
27
+
28
+ for name in dataset_names:
29
+ datasets[name] = load_dataset("rungalileo/ragbench", name, split='train')
30
+ logger.info(f"Loaded {name}")
31
+
32
  def get_system_metrics():
33
+ metrics = {
34
+ 'cpu_percent': psutil.cpu_percent(),
35
+ 'memory_percent': psutil.virtual_memory().percent,
36
+ 'gpu_util': GPUtil.getGPUs()[0].load * 100 if torch.cuda.is_available() else 0,
37
+ 'gpu_memory': GPUtil.getGPUs()[0].memoryUtil * 100 if torch.cuda.is_available() else 0
38
+ }
39
+ return metrics
 
 
 
40
 
41
  def process_query(query, dataset_choice="all"):
42
  start_time = time.time()
43
  try:
44
+ relevant_contexts = []
45
+ search_datasets = [dataset_choice] if dataset_choice != "all" else datasets.keys()
46
+
47
+ for dataset_name in search_datasets:
48
+ if dataset_name in datasets:
49
+ for doc in datasets[dataset_name]['documents']:
50
+ if any(keyword.lower() in doc.lower() for keyword in query.split()):
51
+ relevant_contexts.append((doc, dataset_name))
52
+
53
+ context_info = f"From {relevant_contexts[0][1]}: {relevant_contexts[0][0]}" if relevant_contexts else "Searching across datasets..."
54
+
55
+ response = openai.chat.completions.create(
56
+ model="gpt-3.5-turbo",
57
+ messages=[
58
+ {"role": "system", "content": "You are a knowledgeable expert using E5 embeddings for precise information retrieval."},
59
+ {"role": "user", "content": f"Context: {context_info}\nQuestion: {query}"}
60
+ ],
61
+ max_tokens=300,
62
+ temperature=0.7,
63
+ )
64
 
65
+ # Get performance metrics
66
+ metrics = get_system_metrics()
67
+ metrics['processing_time'] = time.time() - start_time
 
68
 
69
+ metrics_display = f"""
70
+ Processing Time: {metrics['processing_time']:.2f}s
71
+ CPU Usage: {metrics['cpu_percent']}%
72
+ Memory Usage: {metrics['memory_percent']}%
73
+ GPU Utilization: {metrics['gpu_util']:.1f}%
74
+ GPU Memory: {metrics['gpu_memory']:.1f}%
 
75
  """
76
 
77
+ return response.choices[0].message.content.strip(), metrics_display
78
 
79
  except Exception as e:
80
+ return str(e), "Metrics collection in progress"
81
 
82
+ # Create Gradio interface
83
  demo = gr.Interface(
84
  fn=process_query,
85
  inputs=[
86
+ gr.Textbox(label="Question", placeholder="Ask your question here"),
87
  gr.Dropdown(
88
  choices=["all"] + dataset_names,
89
  label="Select Dataset",
 
94
  gr.Textbox(label="Response"),
95
  gr.Textbox(label="Performance Metrics")
96
  ],
97
+ title="E5-Powered Knowledge Base",
98
+ description="Search across RagBench datasets with performance monitoring"
 
 
 
 
 
 
99
  )
100
 
101
  if __name__ == "__main__":
102
+ demo.queue()
103
+ demo.launch(debug=True)