Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,25 +3,75 @@ 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
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
"""
|
| 24 |
-
Fetches all questions, runs the
|
| 25 |
and displays the results.
|
| 26 |
"""
|
| 27 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
|
@@ -38,13 +88,14 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 38 |
questions_url = f"{api_url}/questions"
|
| 39 |
submit_url = f"{api_url}/submit"
|
| 40 |
|
| 41 |
-
# 1. Instantiate Agent (
|
| 42 |
try:
|
| 43 |
agent = BasicAgent()
|
| 44 |
except Exception as e:
|
| 45 |
print(f"Error instantiating agent: {e}")
|
| 46 |
return f"Error initializing agent: {e}", None
|
| 47 |
-
|
|
|
|
| 48 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 49 |
print(agent_code)
|
| 50 |
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
from smolagents import CodeAgent, LiteLLMModel, DuckDuckGoSearchTool, VisitWebpageTool, FinalAnswerTool
|
| 7 |
|
| 8 |
# (Keep Constants as is)
|
| 9 |
# --- Constants ---
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 11 |
|
| 12 |
# --- Basic Agent Definition ---
|
| 13 |
+
# ----- THIS IS WHERE YOU CAN BUILD WHAT YOU WANT ------
|
| 14 |
class BasicAgent:
|
| 15 |
def __init__(self):
|
| 16 |
print("BasicAgent initialized.")
|
| 17 |
+
|
| 18 |
+
# Create model using Hugging Face Inference API
|
| 19 |
+
# This will use the HF_TOKEN environment variable if available
|
| 20 |
+
try:
|
| 21 |
+
# Using a good open model available on HF Inference API
|
| 22 |
+
self.model = LiteLLMModel(
|
| 23 |
+
model_id="huggingface/meta-llama/Llama-3.2-3B-Instruct",
|
| 24 |
+
api_base="https://api-inference.huggingface.co/models/",
|
| 25 |
+
# HF_TOKEN will be automatically used if set in environment
|
| 26 |
+
max_tokens=1024
|
| 27 |
+
)
|
| 28 |
+
print("Initialized Hugging Face Inference API model")
|
| 29 |
+
except Exception as e:
|
| 30 |
+
print(f"Error initializing HF model: {e}")
|
| 31 |
+
# Fallback to a simpler approach if model fails
|
| 32 |
+
self.model = None
|
| 33 |
+
|
| 34 |
+
# Create the CodeAgent with tools if model is available
|
| 35 |
+
if self.model:
|
| 36 |
+
try:
|
| 37 |
+
self.agent = CodeAgent(
|
| 38 |
+
tools=[
|
| 39 |
+
DuckDuckGoSearchTool(),
|
| 40 |
+
VisitWebpageTool(),
|
| 41 |
+
FinalAnswerTool()
|
| 42 |
+
],
|
| 43 |
+
model=self.model,
|
| 44 |
+
additional_authorized_imports=["wikipedia", "requests", "json", "re", "datetime", "os"]
|
| 45 |
+
)
|
| 46 |
+
print("CodeAgent with tools initialized successfully")
|
| 47 |
+
except Exception as e:
|
| 48 |
+
print(f"Error initializing CodeAgent: {e}")
|
| 49 |
+
self.agent = None
|
| 50 |
+
else:
|
| 51 |
+
self.agent = None
|
| 52 |
+
|
| 53 |
def __call__(self, question: str) -> str:
|
| 54 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 55 |
+
|
| 56 |
+
if self.agent:
|
| 57 |
+
try:
|
| 58 |
+
# Use smolagents CodeAgent to process the question
|
| 59 |
+
answer = self.agent.run(question)
|
| 60 |
+
print(f"Agent returning answer: {answer[:100]}...")
|
| 61 |
+
return answer
|
| 62 |
+
except Exception as e:
|
| 63 |
+
print(f"Error in agent processing: {e}")
|
| 64 |
+
# Return a fallback answer in case of errors
|
| 65 |
+
return f"Error processing question: {str(e)}"
|
| 66 |
+
else:
|
| 67 |
+
# Fallback if agent initialization failed
|
| 68 |
+
fallback_answer = "Agent initialization failed. Please check model configuration."
|
| 69 |
+
print(f"Agent returning fallback answer: {fallback_answer}")
|
| 70 |
+
return fallback_answer
|
| 71 |
+
|
| 72 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 73 |
"""
|
| 74 |
+
Fetches all questions, runs the SmolagentsAgent on them, submits all answers,
|
| 75 |
and displays the results.
|
| 76 |
"""
|
| 77 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
|
|
|
| 88 |
questions_url = f"{api_url}/questions"
|
| 89 |
submit_url = f"{api_url}/submit"
|
| 90 |
|
| 91 |
+
# 1. Instantiate Agent (modify this part to create your agent)
|
| 92 |
try:
|
| 93 |
agent = BasicAgent()
|
| 94 |
except Exception as e:
|
| 95 |
print(f"Error instantiating agent: {e}")
|
| 96 |
return f"Error initializing agent: {e}", None
|
| 97 |
+
|
| 98 |
+
# In the case of an app running as a hugging Face space, this link points toward your codebase
|
| 99 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 100 |
print(agent_code)
|
| 101 |
|