AMfeta99 commited on
Commit
5658261
·
verified ·
1 Parent(s): 43b06bc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -48
app.py CHANGED
@@ -1,30 +1,14 @@
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
- inputs = {
12
- "prompt": {
13
- "type": "string",
14
- "description": "Text prompt to generate the image"
15
- }
16
- }
17
-
18
- output_type = "image"
19
-
20
- def __init__(self):
21
- super().__init__()
22
- self.client = Client("m-ric/text-to-image")
23
-
24
- def run(self, prompt): # Must explicitly match 'inputs' keys
25
- return self.client.predict(prompt, api_name="/predict")
26
 
27
- #%% Utility functions
28
  def add_label_to_image(image, label):
29
  draw = ImageDraw.Draw(image)
30
  font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
@@ -33,25 +17,33 @@ def add_label_to_image(image, label):
33
  font = ImageFont.truetype(font_path, font_size)
34
  except:
35
  font = ImageFont.load_default()
 
36
  text_bbox = draw.textbbox((0, 0), label, font=font)
37
- text_width = text_bbox[2] - text_bbox[0]
38
- text_height = text_bbox[3] - text_bbox[1]
39
  position = (image.width - text_width - 20, image.height - text_height - 20)
 
40
  rect_margin = 10
41
  rect_position = [
42
- position[0] - rect_margin, position[1] - rect_margin,
43
- position[0] + text_width + rect_margin, position[1] + text_height + rect_margin
 
 
44
  ]
45
  draw.rectangle(rect_position, fill=(0, 0, 0, 128))
46
  draw.text(position, label, fill="white", font=font)
47
  return image
48
 
49
- def plot_and_save_agent_image(image, label, save_path=None):
50
- labeled_image = add_label_to_image(image, label)
 
 
51
  labeled_image.show()
52
  if save_path:
53
  labeled_image.save(save_path)
54
  print(f"Image saved to {save_path}")
 
 
 
55
 
56
  def generate_prompts_for_object(object_name):
57
  return {
@@ -60,7 +52,43 @@ def generate_prompts_for_object(object_name):
60
  "future": f"Show a futuristic version of a {object_name}, by predicting advanced features and futuristic design."
61
  }
62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  def generate_object_history(object_name):
 
64
  prompts = generate_prompts_for_object(object_name)
65
  labels = {
66
  "past": f"{object_name} - Past",
@@ -68,33 +96,29 @@ def generate_object_history(object_name):
68
  "future": f"{object_name} - Future"
69
  }
70
 
71
- images = []
72
  for time_period, prompt in prompts.items():
73
  print(f"Generating {time_period} frame: {prompt}")
74
- result = agent.run(prompt) # Runs tool
75
- if hasattr(result, "to_raw"): # If wrapped output
76
- result = result.to_raw()
77
- images.append(result)
78
- plot_and_save_agent_image(result, labels[time_period], save_path=f"{object_name}_{time_period}.png")
79
 
80
  gif_path = f"{object_name}_evolution.gif"
81
  images[0].save(gif_path, save_all=True, append_images=images[1:], duration=1000, loop=0)
82
- return images, gif_path
83
-
84
- #%% Tool & Agent Setup
85
- image_generation_tool = TextToImageTool()
86
- search_tool = DuckDuckGoSearchTool()
87
- llm_engine = InferenceClientModel("Qwen/Qwen2.5-72B-Instruct")
88
 
89
- agent = CodeAgent(tools=[image_generation_tool, search_tool], model=llm_engine)
 
 
90
 
91
- #%% Gradio Interface
92
  def create_gradio_interface():
93
  with gr.Blocks() as demo:
94
- gr.Markdown("# TimeMetamorphy: an object Evolution Generator")
95
  gr.Markdown("""
96
- ## Unlocking the secrets of time!
97
- Enter an object name (like bicycle or smartphone), and this app will generate its visual evolution.
98
  """)
99
 
