SurpriseSnap / app.py
mohannad-tazi's picture
Create app.py
77b1b74 verified
import gradio as gr
import datetime
import random
from smolagents import CodeAgent, HfApiModel, Tool, DuckDuckGoSearchTool
# Load AI tools
image_generation_tool = Tool.from_space(
"black-forest-labs/FLUX.1-schnell",
name="image_generator",
description="Generate an image from a prompt"
)
web_search_tool = DuckDuckGoSearchTool()
# Load AI model
model = HfApiModel("Qwen/Qwen2.5-Coder-32B-Instruct")
# Create AI agent with tools
agent = CodeAgent(tools=[web_search_tool, image_generation_tool], model=model)
# Function to generate a historical surprise
def generate_surprise(date):
# Format date
formatted_date = datetime.datetime.strptime(date, "%Y-%m-%d").strftime("%B %d")
# Search for historical events on this date
search_query = f"funny, crazy, or weird historical event that happened on {formatted_date} in history."
event = agent.run(f"Search for {search_query}. Return a short, interesting fact.")
# Generate image prompt
image_prompt = agent.run(f"Convert this historical event into a creative image prompt: {event}")
# Generate AI image
image = agent.run(
"Generate an artistic and visually appealing image based on this prompt.",
additional_args={'user_prompt': image_prompt}
)
return event, image
# Get today's date as default
today = datetime.datetime.today().strftime("%Y-%m-%d")
# Create Gradio UI
with gr.Blocks() as demo:
gr.Markdown("# πŸŽ‰ SurpriseSnap - What Happened Today in History? 🎭")
gr.Markdown("Enter a date and discover a surprising historical event with an AI-generated image!")
date_input = gr.Textbox(value=today, label="Enter a Date (YYYY-MM-DD)")
surprise_text = gr.Textbox(label="Historical Event", interactive=False)
surprise_image = gr.Image(label="Generated Image")
generate_btn = gr.Button("✨ Discover History ✨")
generate_btn.click(generate_surprise, inputs=[date_input], outputs=[surprise_text, surprise_image])
# Launch the Gradio app
demo.launch()