Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,76 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
-
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
-
from
|
| 7 |
-
from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel, tool
|
| 8 |
from bs4 import BeautifulSoup
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
# --- Constants ---
|
| 12 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
def browse_website(url: str) -> str:
|
| 20 |
-
"""Browse a website and extract its content as text.
|
| 21 |
-
|
| 22 |
-
Args:
|
| 23 |
-
url: A string URL to the website
|
| 24 |
-
|
| 25 |
-
Returns:
|
| 26 |
-
The text content of the website
|
| 27 |
-
"""
|
| 28 |
-
try:
|
| 29 |
-
response = requests.get(url, timeout=10)
|
| 30 |
-
soup = BeautifulSoup(response.text, 'html.parser')
|
| 31 |
-
for script in soup(['script', 'style']):
|
| 32 |
-
script.extract()
|
| 33 |
-
text = soup.get_text()
|
| 34 |
-
lines = (line.strip() for line in text.splitlines())
|
| 35 |
-
chunks = (phrase.strip() for line in lines for phrase in line.split(" "))
|
| 36 |
-
text = '\n'.join(chunk for chunk in chunks if chunk)
|
| 37 |
-
return text[:8000]
|
| 38 |
-
except Exception as e:
|
| 39 |
-
return f"Error browsing website: {str(e)}"
|
| 40 |
|
| 41 |
-
|
| 42 |
-
def
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
Args:
|
| 46 |
-
expression: A string containing a mathematical expression
|
| 47 |
|
| 48 |
-
|
| 49 |
-
The result of the evaluation
|
| 50 |
-
"""
|
| 51 |
try:
|
| 52 |
-
#
|
| 53 |
-
|
| 54 |
-
|
| 55 |
except Exception as e:
|
| 56 |
-
|
|
|
|
| 57 |
|
| 58 |
|
| 59 |
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
|
|
|
| 4 |
import pandas as pd
|
| 5 |
+
from duckduckgo_search import DDGS
|
|
|
|
| 6 |
from bs4 import BeautifulSoup
|
| 7 |
|
| 8 |
+
# Constants
|
|
|
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
|
| 11 |
+
# Simple Agent implementation that doesn't need complex model integrations
|
| 12 |
+
class SimpleAgent:
|
| 13 |
+
def __init__(self):
|
| 14 |
+
self.search = DDGS()
|
| 15 |
+
print("SimpleAgent initialized - using direct web search without LLM")
|
| 16 |
+
|
| 17 |
+
def run(self, question):
|
| 18 |
+
try:
|
| 19 |
+
# 1. Search for information
|
| 20 |
+
search_results = self.search.text(question, max_results=5)
|
| 21 |
+
|
| 22 |
+
if not search_results:
|
| 23 |
+
return "No information found."
|
| 24 |
+
|
| 25 |
+
# 2. Extract a simple response from search results
|
| 26 |
+
results_text = []
|
| 27 |
+
for result in search_results[:3]:
|
| 28 |
+
results_text.append(result['body'])
|
| 29 |
+
|
| 30 |
+
# 3. Clean up and format a response
|
| 31 |
+
# For GAIA, often the answer is a simple entity, date, number, or short phrase
|
| 32 |
+
# Extract the first 100 words as a simple approximation
|
| 33 |
+
combined_text = " ".join(results_text)
|
| 34 |
+
words = combined_text.split()[:100]
|
| 35 |
+
answer = " ".join(words)
|
| 36 |
+
|
| 37 |
+
# 4. Further simplify the answer (this helps with GAIA's exact matching)
|
| 38 |
+
# Often GAIA wants very concise answers like names, years, or simple phrases
|
| 39 |
+
if "when" in question.lower() or "year" in question.lower() or "date" in question.lower():
|
| 40 |
+
# Look for years
|
| 41 |
+
import re
|
| 42 |
+
years = re.findall(r'\b(19|20)\d{2}\b', answer)
|
| 43 |
+
if years:
|
| 44 |
+
return years[0] # Return first year found
|
| 45 |
+
|
| 46 |
+
if "who" in question.lower() or "person" in question.lower():
|
| 47 |
+
# Try to find a name
|
| 48 |
+
words = answer.split()
|
| 49 |
+
for i in range(len(words) - 1):
|
| 50 |
+
if words[i][0].isupper() and words[i+1][0].isupper():
|
| 51 |
+
return f"{words[i]} {words[i+1]}"
|
| 52 |
+
|
| 53 |
+
# Default: return first 10 words (which often contains the direct answer)
|
| 54 |
+
return " ".join(words[:10])
|
| 55 |
+
|
| 56 |
+
except Exception as e:
|
| 57 |
+
return f"Error searching for information: {str(e)}"
|
| 58 |
|
| 59 |
+
# Create agent
|
| 60 |
+
agent = SimpleAgent()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
# The rest of your run_and_submit_all function remains the same, just replace this part:
|
| 63 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 64 |
+
# (beginning part remains the same)
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
# 1. Instantiate Agent
|
|
|
|
|
|
|
| 67 |
try:
|
| 68 |
+
# Use our simple agent that doesn't need complex LLM API calls
|
| 69 |
+
# agent is already defined above
|
| 70 |
+
print("Using SimpleAgent for evaluation")
|
| 71 |
except Exception as e:
|
| 72 |
+
print(f"Error instantiating agent: {e}")
|
| 73 |
+
return f"Error initializing agent: {e}", None
|
| 74 |
|
| 75 |
|
| 76 |
|