JeCabrera's picture
Update app.py
23e6cf2 verified
raw
history blame
2.09 kB
from smolagents import CodeAgent, HfApiModel, load_tool
import datetime
import pytz
import yaml
import gradio as gr
from tools.final_answer import FinalAnswerTool
from Gradio_UI import GradioUI
def get_current_time_in_timezone(timezone: str) -> str:
"""Obtiene la hora actual en una zona horaria específica."""
try:
tz = pytz.timezone(timezone)
local_time = datetime.datetime.now(tz).strftime("%H:%M")
return local_time
except Exception as e:
return f"Error obteniendo la hora para '{timezone}': {str(e)}"
def generate_time_based_image(time: str) -> str:
"""Genera una imagen representativa de la hora actual."""
hour = int(time.split(":")[0])
if 5 <= hour < 12:
prompt = "A beautiful sunrise over a city skyline. Morning vibes."
elif 12 <= hour < 18:
prompt = "A lively city during the afternoon with bright daylight."
else:
prompt = "A peaceful night with a starry sky over a quiet city."
image_url = image_generation_tool(prompt) # Genera la imagen con el tool
return image_url
final_answer = FinalAnswerTool()
model = HfApiModel(
max_tokens=2096,
temperature=0.5,
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
)
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
with open("prompts.yaml", 'r') as stream:
prompt_templates = yaml.safe_load(stream)
def chatbot(timezone: str):
time = get_current_time_in_timezone(timezone)
image_url = generate_time_based_image(time)
return f"La hora actual en {timezone} es {time}", image_url
agent = CodeAgent(
model=model,
tools=[final_answer],
max_steps=6,
verbosity_level=1,
grammar=None,
planning_interval=None,
name=None,
description=None,
prompt_templates=prompt_templates
)
gr.Interface(
chatbot,
inputs=gr.Textbox(label="Introduce tu zona horaria (ej. America/New_York)"),
outputs=[gr.Textbox(label="Hora actual"), gr.Image(label="Imagen de la hora")],
title="Chatbot de Hora con Generación de Imágenes",
).launch()