Update app.py
Browse files
app.py
CHANGED
|
@@ -2,72 +2,37 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
-
from smolagents import
|
| 6 |
|
| 7 |
-
# --- Constants ---
|
| 8 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 9 |
|
| 10 |
-
# ====================== TOOLS ======================
|
| 11 |
-
@tool
|
| 12 |
-
def web_search(query: str) -> str:
|
| 13 |
-
"""Search the web."""
|
| 14 |
-
try:
|
| 15 |
-
from duckduckgo_search import DDGS
|
| 16 |
-
with DDGS() as ddgs:
|
| 17 |
-
results = list(ddgs.text(query, max_results=3))
|
| 18 |
-
return "\n".join([f"{r.get('title')}: {r.get('body') or r.get('snippet')}" for r in results])
|
| 19 |
-
except:
|
| 20 |
-
return "Search failed."
|
| 21 |
-
|
| 22 |
-
@tool
|
| 23 |
-
def calculate(expression: str) -> str:
|
| 24 |
-
"""Simple math calculation."""
|
| 25 |
-
try:
|
| 26 |
-
import math
|
| 27 |
-
return str(eval(expression, {"__builtins__": {}}, {"math": math}))
|
| 28 |
-
except:
|
| 29 |
-
return "Calc failed."
|
| 30 |
-
|
| 31 |
-
# ====================== FINAL AGENT ======================
|
| 32 |
class BasicAgent:
|
| 33 |
def __init__(self):
|
|
|
|
| 34 |
self.model = HfApiModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct")
|
| 35 |
-
|
| 36 |
-
self.agent = CodeAgent(
|
| 37 |
-
model=self.model,
|
| 38 |
-
tools=[web_search, calculate],
|
| 39 |
-
add_base_tools=True,
|
| 40 |
-
verbosity_level=0, # CRITICAL: no thinking noise
|
| 41 |
-
max_steps=8,
|
| 42 |
-
planning_interval=3
|
| 43 |
-
)
|
| 44 |
|
| 45 |
def __call__(self, question: str) -> str:
|
| 46 |
try:
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
#
|
| 54 |
-
if "Final Answer:" in answer:
|
| 55 |
-
answer = answer.split("Final Answer:")[-1].strip()
|
| 56 |
if "Answer:" in answer:
|
| 57 |
answer = answer.split("Answer:")[-1].strip()
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
if len(answer) > 400:
|
| 61 |
-
answer = answer.split("\n")[-1].strip()
|
| 62 |
-
|
| 63 |
-
# Final safety: keep it short and clean
|
| 64 |
-
return answer[:600].strip()
|
| 65 |
-
|
| 66 |
except Exception as e:
|
| 67 |
return f"Error: {str(e)[:100]}"
|
| 68 |
|
| 69 |
|
| 70 |
-
# ====================== SUBMISSION ======================
|
| 71 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 72 |
if not profile:
|
| 73 |
return "Please login with Hugging Face first.", None
|
|
@@ -82,7 +47,6 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 82 |
space_id = os.getenv("SPACE_ID")
|
| 83 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else ""
|
| 84 |
|
| 85 |
-
# Fetch questions
|
| 86 |
response = requests.get(questions_url, timeout=20)
|
| 87 |
questions_data = response.json()
|
| 88 |
|
|
@@ -103,7 +67,6 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 103 |
"Answer": answer[:150] + "..." if len(answer) > 150 else answer
|
| 104 |
})
|
| 105 |
|
| 106 |
-
# Submit
|
| 107 |
submission_data = {
|
| 108 |
"username": username,
|
| 109 |
"agent_code": agent_code,
|
|
@@ -115,16 +78,15 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 115 |
response.raise_for_status()
|
| 116 |
result = response.json()
|
| 117 |
score = result.get("score", 0)
|
| 118 |
-
status = f"β
|
| 119 |
return status, pd.DataFrame(results_log)
|
| 120 |
except Exception as e:
|
| 121 |
return f"Submission failed: {str(e)}", pd.DataFrame(results_log)
|
| 122 |
|
| 123 |
|
| 124 |
-
# ====================== GRADIO ======================
|
| 125 |
with gr.Blocks() as demo:
|
| 126 |
-
gr.Markdown("# Unit 4 -
|
| 127 |
-
gr.Markdown("
|
| 128 |
|
| 129 |
gr.LoginButton()
|
| 130 |
btn = gr.Button("π Run Evaluation & Submit All Answers", variant="primary", size="large")
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
+
from smolagents import HfApiModel
|
| 6 |
|
|
|
|
| 7 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
class BasicAgent:
|
| 10 |
def __init__(self):
|
| 11 |
+
print("π Loading model...")
|
| 12 |
self.model = HfApiModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
def __call__(self, question: str) -> str:
|
| 15 |
try:
|
| 16 |
+
prompt = f"""Answer the following question with ONLY the final answer.
|
| 17 |
+
Do not write any explanation, no reasoning, no "Final Answer", no extra text.
|
| 18 |
+
Just give the direct answer.
|
| 19 |
+
|
| 20 |
+
Question: {question}
|
| 21 |
+
|
| 22 |
+
Answer:"""
|
| 23 |
+
|
| 24 |
+
output = self.model.generate(prompt, max_new_tokens=200)
|
| 25 |
+
answer = str(output).strip()
|
| 26 |
|
| 27 |
+
# Final cleaning
|
|
|
|
|
|
|
| 28 |
if "Answer:" in answer:
|
| 29 |
answer = answer.split("Answer:")[-1].strip()
|
| 30 |
+
return answer[:500].strip()
|
| 31 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
except Exception as e:
|
| 33 |
return f"Error: {str(e)[:100]}"
|
| 34 |
|
| 35 |
|
|
|
|
| 36 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 37 |
if not profile:
|
| 38 |
return "Please login with Hugging Face first.", None
|
|
|
|
| 47 |
space_id = os.getenv("SPACE_ID")
|
| 48 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else ""
|
| 49 |
|
|
|
|
| 50 |
response = requests.get(questions_url, timeout=20)
|
| 51 |
questions_data = response.json()
|
| 52 |
|
|
|
|
| 67 |
"Answer": answer[:150] + "..." if len(answer) > 150 else answer
|
| 68 |
})
|
| 69 |
|
|
|
|
| 70 |
submission_data = {
|
| 71 |
"username": username,
|
| 72 |
"agent_code": agent_code,
|
|
|
|
| 78 |
response.raise_for_status()
|
| 79 |
result = response.json()
|
| 80 |
score = result.get("score", 0)
|
| 81 |
+
status = f"β
Submission Successful!\nScore: {score}% ({result.get('correct_count',0)}/20)\nMessage: {result.get('message','')}"
|
| 82 |
return status, pd.DataFrame(results_log)
|
| 83 |
except Exception as e:
|
| 84 |
return f"Submission failed: {str(e)}", pd.DataFrame(results_log)
|
| 85 |
|
| 86 |
|
|
|
|
| 87 |
with gr.Blocks() as demo:
|
| 88 |
+
gr.Markdown("# Unit 4 - GAIA Agent (Simple Version)")
|
| 89 |
+
gr.Markdown("Login β Click the button below.")
|
| 90 |
|
| 91 |
gr.LoginButton()
|
| 92 |
btn = gr.Button("π Run Evaluation & Submit All Answers", variant="primary", size="large")
|