Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,79 +10,14 @@ 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 |
-
import os
|
| 14 |
-
import gradio as gr
|
| 15 |
-
import requests
|
| 16 |
-
import pandas as pd
|
| 17 |
-
from langchain.agents import load_tools, initialize_agent
|
| 18 |
-
from langchain.chat_models import ChatOpenAI
|
| 19 |
-
|
| 20 |
-
# --- System Prompt Definition ---
|
| 21 |
-
SYSTEM_PROMPT = """
|
| 22 |
-
You are a general AI assistant with access to these tools:
|
| 23 |
-
- search(query): web search
|
| 24 |
-
- python(code): Python REPL
|
| 25 |
-
- read_file(path): load local documents
|
| 26 |
-
- vision(image): OCR/vision
|
| 27 |
-
- calculator(expr): arithmetic
|
| 28 |
-
|
| 29 |
-
When you get a question, think step by step:
|
| 30 |
-
|
| 31 |
-
Thought: decide what to do next
|
| 32 |
-
Action: call one tool (name + args) or “Answer” if ready
|
| 33 |
-
Observation: result from the tool
|
| 34 |
-
|
| 35 |
-
…repeat Thought/Action/Observation until you have what you need…
|
| 36 |
-
|
| 37 |
-
Final Answer: [YOUR FINAL ANSWER]
|
| 38 |
-
|
| 39 |
-
Constraints on YOUR FINAL ANSWER:
|
| 40 |
-
• If it’s a number, write digits without commas or units (unless asked).
|
| 41 |
-
• If it’s a string, omit articles (“a”, “the”), abbreviations, and write any digits in words.
|
| 42 |
-
• If it’s a list, output a comma-separated list of numbers and/or strings, each following the above rules.
|
| 43 |
-
"""
|
| 44 |
-
|
| 45 |
-
# --- Constants ---
|
| 46 |
-
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 47 |
-
|
| 48 |
-
# --- SmolAgent Definition ---
|
| 49 |
class BasicAgent:
|
| 50 |
-
"""
|
| 51 |
-
A lightweight agent configured with GAIA tools, using GPT-4.1 via OpenAI API.
|
| 52 |
-
"""
|
| 53 |
def __init__(self):
|
| 54 |
-
|
| 55 |
-
api_key = os.getenv("OPENAI_API_KEY")
|
| 56 |
-
if not api_key:
|
| 57 |
-
raise ValueError("OPENAI_API_KEY environment variable not set")
|
| 58 |
-
# Initialize LLM with system prompt
|
| 59 |
-
self.llm = ChatOpenAI(
|
| 60 |
-
model_name="gpt-4.1",
|
| 61 |
-
temperature=0,
|
| 62 |
-
openai_api_key=api_key,
|
| 63 |
-
system_message=SYSTEM_PROMPT # apply our GAIA prompt
|
| 64 |
-
)
|
| 65 |
-
# Load required GAIA tools
|
| 66 |
-
self.tools = load_tools(
|
| 67 |
-
[
|
| 68 |
-
"serpapi", # web search
|
| 69 |
-
"requests", # HTTP requests
|
| 70 |
-
"python_repl" # python execution
|
| 71 |
-
],
|
| 72 |
-
llm=self.llm
|
| 73 |
-
)
|
| 74 |
-
# Initialize the agent with zero-shot reasoning
|
| 75 |
-
self.agent = initialize_agent(
|
| 76 |
-
self.tools,
|
| 77 |
-
self.llm,
|
| 78 |
-
agent="zero-shot-react-description",
|
| 79 |
-
verbose=True
|
| 80 |
-
)
|
| 81 |
-
|
| 82 |
def __call__(self, question: str) -> str:
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
| 86 |
|
| 87 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 88 |
"""
|
|
@@ -211,11 +146,9 @@ with gr.Blocks() as demo:
|
|
| 211 |
gr.Markdown(
|
| 212 |
"""
|
| 213 |
**Instructions:**
|
| 214 |
-
|
| 215 |
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
| 216 |
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
| 217 |
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
| 218 |
-
|
| 219 |
---
|
| 220 |
**Disclaimers:**
|
| 221 |
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).
|
|
|
|
| 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 |
+
fixed_answer = "This is a default answer."
|
| 19 |
+
print(f"Agent returning fixed answer: {fixed_answer}")
|
| 20 |
+
return fixed_answer
|
| 21 |
|
| 22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 23 |
"""
|
|
|
|
| 146 |
gr.Markdown(
|
| 147 |
"""
|
| 148 |
**Instructions:**
|
|
|
|
| 149 |
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
| 150 |
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
| 151 |
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
|
|
|
| 152 |
---
|
| 153 |
**Disclaimers:**
|
| 154 |
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).
|