Spaces:
Runtime error
Runtime error
Initial commit with LlamaIndex-based agent
Browse files- app.py +113 -19
- requirements.txt +1 -0
- txt.txt +383 -0
app.py
CHANGED
@@ -8,6 +8,7 @@ import gradio as gr
|
|
8 |
import requests
|
9 |
import pandas as pd
|
10 |
import traceback
|
|
|
11 |
|
12 |
# Import real tool dependencies
|
13 |
try:
|
@@ -32,15 +33,63 @@ class SmartAgent:
|
|
32 |
def __init__(self):
|
33 |
print("Initializing Local LLM Agent...")
|
34 |
|
35 |
-
#
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
# Define tools with real implementations
|
46 |
self.tools = [
|
@@ -57,12 +106,19 @@ class SmartAgent:
|
|
57 |
]
|
58 |
|
59 |
# Create ReAct agent with tools
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
def web_search(self, query: str) -> str:
|
68 |
"""Real web search using DuckDuckGo"""
|
@@ -116,14 +172,43 @@ class SmartAgent:
|
|
116 |
def __call__(self, question: str) -> str:
|
117 |
print(f"Processing question (first 50 chars): {question[:50]}...")
|
118 |
try:
|
119 |
-
|
120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
except Exception as e:
|
122 |
print(f"Agent error: {str(e)}")
|
123 |
print(f"Full traceback: {traceback.format_exc()}")
|
124 |
return f"Error processing question: {str(e)}"
|
125 |
|
126 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
# --- Submission Logic ---
|
128 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
129 |
"""
|
@@ -143,6 +228,9 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
143 |
questions_url = f"{api_url}/questions"
|
144 |
submit_url = f"{api_url}/submit"
|
145 |
|
|
|
|
|
|
|
146 |
# Instantiate Agent
|
147 |
try:
|
148 |
agent = SmartAgent()
|
@@ -198,6 +286,11 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
198 |
"Submitted Answer": submitted_answer[:200] + "..." if len(submitted_answer) > 200 else submitted_answer
|
199 |
})
|
200 |
print(f"โ
Completed question {i}: {task_id}")
|
|
|
|
|
|
|
|
|
|
|
201 |
except Exception as e:
|
202 |
print(f"โ Error running agent on task {task_id}: {e}")
|
203 |
error_answer = f"AGENT ERROR: {str(e)}"
|
@@ -279,13 +372,14 @@ with gr.Blocks(title="Local LLM Agent Evaluation") as demo:
|
|
279 |
**Instructions:**
|
280 |
1. ๐ Log in to your Hugging Face account using the button below
|
281 |
2. ๐ Click 'Run Evaluation & Submit All Answers'
|
282 |
-
3. โณ Wait for the local LLM
|
283 |
4. ๐ View your results and submission status
|
284 |
|
285 |
**Features:**
|
286 |
- ๐ Real web search using DuckDuckGo
|
287 |
- ๐งฎ Advanced math calculations with SymPy
|
288 |
-
- ๐ง
|
|
|
289 |
"""
|
290 |
)
|
291 |
|
|
|
8 |
import requests
|
9 |
import pandas as pd
|
10 |
import traceback
|
11 |
+
import torch
|
12 |
|
13 |
# Import real tool dependencies
|
14 |
try:
|
|
|
33 |
def __init__(self):
|
34 |
print("Initializing Local LLM Agent...")
|
35 |
|
36 |
+
# Check available memory and CUDA
|
37 |
+
if torch.cuda.is_available():
|
38 |
+
print(f"CUDA available. GPU memory: {torch.cuda.get_device_properties(0).total_memory / 1e9:.1f}GB")
|
39 |
+
else:
|
40 |
+
print("CUDA not available, using CPU")
|
41 |
+
|
42 |
+
# Use a smaller, more efficient model for Hugging Face Spaces
|
43 |
+
model_options = [
|
44 |
+
"microsoft/DialoGPT-medium", # Much smaller, works well for chat
|
45 |
+
"google/flan-t5-base", # Good for reasoning tasks
|
46 |
+
"HuggingFaceH4/zephyr-7b-beta" # Original (may fail in limited memory)
|
47 |
+
]
|
48 |
+
|
49 |
+
model_name = model_options[1] # Start with flan-t5-base
|
50 |
+
print(f"Attempting to load model: {model_name}")
|
51 |
+
|
52 |
+
try:
|
53 |
+
# Initialize with memory-efficient settings
|
54 |
+
self.llm = HuggingFaceLLM(
|
55 |
+
model_name=model_name,
|
56 |
+
tokenizer_name=model_name,
|
57 |
+
context_window=512, # Reduced context window
|
58 |
+
max_new_tokens=128, # Reduced max tokens
|
59 |
+
generate_kwargs={
|
60 |
+
"temperature": 0.7,
|
61 |
+
"do_sample": True,
|
62 |
+
"pad_token_id": 0 # Prevent padding issues
|
63 |
+
},
|
64 |
+
device_map="auto",
|
65 |
+
# Add memory optimization parameters
|
66 |
+
model_kwargs={
|
67 |
+
"torch_dtype": torch.float16, # Use half precision
|
68 |
+
"low_cpu_mem_usage": True,
|
69 |
+
"load_in_8bit": True, # Enable 8-bit quantization if available
|
70 |
+
}
|
71 |
+
)
|
72 |
+
print(f"Successfully loaded model: {model_name}")
|
73 |
+
|
74 |
+
except Exception as e:
|
75 |
+
print(f"Failed to load {model_name}: {e}")
|
76 |
+
# Fallback to an even smaller model
|
77 |
+
try:
|
78 |
+
fallback_model = "microsoft/DialoGPT-small"
|
79 |
+
print(f"Falling back to: {fallback_model}")
|
80 |
+
self.llm = HuggingFaceLLM(
|
81 |
+
model_name=fallback_model,
|
82 |
+
tokenizer_name=fallback_model,
|
83 |
+
context_window=256,
|
84 |
+
max_new_tokens=64,
|
85 |
+
generate_kwargs={"temperature": 0.7, "do_sample": True},
|
86 |
+
device_map="cpu", # Force CPU to avoid memory issues
|
87 |
+
model_kwargs={"low_cpu_mem_usage": True}
|
88 |
+
)
|
89 |
+
print(f"Successfully loaded fallback model: {fallback_model}")
|
90 |
+
except Exception as e2:
|
91 |
+
print(f"All model loading attempts failed: {e2}")
|
92 |
+
raise Exception("Unable to load any language model")
|
93 |
|
94 |
# Define tools with real implementations
|
95 |
self.tools = [
|
|
|
106 |
]
|
107 |
|
108 |
# Create ReAct agent with tools
|
109 |
+
try:
|
110 |
+
self.agent = ReActAgent.from_tools(
|
111 |
+
tools=self.tools,
|
112 |
+
llm=self.llm,
|
113 |
+
verbose=True,
|
114 |
+
max_iterations=3 # Limit iterations to prevent infinite loops
|
115 |
+
)
|
116 |
+
print("Local LLM Agent initialized successfully.")
|
117 |
+
except Exception as e:
|
118 |
+
print(f"Error creating ReAct agent: {e}")
|
119 |
+
# Create a simple fallback agent
|
120 |
+
self.agent = None
|
121 |
+
print("Using fallback direct tool calling approach")
|
122 |
|
123 |
def web_search(self, query: str) -> str:
|
124 |
"""Real web search using DuckDuckGo"""
|
|
|
172 |
def __call__(self, question: str) -> str:
|
173 |
print(f"Processing question (first 50 chars): {question[:50]}...")
|
174 |
try:
|
175 |
+
if self.agent:
|
176 |
+
response = self.agent.query(question)
|
177 |
+
return str(response)
|
178 |
+
else:
|
179 |
+
# Fallback: Direct tool usage based on question content
|
180 |
+
question_lower = question.lower()
|
181 |
+
if any(word in question_lower for word in ['calculate', 'math', 'equation', '+', '-', '*', '/', '=']):
|
182 |
+
# Try math calculator
|
183 |
+
math_terms = []
|
184 |
+
for word in question.split():
|
185 |
+
if any(char in word for char in '0123456789+-*/()'):
|
186 |
+
math_terms.append(word)
|
187 |
+
if math_terms:
|
188 |
+
expression = ' '.join(math_terms)
|
189 |
+
return self.math_calculator(expression)
|
190 |
+
|
191 |
+
if any(word in question_lower for word in ['search', 'find', 'what is', 'current', 'latest', 'news']):
|
192 |
+
# Try web search
|
193 |
+
return self.web_search(question)
|
194 |
+
|
195 |
+
# Default response
|
196 |
+
return f"I understand you're asking: {question[:100]}... However, I'm having trouble processing this with the current model configuration. Please try rephrasing your question or breaking it into smaller parts."
|
197 |
+
|
198 |
except Exception as e:
|
199 |
print(f"Agent error: {str(e)}")
|
200 |
print(f"Full traceback: {traceback.format_exc()}")
|
201 |
return f"Error processing question: {str(e)}"
|
202 |
|
203 |
|
204 |
+
# --- Memory cleanup function ---
|
205 |
+
def cleanup_memory():
|
206 |
+
"""Clean up GPU memory"""
|
207 |
+
if torch.cuda.is_available():
|
208 |
+
torch.cuda.empty_cache()
|
209 |
+
print("GPU memory cleared")
|
210 |
+
|
211 |
+
|
212 |
# --- Submission Logic ---
|
213 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
214 |
"""
|
|
|
228 |
questions_url = f"{api_url}/questions"
|
229 |
submit_url = f"{api_url}/submit"
|
230 |
|
231 |
+
# Clean memory before starting
|
232 |
+
cleanup_memory()
|
233 |
+
|
234 |
# Instantiate Agent
|
235 |
try:
|
236 |
agent = SmartAgent()
|
|
|
286 |
"Submitted Answer": submitted_answer[:200] + "..." if len(submitted_answer) > 200 else submitted_answer
|
287 |
})
|
288 |
print(f"โ
Completed question {i}: {task_id}")
|
289 |
+
|
290 |
+
# Clean memory after each question
|
291 |
+
if i % 5 == 0: # Every 5 questions
|
292 |
+
cleanup_memory()
|
293 |
+
|
294 |
except Exception as e:
|
295 |
print(f"โ Error running agent on task {task_id}: {e}")
|
296 |
error_answer = f"AGENT ERROR: {str(e)}"
|
|
|
372 |
**Instructions:**
|
373 |
1. ๐ Log in to your Hugging Face account using the button below
|
374 |
2. ๐ Click 'Run Evaluation & Submit All Answers'
|
375 |
+
3. โณ Wait for the local LLM to process all questions (using memory-optimized smaller model)
|
376 |
4. ๐ View your results and submission status
|
377 |
|
378 |
**Features:**
|
379 |
- ๐ Real web search using DuckDuckGo
|
380 |
- ๐งฎ Advanced math calculations with SymPy
|
381 |
+
- ๐ง Memory-optimized language model with fallback options
|
382 |
+
- ๐ก๏ธ Error handling and recovery mechanisms
|
383 |
"""
|
384 |
)
|
385 |
|
requirements.txt
CHANGED
@@ -3,6 +3,7 @@ llama-index-llms-huggingface
|
|
3 |
transformers>=4.30.0
|
4 |
torch>=2.0.0
|
5 |
accelerate
|
|
|
6 |
gradio>=4.0.0
|
7 |
requests
|
8 |
pandas
|
|
|
3 |
transformers>=4.30.0
|
4 |
torch>=2.0.0
|
5 |
accelerate
|
6 |
+
bitsandbytes # For 8-bit quantization
|
7 |
gradio>=4.0.0
|
8 |
requests
|
9 |
pandas
|
txt.txt
ADDED
@@ -0,0 +1,383 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
llama-index-core
|
2 |
+
llama-index-llms-huggingface
|
3 |
+
transformers>=4.30.0
|
4 |
+
torch>=2.0.0
|
5 |
+
accelerate
|
6 |
+
gradio>=4.0.0
|
7 |
+
requests
|
8 |
+
pandas
|
9 |
+
python-dotenv
|
10 |
+
duckduckgo-search
|
11 |
+
sympy
|
12 |
+
sentencepiece
|
13 |
+
protobuf
|
14 |
+
|
15 |
+
app.py
|
16 |
+
# app.py
|
17 |
+
from llama_index.llms.huggingface import HuggingFaceLLM
|
18 |
+
from llama_index.core.agent import ReActAgent
|
19 |
+
from llama_index.core.tools import FunctionTool
|
20 |
+
from transformers import AutoTokenizer
|
21 |
+
import os
|
22 |
+
import gradio as gr
|
23 |
+
import requests
|
24 |
+
import pandas as pd
|
25 |
+
import traceback
|
26 |
+
|
27 |
+
# Import real tool dependencies
|
28 |
+
try:
|
29 |
+
from duckduckgo_search import DDGS
|
30 |
+
except ImportError:
|
31 |
+
print("Warning: duckduckgo_search not installed. Web search will be limited.")
|
32 |
+
DDGS = None
|
33 |
+
|
34 |
+
try:
|
35 |
+
from sympy import sympify
|
36 |
+
from sympy.core.sympify import SympifyError
|
37 |
+
except ImportError:
|
38 |
+
print("Warning: sympy not installed. Math calculator will be limited.")
|
39 |
+
sympify = None
|
40 |
+
SympifyError = Exception
|
41 |
+
|
42 |
+
# --- Constants ---
|
43 |
+
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
44 |
+
|
45 |
+
# --- Advanced Agent Definition ---
|
46 |
+
class SmartAgent:
|
47 |
+
def __init__(self):
|
48 |
+
print("Initializing Local LLM Agent...")
|
49 |
+
|
50 |
+
# Initialize Zephyr-7B model
|
51 |
+
self.llm = HuggingFaceLLM(
|
52 |
+
model_name="HuggingFaceH4/zephyr-7b-beta",
|
53 |
+
tokenizer_name="HuggingFaceH4/zephyr-7b-beta",
|
54 |
+
context_window=2048,
|
55 |
+
max_new_tokens=256,
|
56 |
+
generate_kwargs={"temperature": 0.7, "do_sample": True},
|
57 |
+
device_map="auto"
|
58 |
+
)
|
59 |
+
|
60 |
+
# Define tools with real implementations
|
61 |
+
self.tools = [
|
62 |
+
FunctionTool.from_defaults(
|
63 |
+
fn=self.web_search,
|
64 |
+
name="web_search",
|
65 |
+
description="Searches the web for current information using DuckDuckGo when questions require up-to-date knowledge"
|
66 |
+
),
|
67 |
+
FunctionTool.from_defaults(
|
68 |
+
fn=self.math_calculator,
|
69 |
+
name="math_calculator",
|
70 |
+
description="Performs mathematical calculations and symbolic math using SymPy when questions involve numbers or equations"
|
71 |
+
)
|
72 |
+
]
|
73 |
+
|
74 |
+
# Create ReAct agent with tools
|
75 |
+
self.agent = ReActAgent.from_tools(
|
76 |
+
tools=self.tools,
|
77 |
+
llm=self.llm,
|
78 |
+
verbose=True
|
79 |
+
)
|
80 |
+
print("Local LLM Agent initialized successfully.")
|
81 |
+
|
82 |
+
def web_search(self, query: str) -> str:
|
83 |
+
"""Real web search using DuckDuckGo"""
|
84 |
+
print(f"Web search triggered for: {query[:50]}...")
|
85 |
+
|
86 |
+
if not DDGS:
|
87 |
+
return "Web search unavailable - duckduckgo_search not installed"
|
88 |
+
|
89 |
+
try:
|
90 |
+
with DDGS() as ddgs:
|
91 |
+
results = list(ddgs.text(query, max_results=3))
|
92 |
+
if results:
|
93 |
+
formatted_results = []
|
94 |
+
for i, r in enumerate(results, 1):
|
95 |
+
title = r.get('title', 'No title')
|
96 |
+
body = r.get('body', 'No description')[:200]
|
97 |
+
url = r.get('href', '')
|
98 |
+
formatted_results.append(f"{i}. {title}\n{body}...\nSource: {url}")
|
99 |
+
return "\n\n".join(formatted_results)
|
100 |
+
else:
|
101 |
+
return "No search results found for the query."
|
102 |
+
except Exception as e:
|
103 |
+
print(f"Web search error: {e}")
|
104 |
+
return f"Error during web search: {str(e)}"
|
105 |
+
|
106 |
+
def math_calculator(self, expression: str) -> str:
|
107 |
+
"""Safe math evaluation using SymPy"""
|
108 |
+
print(f"Math calculation triggered for: {expression}")
|
109 |
+
|
110 |
+
if not sympify:
|
111 |
+
# Fallback to basic eval with safety checks
|
112 |
+
try:
|
113 |
+
# Only allow basic math operations
|
114 |
+
allowed_chars = set('0123456789+-*/().^ ')
|
115 |
+
if not all(c in allowed_chars for c in expression.replace(' ', '')):
|
116 |
+
return "Error: Only basic math operations are allowed"
|
117 |
+
result = eval(expression.replace('^', '**'))
|
118 |
+
return str(result)
|
119 |
+
except Exception as e:
|
120 |
+
return f"Error: Could not evaluate the mathematical expression - {str(e)}"
|
121 |
+
|
122 |
+
try:
|
123 |
+
# Use SymPy for safe evaluation
|
124 |
+
result = sympify(expression).evalf()
|
125 |
+
return str(result)
|
126 |
+
except SympifyError as e:
|
127 |
+
return f"Error: Could not parse the mathematical expression - {str(e)}"
|
128 |
+
except Exception as e:
|
129 |
+
return f"Error: Calculation failed - {str(e)}"
|
130 |
+
|
131 |
+
def __call__(self, question: str) -> str:
|
132 |
+
print(f"Processing question (first 50 chars): {question[:50]}...")
|
133 |
+
try:
|
134 |
+
response = self.agent.query(question)
|
135 |
+
return str(response)
|
136 |
+
except Exception as e:
|
137 |
+
print(f"Agent error: {str(e)}")
|
138 |
+
print(f"Full traceback: {traceback.format_exc()}")
|
139 |
+
return f"Error processing question: {str(e)}"
|
140 |
+
|
141 |
+
|
142 |
+
# --- Submission Logic ---
|
143 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
144 |
+
"""
|
145 |
+
Fetches all questions, runs the agent on them, submits all answers,
|
146 |
+
and displays the results.
|
147 |
+
"""
|
148 |
+
space_id = os.getenv("SPACE_ID")
|
149 |
+
|
150 |
+
if profile:
|
151 |
+
username = f"{profile.username}"
|
152 |
+
print(f"User logged in: {username}")
|
153 |
+
else:
|
154 |
+
print("User not logged in.")
|
155 |
+
return "Please Login to Hugging Face with the button.", None
|
156 |
+
|
157 |
+
api_url = DEFAULT_API_URL
|
158 |
+
questions_url = f"{api_url}/questions"
|
159 |
+
submit_url = f"{api_url}/submit"
|
160 |
+
|
161 |
+
# Instantiate Agent
|
162 |
+
try:
|
163 |
+
agent = SmartAgent()
|
164 |
+
except Exception as e:
|
165 |
+
print(f"Error instantiating agent: {e}")
|
166 |
+
print(f"Full traceback: {traceback.format_exc()}")
|
167 |
+
return f"Error initializing agent: {e}", None
|
168 |
+
|
169 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
170 |
+
print(f"Agent code URL: {agent_code}")
|
171 |
+
|
172 |
+
# Fetch Questions
|
173 |
+
print(f"Fetching questions from: {questions_url}")
|
174 |
+
try:
|
175 |
+
response = requests.get(questions_url, timeout=15)
|
176 |
+
response.raise_for_status()
|
177 |
+
questions_data = response.json()
|
178 |
+
if not questions_data:
|
179 |
+
print("Fetched questions list is empty.")
|
180 |
+
return "Fetched questions list is empty or invalid format.", None
|
181 |
+
print(f"Fetched {len(questions_data)} questions.")
|
182 |
+
except requests.exceptions.RequestException as e:
|
183 |
+
print(f"Error fetching questions: {e}")
|
184 |
+
return f"Error fetching questions: {e}", None
|
185 |
+
except requests.exceptions.JSONDecodeError as e:
|
186 |
+
print(f"Error decoding JSON response from questions endpoint: {e}")
|
187 |
+
return f"Error decoding server response for questions: {e}", None
|
188 |
+
except Exception as e:
|
189 |
+
print(f"An unexpected error occurred fetching questions: {e}")
|
190 |
+
return f"An unexpected error occurred fetching questions: {e}", None
|
191 |
+
|
192 |
+
# Run Agent on all questions
|
193 |
+
results_log = []
|
194 |
+
answers_payload = []
|
195 |
+
print(f"Running agent on {len(questions_data)} questions...")
|
196 |
+
|
197 |
+
for i, item in enumerate(questions_data, 1):
|
198 |
+
task_id = item.get("task_id")
|
199 |
+
question_text = item.get("question")
|
200 |
+
|
201 |
+
if not task_id or question_text is None:
|
202 |
+
print(f"Skipping item with missing task_id or question: {item}")
|
203 |
+
continue
|
204 |
+
|
205 |
+
print(f"Processing question {i}/{len(questions_data)}: {task_id}")
|
206 |
+
|
207 |
+
try:
|
208 |
+
submitted_answer = agent(question_text)
|
209 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
210 |
+
results_log.append({
|
211 |
+
"Task ID": task_id,
|
212 |
+
"Question": question_text[:100] + "..." if len(question_text) > 100 else question_text,
|
213 |
+
"Submitted Answer": submitted_answer[:200] + "..." if len(submitted_answer) > 200 else submitted_answer
|
214 |
+
})
|
215 |
+
print(f"โ
Completed question {i}: {task_id}")
|
216 |
+
except Exception as e:
|
217 |
+
print(f"โ Error running agent on task {task_id}: {e}")
|
218 |
+
error_answer = f"AGENT ERROR: {str(e)}"
|
219 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": error_answer})
|
220 |
+
results_log.append({
|
221 |
+
"Task ID": task_id,
|
222 |
+
"Question": question_text[:100] + "..." if len(question_text) > 100 else question_text,
|
223 |
+
"Submitted Answer": error_answer
|
224 |
+
})
|
225 |
+
|
226 |
+
if not answers_payload:
|
227 |
+
print("Agent did not produce any answers to submit.")
|
228 |
+
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
229 |
+
|
230 |
+
# Prepare submission
|
231 |
+
submission_data = {
|
232 |
+
"username": username.strip(),
|
233 |
+
"agent_code": agent_code,
|
234 |
+
"answers": answers_payload
|
235 |
+
}
|
236 |
+
|
237 |
+
status_update = f"Agent finished processing. Submitting {len(answers_payload)} answers for user '{username}'..."
|
238 |
+
print(status_update)
|
239 |
+
|
240 |
+
# Submit answers
|
241 |
+
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
242 |
+
try:
|
243 |
+
response = requests.post(submit_url, json=submission_data, timeout=60)
|
244 |
+
response.raise_for_status()
|
245 |
+
result_data = response.json()
|
246 |
+
|
247 |
+
final_status = (
|
248 |
+
f"๐ Submission Successful!\n\n"
|
249 |
+
f"User: {result_data.get('username')}\n"
|
250 |
+
f"Overall Score: {result_data.get('score', 'N/A')}% "
|
251 |
+
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
252 |
+
f"Message: {result_data.get('message', 'No message received.')}"
|
253 |
+
)
|
254 |
+
print("โ
Submission successful!")
|
255 |
+
results_df = pd.DataFrame(results_log)
|
256 |
+
return final_status, results_df
|
257 |
+
|
258 |
+
except requests.exceptions.HTTPError as e:
|
259 |
+
error_detail = f"Server responded with status {e.response.status_code}."
|
260 |
+
try:
|
261 |
+
error_json = e.response.json()
|
262 |
+
error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
|
263 |
+
except requests.exceptions.JSONDecodeError:
|
264 |
+
error_detail += f" Response: {e.response.text[:500]}"
|
265 |
+
status_message = f"โ Submission Failed: {error_detail}"
|
266 |
+
print(status_message)
|
267 |
+
results_df = pd.DataFrame(results_log)
|
268 |
+
return status_message, results_df
|
269 |
+
|
270 |
+
except requests.exceptions.Timeout:
|
271 |
+
status_message = "โ Submission Failed: The request timed out."
|
272 |
+
print(status_message)
|
273 |
+
results_df = pd.DataFrame(results_log)
|
274 |
+
return status_message, results_df
|
275 |
+
|
276 |
+
except requests.exceptions.RequestException as e:
|
277 |
+
status_message = f"โ Submission Failed: Network error - {e}"
|
278 |
+
print(status_message)
|
279 |
+
results_df = pd.DataFrame(results_log)
|
280 |
+
return status_message, results_df
|
281 |
+
|
282 |
+
except Exception as e:
|
283 |
+
status_message = f"โ An unexpected error occurred during submission: {e}"
|
284 |
+
print(status_message)
|
285 |
+
results_df = pd.DataFrame(results_log)
|
286 |
+
return status_message, results_df
|
287 |
+
|
288 |
+
|
289 |
+
# --- Gradio UI ---
|
290 |
+
with gr.Blocks(title="Local LLM Agent Evaluation") as demo:
|
291 |
+
gr.Markdown("# ๐ค Local LLM Agent Evaluation Runner")
|
292 |
+
gr.Markdown(
|
293 |
+
"""
|
294 |
+
**Instructions:**
|
295 |
+
1. ๐ Log in to your Hugging Face account using the button below
|
296 |
+
2. ๐ Click 'Run Evaluation & Submit All Answers'
|
297 |
+
3. โณ Wait for the local LLM (Zephyr-7B) to process all questions
|
298 |
+
4. ๐ View your results and submission status
|
299 |
+
|
300 |
+
**Features:**
|
301 |
+
- ๐ Real web search using DuckDuckGo
|
302 |
+
- ๐งฎ Advanced math calculations with SymPy
|
303 |
+
- ๐ง Powered by HuggingFace Zephyr-7B model
|
304 |
+
"""
|
305 |
+
)
|
306 |
+
|
307 |
+
with gr.Row():
|
308 |
+
gr.LoginButton()
|
309 |
+
|
310 |
+
with gr.Row():
|
311 |
+
run_button = gr.Button(
|
312 |
+
"๐ Run Evaluation & Submit All Answers",
|
313 |
+
variant="primary",
|
314 |
+
size="lg"
|
315 |
+
)
|
316 |
+
|
317 |
+
status_output = gr.Textbox(
|
318 |
+
label="๐ Run Status / Submission Result",
|
319 |
+
lines=8,
|
320 |
+
interactive=False,
|
321 |
+
placeholder="Click the button above to start the evaluation..."
|
322 |
+
)
|
323 |
+
|
324 |
+
results_table = gr.DataFrame(
|
325 |
+
label="๐ Questions and Agent Answers",
|
326 |
+
wrap=True,
|
327 |
+
interactive=False
|
328 |
+
)
|
329 |
+
|
330 |
+
# Wire up the button
|
331 |
+
run_button.click(
|
332 |
+
fn=run_and_submit_all,
|
333 |
+
outputs=[status_output, results_table]
|
334 |
+
)
|
335 |
+
|
336 |
+
|
337 |
+
if __name__ == "__main__":
|
338 |
+
print("\n" + "="*60)
|
339 |
+
print("๐ Application Startup at", pd.Timestamp.now().strftime("%Y-%m-%d %H:%M:%S"))
|
340 |
+
print("="*60)
|
341 |
+
|
342 |
+
space_host_startup = os.getenv("SPACE_HOST")
|
343 |
+
space_id_startup = os.getenv("SPACE_ID")
|
344 |
+
|
345 |
+
if space_host_startup:
|
346 |
+
print(f"โ
SPACE_HOST found: {space_host_startup}")
|
347 |
+
print(f" Runtime URL should be: https://{space_host_startup}")
|
348 |
+
else:
|
349 |
+
print("โน๏ธ SPACE_HOST environment variable not found (running locally?).")
|
350 |
+
|
351 |
+
if space_id_startup:
|
352 |
+
print(f"โ
SPACE_ID found: {space_id_startup}")
|
353 |
+
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
354 |
+
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
355 |
+
else:
|
356 |
+
print("โน๏ธ SPACE_ID environment variable not found (running locally?).")
|
357 |
+
|
358 |
+
print("-" * 60)
|
359 |
+
print("๐ฏ Launching Gradio Interface for Local LLM Agent Evaluation...")
|
360 |
+
|
361 |
+
# Launch without share=True for Hugging Face Spaces
|
362 |
+
demo.launch(
|
363 |
+
server_name="0.0.0.0",
|
364 |
+
server_port=7860,
|
365 |
+
show_error=True
|
366 |
+
)
|
367 |
+
|
368 |
+
readme
|
369 |
+
---
|
370 |
+
title: Template Final Assignment
|
371 |
+
emoji: ๐ต๐ปโโ๏ธ
|
372 |
+
colorFrom: indigo
|
373 |
+
colorTo: indigo
|
374 |
+
sdk: gradio
|
375 |
+
sdk_version: 5.25.2
|
376 |
+
app_file: app.py
|
377 |
+
pinned: false
|
378 |
+
hf_oauth: true
|
379 |
+
# optional, default duration is 8 hours/480 minutes. Max duration is 30 days/43200 minutes.
|
380 |
+
hf_oauth_expiration_minutes: 480
|
381 |
+
---
|
382 |
+
|
383 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|