Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,21 +2,15 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
-
from smolagents import CodeAgent, DuckDuckGoSearchTool,
|
| 6 |
|
| 7 |
-
# --- Constants ---
|
| 8 |
-
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 9 |
-
import gradio as gr
|
| 10 |
-
import requests
|
| 11 |
-
import pandas as pd
|
| 12 |
# --- Constants ---
|
| 13 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 14 |
|
| 15 |
# --- Agent Definition ---
|
| 16 |
-
# This defines the "Brain" and "Tools" for your agent
|
| 17 |
def create_agent():
|
| 18 |
# We use Qwen2.5-Coder as the brain 🧠
|
| 19 |
-
model =
|
| 20 |
|
| 21 |
# We give it the search tool so it can find answers on the web 🔍
|
| 22 |
search_tool = DuckDuckGoSearchTool()
|
|
@@ -27,28 +21,29 @@ def create_agent():
|
|
| 27 |
model=model,
|
| 28 |
additional_authorized_imports=["requests", "pandas", "numpy", "time", "re", "math"]
|
| 29 |
)
|
|
|
|
| 30 |
return agent
|
| 31 |
|
| 32 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 33 |
space_id = os.getenv("SPACE_ID")
|
|
|
|
| 34 |
if profile:
|
| 35 |
username = f"{profile.username}"
|
| 36 |
else:
|
| 37 |
return "Please Login to Hugging Face with the button.", None
|
| 38 |
-
|
| 39 |
api_url = DEFAULT_API_URL
|
| 40 |
questions_url = f"{api_url}/questions"
|
| 41 |
submit_url = f"{api_url}/submit"
|
| 42 |
-
|
| 43 |
# 1. Instantiate the real Agent
|
| 44 |
try:
|
| 45 |
-
# Instead of BasicAgent(), we run the run() method of our CodeAgent
|
| 46 |
smol_agent = create_agent()
|
| 47 |
except Exception as e:
|
| 48 |
return f"Error initializing agent: {e}", None
|
| 49 |
-
|
| 50 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 51 |
-
|
| 52 |
# 2. Fetch Questions
|
| 53 |
try:
|
| 54 |
response = requests.get(questions_url, timeout=15)
|
|
@@ -56,7 +51,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 56 |
questions_data = response.json()
|
| 57 |
except Exception as e:
|
| 58 |
return f"Error fetching questions: {e}", None
|
| 59 |
-
|
| 60 |
# 3. Run your Agent
|
| 61 |
results_log = []
|
| 62 |
answers_payload = []
|
|
@@ -72,9 +67,10 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 72 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": str(submitted_answer)})
|
| 73 |
except Exception as e:
|
| 74 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"ERROR: {e}"})
|
| 75 |
-
|
| 76 |
# 4. Submit
|
| 77 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
|
|
|
| 78 |
try:
|
| 79 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
| 80 |
response.raise_for_status()
|
|
@@ -91,7 +87,6 @@ with gr.Blocks() as demo:
|
|
| 91 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 92 |
status_output = gr.Textbox(label="Status", lines=5)
|
| 93 |
results_table = gr.DataFrame(label="Results", wrap=True)
|
| 94 |
-
|
| 95 |
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
|
| 96 |
|
| 97 |
if __name__ == "__main__":
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, ApiModel #
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# --- Constants ---
|
| 8 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 9 |
|
| 10 |
# --- Agent Definition ---
|
|
|
|
| 11 |
def create_agent():
|
| 12 |
# We use Qwen2.5-Coder as the brain 🧠
|
| 13 |
+
model = ApiModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct") #
|
| 14 |
|
| 15 |
# We give it the search tool so it can find answers on the web 🔍
|
| 16 |
search_tool = DuckDuckGoSearchTool()
|
|
|
|
| 21 |
model=model,
|
| 22 |
additional_authorized_imports=["requests", "pandas", "numpy", "time", "re", "math"]
|
| 23 |
)
|
| 24 |
+
|
| 25 |
return agent
|
| 26 |
|
| 27 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 28 |
space_id = os.getenv("SPACE_ID")
|
| 29 |
+
|
| 30 |
if profile:
|
| 31 |
username = f"{profile.username}"
|
| 32 |
else:
|
| 33 |
return "Please Login to Hugging Face with the button.", None
|
| 34 |
+
|
| 35 |
api_url = DEFAULT_API_URL
|
| 36 |
questions_url = f"{api_url}/questions"
|
| 37 |
submit_url = f"{api_url}/submit"
|
| 38 |
+
|
| 39 |
# 1. Instantiate the real Agent
|
| 40 |
try:
|
|
|
|
| 41 |
smol_agent = create_agent()
|
| 42 |
except Exception as e:
|
| 43 |
return f"Error initializing agent: {e}", None
|
| 44 |
+
|
| 45 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 46 |
+
|
| 47 |
# 2. Fetch Questions
|
| 48 |
try:
|
| 49 |
response = requests.get(questions_url, timeout=15)
|
|
|
|
| 51 |
questions_data = response.json()
|
| 52 |
except Exception as e:
|
| 53 |
return f"Error fetching questions: {e}", None
|
| 54 |
+
|
| 55 |
# 3. Run your Agent
|
| 56 |
results_log = []
|
| 57 |
answers_payload = []
|
|
|
|
| 67 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": str(submitted_answer)})
|
| 68 |
except Exception as e:
|
| 69 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"ERROR: {e}"})
|
| 70 |
+
|
| 71 |
# 4. Submit
|
| 72 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 73 |
+
|
| 74 |
try:
|
| 75 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
| 76 |
response.raise_for_status()
|
|
|
|
| 87 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 88 |
status_output = gr.Textbox(label="Status", lines=5)
|
| 89 |
results_table = gr.DataFrame(label="Results", wrap=True)
|
|
|
|
| 90 |
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
|
| 91 |
|
| 92 |
if __name__ == "__main__":
|