100
  default_images = [
@@ -106,14 +130,18 @@ def create_gradio_interface():
106
 
107
  with gr.Row():
108
  with gr.Column():
109
- object_name_input = gr.Textbox(label="Enter an object name", placeholder="e.g., bicycle, phone")
110
  generate_button = gr.Button("Generate Evolution")
111
  image_gallery = gr.Gallery(label="Generated Images", columns=3, rows=1, value=default_images)
112
  gif_output = gr.Image(label="Generated GIF", value=default_gif_path)
113
 
114
  generate_button.click(fn=generate_object_history, inputs=[object_name_input], outputs=[image_gallery, gif_output])
 
115
  return demo
116
 
117
- # Launch app
 
 
 
118
  demo = create_gradio_interface()
119
  demo.launch(share=True)
 
1
  from PIL import Image, ImageDraw, ImageFont
2
+ import tempfile
3
  import gradio as gr
4
+ from smolagents import CodeAgent, InferenceClientModel
5
+ from smolagents import DuckDuckGoSearchTool, Tool
6
+ from huggingface_hub import InferenceClient
7
 
8
+ # =========================================================
9
+ # Utility functions
10
+ # =========================================================
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
 
12
  def add_label_to_image(image, label):
13
  draw = ImageDraw.Draw(image)
14
  font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
 
17
  font = ImageFont.truetype(font_path, font_size)
18
  except:
19
  font = ImageFont.load_default()
20
+
21
  text_bbox = draw.textbbox((0, 0), label, font=font)
22
+ text_width, text_height = text_bbox[2] - text_bbox[0], text_bbox[3] - text_bbox[1]
 
23
  position = (image.width - text_width - 20, image.height - text_height - 20)
24
+
25
  rect_margin = 10
26
  rect_position = [
27
+ position[0] - rect_margin,
28
+ position[1] - rect_margin,
29
+ position[0] + text_width + rect_margin,
30
+ position[1] + text_height + rect_margin,
31
  ]
32
  draw.rectangle(rect_position, fill=(0, 0, 0, 128))
33
  draw.text(position, label, fill="white", font=font)
34
  return image
35
 
36
+
37
+ def plot_and_save_agent_image(agent_image, label, save_path=None):
38
+ pil_image = agent_image.to_raw()
39
+ labeled_image = add_label_to_image(pil_image, label)
40
  labeled_image.show()
41
  if save_path:
42
  labeled_image.save(save_path)
43
  print(f"Image saved to {save_path}")
44
+ else:
45
+ print("No save path provided. Image not saved.")
46
+
47
 
48
  def generate_prompts_for_object(object_name):
49
  return {
 
52
  "future": f"Show a futuristic version of a {object_name}, by predicting advanced features and futuristic design."
53
  }
54
 
55
+ # =========================================================
56
+ # Tool wrapper for m-ric/text-to-image
57
+ # =========================================================
58
+
59
+ class WrappedTextToImageTool(Tool):
60
+ name = "text_to_image"
61
+ description = "Generates an image from a text prompt using the m-ric/text-to-image tool."
62
+ inputs = {
63
+ "prompt": {
64
+ "type": "string",
65
+ "description": "Text prompt to generate an image"
66
+ }
67
+ }
68
+ output_type = "image"
69
+
70
+ def __init__(self):
71
+ self.client = InferenceClient("m-ric/text-to-image")
72
+
73
+ def forward(self, prompt):
74
+ return self.client.text_to_image(prompt)
75
+
76
+ # =========================================================
77
+ # Tool and Agent Initialization
78
+ # =========================================================
79
+
80
+ image_generation_tool = WrappedTextToImageTool()
81
+ search_tool = DuckDuckGoSearchTool()
82
+ llm_engine = InferenceClientModel("Qwen/Qwen2.5-72B-Instruct")
83
+
84
+ agent = CodeAgent(tools=[image_generation_tool, search_tool], model=llm_engine)
85
+
86
+ # =========================================================
87
+ # Main logic for image generation
88
+ # =========================================================
89
+
90
  def generate_object_history(object_name):
91
+ images = []
92
  prompts = generate_prompts_for_object(object_name)
93
  labels = {
94
  "past": f"{object_name} - Past",
 
96
  "future": f"{object_name} - Future"
97
  }
98
 
 
99
  for time_period, prompt in prompts.items():
100
  print(f"Generating {time_period} frame: {prompt}")
101
+ result = agent.run(prompt)
102
+ images.append(result.to_raw())
103
+ image_filename = f"{object_name}_{time_period}.png"
104
+ plot_and_save_agent_image(result, labels[time_period], save_path=image_filename)
 
105
 
106
  gif_path = f"{object_name}_evolution.gif"
107
  images[0].save(gif_path, save_all=True, append_images=images[1:], duration=1000, loop=0)
108
+ return [(f"{object_name}_past.png", labels["past"]),
109
+ (f"{object_name}_present.png", labels["present"]),
110
+ (f"{object_name}_future.png", labels["future"])], gif_path
 
 
 
111
 
112
+ # =========================================================
113
+ # Gradio Interface
114
+ # =========================================================
115
 
 
116
  def create_gradio_interface():
117
  with gr.Blocks() as demo:
118
+ gr.Markdown("# TimeMetamorphy: An Object Evolution Generator")
119
  gr.Markdown("""
120
+ Explore how everyday objects evolved over time. Enter an object name like "phone", "car", or "bicycle"
121
+ and see its past, present, and future visualized with AI!
122
  """)
123
 
124
  default_images = [
 
130
 
131
  with gr.Row():
132
  with gr.Column():
133
+ object_name_input = gr.Textbox(label="Enter an object name", placeholder="e.g. bicycle, car, phone")
134
  generate_button = gr.Button("Generate Evolution")
135
  image_gallery = gr.Gallery(label="Generated Images", columns=3, rows=1, value=default_images)
136
  gif_output = gr.Image(label="Generated GIF", value=default_gif_path)
137
 
138
  generate_button.click(fn=generate_object_history, inputs=[object_name_input], outputs=[image_gallery, gif_output])
139
+
140
  return demo
141
 
142
+ # =========================================================
143
+ # Run the app
144
+ # =========================================================
145
+
146
  demo = create_gradio_interface()
147
  demo.launch(share=True)