Update app.py
Browse files
app.py
CHANGED
|
@@ -1,49 +1,40 @@
|
|
| 1 |
import os
|
| 2 |
-
from dotenv import load_dotenv
|
| 3 |
import gradio as gr
|
| 4 |
import requests
|
| 5 |
-
import
|
| 6 |
import pandas as pd
|
|
|
|
| 7 |
|
| 8 |
-
from
|
| 9 |
-
from langfuse.callback import CallbackHandler
|
| 10 |
|
| 11 |
-
# env variables test
|
| 12 |
load_dotenv()
|
| 13 |
|
| 14 |
-
|
| 15 |
-
os.environ["LANGFUSE_SECRET_KEY"] = os.getenv("LANGFUSE_SECRET_KEY")
|
| 16 |
# (Keep Constants as is)
|
| 17 |
# --- Constants ---
|
| 18 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 19 |
|
| 20 |
# --- Basic Agent Definition ---
|
| 21 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 22 |
-
|
| 23 |
-
|
| 24 |
class BasicAgent:
|
| 25 |
def __init__(self):
|
| 26 |
-
self.langfuse_handler = CallbackHandler()
|
| 27 |
print("BasicAgent initialized.")
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
print(f"Agent returning fixed answer: {fixed_answer['expected_answer']}")
|
| 45 |
-
return fixed_answer['expected_answer']
|
| 46 |
-
|
| 47 |
|
| 48 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 49 |
"""
|
|
@@ -51,7 +42,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 51 |
and displays the results.
|
| 52 |
"""
|
| 53 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 54 |
-
space_id = os.getenv("SPACE_ID")
|
| 55 |
|
| 56 |
if profile:
|
| 57 |
username= f"{profile.username}"
|
|
@@ -70,8 +61,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 70 |
except Exception as e:
|
| 71 |
print(f"Error instantiating agent: {e}")
|
| 72 |
return f"Error initializing agent: {e}", None
|
| 73 |
-
# In the case of an app running as a hugging Face space, this link points toward your codebase
|
| 74 |
-
# ( useful for others so please keep it public)
|
| 75 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 76 |
print(agent_code)
|
| 77 |
|
|
@@ -101,14 +91,13 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 101 |
answers_payload = []
|
| 102 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 103 |
for item in questions_data:
|
| 104 |
-
time.sleep(10)
|
| 105 |
task_id = item.get("task_id")
|
| 106 |
question_text = item.get("question")
|
| 107 |
if not task_id or question_text is None:
|
| 108 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 109 |
continue
|
| 110 |
try:
|
| 111 |
-
submitted_answer = agent(
|
| 112 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 113 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 114 |
except Exception as e:
|
|
@@ -173,16 +162,14 @@ with gr.Blocks() as demo:
|
|
| 173 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
| 174 |
gr.Markdown(
|
| 175 |
"""
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
| 181 |
-
|
| 182 |
---
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
"""
|
| 187 |
)
|
| 188 |
|
|
@@ -203,7 +190,7 @@ if __name__ == "__main__":
|
|
| 203 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 204 |
# Check for SPACE_HOST and SPACE_ID at startup for information
|
| 205 |
space_host_startup = os.getenv("SPACE_HOST")
|
| 206 |
-
space_id_startup = os.getenv("SPACE_ID")
|
| 207 |
|
| 208 |
if space_host_startup:
|
| 209 |
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
+
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, VisitWebpageTool
|
| 7 |
|
| 8 |
+
from dotenv import load_dotenv
|
|
|
|
| 9 |
|
|
|
|
| 10 |
load_dotenv()
|
| 11 |
|
| 12 |
+
|
|
|
|
| 13 |
# (Keep Constants as is)
|
| 14 |
# --- Constants ---
|
| 15 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 16 |
|
| 17 |
# --- Basic Agent Definition ---
|
| 18 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
|
|
|
|
|
|
| 19 |
class BasicAgent:
|
| 20 |
def __init__(self):
|
|
|
|
| 21 |
print("BasicAgent initialized.")
|
| 22 |
+
model = HfApiModel(
|
| 23 |
+
"Qwen/Qwen2.5-72B-Instruct",
|
| 24 |
+
provider="together", # Choose a specific inference provider
|
| 25 |
+
max_tokens=4096,
|
| 26 |
+
temperature=0.1
|
| 27 |
+
)
|
| 28 |
+
self.agent = CodeAgent(
|
| 29 |
+
tools=[DuckDuckGoSearchTool(), VisitWebpageTool()],
|
| 30 |
+
model=model,
|
| 31 |
+
additional_authorized_imports=["pandas", "numpy","re"])
|
| 32 |
+
|
| 33 |
+
def __call__(self, question: str) -> str:
|
| 34 |
+
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 35 |
+
fixed_answer = self.agent.run(question)
|
| 36 |
+
print(f"Agent returning fixed answer: {fixed_answer}")
|
| 37 |
+
return fixed_answer
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 40 |
"""
|
|
|
|
| 42 |
and displays the results.
|
| 43 |
"""
|
| 44 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 45 |
+
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
| 46 |
|
| 47 |
if profile:
|
| 48 |
username= f"{profile.username}"
|
|
|
|
| 61 |
except Exception as e:
|
| 62 |
print(f"Error instantiating agent: {e}")
|
| 63 |
return f"Error initializing agent: {e}", None
|
| 64 |
+
# In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public)
|
|
|
|
| 65 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 66 |
print(agent_code)
|
| 67 |
|
|
|
|
| 91 |
answers_payload = []
|
| 92 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 93 |
for item in questions_data:
|
|
|
|
| 94 |
task_id = item.get("task_id")
|
| 95 |
question_text = item.get("question")
|
| 96 |
if not task_id or question_text is None:
|
| 97 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 98 |
continue
|
| 99 |
try:
|
| 100 |
+
submitted_answer = agent(question_text)
|
| 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:
|
|
|
|
| 162 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
| 163 |
gr.Markdown(
|
| 164 |
"""
|
| 165 |
+
**Instructions:**
|
| 166 |
+
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
| 167 |
+
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
| 168 |
+
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
|
|
|
|
|
|
| 169 |
---
|
| 170 |
+
**Disclaimers:**
|
| 171 |
+
Once clicking on the "submit button, it can take quite some time ( this is the time for the agent to go through all the questions).
|
| 172 |
+
This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a seperate action or even to answer the questions in async.
|
| 173 |
"""
|
| 174 |
)
|
| 175 |
|
|
|
|
| 190 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 191 |
# Check for SPACE_HOST and SPACE_ID at startup for information
|
| 192 |
space_host_startup = os.getenv("SPACE_HOST")
|
| 193 |
+
space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
|
| 194 |
|
| 195 |
if space_host_startup:
|
| 196 |
print(f"✅ SPACE_HOST found: {space_host_startup}")
|