Spaces:
Runtime error
Runtime error
fix
Browse files
app.py
CHANGED
@@ -1,34 +1,24 @@
|
|
1 |
import os
|
2 |
-
import gradio as gr
|
3 |
-
import requests
|
4 |
-
import pandas as pd
|
5 |
-
import json
|
6 |
-
import re
|
7 |
import time
|
|
|
8 |
import random
|
|
|
|
|
|
|
9 |
from smolagents import CodeAgent, tool
|
10 |
-
from
|
11 |
-
import base64
|
12 |
-
from urllib.parse import urlparse, parse_qs
|
13 |
-
|
14 |
-
# --- Constants ---
|
15 |
-
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
16 |
|
17 |
# --- Tools ---
|
18 |
|
19 |
@tool
|
20 |
def smart_web_search(query: str) -> str:
|
21 |
-
"""
|
22 |
-
Perform a smart web search using Serper API with Wikipedia fallback.
|
23 |
-
|
24 |
-
This tool queries the Serper API (if the key is set), formats the top results, and falls back to
|
25 |
-
Wikipedia if no data is returned or if the key is missing.
|
26 |
|
27 |
Args:
|
28 |
-
query (str): The search query to execute
|
29 |
|
30 |
Returns:
|
31 |
-
str:
|
32 |
"""
|
33 |
try:
|
34 |
time.sleep(random.uniform(1, 3))
|
@@ -36,10 +26,7 @@ def smart_web_search(query: str) -> str:
|
|
36 |
if serper_key:
|
37 |
url = "https://google.serper.dev/search"
|
38 |
payload = json.dumps({"q": query, "num": 5})
|
39 |
-
headers = {
|
40 |
-
'X-API-KEY': serper_key,
|
41 |
-
'Content-Type': 'application/json'
|
42 |
-
}
|
43 |
response = requests.post(url, headers=headers, data=payload, timeout=15)
|
44 |
if response.status_code == 200:
|
45 |
data = response.json()
|
@@ -57,25 +44,16 @@ def smart_web_search(query: str) -> str:
|
|
57 |
except Exception as e:
|
58 |
return f"Search error: {str(e)}"
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
@tool
|
64 |
def extract_youtube_details(url: str) -> str:
|
65 |
-
"""
|
66 |
-
Extract details from a YouTube video URL.
|
67 |
-
|
68 |
-
This tool fetches video metadata (title, author, etc.) using the YouTube oEmbed API,
|
69 |
-
and scrapes the video page for additional details like bird species mentions and view count.
|
70 |
|
71 |
Args:
|
72 |
-
url (str): The
|
73 |
|
74 |
Returns:
|
75 |
-
str:
|
76 |
-
bird species count (if mentioned), and number of views.
|
77 |
"""
|
78 |
-
|
79 |
try:
|
80 |
video_id = None
|
81 |
patterns = [
|
@@ -91,7 +69,6 @@ def extract_youtube_details(url: str) -> str:
|
|
91 |
if not video_id:
|
92 |
return "Invalid YouTube URL"
|
93 |
results = []
|
94 |
-
# oEmbed API
|
95 |
oembed_url = f"https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v={video_id}&format=json"
|
96 |
response = requests.get(oembed_url, timeout=10)
|
97 |
if response.status_code == 200:
|
@@ -99,7 +76,6 @@ def extract_youtube_details(url: str) -> str:
|
|
99 |
results.append(f"TITLE: {data.get('title', '')}")
|
100 |
results.append(f"AUTHOR: {data.get('author_name', '')}")
|
101 |
results.append(f"PROVIDER: {data.get('provider_name', '')}")
|
102 |
-
# Page scraping for bird species count
|
103 |
video_url = f"https://www.youtube.com/watch?v={video_id}"
|
104 |
headers = {'User-Agent': 'Mozilla/5.0'}
|
105 |
page_response = requests.get(video_url, headers=headers, timeout=15)
|
@@ -132,37 +108,42 @@ def extract_youtube_details(url: str) -> str:
|
|
132 |
|
133 |
@tool
|
134 |
def decode_reversed_text(text: str) -> str:
|
135 |
-
"""
|
136 |
-
Decode reversed text, optionally identifying directional opposites.
|
137 |
-
|
138 |
-
If the input appears to be written backward and contains known patterns, this tool reverses it
|
139 |
-
and checks for direction-related words (e.g., left/right) to return their opposites.
|
140 |
|
141 |
Args:
|
142 |
-
text (str):
|
143 |
|
144 |
Returns:
|
145 |
-
str:
|
146 |
"""
|
147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
148 |
|
149 |
@tool
|
150 |
def solve_advanced_math(problem: str) -> str:
|
151 |
-
"""
|
152 |
-
Solve advanced math problems, including commutative table checks and numeric computations.
|
153 |
-
|
154 |
-
This tool can:
|
155 |
-
- Detect which elements break commutativity in a given operation table.
|
156 |
-
- Extract and analyze chess notation for move-related questions.
|
157 |
-
- Compute arithmetic operations such as sum, average, product, and percentages from a text-based problem.
|
158 |
|
159 |
Args:
|
160 |
-
problem (str):
|
161 |
|
162 |
Returns:
|
163 |
-
str:
|
164 |
"""
|
165 |
-
|
166 |
try:
|
167 |
problem_lower = problem.lower()
|
168 |
if "commutative" in problem_lower and "|" in problem:
|
@@ -199,17 +180,14 @@ def solve_advanced_math(problem: str) -> str:
|
|
199 |
if numbers:
|
200 |
nums = [float(n) for n in numbers if n.replace('.', '').replace('-', '').isdigit()]
|
201 |
if "average" in problem_lower or "mean" in problem_lower:
|
202 |
-
|
203 |
-
return str(sum(nums) / len(nums))
|
204 |
if "sum" in problem_lower or "total" in problem_lower:
|
205 |
-
|
206 |
-
return str(sum(nums))
|
207 |
if "product" in problem_lower:
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
return str(result)
|
213 |
if "%" in problem or "percent" in problem_lower:
|
214 |
percentages = re.findall(r'(\d+\.?\d*)%', problem)
|
215 |
if percentages:
|
@@ -220,19 +198,14 @@ def solve_advanced_math(problem: str) -> str:
|
|
220 |
|
221 |
@tool
|
222 |
def get_detailed_wikipedia(topic: str) -> str:
|
223 |
-
"""
|
224 |
-
Get a detailed summary and metadata from Wikipedia for a given topic.
|
225 |
-
|
226 |
-
This tool first attempts to fetch a summary from Wikipedia's REST API.
|
227 |
-
If that fails, it uses the MediaWiki search API as a fallback to retrieve top matches.
|
228 |
|
229 |
Args:
|
230 |
-
topic (str):
|
231 |
|
232 |
Returns:
|
233 |
-
str:
|
234 |
"""
|
235 |
-
|
236 |
try:
|
237 |
time.sleep(1)
|
238 |
topic_clean = topic.replace(" ", "_").strip()
|
@@ -240,36 +213,19 @@ def get_detailed_wikipedia(topic: str) -> str:
|
|
240 |
response = requests.get(summary_url, timeout=12)
|
241 |
if response.status_code == 200:
|
242 |
data = response.json()
|
243 |
-
results = [
|
244 |
-
|
245 |
-
|
|
|
246 |
page_url = data.get('content_urls', {}).get('desktop', {}).get('page', '')
|
247 |
if page_url:
|
248 |
results.append(f"URL: {page_url}")
|
249 |
return "\n".join(results)
|
250 |
-
|
251 |
-
search_url = "https://en.wikipedia.org/w/api.php"
|
252 |
-
params = {
|
253 |
-
"action": "query",
|
254 |
-
"format": "json",
|
255 |
-
"list": "search",
|
256 |
-
"srsearch": topic,
|
257 |
-
"srlimit": 5
|
258 |
-
}
|
259 |
-
search_response = requests.get(search_url, params=params, timeout=12)
|
260 |
-
if search_response.status_code == 200:
|
261 |
-
search_data = search_response.json()
|
262 |
-
results = []
|
263 |
-
for item in search_data.get('query', {}).get('search', [])[:3]:
|
264 |
-
title = item['title']
|
265 |
-
snippet = re.sub(r'<[^>]+>', '', item['snippet'])
|
266 |
-
results.append(f"TITLE: {title}\nSNIPPET: {snippet}")
|
267 |
-
return "\n\n".join(results) if results else "No Wikipedia results found"
|
268 |
-
return f"Wikipedia lookup failed for: {topic}"
|
269 |
except Exception as e:
|
270 |
return f"Wikipedia error: {str(e)}"
|
271 |
|
272 |
-
# ---
|
273 |
|
274 |
class OptimizedGAIAAgent:
|
275 |
def __init__(self):
|
@@ -282,18 +238,21 @@ class OptimizedGAIAAgent:
|
|
282 |
get_detailed_wikipedia
|
283 |
]
|
284 |
try:
|
|
|
|
|
|
|
|
|
|
|
285 |
self.agent = CodeAgent(
|
286 |
tools=self.tools,
|
287 |
-
|
288 |
-
|
289 |
)
|
290 |
-
print("✅ CodeAgent initialized")
|
291 |
except Exception as e:
|
292 |
print(f"⚠️ CodeAgent failed: {e}")
|
293 |
self.agent = None
|
294 |
|
295 |
def analyze_and_solve(self, question: str) -> str:
|
296 |
-
"""Analyze question type and provide targeted solution."""
|
297 |
question_lower = question.lower()
|
298 |
if "ecnetnes siht dnatsrednu uoy fi" in question_lower:
|
299 |
return decode_reversed_text(question)
|
@@ -308,7 +267,6 @@ class OptimizedGAIAAgent:
|
|
308 |
return result
|
309 |
if any(term in question_lower for term in ["commutative", "operation", "table", "chess", "checkmate"]):
|
310 |
return solve_advanced_math(question)
|
311 |
-
# Default: Use agent if available
|
312 |
if self.agent:
|
313 |
try:
|
314 |
return self.agent.run(question)
|
@@ -316,10 +274,7 @@ class OptimizedGAIAAgent:
|
|
316 |
return f"Agent error: {str(e)}"
|
317 |
return "No agent available to process the question."
|
318 |
|
319 |
-
#
|
320 |
-
|
321 |
if __name__ == "__main__":
|
322 |
agent = OptimizedGAIAAgent()
|
323 |
-
|
324 |
-
Q = "How many studio albums were published by Mercedes Sosa between 2000 and 2009?"
|
325 |
-
print(agent.analyze_and_solve(Q))
|
|
|
1 |
import os
|
|
|
|
|
|
|
|
|
|
|
2 |
import time
|
3 |
+
import json
|
4 |
import random
|
5 |
+
import re
|
6 |
+
import requests
|
7 |
+
from typing import Dict, Any, List
|
8 |
from smolagents import CodeAgent, tool
|
9 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
# --- Tools ---
|
12 |
|
13 |
@tool
|
14 |
def smart_web_search(query: str) -> str:
|
15 |
+
"""Smart web search with Serper API and Wikipedia fallback.
|
|
|
|
|
|
|
|
|
16 |
|
17 |
Args:
|
18 |
+
query (str): The search query to execute
|
19 |
|
20 |
Returns:
|
21 |
+
str: Search results from Serper API or Wikipedia
|
22 |
"""
|
23 |
try:
|
24 |
time.sleep(random.uniform(1, 3))
|
|
|
26 |
if serper_key:
|
27 |
url = "https://google.serper.dev/search"
|
28 |
payload = json.dumps({"q": query, "num": 5})
|
29 |
+
headers = {'X-API-KEY': serper_key, 'Content-Type': 'application/json'}
|
|
|
|
|
|
|
30 |
response = requests.post(url, headers=headers, data=payload, timeout=15)
|
31 |
if response.status_code == 200:
|
32 |
data = response.json()
|
|
|
44 |
except Exception as e:
|
45 |
return f"Search error: {str(e)}"
|
46 |
|
|
|
|
|
|
|
47 |
@tool
|
48 |
def extract_youtube_details(url: str) -> str:
|
49 |
+
"""Extract details from a YouTube video.
|
|
|
|
|
|
|
|
|
50 |
|
51 |
Args:
|
52 |
+
url (str): The YouTube video URL
|
53 |
|
54 |
Returns:
|
55 |
+
str: Extracted video metadata and bird species info if available
|
|
|
56 |
"""
|
|
|
57 |
try:
|
58 |
video_id = None
|
59 |
patterns = [
|
|
|
69 |
if not video_id:
|
70 |
return "Invalid YouTube URL"
|
71 |
results = []
|
|
|
72 |
oembed_url = f"https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v={video_id}&format=json"
|
73 |
response = requests.get(oembed_url, timeout=10)
|
74 |
if response.status_code == 200:
|
|
|
76 |
results.append(f"TITLE: {data.get('title', '')}")
|
77 |
results.append(f"AUTHOR: {data.get('author_name', '')}")
|
78 |
results.append(f"PROVIDER: {data.get('provider_name', '')}")
|
|
|
79 |
video_url = f"https://www.youtube.com/watch?v={video_id}"
|
80 |
headers = {'User-Agent': 'Mozilla/5.0'}
|
81 |
page_response = requests.get(video_url, headers=headers, timeout=15)
|
|
|
108 |
|
109 |
@tool
|
110 |
def decode_reversed_text(text: str) -> str:
|
111 |
+
"""Decode reversed text.
|
|
|
|
|
|
|
|
|
112 |
|
113 |
Args:
|
114 |
+
text (str): Reversed input text
|
115 |
|
116 |
Returns:
|
117 |
+
str: Decoded text or direction
|
118 |
"""
|
119 |
+
try:
|
120 |
+
if "ecnetnes siht dnatsrednu uoy fi" in text.lower():
|
121 |
+
reversed_text = text[::-1]
|
122 |
+
reversed_lower = reversed_text.lower()
|
123 |
+
opposites = {
|
124 |
+
"left": "right", "right": "left",
|
125 |
+
"up": "down", "down": "up",
|
126 |
+
"north": "south", "south": "north",
|
127 |
+
"east": "west", "west": "east"
|
128 |
+
}
|
129 |
+
for key, value in opposites.items():
|
130 |
+
if key in reversed_lower:
|
131 |
+
return value
|
132 |
+
return reversed_text
|
133 |
+
return text[::-1]
|
134 |
+
except Exception as e:
|
135 |
+
return f"Text decoding error: {str(e)}"
|
136 |
|
137 |
@tool
|
138 |
def solve_advanced_math(problem: str) -> str:
|
139 |
+
"""Solve advanced math problems including commutative tables.
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
|
141 |
Args:
|
142 |
+
problem (str): The math problem or table
|
143 |
|
144 |
Returns:
|
145 |
+
str: Solution or analysis
|
146 |
"""
|
|
|
147 |
try:
|
148 |
problem_lower = problem.lower()
|
149 |
if "commutative" in problem_lower and "|" in problem:
|
|
|
180 |
if numbers:
|
181 |
nums = [float(n) for n in numbers if n.replace('.', '').replace('-', '').isdigit()]
|
182 |
if "average" in problem_lower or "mean" in problem_lower:
|
183 |
+
return str(sum(nums) / len(nums))
|
|
|
184 |
if "sum" in problem_lower or "total" in problem_lower:
|
185 |
+
return str(sum(nums))
|
|
|
186 |
if "product" in problem_lower:
|
187 |
+
result = 1
|
188 |
+
for n in nums:
|
189 |
+
result *= n
|
190 |
+
return str(result)
|
|
|
191 |
if "%" in problem or "percent" in problem_lower:
|
192 |
percentages = re.findall(r'(\d+\.?\d*)%', problem)
|
193 |
if percentages:
|
|
|
198 |
|
199 |
@tool
|
200 |
def get_detailed_wikipedia(topic: str) -> str:
|
201 |
+
"""Get detailed Wikipedia summary.
|
|
|
|
|
|
|
|
|
202 |
|
203 |
Args:
|
204 |
+
topic (str): Topic to search
|
205 |
|
206 |
Returns:
|
207 |
+
str: Summary with title and link
|
208 |
"""
|
|
|
209 |
try:
|
210 |
time.sleep(1)
|
211 |
topic_clean = topic.replace(" ", "_").strip()
|
|
|
213 |
response = requests.get(summary_url, timeout=12)
|
214 |
if response.status_code == 200:
|
215 |
data = response.json()
|
216 |
+
results = [
|
217 |
+
f"TITLE: {data.get('title', '')}",
|
218 |
+
f"EXTRACT: {data.get('extract', '')}"
|
219 |
+
]
|
220 |
page_url = data.get('content_urls', {}).get('desktop', {}).get('page', '')
|
221 |
if page_url:
|
222 |
results.append(f"URL: {page_url}")
|
223 |
return "\n".join(results)
|
224 |
+
return "Wikipedia lookup failed."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
225 |
except Exception as e:
|
226 |
return f"Wikipedia error: {str(e)}"
|
227 |
|
228 |
+
# --- Agent Definition ---
|
229 |
|
230 |
class OptimizedGAIAAgent:
|
231 |
def __init__(self):
|
|
|
238 |
get_detailed_wikipedia
|
239 |
]
|
240 |
try:
|
241 |
+
model_name = "gpt2" # Replace with larger model if needed
|
242 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
243 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
244 |
+
generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
245 |
+
|
246 |
self.agent = CodeAgent(
|
247 |
tools=self.tools,
|
248 |
+
model=generator # ✅ Model object passed here
|
|
|
249 |
)
|
250 |
+
print("✅ CodeAgent initialized with model object")
|
251 |
except Exception as e:
|
252 |
print(f"⚠️ CodeAgent failed: {e}")
|
253 |
self.agent = None
|
254 |
|
255 |
def analyze_and_solve(self, question: str) -> str:
|
|
|
256 |
question_lower = question.lower()
|
257 |
if "ecnetnes siht dnatsrednu uoy fi" in question_lower:
|
258 |
return decode_reversed_text(question)
|
|
|
267 |
return result
|
268 |
if any(term in question_lower for term in ["commutative", "operation", "table", "chess", "checkmate"]):
|
269 |
return solve_advanced_math(question)
|
|
|
270 |
if self.agent:
|
271 |
try:
|
272 |
return self.agent.run(question)
|
|
|
274 |
return f"Agent error: {str(e)}"
|
275 |
return "No agent available to process the question."
|
276 |
|
277 |
+
# To test:
|
|
|
278 |
if __name__ == "__main__":
|
279 |
agent = OptimizedGAIAAgent()
|
280 |
+
print(agent.analyze_and_solve("How many studio albums were published by Mercedes Sosa between 2000 and 2009?"))
|
|
|
|