Upload app.py
#585
by Kushagravr16 - opened
app.py
CHANGED
|
@@ -1,23 +1,75 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# (Keep Constants as is)
|
| 8 |
# --- Constants ---
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
|
| 11 |
# --- Basic Agent Definition ---
|
| 12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
class BasicAgent:
|
| 14 |
def __init__(self):
|
| 15 |
print("BasicAgent initialized.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def __call__(self, question: str) -> str:
|
| 17 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 23 |
"""
|
|
@@ -146,11 +198,9 @@ with gr.Blocks() as demo:
|
|
| 146 |
gr.Markdown(
|
| 147 |
"""
|
| 148 |
**Instructions:**
|
| 149 |
-
|
| 150 |
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
| 151 |
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
| 152 |
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
| 153 |
-
|
| 154 |
---
|
| 155 |
**Disclaimers:**
|
| 156 |
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).
|
|
@@ -193,4 +243,4 @@ if __name__ == "__main__":
|
|
| 193 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 194 |
|
| 195 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 196 |
-
demo.launch(debug=True, share=False)
|
|
|
|
| 1 |
import os
|
| 2 |
+
import re
|
| 3 |
import gradio as gr
|
| 4 |
import requests
|
| 5 |
import inspect
|
| 6 |
import pandas as pd
|
| 7 |
|
| 8 |
+
from smolagents import (
|
| 9 |
+
CodeAgent,
|
| 10 |
+
InferenceClientModel,
|
| 11 |
+
DuckDuckGoSearchTool,
|
| 12 |
+
PythonInterpreterTool,
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
# (Keep Constants as is)
|
| 16 |
# --- Constants ---
|
| 17 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 18 |
|
| 19 |
# --- Basic Agent Definition ---
|
| 20 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 21 |
+
|
| 22 |
+
# Free model available on HF's serverless Inference API.
|
| 23 |
+
MODEL_ID = "Qwen/Qwen2.5-Coder-32B-Instruct"
|
| 24 |
+
|
| 25 |
+
SYSTEM_PROMPT = """You are a general-purpose research and reasoning agent being
|
| 26 |
+
graded on an exact-match benchmark. For every question you must:
|
| 27 |
+
|
| 28 |
+
1. Think step by step and use your tools (web search, code execution) as
|
| 29 |
+
needed to find the correct answer.
|
| 30 |
+
2. Once you are confident, respond with ONLY the final answer - no
|
| 31 |
+
explanation, no "The answer is", no restated question, no extra
|
| 32 |
+
punctuation or units unless the question explicitly asks for them.
|
| 33 |
+
3. If the answer is a number, give just the number.
|
| 34 |
+
4. If the answer is a short string, match the wording/capitalization implied
|
| 35 |
+
by the question as closely as possible.
|
| 36 |
+
5. Never wrap your final answer in quotes or markdown formatting.
|
| 37 |
+
"""
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def _clean_answer(raw: str) -> str:
|
| 41 |
+
"""Strip common wrapper text so answers match exact-match grading."""
|
| 42 |
+
text = raw.strip()
|
| 43 |
+
text = re.sub(r"(?i)^\s*(final answer|answer)\s*[:\-]\s*", "", text)
|
| 44 |
+
if len(text) >= 2 and text[0] == text[-1] and text[0] in ("'", '"'):
|
| 45 |
+
text = text[1:-1]
|
| 46 |
+
return text.strip()
|
| 47 |
+
|
| 48 |
+
|
| 49 |
class BasicAgent:
|
| 50 |
def __init__(self):
|
| 51 |
print("BasicAgent initialized.")
|
| 52 |
+
model = InferenceClientModel(
|
| 53 |
+
model_id=MODEL_ID,
|
| 54 |
+
token=os.environ.get("HF_TOKEN"),
|
| 55 |
+
)
|
| 56 |
+
self.agent = CodeAgent(
|
| 57 |
+
tools=[DuckDuckGoSearchTool(), PythonInterpreterTool()],
|
| 58 |
+
model=model,
|
| 59 |
+
add_base_tools=False,
|
| 60 |
+
max_steps=8,
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
def __call__(self, question: str) -> str:
|
| 64 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 65 |
+
try:
|
| 66 |
+
result = self.agent.run(SYSTEM_PROMPT + "\n\nQuestion:\n" + question)
|
| 67 |
+
answer = _clean_answer(str(result))
|
| 68 |
+
except Exception as e:
|
| 69 |
+
print(f"Agent error: {e}")
|
| 70 |
+
answer = ""
|
| 71 |
+
print(f"Agent returning answer: {answer}")
|
| 72 |
+
return answer
|
| 73 |
|
| 74 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 75 |
"""
|
|
|
|
| 198 |
gr.Markdown(
|
| 199 |
"""
|
| 200 |
**Instructions:**
|
|
|
|
| 201 |
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
| 202 |
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
| 203 |
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
|
|
|
| 204 |
---
|
| 205 |
**Disclaimers:**
|
| 206 |
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).
|
|
|
|
| 243 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 244 |
|
| 245 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 246 |
+
demo.launch(debug=True, share=False)
|