Update app.py
Browse files
app.py
CHANGED
@@ -2,96 +2,305 @@ import os
|
|
2 |
import gradio as gr
|
3 |
import requests
|
4 |
import pandas as pd
|
5 |
-
|
|
|
|
|
6 |
|
7 |
# Config
|
8 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
9 |
-
MODEL_NAME = "
|
10 |
SPACE_ID = os.getenv("SPACE_ID", "sirine1712/Final_Assignment_Template")
|
11 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
16 |
self.model = model
|
17 |
self.api_url = f"https://api-inference.huggingface.co/models/{model}"
|
18 |
self.headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
def __call__(self, question: str) -> str:
|
21 |
-
|
|
|
|
|
22 |
try:
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
)
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
except Exception as e:
|
36 |
-
|
37 |
-
|
|
|
38 |
|
39 |
-
# Function to run agent and submit to GAIA scoring API
|
40 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
|
41 |
if not profile:
|
42 |
-
return "โ Please log in first.", None
|
43 |
-
|
44 |
username = profile.username or "anonymous"
|
45 |
agent_code = f"https://huggingface.co/spaces/{SPACE_ID}/tree/main"
|
|
|
|
|
|
|
|
|
46 |
agent = HuggingFaceAPIAgent()
|
47 |
-
|
|
|
48 |
try:
|
49 |
-
questions
|
|
|
|
|
|
|
|
|
50 |
except Exception as e:
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
try:
|
|
|
57 |
answer = agent(q["question"])
|
58 |
except Exception as e:
|
59 |
-
answer = f"Error: {e}"
|
60 |
-
|
61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
"Task ID": q["task_id"],
|
63 |
-
"Question": q["question"],
|
64 |
-
"Submitted Answer": answer
|
|
|
65 |
})
|
66 |
-
|
|
|
67 |
try:
|
68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
f"{DEFAULT_API_URL}/submit",
|
70 |
-
json=
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
|
|
|
|
77 |
except Exception as e:
|
78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
-
|
81 |
-
f"โ
**Submission complete!**\n"
|
82 |
-
f"**Score:** {result.get('score')}%\n"
|
83 |
-
f"**Correct:** {result.get('correct_count')}/{result.get('total_attempted')}\n"
|
84 |
-
f"**Message:** {result.get('message')}"
|
85 |
-
)
|
86 |
-
return message, pd.DataFrame(log)
|
87 |
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
|
97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import gradio as gr
|
3 |
import requests
|
4 |
import pandas as pd
|
5 |
+
import json
|
6 |
+
import time
|
7 |
+
from typing import Dict, List, Any, Optional
|
8 |
|
9 |
# Config
|
10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
11 |
+
MODEL_NAME = "microsoft/DialoGPT-medium" # Better conversational model
|
12 |
SPACE_ID = os.getenv("SPACE_ID", "sirine1712/Final_Assignment_Template")
|
13 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
14 |
|
15 |
+
class HuggingFaceAPIAgent:
|
16 |
+
"""Enhanced Hugging Face Inference Agent with better question processing"""
|
17 |
+
|
18 |
+
def __init__(self, model: str = MODEL_NAME):
|
19 |
self.model = model
|
20 |
self.api_url = f"https://api-inference.huggingface.co/models/{model}"
|
21 |
self.headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
22 |
+
|
23 |
+
def preprocess_question(self, question: str) -> str:
|
24 |
+
"""Preprocess question to improve model understanding"""
|
25 |
+
# Add context markers for better comprehension
|
26 |
+
processed = f"Question: {question.strip()}"
|
27 |
+
|
28 |
+
# Handle specific question types
|
29 |
+
if any(word in question.lower() for word in ['calculate', 'compute', 'math', 'number']):
|
30 |
+
processed = f"Math problem: {question.strip()} Please provide the numerical answer."
|
31 |
+
elif any(word in question.lower() for word in ['when', 'what year', 'date']):
|
32 |
+
processed = f"Factual question about time: {question.strip()} Please provide the specific date or year."
|
33 |
+
elif any(word in question.lower() for word in ['who', 'person', 'people']):
|
34 |
+
processed = f"Question about people: {question.strip()} Please provide the name(s)."
|
35 |
+
elif any(word in question.lower() for word in ['where', 'location', 'place']):
|
36 |
+
processed = f"Location question: {question.strip()} Please provide the specific location."
|
37 |
+
elif any(word in question.lower() for word in ['how many', 'count', 'quantity']):
|
38 |
+
processed = f"Counting question: {question.strip()} Please provide the exact number."
|
39 |
+
|
40 |
+
return processed
|
41 |
+
|
42 |
+
def postprocess_answer(self, raw_answer: str, question: str) -> str:
|
43 |
+
"""Clean and format the model's response"""
|
44 |
+
if not raw_answer:
|
45 |
+
return "Unable to generate answer"
|
46 |
+
|
47 |
+
# Remove common prefixes/suffixes
|
48 |
+
answer = raw_answer.strip()
|
49 |
+
prefixes_to_remove = [
|
50 |
+
"Question:", "Answer:", "Response:", "Output:",
|
51 |
+
"The answer is:", "Based on the question:",
|
52 |
+
"Math problem:", "Factual question about time:",
|
53 |
+
"Question about people:", "Location question:",
|
54 |
+
"Counting question:"
|
55 |
+
]
|
56 |
+
|
57 |
+
for prefix in prefixes_to_remove:
|
58 |
+
if answer.lower().startswith(prefix.lower()):
|
59 |
+
answer = answer[len(prefix):].strip()
|
60 |
+
|
61 |
+
# Extract specific answer patterns
|
62 |
+
if any(word in question.lower() for word in ['calculate', 'compute', 'math']):
|
63 |
+
# Try to extract numbers from the response
|
64 |
+
import re
|
65 |
+
numbers = re.findall(r'-?\d+\.?\d*', answer)
|
66 |
+
if numbers:
|
67 |
+
return numbers[-1] # Return the last number found
|
68 |
+
|
69 |
+
# Limit answer length for conciseness
|
70 |
+
if len(answer) > 200:
|
71 |
+
sentences = answer.split('.')
|
72 |
+
answer = sentences[0] + '.' if sentences else answer[:200]
|
73 |
+
|
74 |
+
return answer
|
75 |
+
|
76 |
def __call__(self, question: str) -> str:
|
77 |
+
"""Main method to process questions"""
|
78 |
+
print(f"โณ Processing question: {question[:80]}...")
|
79 |
+
|
80 |
try:
|
81 |
+
# Preprocess the question
|
82 |
+
processed_question = self.preprocess_question(question)
|
83 |
+
|
84 |
+
# Make API call with retry logic
|
85 |
+
max_retries = 3
|
86 |
+
for attempt in range(max_retries):
|
87 |
+
try:
|
88 |
+
response = requests.post(
|
89 |
+
self.api_url,
|
90 |
+
headers=self.headers,
|
91 |
+
json={
|
92 |
+
"inputs": processed_question,
|
93 |
+
"parameters": {
|
94 |
+
"max_length": 150,
|
95 |
+
"temperature": 0.3, # Lower temperature for more focused answers
|
96 |
+
"do_sample": True,
|
97 |
+
"top_p": 0.9
|
98 |
+
}
|
99 |
+
},
|
100 |
+
timeout=15
|
101 |
+
)
|
102 |
+
|
103 |
+
if response.status_code == 503: # Model loading
|
104 |
+
print(f"โณ Model loading, waiting... (attempt {attempt + 1})")
|
105 |
+
time.sleep(10)
|
106 |
+
continue
|
107 |
+
|
108 |
+
response.raise_for_status()
|
109 |
+
output = response.json()
|
110 |
+
|
111 |
+
# Extract generated text
|
112 |
+
if isinstance(output, list) and len(output) > 0:
|
113 |
+
raw_answer = output[0].get("generated_text", "")
|
114 |
+
elif isinstance(output, dict):
|
115 |
+
raw_answer = output.get("generated_text", "")
|
116 |
+
else:
|
117 |
+
raw_answer = str(output)
|
118 |
+
|
119 |
+
# Postprocess the answer
|
120 |
+
final_answer = self.postprocess_answer(raw_answer, question)
|
121 |
+
print(f"โ
Generated answer: {final_answer[:60]}...")
|
122 |
+
return final_answer
|
123 |
+
|
124 |
+
except requests.exceptions.RequestException as e:
|
125 |
+
if attempt == max_retries - 1:
|
126 |
+
raise e
|
127 |
+
print(f"โ ๏ธ Request failed (attempt {attempt + 1}), retrying...")
|
128 |
+
time.sleep(2)
|
129 |
+
|
130 |
except Exception as e:
|
131 |
+
error_msg = f"Error processing question: {str(e)}"
|
132 |
+
print(f"โ {error_msg}")
|
133 |
+
return error_msg
|
134 |
|
|
|
135 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
136 |
+
"""Main function to run agent on all questions and submit results"""
|
137 |
if not profile:
|
138 |
+
return "โ Please log in with your Hugging Face account first.", None
|
139 |
+
|
140 |
username = profile.username or "anonymous"
|
141 |
agent_code = f"https://huggingface.co/spaces/{SPACE_ID}/tree/main"
|
142 |
+
|
143 |
+
print(f"๐ Starting agent run for user: {username}")
|
144 |
+
|
145 |
+
# Initialize the agent
|
146 |
agent = HuggingFaceAPIAgent()
|
147 |
+
|
148 |
+
# Fetch questions from GAIA API
|
149 |
try:
|
150 |
+
print("๐ฅ Fetching questions from GAIA API...")
|
151 |
+
questions_response = requests.get(f"{DEFAULT_API_URL}/questions", timeout=20)
|
152 |
+
questions_response.raise_for_status()
|
153 |
+
questions = questions_response.json()
|
154 |
+
print(f"โ
Retrieved {len(questions)} questions")
|
155 |
except Exception as e:
|
156 |
+
error_msg = f"โ Failed to fetch questions: {str(e)}"
|
157 |
+
print(error_msg)
|
158 |
+
return error_msg, None
|
159 |
+
|
160 |
+
# Process each question
|
161 |
+
answers = []
|
162 |
+
log_entries = []
|
163 |
+
|
164 |
+
for i, q in enumerate(questions, 1):
|
165 |
+
print(f"\n๐ Processing question {i}/{len(questions)}")
|
166 |
+
print(f"Task ID: {q.get('task_id', 'Unknown')}")
|
167 |
+
|
168 |
try:
|
169 |
+
# Get answer from agent
|
170 |
answer = agent(q["question"])
|
171 |
except Exception as e:
|
172 |
+
answer = f"Error: {str(e)}"
|
173 |
+
print(f"โ Error processing question: {e}")
|
174 |
+
|
175 |
+
# Prepare submission format
|
176 |
+
answers.append({
|
177 |
+
"task_id": q["task_id"],
|
178 |
+
"submitted_answer": answer
|
179 |
+
})
|
180 |
+
|
181 |
+
# Log for display
|
182 |
+
log_entries.append({
|
183 |
"Task ID": q["task_id"],
|
184 |
+
"Question": q["question"][:100] + "..." if len(q["question"]) > 100 else q["question"],
|
185 |
+
"Submitted Answer": answer[:100] + "..." if len(str(answer)) > 100 else str(answer),
|
186 |
+
"Status": "โ
Completed" if "Error:" not in str(answer) else "โ Failed"
|
187 |
})
|
188 |
+
|
189 |
+
# Submit answers to GAIA scoring API
|
190 |
try:
|
191 |
+
print(f"\n๐ค Submitting {len(answers)} answers to GAIA API...")
|
192 |
+
submission_data = {
|
193 |
+
"username": username,
|
194 |
+
"agent_code": agent_code,
|
195 |
+
"answers": answers
|
196 |
+
}
|
197 |
+
|
198 |
+
submit_response = requests.post(
|
199 |
f"{DEFAULT_API_URL}/submit",
|
200 |
+
json=submission_data,
|
201 |
+
timeout=30
|
202 |
+
)
|
203 |
+
submit_response.raise_for_status()
|
204 |
+
result = submit_response.json()
|
205 |
+
|
206 |
+
print(f"โ
Submission successful!")
|
207 |
+
print(f"Score: {result.get('score', 'N/A')}%")
|
208 |
+
|
209 |
except Exception as e:
|
210 |
+
error_msg = f"โ Submission failed: {str(e)}"
|
211 |
+
print(error_msg)
|
212 |
+
return error_msg, pd.DataFrame(log_entries)
|
213 |
+
|
214 |
+
# Format success message
|
215 |
+
score = result.get('score', 'N/A')
|
216 |
+
correct_count = result.get('correct_count', 'N/A')
|
217 |
+
total_attempted = result.get('total_attempted', 'N/A')
|
218 |
+
message = result.get('message', 'No additional message')
|
219 |
+
|
220 |
+
success_message = f"""โ
**Submission Complete!**
|
221 |
+
|
222 |
+
**๐ Results:**
|
223 |
+
- **Score:** {score}%
|
224 |
+
- **Correct Answers:** {correct_count}/{total_attempted}
|
225 |
+
- **Total Questions:** {len(questions)}
|
226 |
|
227 |
+
**๐ Message:** {message}
|
|
|
|
|
|
|
|
|
|
|
|
|
228 |
|
229 |
+
**๐ฏ Target:** 30% ({"โ
ACHIEVED!" if isinstance(score, (int, float)) and score >= 30 else "Keep trying!"})
|
230 |
+
"""
|
231 |
+
|
232 |
+
print(success_message)
|
233 |
+
return success_message, pd.DataFrame(log_entries)
|
234 |
+
|
235 |
+
# Create Gradio Interface
|
236 |
+
def create_interface():
|
237 |
+
"""Create the Gradio interface"""
|
238 |
+
with gr.Blocks(
|
239 |
+
title="๐ค GAIA Challenge Agent",
|
240 |
+
theme=gr.themes.Soft()
|
241 |
+
) as demo:
|
242 |
+
|
243 |
+
gr.Markdown("""
|
244 |
+
# ๐ค GAIA Challenge Agent
|
245 |
+
|
246 |
+
An AI agent built to tackle the GAIA benchmark questions using Hugging Face models.
|
247 |
+
|
248 |
+
**Target:** Achieve 30% accuracy on GAIA evaluation questions.
|
249 |
+
|
250 |
+
**Instructions:**
|
251 |
+
1. Log in with your Hugging Face account
|
252 |
+
2. Click "๐ Run Agent & Submit" to start the evaluation
|
253 |
+
3. Wait for the agent to process all questions and submit results
|
254 |
+
""")
|
255 |
+
|
256 |
+
# Login section
|
257 |
+
gr.Markdown("### ๐ Authentication")
|
258 |
+
gr.LoginButton(value="Login with Hugging Face")
|
259 |
+
|
260 |
+
# Control section
|
261 |
+
gr.Markdown("### ๐ฎ Controls")
|
262 |
+
with gr.Row():
|
263 |
+
run_button = gr.Button(
|
264 |
+
"๐ Run Agent & Submit",
|
265 |
+
variant="primary",
|
266 |
+
size="lg"
|
267 |
+
)
|
268 |
+
|
269 |
+
# Results section
|
270 |
+
gr.Markdown("### ๐ Results")
|
271 |
+
status_output = gr.Textbox(
|
272 |
+
label="๐ Status & Results",
|
273 |
+
lines=8,
|
274 |
+
max_lines=15,
|
275 |
+
placeholder="Results will appear here after submission..."
|
276 |
+
)
|
277 |
+
|
278 |
+
gr.Markdown("### ๐ Detailed Log")
|
279 |
+
results_table = gr.DataFrame(
|
280 |
+
label="Agent Processing Log",
|
281 |
+
headers=["Task ID", "Question", "Submitted Answer", "Status"],
|
282 |
+
wrap=True
|
283 |
+
)
|
284 |
+
|
285 |
+
# Event handlers
|
286 |
+
run_button.click(
|
287 |
+
fn=run_and_submit_all,
|
288 |
+
outputs=[status_output, results_table]
|
289 |
+
)
|
290 |
+
|
291 |
+
# Footer
|
292 |
+
gr.Markdown("""
|
293 |
+
---
|
294 |
+
**Note:** Make sure your `HF_TOKEN` is set in the Space secrets for API access.
|
295 |
+
""")
|
296 |
+
|
297 |
+
return demo
|
298 |
|
299 |
+
# Launch the app
|
300 |
+
if __name__ == "__main__":
|
301 |
+
demo = create_interface()
|
302 |
+
demo.launch(
|
303 |
+
server_name="0.0.0.0",
|
304 |
+
server_port=7860,
|
305 |
+
share=False
|
306 |
+
)
|