Commit
·
d4e4913
1
Parent(s):
81917a3
Basic functionality test
Browse files
app.py
CHANGED
|
@@ -3,6 +3,20 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# (Keep Constants as is)
|
| 8 |
# --- Constants ---
|
|
@@ -13,11 +27,28 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 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 |
"""
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
from google.colab import userdata
|
| 7 |
+
from smolagents import ToolCallingAgent, CodeAgent, LiteLLMModel
|
| 8 |
+
|
| 9 |
+
from tools import DuckDuckGoSearchTool, WeatherInfoTool, HubStatsTool
|
| 10 |
+
|
| 11 |
+
# Initialize the web search tool
|
| 12 |
+
search_tool = DuckDuckGoSearchTool()
|
| 13 |
+
|
| 14 |
+
# Initialize the weather tool
|
| 15 |
+
weather_info_tool = WeatherInfoTool()
|
| 16 |
+
|
| 17 |
+
# Initialize the Hub stats tool
|
| 18 |
+
hub_stats_tool = HubStatsTool()
|
| 19 |
+
|
| 20 |
|
| 21 |
# (Keep Constants as is)
|
| 22 |
# --- Constants ---
|
|
|
|
| 27 |
class BasicAgent:
|
| 28 |
def __init__(self):
|
| 29 |
print("BasicAgent initialized.")
|
| 30 |
+
|
| 31 |
def __call__(self, question: str) -> str:
|
| 32 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 33 |
+
|
| 34 |
+
## new agent code
|
| 35 |
+
model = LiteLLMModel(model_id="gemini/gemini-2.5-pro-preview-06-05", api_key=os.getenv(key="GEMINI_API_KEY"))
|
| 36 |
+
agent = CodeAgent(
|
| 37 |
+
model=model,
|
| 38 |
+
tools=[DuckDuckGoSearchTool()]
|
| 39 |
+
)
|
| 40 |
+
answer = agent.run(question)
|
| 41 |
+
|
| 42 |
+
print(f"Agent returning answer: {answer}")
|
| 43 |
+
return answer
|
| 44 |
+
|
| 45 |
+
def test_agent():
|
| 46 |
+
# Use Google Gemini
|
| 47 |
+
model = LiteLLMModel(model_id="gemini/gemini-2.5-pro-preview-06-05", api_key=os.getenv(key="GEMINI_API_KEY"))
|
| 48 |
+
|
| 49 |
+
agent = ToolCallingAgent(tools=[DuckDuckGoSearchTool()], model=model)
|
| 50 |
+
agent.run("Search for the best music recommendations for a party at the Wayne's mansion.")
|
| 51 |
+
|
| 52 |
|
| 53 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 54 |
"""
|
tools.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import DuckDuckGoSearchTool
|
| 2 |
+
from smolagents import Tool
|
| 3 |
+
import random
|
| 4 |
+
from huggingface_hub import list_models
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class WeatherInfoTool(Tool):
|
| 8 |
+
name = "weather_info"
|
| 9 |
+
description = "Fetches dummy weather information for a given location."
|
| 10 |
+
inputs = {
|
| 11 |
+
"location": {
|
| 12 |
+
"type": "string",
|
| 13 |
+
"description": "The location to get weather information for."
|
| 14 |
+
}
|
| 15 |
+
}
|
| 16 |
+
output_type = "string"
|
| 17 |
+
|
| 18 |
+
def forward(self, location: str):
|
| 19 |
+
# Dummy weather data
|
| 20 |
+
weather_conditions = [
|
| 21 |
+
{"condition": "Rainy", "temp_c": 15},
|
| 22 |
+
{"condition": "Clear", "temp_c": 25},
|
| 23 |
+
{"condition": "Windy", "temp_c": 20}
|
| 24 |
+
]
|
| 25 |
+
# Randomly select a weather condition
|
| 26 |
+
data = random.choice(weather_conditions)
|
| 27 |
+
return f"Weather in {location}: {data['condition']}, {data['temp_c']}°C"
|
| 28 |
+
|
| 29 |
+
class HubStatsTool(Tool):
|
| 30 |
+
name = "hub_stats"
|
| 31 |
+
description = "Fetches the most downloaded model from a specific author on the Hugging Face Hub."
|
| 32 |
+
inputs = {
|
| 33 |
+
"author": {
|
| 34 |
+
"type": "string",
|
| 35 |
+
"description": "The username of the model author/organization to find models from."
|
| 36 |
+
}
|
| 37 |
+
}
|
| 38 |
+
output_type = "string"
|
| 39 |
+
|
| 40 |
+
def forward(self, author: str):
|
| 41 |
+
try:
|
| 42 |
+
# List models from the specified author, sorted by downloads
|
| 43 |
+
models = list(list_models(author=author, sort="downloads", direction=-1, limit=1))
|
| 44 |
+
|
| 45 |
+
if models:
|
| 46 |
+
model = models[0]
|
| 47 |
+
return f"The most downloaded model by {author} is {model.id} with {model.downloads:,} downloads."
|
| 48 |
+
else:
|
| 49 |
+
return f"No models found for author {author}."
|
| 50 |
+
except Exception as e:
|
| 51 |
+
return f"Error fetching models for {author}: {str(e)}"
|