New attempt.
Browse files- app.py +58 -27
- requirements.txt +1 -2
app.py
CHANGED
@@ -1,11 +1,7 @@
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
import requests
|
4 |
-
import inspect
|
5 |
import pandas as pd
|
6 |
-
from smolagents.agent import PythonInterpreterAgent
|
7 |
-
from smolagents.search import WebSummarySearch
|
8 |
-
from smolagents.task_manager import PythonInterpreterTaskManager
|
9 |
|
10 |
# (Keep Constants as is)
|
11 |
# --- Constants ---
|
@@ -16,32 +12,67 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
16 |
class BasicAgent:
|
17 |
def __init__(self):
|
18 |
print("BasicAgent initialized.")
|
19 |
-
# Initialize web search capability
|
20 |
-
self.search_tool = WebSummarySearch()
|
21 |
-
# Initialize Python interpreter agent with search tool
|
22 |
-
self.task_manager = PythonInterpreterTaskManager(
|
23 |
-
tools=[self.search_tool],
|
24 |
-
)
|
25 |
-
self.agent = PythonInterpreterAgent(self.task_manager)
|
26 |
-
print("Search capability initialized.")
|
27 |
|
28 |
-
def
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
try:
|
31 |
-
#
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
except Exception as e:
|
43 |
-
print(f"Error
|
44 |
-
return f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
47 |
"""
|
@@ -166,7 +197,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
166 |
|
167 |
# --- Build Gradio Interface using Blocks ---
|
168 |
with gr.Blocks() as demo:
|
169 |
-
gr.Markdown("# Basic Agent Evaluation Runner (Attempt #
|
170 |
gr.Markdown(
|
171 |
"""
|
172 |
**Instructions:**
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
import requests
|
|
|
4 |
import pandas as pd
|
|
|
|
|
|
|
5 |
|
6 |
# (Keep Constants as is)
|
7 |
# --- Constants ---
|
|
|
12 |
class BasicAgent:
|
13 |
def __init__(self):
|
14 |
print("BasicAgent initialized.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
+
def search_internet(self, query: str) -> str:
|
17 |
+
"""
|
18 |
+
Search the internet for information using DuckDuckGo.
|
19 |
+
This is a simple implementation that returns search results as text.
|
20 |
+
|
21 |
+
Args:
|
22 |
+
query (str): The search query
|
23 |
+
|
24 |
+
Returns:
|
25 |
+
str: Search results as text
|
26 |
+
"""
|
27 |
+
print(f"Searching internet for: {query}")
|
28 |
try:
|
29 |
+
# Using DuckDuckGo search API
|
30 |
+
headers = {
|
31 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
|
32 |
+
}
|
33 |
+
search_url = f"https://api.duckduckgo.com/?q={query}&format=json&no_html=1&no_redirect=1&skip_disambig=1"
|
34 |
+
|
35 |
+
response = requests.get(search_url, headers=headers, timeout=10)
|
36 |
+
response.raise_for_status()
|
37 |
+
data = response.json()
|
38 |
|
39 |
+
# Extract information from the response
|
40 |
+
results = []
|
41 |
+
|
42 |
+
# Extract abstract information if available
|
43 |
+
if data.get('Abstract'):
|
44 |
+
results.append(f"Abstract: {data.get('Abstract')}")
|
45 |
+
|
46 |
+
# Extract related topics
|
47 |
+
for topic in data.get('RelatedTopics', [])[:5]: # Limit to first 5 topics
|
48 |
+
if 'Text' in topic:
|
49 |
+
results.append(f"- {topic['Text']}")
|
50 |
+
|
51 |
+
if results:
|
52 |
+
return "\n".join(results)
|
53 |
+
else:
|
54 |
+
return "No relevant information found."
|
55 |
+
|
56 |
except Exception as e:
|
57 |
+
print(f"Error searching internet: {e}")
|
58 |
+
return f"Error performing internet search: {str(e)}"
|
59 |
+
|
60 |
+
def __call__(self, question: str) -> str:
|
61 |
+
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
62 |
+
|
63 |
+
# Use internet search to find answer
|
64 |
+
search_results = self.search_internet(question)
|
65 |
+
|
66 |
+
# Create a response based on search results
|
67 |
+
if (search_results and search_results != "No relevant information found." and
|
68 |
+
not search_results.startswith("Error")):
|
69 |
+
answer = f"Based on my search, I found this information:\n\n{search_results}"
|
70 |
+
else:
|
71 |
+
# Fallback to default answer if search fails
|
72 |
+
answer = "I couldn't find specific information about that question."
|
73 |
+
|
74 |
+
print(f"Agent returning answer based on internet search.")
|
75 |
+
return answer
|
76 |
|
77 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
78 |
"""
|
|
|
197 |
|
198 |
# --- Build Gradio Interface using Blocks ---
|
199 |
with gr.Blocks() as demo:
|
200 |
+
gr.Markdown("# Basic Agent Evaluation Runner (Attempt #1)")
|
201 |
gr.Markdown(
|
202 |
"""
|
203 |
**Instructions:**
|
requirements.txt
CHANGED
@@ -1,3 +1,2 @@
|
|
1 |
gradio
|
2 |
-
requests
|
3 |
-
git+https://github.com/smol-ai/smolagent.git
|
|
|
1 |
gradio
|
2 |
+
requests
|
|