Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,21 +3,76 @@ 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 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
class BasicAgent:
|
| 14 |
def __init__(self):
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def __call__(self, question: str) -> str:
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 23 |
"""
|
|
@@ -191,6 +246,5 @@ if __name__ == "__main__":
|
|
| 191 |
print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
| 192 |
|
| 193 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 194 |
-
|
| 195 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 196 |
demo.launch(debug=True, share=False)
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
from smolagents.models import InferenceClientModel
|
| 7 |
+
from smolagents import ToolCallingAgent
|
| 8 |
+
from smolagents import DuckDuckGoSearchTool
|
| 9 |
+
from smolagents import Tool
|
| 10 |
+
import traceback
|
| 11 |
# (Keep Constants as is)
|
| 12 |
# --- Constants ---
|
| 13 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 14 |
+
print("Token loaded:", bool(os.getenv("zinebhftoken")))
|
| 15 |
+
custom_prompt = """
|
| 16 |
+
You are an intelligent AI agent participating in the GALA (Generative Agent Learning Assignment).
|
| 17 |
+
Your goal is to answer each question accurately, concisely, and factually.
|
| 18 |
+
|
| 19 |
+
Instructions:
|
| 20 |
+
1. Analyze the user's question carefully.
|
| 21 |
+
2. If you can answer it directly from your own knowledge, do so in one or two sentences.
|
| 22 |
+
3. If the question requires recent, factual, or numeric data (e.g. "current CEO", "latest version", "in 2025", etc.), use the DuckDuckGoSearchTool to find the answer.
|
| 23 |
+
4. When using a tool, be precise in your query and summarize the result clearly.
|
| 24 |
+
5. Always provide clean, readable text without markdown or citations unless explicitly requested.
|
| 25 |
+
6. If a question is ambiguous, briefly explain your assumption before answering.
|
| 26 |
+
7. Never refuse to answer unless the question is unrelated to the task or violates safety rules.
|
| 27 |
+
|
| 28 |
+
Format:
|
| 29 |
+
- Respond directly with the final answer only.
|
| 30 |
+
- Do not include reasoning steps or technical metadata.
|
| 31 |
+
|
| 32 |
+
Example behaviors:
|
| 33 |
+
User: Who is the CEO of OpenAI?
|
| 34 |
+
→ Use search tool → "The CEO of OpenAI is Sam Altman."
|
| 35 |
+
|
| 36 |
+
User: What is the capital of Japan?
|
| 37 |
+
→ Direct answer → "The capital of Japan is Tokyo."
|
| 38 |
+
|
| 39 |
+
Remember: you are being evaluated for correctness and clarity.
|
| 40 |
+
"""
|
| 41 |
+
|
| 42 |
+
@Tool
|
| 43 |
+
def calculatorTool(query:str) -> str:
|
| 44 |
+
try:
|
| 45 |
+
result=eval(query)
|
| 46 |
+
return str(result)
|
| 47 |
+
except Exception as e:
|
| 48 |
+
return f"error while calculating {e}"
|
| 49 |
+
CalculatorTool=Tool(
|
| 50 |
+
name="calculator",
|
| 51 |
+
description="it calculates basic arithmetics",
|
| 52 |
+
func=calculatorTool
|
| 53 |
+
)
|
| 54 |
class BasicAgent:
|
| 55 |
def __init__(self):
|
| 56 |
+
api_key = os.getenv('zinebhftoken')
|
| 57 |
+
if not api_key:
|
| 58 |
+
raise ValueError("❌ Missing Hugging Face token. Add it in Settings → Secrets with the name 'chatbotagenthf'.")
|
| 59 |
+
model = InferenceClientModel(
|
| 60 |
+
model_id="mistralai/Mixtral-8x7B-Instruct-v0.1",
|
| 61 |
+
token=api_key
|
| 62 |
+
)
|
| 63 |
+
self.agent=ToolCallingAgent(tools=[DuckDuckGoSearchTool()], model=model,
|
| 64 |
+
api_key=api_key)
|
| 65 |
+
|
| 66 |
def __call__(self, question: str) -> str:
|
| 67 |
+
try:
|
| 68 |
+
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 69 |
+
agent_answer=self.agent.run(question)
|
| 70 |
+
print(f"Agent returning answer: {agent_answer}")
|
| 71 |
+
return agent_answer
|
| 72 |
+
except Exception as e:
|
| 73 |
+
print(f"Agent encountered an error: {e}")
|
| 74 |
+
traceback.print_exc()
|
| 75 |
+
return 'Error generating answer'
|
| 76 |
|
| 77 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 78 |
"""
|
|
|
|
| 246 |
print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
| 247 |
|
| 248 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
|
|
|
| 249 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 250 |
demo.launch(debug=True, share=False)
|