Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,57 +1,43 @@
|
|
| 1 |
-
from smolagents import CodeAgent, HfApiModel, load_tool, tool
|
| 2 |
import datetime
|
|
|
|
| 3 |
import pytz
|
| 4 |
import yaml
|
| 5 |
-
import requests
|
| 6 |
-
import io
|
| 7 |
-
import base64
|
| 8 |
-
from PIL import Image
|
| 9 |
from tools.final_answer import FinalAnswerTool
|
| 10 |
from Gradio_UI import GradioUI
|
|
|
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
IMAGE_GEN_MODEL = "stabilityai/stable-diffusion-2"
|
| 14 |
-
|
| 15 |
@tool
|
| 16 |
-
def my_custom_tool(
|
| 17 |
-
"""A tool that generates an image
|
| 18 |
-
|
| 19 |
Args:
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
Returns:
|
| 23 |
-
A base64-encoded image string that can be displayed in a browser.
|
| 24 |
"""
|
| 25 |
try:
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
headers = {"Authorization": "Bearer YOUR_HUGGINGFACE_API_KEY"} # Replace with your Hugging Face API key
|
| 29 |
-
payload = {"inputs": prompt}
|
| 30 |
-
|
| 31 |
-
response = requests.post(hf_url, headers=headers, json=payload)
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
img.save(buffered, format="JPEG")
|
| 38 |
-
img_str = base64.b64encode(buffered.getvalue()).decode()
|
| 39 |
-
return f"data:image/jpeg;base64,{img_str}" # Returns a base64 image for display
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
except Exception as e:
|
| 44 |
-
return f"
|
| 45 |
|
| 46 |
@tool
|
| 47 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 48 |
-
"""
|
| 49 |
-
|
| 50 |
Args:
|
| 51 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 52 |
-
|
| 53 |
-
Returns:
|
| 54 |
-
A string with the current local time or an error message.
|
| 55 |
"""
|
| 56 |
try:
|
| 57 |
tz = pytz.timezone(timezone)
|
|
@@ -60,6 +46,25 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 60 |
except Exception as e:
|
| 61 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
final_answer = FinalAnswerTool()
|
| 64 |
|
| 65 |
model = HfApiModel(
|
|
@@ -69,12 +74,14 @@ model = HfApiModel(
|
|
| 69 |
custom_role_conversions=None,
|
| 70 |
)
|
| 71 |
|
|
|
|
|
|
|
| 72 |
with open("prompts.yaml", 'r') as stream:
|
| 73 |
prompt_templates = yaml.safe_load(stream)
|
| 74 |
-
|
| 75 |
agent = CodeAgent(
|
| 76 |
model=model,
|
| 77 |
-
tools=[final_answer, my_custom_tool], #
|
| 78 |
max_steps=6,
|
| 79 |
verbosity_level=1,
|
| 80 |
grammar=None,
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
| 2 |
import datetime
|
| 3 |
+
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
from Gradio_UI import GradioUI
|
| 8 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 9 |
|
| 10 |
+
# A tool that generates an image displaying the city name and current local time.
|
|
|
|
|
|
|
| 11 |
@tool
|
| 12 |
+
def my_custom_tool(city: str, timezone: str) -> str:
|
| 13 |
+
"""A tool that generates an image displaying the city name and current local time.
|
|
|
|
| 14 |
Args:
|
| 15 |
+
city: The name of the city.
|
| 16 |
+
timezone: The timezone of the city.
|
|
|
|
|
|
|
| 17 |
"""
|
| 18 |
try:
|
| 19 |
+
tz = pytz.timezone(timezone)
|
| 20 |
+
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
# Create an image
|
| 23 |
+
img = Image.new('RGB', (400, 200), color=(255, 255, 255))
|
| 24 |
+
draw = ImageDraw.Draw(img)
|
| 25 |
+
font = ImageFont.load_default()
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
draw.text((50, 80), f"City: {city}", fill=(0, 0, 0), font=font)
|
| 28 |
+
draw.text((50, 100), f"Time: {local_time}", fill=(0, 0, 0), font=font)
|
| 29 |
+
|
| 30 |
+
img_path = f"{city}_time.png"
|
| 31 |
+
img.save(img_path)
|
| 32 |
+
return f"Image generated: {img_path}"
|
| 33 |
except Exception as e:
|
| 34 |
+
return f"Error generating image: {str(e)}"
|
| 35 |
|
| 36 |
@tool
|
| 37 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 38 |
+
"""A tool that fetches the current local time in a specified timezone.
|
|
|
|
| 39 |
Args:
|
| 40 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
|
|
|
|
|
|
|
|
|
| 41 |
"""
|
| 42 |
try:
|
| 43 |
tz = pytz.timezone(timezone)
|
|
|
|
| 46 |
except Exception as e:
|
| 47 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 48 |
|
| 49 |
+
@tool
|
| 50 |
+
def get_temperature(city: str, api_key: str) -> str:
|
| 51 |
+
"""A tool that fetches the current temperature for a given city.
|
| 52 |
+
Args:
|
| 53 |
+
city: The name of the city.
|
| 54 |
+
api_key: "d2eb0cca91754320fb0f471fd60c6928"
|
| 55 |
+
"""
|
| 56 |
+
try:
|
| 57 |
+
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
|
| 58 |
+
response = requests.get(url)
|
| 59 |
+
data = response.json()
|
| 60 |
+
if response.status_code == 200:
|
| 61 |
+
temperature = data["main"]["temp"]
|
| 62 |
+
return f"The current temperature in {city} is {temperature}°C."
|
| 63 |
+
else:
|
| 64 |
+
return f"Error: {data.get('message', 'Unable to fetch temperature.')}"
|
| 65 |
+
except Exception as e:
|
| 66 |
+
return f"Error fetching temperature: {str(e)}"
|
| 67 |
+
|
| 68 |
final_answer = FinalAnswerTool()
|
| 69 |
|
| 70 |
model = HfApiModel(
|
|
|
|
| 74 |
custom_role_conversions=None,
|
| 75 |
)
|
| 76 |
|
| 77 |
+
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 78 |
+
|
| 79 |
with open("prompts.yaml", 'r') as stream:
|
| 80 |
prompt_templates = yaml.safe_load(stream)
|
| 81 |
+
|
| 82 |
agent = CodeAgent(
|
| 83 |
model=model,
|
| 84 |
+
tools=[final_answer, get_temperature, my_custom_tool], # Added my_custom_tool
|
| 85 |
max_steps=6,
|
| 86 |
verbosity_level=1,
|
| 87 |
grammar=None,
|