AMfeta99 commited on
Commit
3db8e9e
·
verified ·
1 Parent(s): ed3c51d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +111 -0
app.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL import Image, ImageDraw, ImageFont
2
+ import gradio as gr
3
+ from smolagents import CodeAgent, InferenceClientModel, DuckDuckGoSearchTool, Tool
4
+ from gradio_client import Client
5
+
6
+ #%% Tool Wrapper for the Hugging Face Space
7
+ class TextToImageTool(Tool):
8
+ name = "text_to_image"
9
+ description = "Generate an image from a text prompt using m-ric/text-to-image."
10
+
11
+ def __init__(self):
12
+ super().__init__()
13
+ self.client = Client("m-ric/text-to-image") # Calls HF Space
14
+
15
+ def run(self, prompt: str):
16
+ image = self.client.predict(prompt, api_name="/predict")
17
+ return image # This is a PIL image
18
+
19
+ #%% Utility functions
20
+ def add_label_to_image(image, label):
21
+ draw = ImageDraw.Draw(image)
22
+ font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
23
+ font_size = 30
24
+ try:
25
+ font = ImageFont.truetype(font_path, font_size)
26
+ except:
27
+ font = ImageFont.load_default()
28
+ text_bbox = draw.textbbox((0, 0), label, font=font)
29
+ text_width = text_bbox[2] - text_bbox[0]
30
+ text_height = text_bbox[3] - text_bbox[1]
31
+ position = (image.width - text_width - 20, image.height - text_height - 20)
32
+ rect_margin = 10
33
+ rect_position = [
34
+ position[0] - rect_margin, position[1] - rect_margin,
35
+ position[0] + text_width + rect_margin, position[1] + text_height + rect_margin
36
+ ]
37
+ draw.rectangle(rect_position, fill=(0, 0, 0, 128))
38
+ draw.text(position, label, fill="white", font=font)
39
+ return image
40
+
41
+ def plot_and_save_agent_image(image, label, save_path=None):
42
+ labeled_image = add_label_to_image(image, label)
43
+ labeled_image.show()
44
+ if save_path:
45
+ labeled_image.save(save_path)
46
+ print(f"Image saved to {save_path}")
47
+
48
+ def generate_prompts_for_object(object_name):
49
+ return {
50
+ "past": f"Show an old version of a {object_name} from its early days.",
51
+ "present": f"Show a {object_name} with current features/design/technology.",
52
+ "future": f"Show a futuristic version of a {object_name}, by predicting advanced features and futuristic design."
53
+ }
54
+
55
+ def generate_object_history(object_name):
56
+ prompts = generate_prompts_for_object(object_name)
57
+ labels = {
58
+ "past": f"{object_name} - Past",
59
+ "present": f"{object_name} - Present",
60
+ "future": f"{object_name} - Future"
61
+ }
62
+
63
+ images = []
64
+ for time_period, prompt in prompts.items():
65
+ print(f"Generating {time_period} frame: {prompt}")
66
+ result = agent.run(prompt) # Runs tool
67
+ if hasattr(result, "to_raw"): # If wrapped output
68
+ result = result.to_raw()
69
+ images.append(result)
70
+ plot_and_save_agent_image(result, labels[time_period], save_path=f"{object_name}_{time_period}.png")
71
+
72
+ gif_path = f"{object_name}_evolution.gif"
73
+ images[0].save(gif_path, save_all=True, append_images=images[1:], duration=1000, loop=0)
74
+ return images, gif_path
75
+
76
+ #%% Tool & Agent Setup
77
+ image_generation_tool = TextToImageTool()
78
+ search_tool = DuckDuckGoSearchTool()
79
+ llm_engine = InferenceClientModel("Qwen/Qwen2.5-72B-Instruct")
80
+
81
+ agent = CodeAgent(tools=[image_generation_tool, search_tool], model=llm_engine)
82
+
83
+ #%% Gradio Interface
84
+ def create_gradio_interface():
85
+ with gr.Blocks() as demo:
86
+ gr.Markdown("# TimeMetamorphy: an object Evolution Generator")
87
+ gr.Markdown("""
88
+ ## Unlocking the secrets of time!
89
+ Enter an object name (like bicycle or smartphone), and this app will generate its visual evolution.
90
+ """)
91
+
92
+ default_images = [
93
+ ("car_past.png", "Car - Past"),
94
+ ("car_present.png", "Car - Present"),
95
+ ("car_future.png", "Car - Future")
96
+ ]
97
+ default_gif_path = "car_evolution.gif"
98
+
99
+ with gr.Row():
100
+ with gr.Column():
101
+ object_name_input = gr.Textbox(label="Enter an object name", placeholder="e.g., bicycle, phone")
102
+ generate_button = gr.Button("Generate Evolution")
103
+ image_gallery = gr.Gallery(label="Generated Images", columns=3, rows=1, value=default_images)
104
+ gif_output = gr.Image(label="Generated GIF", value=default_gif_path)
105
+
106
+ generate_button.click(fn=generate_object_history, inputs=[object_name_input], outputs=[image_gallery, gif_output])
107
+ return demo
108
+
109
+ # Launch app
110
+ demo = create_gradio_interface()
111
+ demo.launch(share=True)