Commit ·
fe4bb03
1
Parent(s): 81917a3
Updated BasicAgent logic
Browse files- app.py +34 -4
- requirements.txt +2 -1
app.py
CHANGED
|
@@ -13,11 +13,41 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 13 |
class BasicAgent:
|
| 14 |
def __init__(self):
|
| 15 |
print("BasicAgent initialized.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def __call__(self, question: str) -> str:
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 23 |
"""
|
|
|
|
| 13 |
class BasicAgent:
|
| 14 |
def __init__(self):
|
| 15 |
print("BasicAgent initialized.")
|
| 16 |
+
|
| 17 |
+
def search_wikipedia(self, query):
|
| 18 |
+
try:
|
| 19 |
+
url = "https://en.wikipedia.org/api/rest_v1/page/summary/" + query.replace(" ", "_")
|
| 20 |
+
response = requests.get(url, timeout=10)
|
| 21 |
+
|
| 22 |
+
if response.status_code == 200:
|
| 23 |
+
data = response.json()
|
| 24 |
+
return data.get("extract", "")
|
| 25 |
+
|
| 26 |
+
return ""
|
| 27 |
+
|
| 28 |
+
except Exception as e:
|
| 29 |
+
print(f"Search error: {e}")
|
| 30 |
+
return ""
|
| 31 |
+
|
| 32 |
def __call__(self, question: str) -> str:
|
| 33 |
+
|
| 34 |
+
print(f"Question: {question}")
|
| 35 |
+
|
| 36 |
+
try:
|
| 37 |
+
context = self.search_wikipedia(question)
|
| 38 |
+
|
| 39 |
+
if context:
|
| 40 |
+
answer = context[:300]
|
| 41 |
+
else:
|
| 42 |
+
answer = "Could not determine the answer."
|
| 43 |
+
|
| 44 |
+
print(f"Answer: {answer}")
|
| 45 |
+
|
| 46 |
+
return answer
|
| 47 |
+
|
| 48 |
+
except Exception as e:
|
| 49 |
+
print(f"Agent error: {e}")
|
| 50 |
+
return f"Error: {str(e)}"
|
| 51 |
|
| 52 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 53 |
"""
|
requirements.txt
CHANGED
|
@@ -1,2 +1,3 @@
|
|
| 1 |
gradio
|
| 2 |
-
requests
|
|
|
|
|
|
| 1 |
gradio
|
| 2 |
+
requests
|
| 3 |
+
pandas
|