File size: 3,559 Bytes
42fae81
9b5b26a
 
c19d193
1f85c06
42fae81
9b5b26a
42fae81
f43cc5e
9b5b26a
5e17b66
2ef1097
f43cc5e
9b5b26a
f43cc5e
2ef1097
 
 
 
 
9b5b26a
f43cc5e
2ef1097
 
f43cc5e
9b5b26a
2ef1097
f43cc5e
2ef1097
 
 
 
42fae81
2ef1097
 
 
 
 
 
 
 
 
 
 
 
 
 
44fc028
42fae81
44fc028
 
 
 
 
 
 
f43cc5e
2ef1097
 
9b5b26a
 
 
42fae81
 
 
9b5b26a
42fae81
 
 
 
9b5b26a
 
 
 
 
 
 
8c01ffb
6aae614
ae7a494
e121372
5e17b66
 
42fae81
5e17b66
42fae81
13d500a
8c01ffb
42fae81
 
 
 
 
 
8c01ffb
861422e
 
9b5b26a
8c01ffb
8fe992b
475a614
8c01ffb
 
861422e
8fe992b
 
42fae81
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from smolagents import CodeAgent, HfApiModel, tool
import datetime
import pytz
import yaml
import os
from tools.final_answer import FinalAnswerTool
from Gradio_UI import GradioUI
from transformers import Tool  # Import the Tool class
from PIL import Image

hf_token = os.getenv("HF_ACCESS_TOKEN")


@tool
def generate_landscape_image(timezone: str) -> Image.Image:
    """
    A tool that generates a landscape image based on the local time in a given timezone.
    If it's daytime, the image will feature the sun in a position corresponding to the current time.
    If it's nighttime, the image will feature the moon without a preferred position.
    
    Args:
        timezone: A valid timezone string (e.g., 'America/New_York').
        
    Returns:
        A PIL.Image object of the generated image or an error message as a string.
    """
    try:
        # Create timezone object and get current time in that zone
        tz = pytz.timezone(timezone)
        now = datetime.datetime.now(tz)
        hour = now.hour
        
        # Build prompt based on day or night
        if 6 <= hour < 18:
            if hour < 12:
                position = "rising in the east"
            elif hour == 12:
                position = "directly overhead"
            else:
                position = "setting in the west"
            prompt = (
                f"A breathtaking daytime landscape with the sun {position} at {now.strftime('%H:%M')}, "
                "illuminating the scene with warm, golden light."
            )
        else:
            prompt = "A serene nighttime landscape featuring a luminous moon shining in the dark sky."
        
        # Call the image generation tool loaded from Space
        result = image_generation_tool(prompt)
        
        # If the result is an AgentImage, extract the underlying image.
        if hasattr(result, "to_raw"):
            image = result.to_raw()
        else:
            # Otherwise assume it's a file path and open it.
            image = Image.open(result)
        return image
    except Exception as e:
        return f"Error generating image for timezone '{timezone}': {str(e)}"

@tool
def get_current_time_in_timezone(timezone: str) -> str:
    """
    A tool that fetches the current local time in a specified timezone.
    
    Args:
        timezone: a valid timezone string (e.g., 'America/New_York')
    
    Returns:
        A string with the current local time or an error message.
    """
    try:
        tz = pytz.timezone(timezone)
        local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
        return f"The current local time in {timezone} is: {local_time}"
    except Exception as e:
        return f"Error fetching time for timezone '{timezone}': {str(e)}"

final_answer = FinalAnswerTool()

model = HfApiModel(
    max_tokens=2096,
    temperature=0.5,
    model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',
    custom_role_conversions=None,
    token=hf_token
)

# Load the image generation tool directly from the public FLUX Space
image_generation_tool = Tool.from_space(
    space_id="black-forest-labs/FLUX.1-dev",
    name="image_generator",
    description="Generate an image from a prompt"
)

with open("prompts.yaml", 'r') as stream:
    prompt_templates = yaml.safe_load(stream)
    
agent = CodeAgent(
    model=model,
    tools=[final_answer, generate_landscape_image, get_current_time_in_timezone],
    max_steps=6,
    verbosity_level=1,
    prompt_templates=prompt_templates
)

GradioUI(agent).launch()