Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,24 +6,27 @@ import os
|
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
from Gradio_UI import GradioUI
|
| 8 |
from transformers import Tool # Import the Tool class
|
|
|
|
| 9 |
|
| 10 |
hf_token = os.getenv("HF_ACCESS_TOKEN")
|
| 11 |
|
|
|
|
|
|
|
| 12 |
@tool
|
| 13 |
-
def generate_landscape_image(timezone: str) ->
|
| 14 |
"""
|
| 15 |
A tool that generates a landscape image based on the local time in a given timezone.
|
| 16 |
If it's daytime, the image will feature the sun in a position corresponding to the current time.
|
| 17 |
If it's nighttime, the image will feature the moon without a preferred position.
|
| 18 |
|
| 19 |
Args:
|
| 20 |
-
timezone:
|
| 21 |
|
| 22 |
Returns:
|
| 23 |
-
A
|
| 24 |
"""
|
| 25 |
try:
|
| 26 |
-
#
|
| 27 |
tz = pytz.timezone(timezone)
|
| 28 |
now = datetime.datetime.now(tz)
|
| 29 |
hour = now.hour
|
|
@@ -43,10 +46,14 @@ def generate_landscape_image(timezone: str) -> str:
|
|
| 43 |
else:
|
| 44 |
prompt = "A serene nighttime landscape featuring a luminous moon shining in the dark sky."
|
| 45 |
|
| 46 |
-
#
|
| 47 |
result = image_generation_tool(prompt)
|
| 48 |
-
|
|
|
|
|
|
|
| 49 |
except Exception as e:
|
|
|
|
|
|
|
| 50 |
return f"Error generating image for timezone '{timezone}': {str(e)}"
|
| 51 |
|
| 52 |
@tool
|
|
|
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
from Gradio_UI import GradioUI
|
| 8 |
from transformers import Tool # Import the Tool class
|
| 9 |
+
from PIL import Image
|
| 10 |
|
| 11 |
hf_token = os.getenv("HF_ACCESS_TOKEN")
|
| 12 |
|
| 13 |
+
|
| 14 |
+
|
| 15 |
@tool
|
| 16 |
+
def generate_landscape_image(timezone: str) -> Image.Image:
|
| 17 |
"""
|
| 18 |
A tool that generates a landscape image based on the local time in a given timezone.
|
| 19 |
If it's daytime, the image will feature the sun in a position corresponding to the current time.
|
| 20 |
If it's nighttime, the image will feature the moon without a preferred position.
|
| 21 |
|
| 22 |
Args:
|
| 23 |
+
timezone: A valid timezone string (e.g., 'America/New_York').
|
| 24 |
|
| 25 |
Returns:
|
| 26 |
+
A PIL.Image object of the generated image or an error message as a string.
|
| 27 |
"""
|
| 28 |
try:
|
| 29 |
+
# Create timezone object and get current time in that zone
|
| 30 |
tz = pytz.timezone(timezone)
|
| 31 |
now = datetime.datetime.now(tz)
|
| 32 |
hour = now.hour
|
|
|
|
| 46 |
else:
|
| 47 |
prompt = "A serene nighttime landscape featuring a luminous moon shining in the dark sky."
|
| 48 |
|
| 49 |
+
# Generate the image using the image generation tool from Space
|
| 50 |
result = image_generation_tool(prompt)
|
| 51 |
+
# result is a file path; open the image using PIL
|
| 52 |
+
image = Image.open(result)
|
| 53 |
+
return image
|
| 54 |
except Exception as e:
|
| 55 |
+
# If there's an error, you can either return a string or raise an error.
|
| 56 |
+
# Here we return a string, but Gradio might display it as plain text.
|
| 57 |
return f"Error generating image for timezone '{timezone}': {str(e)}"
|
| 58 |
|
| 59 |
@tool
|