Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,35 @@
|
|
| 1 |
-
from smolagents import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
-
# Initialize
|
| 5 |
client = InferenceClient()
|
| 6 |
|
|
|
|
| 7 |
@tool
|
| 8 |
def generate_image(description: str) -> str:
|
| 9 |
"""
|
| 10 |
Generates an image based on the given text description.
|
| 11 |
-
|
| 12 |
Args:
|
| 13 |
-
description (str): The
|
| 14 |
-
|
| 15 |
Returns:
|
| 16 |
-
str: A URL
|
| 17 |
"""
|
| 18 |
try:
|
| 19 |
-
# Stable Diffusion model ID
|
| 20 |
model_id = "stabilityai/stable-diffusion-xl-base-1.0"
|
| 21 |
-
|
| 22 |
-
# Ensure correct API call
|
| 23 |
result = client.post(
|
| 24 |
model=model_id,
|
| 25 |
data={"inputs": description}
|
| 26 |
)
|
| 27 |
-
|
| 28 |
-
# Handle response format
|
| 29 |
-
if isinstance(result, bytes): # Hugging Face might return raw image data
|
| 30 |
image_path = "generated_image.png"
|
| 31 |
with open(image_path, "wb") as f:
|
| 32 |
f.write(result)
|
|
@@ -35,3 +38,28 @@ def generate_image(description: str) -> str:
|
|
| 35 |
return "Error: Unexpected response format from the API."
|
| 36 |
except Exception as e:
|
| 37 |
return f"Error generating image: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
| 2 |
+
import datetime
|
| 3 |
+
import requests
|
| 4 |
+
import pytz
|
| 5 |
+
import yaml
|
| 6 |
+
import wikipedia
|
| 7 |
+
from tools.final_answer import FinalAnswerTool
|
| 8 |
+
from Gradio_UI import GradioUI
|
| 9 |
from huggingface_hub import InferenceClient
|
| 10 |
|
| 11 |
+
# Initialize Hugging Face Inference Client
|
| 12 |
client = InferenceClient()
|
| 13 |
|
| 14 |
+
# Custom Tool 1: Image Generation
|
| 15 |
@tool
|
| 16 |
def generate_image(description: str) -> str:
|
| 17 |
"""
|
| 18 |
Generates an image based on the given text description.
|
| 19 |
+
|
| 20 |
Args:
|
| 21 |
+
description (str): The textual description of the image to generate.
|
| 22 |
+
|
| 23 |
Returns:
|
| 24 |
+
str: A URL to the generated image or an error message.
|
| 25 |
"""
|
| 26 |
try:
|
|
|
|
| 27 |
model_id = "stabilityai/stable-diffusion-xl-base-1.0"
|
|
|
|
|
|
|
| 28 |
result = client.post(
|
| 29 |
model=model_id,
|
| 30 |
data={"inputs": description}
|
| 31 |
)
|
| 32 |
+
if isinstance(result, bytes):
|
|
|
|
|
|
|
| 33 |
image_path = "generated_image.png"
|
| 34 |
with open(image_path, "wb") as f:
|
| 35 |
f.write(result)
|
|
|
|
| 38 |
return "Error: Unexpected response format from the API."
|
| 39 |
except Exception as e:
|
| 40 |
return f"Error generating image: {str(e)}"
|
| 41 |
+
|
| 42 |
+
# Custom Tool 2: Get Current Time in a Given Timezone
|
| 43 |
+
@tool
|
| 44 |
+
def get_current_time_in_timezone(timezone: str) -> str:
|
| 45 |
+
"""
|
| 46 |
+
Fetches the current local time in a specified timezone.
|
| 47 |
+
|
| 48 |
+
Args:
|
| 49 |
+
timezone (str): A valid timezone (e.g., 'America/New_York').
|
| 50 |
+
|
| 51 |
+
Returns:
|
| 52 |
+
str: The current time in the specified timezone.
|
| 53 |
+
"""
|
| 54 |
+
try:
|
| 55 |
+
tz = pytz.timezone(timezone)
|
| 56 |
+
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 57 |
+
return f"The current local time in {timezone} is: {local_time}"
|
| 58 |
+
except Exception as e:
|
| 59 |
+
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 60 |
+
|
| 61 |
+
# Custom Tool 3: Fetch Weather Information
|
| 62 |
+
@tool
|
| 63 |
+
def get_weather(city: str, api_key: str) -> str:
|
| 64 |
+
"""
|
| 65 |
+
Fetches the current
|