Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import requests
|
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
import wikipedia
|
|
|
|
| 7 |
# (Keep Constants as is)
|
| 8 |
# --- Constants ---
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
@@ -24,14 +25,27 @@ class EchoAgent:
|
|
| 24 |
|
| 25 |
class WikipediaAgent:
|
| 26 |
def __init__(self):
|
| 27 |
-
|
| 28 |
def __call__(self, question: str) -> str:
|
| 29 |
try:
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
except Exception as e:
|
| 33 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
def run_and_submit_all(agent_name: str, profile: gr.OAuthProfile | None = None):
|
| 36 |
"""
|
| 37 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
|
@@ -59,6 +73,8 @@ def run_and_submit_all(agent_name: str, profile: gr.OAuthProfile | None = None):
|
|
| 59 |
agent = EchoAgent()
|
| 60 |
elif agent_name == "WikipediaAgent":
|
| 61 |
agent = WikipediaAgent()
|
|
|
|
|
|
|
| 62 |
else:
|
| 63 |
return f"Unknown agent selected: {agent_name}", None
|
| 64 |
except Exception as e:
|
|
@@ -167,7 +183,7 @@ with gr.Blocks() as demo:
|
|
| 167 |
gr.LoginButton()
|
| 168 |
|
| 169 |
agent_dropdown = gr.Dropdown(
|
| 170 |
-
choices=["BasicAgent", "EchoAgent", "WikipediaAgent"],
|
| 171 |
label="Select Agent",
|
| 172 |
value="BasicAgent"
|
| 173 |
)
|
|
|
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
import wikipedia
|
| 7 |
+
from transformers import pipeline
|
| 8 |
# (Keep Constants as is)
|
| 9 |
# --- Constants ---
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
| 25 |
|
| 26 |
class WikipediaAgent:
|
| 27 |
def __init__(self):
|
| 28 |
+
wikipedia.set_lang("en")
|
| 29 |
def __call__(self, question: str) -> str:
|
| 30 |
try:
|
| 31 |
+
# Extract potential keyword
|
| 32 |
+
query = question.split(" ")[-1] # naive keyword pick
|
| 33 |
+
page = wikipedia.page(query)
|
| 34 |
+
return wikipedia.summary(page.title, sentences=1)
|
| 35 |
except Exception as e:
|
| 36 |
+
return "Could not answer"
|
| 37 |
+
class TransformersAgent:
|
| 38 |
+
def __init__(self):
|
| 39 |
+
print("Loading transformers pipeline (flan-t5-base)...")
|
| 40 |
+
self.model = pipeline("text2text-generation", model="google/flan-t5-base", device=-1) # CPU only
|
| 41 |
|
| 42 |
+
def __call__(self, question: str) -> str:
|
| 43 |
+
try:
|
| 44 |
+
response = self.model(question, max_new_tokens=100)
|
| 45 |
+
return response[0]['generated_text'].strip()
|
| 46 |
+
except Exception as e:
|
| 47 |
+
return f"Error: {str(e)}"
|
| 48 |
+
|
| 49 |
def run_and_submit_all(agent_name: str, profile: gr.OAuthProfile | None = None):
|
| 50 |
"""
|
| 51 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
|
|
|
| 73 |
agent = EchoAgent()
|
| 74 |
elif agent_name == "WikipediaAgent":
|
| 75 |
agent = WikipediaAgent()
|
| 76 |
+
elif agent_name == "TransformersAgent":
|
| 77 |
+
agent = TransformersAgent()
|
| 78 |
else:
|
| 79 |
return f"Unknown agent selected: {agent_name}", None
|
| 80 |
except Exception as e:
|
|
|
|
| 183 |
gr.LoginButton()
|
| 184 |
|
| 185 |
agent_dropdown = gr.Dropdown(
|
| 186 |
+
choices=["BasicAgent", "EchoAgent", "WikipediaAgent", "TransformersAgent"],
|
| 187 |
label="Select Agent",
|
| 188 |
value="BasicAgent"
|
| 189 |
)
|