mohannad-tazi commited on
Commit
77b1b74
·
verified ·
1 Parent(s): da98375

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -0
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import datetime
3
+ import random
4
+ from smolagents import CodeAgent, HfApiModel, Tool, DuckDuckGoSearchTool
5
+
6
+ # Load AI tools
7
+ image_generation_tool = Tool.from_space(
8
+ "black-forest-labs/FLUX.1-schnell",
9
+ name="image_generator",
10
+ description="Generate an image from a prompt"
11
+ )
12
+
13
+ web_search_tool = DuckDuckGoSearchTool()
14
+
15
+ # Load AI model
16
+ model = HfApiModel("Qwen/Qwen2.5-Coder-32B-Instruct")
17
+
18
+ # Create AI agent with tools
19
+ agent = CodeAgent(tools=[web_search_tool, image_generation_tool], model=model)
20
+
21
+ # Function to generate a historical surprise
22
+ def generate_surprise(date):
23
+ # Format date
24
+ formatted_date = datetime.datetime.strptime(date, "%Y-%m-%d").strftime("%B %d")
25
+
26
+ # Search for historical events on this date
27
+ search_query = f"funny, crazy, or weird historical event that happened on {formatted_date} in history."
28
+ event = agent.run(f"Search for {search_query}. Return a short, interesting fact.")
29
+
30
+ # Generate image prompt
31
+ image_prompt = agent.run(f"Convert this historical event into a creative image prompt: {event}")
32
+
33
+ # Generate AI image
34
+ image = agent.run(
35
+ "Generate an artistic and visually appealing image based on this prompt.",
36
+ additional_args={'user_prompt': image_prompt}
37
+ )
38
+
39
+ return event, image
40
+
41
+ # Get today's date as default
42
+ today = datetime.datetime.today().strftime("%Y-%m-%d")
43
+
44
+ # Create Gradio UI
45
+ with gr.Blocks() as demo:
46
+ gr.Markdown("# 🎉 SurpriseSnap - What Happened Today in History? 🎭")
47
+ gr.Markdown("Enter a date and discover a surprising historical event with an AI-generated image!")
48
+
49
+ date_input = gr.Textbox(value=today, label="Enter a Date (YYYY-MM-DD)")
50
+ surprise_text = gr.Textbox(label="Historical Event", interactive=False)
51
+ surprise_image = gr.Image(label="Generated Image")
52
+
53
+ generate_btn = gr.Button("✨ Discover History ✨")
54
+ generate_btn.click(generate_surprise, inputs=[date_input], outputs=[surprise_text, surprise_image])
55
+
56
+ # Launch the Gradio app
57
+ demo.launch()