Spaces:
Configuration error
Configuration error
Arvid Zöllner commited on
Commit ·
a2cfc30
1
Parent(s): 4ea534f
Agent llm changed to duckduckgo
Browse files
app.py
CHANGED
|
@@ -1,62 +1,54 @@
|
|
| 1 |
import os
|
| 2 |
-
import requests
|
| 3 |
import gradio as gr
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
from
|
| 7 |
-
from
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
def __init__(self):
|
| 12 |
-
self.name = "DuckDuckGoSearch"
|
| 13 |
-
self.description = "Tool to search DuckDuckGo for an answer."
|
| 14 |
|
| 15 |
-
def __call__(self, query: str) -> str:
|
| 16 |
-
"""Durchführt eine Suche mit DuckDuckGo und gibt die ersten 3 Ergebnisse zurück."""
|
| 17 |
-
try:
|
| 18 |
-
search_results = ddg_search(query)
|
| 19 |
-
results = [result["title"] + " - " + result["url"] for result in search_results[:3]]
|
| 20 |
-
return "\n".join(results)
|
| 21 |
-
except Exception as e:
|
| 22 |
-
return f"Fehler bei der DuckDuckGo-Suche: {e}"
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
class
|
| 26 |
def __init__(self):
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
token = os.getenv("HF_TOKEN")
|
| 31 |
-
if not token:
|
| 32 |
-
raise ValueError("HF_TOKEN environment variable is missing.")
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
# Modell initialisieren
|
| 38 |
-
self.model = HfApiModel(model="mistralai/Mistral-7B-Instruct-v0.1")
|
| 39 |
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
self.agent = CodeAgent(
|
| 42 |
-
tools=[
|
| 43 |
-
model=self.model,
|
| 44 |
-
max_steps=5,
|
| 45 |
name="search_agent",
|
| 46 |
-
description="Agent that performs web searches using DuckDuckGo."
|
|
|
|
| 47 |
)
|
| 48 |
|
| 49 |
def __call__(self, question: str) -> str:
|
| 50 |
print(f"Agent running for question: {question[:60]}...")
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
# Funktion, um Fragen zu beantworten und die Antworten zu senden
|
| 54 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 55 |
"""
|
| 56 |
-
|
| 57 |
-
|
| 58 |
"""
|
| 59 |
-
# ---
|
| 60 |
space_id = os.getenv("SPACE_ID")
|
| 61 |
|
| 62 |
if profile:
|
|
@@ -66,11 +58,11 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 66 |
print("User not logged in.")
|
| 67 |
return "Please Login to Hugging Face with the button.", None
|
| 68 |
|
| 69 |
-
api_url =
|
| 70 |
questions_url = f"{api_url}/questions"
|
| 71 |
submit_url = f"{api_url}/submit"
|
| 72 |
|
| 73 |
-
# 1.
|
| 74 |
try:
|
| 75 |
agent = MySmolAgent()
|
| 76 |
except Exception as e:
|
|
@@ -80,7 +72,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 80 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 81 |
print(agent_code)
|
| 82 |
|
| 83 |
-
# 2.
|
| 84 |
print(f"Fetching questions from: {questions_url}")
|
| 85 |
try:
|
| 86 |
response = requests.get(questions_url, timeout=15)
|
|
@@ -93,14 +85,8 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 93 |
except requests.exceptions.RequestException as e:
|
| 94 |
print(f"Error fetching questions: {e}")
|
| 95 |
return f"Error fetching questions: {e}", None
|
| 96 |
-
except requests.exceptions.JSONDecodeError as e:
|
| 97 |
-
print(f"Error decoding JSON response from questions endpoint: {e}")
|
| 98 |
-
return f"Error decoding server response for questions: {e}", None
|
| 99 |
-
except Exception as e:
|
| 100 |
-
print(f"An unexpected error occurred fetching questions: {e}")
|
| 101 |
-
return f"An unexpected error occurred fetching questions: {e}", None
|
| 102 |
|
| 103 |
-
# 3.
|
| 104 |
results_log = []
|
| 105 |
answers_payload = []
|
| 106 |
print(f"Running agent on {len(questions_data)} questions...")
|
|
@@ -115,19 +101,19 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 115 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 116 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 117 |
except Exception as e:
|
| 118 |
-
|
| 119 |
-
|
| 120 |
|
| 121 |
if not answers_payload:
|
| 122 |
print("Agent did not produce any answers to submit.")
|
| 123 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 124 |
|
| 125 |
-
# 4.
|
| 126 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 127 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 128 |
print(status_update)
|
| 129 |
|
| 130 |
-
# 5.
|
| 131 |
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
| 132 |
try:
|
| 133 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
|
@@ -143,18 +129,35 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 143 |
print("Submission successful.")
|
| 144 |
results_df = pd.DataFrame(results_log)
|
| 145 |
return final_status, results_df
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
except requests.exceptions.RequestException as e:
|
| 147 |
-
|
| 148 |
-
print(
|
| 149 |
results_df = pd.DataFrame(results_log)
|
| 150 |
-
return
|
| 151 |
except Exception as e:
|
| 152 |
status_message = f"An unexpected error occurred during submission: {e}"
|
| 153 |
print(status_message)
|
| 154 |
results_df = pd.DataFrame(results_log)
|
| 155 |
return status_message, results_df
|
| 156 |
|
| 157 |
-
|
|
|
|
| 158 |
with gr.Blocks() as demo:
|
| 159 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
| 160 |
gr.Markdown(
|
|
@@ -175,6 +178,7 @@ with gr.Blocks() as demo:
|
|
| 175 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 176 |
|
| 177 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
|
|
|
| 178 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 179 |
|
| 180 |
run_button.click(
|
|
@@ -183,5 +187,4 @@ with gr.Blocks() as demo:
|
|
| 183 |
)
|
| 184 |
|
| 185 |
if __name__ == "__main__":
|
| 186 |
-
|
| 187 |
-
demo.launch(debug=True, share=False)
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
+
import requests
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from duckduckgo_search import DDGS
|
| 6 |
+
from smolagents import CodeAgent, Tool
|
| 7 |
|
| 8 |
+
# --- Constants ---
|
| 9 |
+
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
|
|
|
|
|
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# --- DuckDuckGo Search Tool ---
|
| 13 |
+
class DuckDuckGoSearchTool(Tool):
|
| 14 |
def __init__(self):
|
| 15 |
+
self.name = "DuckDuckGo Search"
|
| 16 |
+
self.description = "Searches DuckDuckGo for answers."
|
| 17 |
+
self.ddgs = DDGS()
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
def run(self, inputs: dict) -> str:
|
| 20 |
+
query = inputs.get("query", "")
|
| 21 |
+
results = self.ddgs.text(query)
|
| 22 |
+
if results:
|
| 23 |
+
return results['abstract']
|
| 24 |
+
return "No results found."
|
| 25 |
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
# --- Basic Agent Definition ---
|
| 28 |
+
class MySmolAgent:
|
| 29 |
+
def __init__(self):
|
| 30 |
+
print("Initializing SmolAgent...")
|
| 31 |
+
self.search_tool = DuckDuckGoSearchTool() # The search tool
|
| 32 |
self.agent = CodeAgent(
|
| 33 |
+
tools=[self.search_tool], # Add DuckDuckGoSearchTool to the agent
|
|
|
|
|
|
|
| 34 |
name="search_agent",
|
| 35 |
+
description="Agent that performs web searches using DuckDuckGo.",
|
| 36 |
+
max_steps=5
|
| 37 |
)
|
| 38 |
|
| 39 |
def __call__(self, question: str) -> str:
|
| 40 |
print(f"Agent running for question: {question[:60]}...")
|
| 41 |
+
# Input must be a dictionary where "query" is the search term
|
| 42 |
+
inputs = {"query": question}
|
| 43 |
+
return self.agent.run(inputs)
|
| 44 |
+
|
| 45 |
|
|
|
|
| 46 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 47 |
"""
|
| 48 |
+
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 49 |
+
and displays the results.
|
| 50 |
"""
|
| 51 |
+
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 52 |
space_id = os.getenv("SPACE_ID")
|
| 53 |
|
| 54 |
if profile:
|
|
|
|
| 58 |
print("User not logged in.")
|
| 59 |
return "Please Login to Hugging Face with the button.", None
|
| 60 |
|
| 61 |
+
api_url = DEFAULT_API_URL
|
| 62 |
questions_url = f"{api_url}/questions"
|
| 63 |
submit_url = f"{api_url}/submit"
|
| 64 |
|
| 65 |
+
# 1. Instantiate Agent (modify this part to create your agent)
|
| 66 |
try:
|
| 67 |
agent = MySmolAgent()
|
| 68 |
except Exception as e:
|
|
|
|
| 72 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 73 |
print(agent_code)
|
| 74 |
|
| 75 |
+
# 2. Fetch Questions
|
| 76 |
print(f"Fetching questions from: {questions_url}")
|
| 77 |
try:
|
| 78 |
response = requests.get(questions_url, timeout=15)
|
|
|
|
| 85 |
except requests.exceptions.RequestException as e:
|
| 86 |
print(f"Error fetching questions: {e}")
|
| 87 |
return f"Error fetching questions: {e}", None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
+
# 3. Run your Agent
|
| 90 |
results_log = []
|
| 91 |
answers_payload = []
|
| 92 |
print(f"Running agent on {len(questions_data)} questions...")
|
|
|
|
| 101 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 102 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 103 |
except Exception as e:
|
| 104 |
+
print(f"Error running agent on task {task_id}: {e}")
|
| 105 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
| 106 |
|
| 107 |
if not answers_payload:
|
| 108 |
print("Agent did not produce any answers to submit.")
|
| 109 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 110 |
|
| 111 |
+
# 4. Prepare Submission
|
| 112 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 113 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 114 |
print(status_update)
|
| 115 |
|
| 116 |
+
# 5. Submit
|
| 117 |
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
| 118 |
try:
|
| 119 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
|
|
|
| 129 |
print("Submission successful.")
|
| 130 |
results_df = pd.DataFrame(results_log)
|
| 131 |
return final_status, results_df
|
| 132 |
+
except requests.exceptions.HTTPError as e:
|
| 133 |
+
error_detail = f"Server responded with status {e.response.status_code}."
|
| 134 |
+
try:
|
| 135 |
+
error_json = e.response.json()
|
| 136 |
+
error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
|
| 137 |
+
except requests.exceptions.JSONDecodeError:
|
| 138 |
+
error_detail += f" Response: {e.response.text[:500]}"
|
| 139 |
+
status_message = f"Submission Failed: {error_detail}"
|
| 140 |
+
print(status_message)
|
| 141 |
+
results_df = pd.DataFrame(results_log)
|
| 142 |
+
return status_message, results_df
|
| 143 |
+
except requests.exceptions.Timeout:
|
| 144 |
+
status_message = "Submission Failed: The request timed out."
|
| 145 |
+
print(status_message)
|
| 146 |
+
results_df = pd.DataFrame(results_log)
|
| 147 |
+
return status_message, results_df
|
| 148 |
except requests.exceptions.RequestException as e:
|
| 149 |
+
status_message = f"Submission Failed: Network error - {e}"
|
| 150 |
+
print(status_message)
|
| 151 |
results_df = pd.DataFrame(results_log)
|
| 152 |
+
return status_message, results_df
|
| 153 |
except Exception as e:
|
| 154 |
status_message = f"An unexpected error occurred during submission: {e}"
|
| 155 |
print(status_message)
|
| 156 |
results_df = pd.DataFrame(results_log)
|
| 157 |
return status_message, results_df
|
| 158 |
|
| 159 |
+
|
| 160 |
+
# --- Build Gradio Interface using Blocks ---
|
| 161 |
with gr.Blocks() as demo:
|
| 162 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
| 163 |
gr.Markdown(
|
|
|
|
| 178 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 179 |
|
| 180 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 181 |
+
# Removed max_rows=10 from DataFrame constructor
|
| 182 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 183 |
|
| 184 |
run_button.click(
|
|
|
|
| 187 |
)
|
| 188 |
|
| 189 |
if __name__ == "__main__":
|
| 190 |
+
demo.launch(debug=True)
|
|
|