AMfeta99 commited on
Commit
642ff3f
·
verified ·
1 Parent(s): 92c06c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -39
app.py CHANGED
@@ -90,50 +90,41 @@ agent = CodeAgent(tools=[image_generation_tool, search_tool], model=llm_engine)
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",
95
- "present": f"{object_name} - Present",
96
- "future": f"{object_name} - Future"
97
- }
98
-
99
 
100
  general_instruction = (
101
  "Search the necessary information and features for the following prompt, "
102
  "then generate an image of it."
103
  )
104
 
 
105
  for time_period, prompt in prompts.items():
106
  print(f"Generating {time_period} frame: {prompt}")
107
- #result = agent.run(prompt)
108
 
109
  try:
110
  result = agent.run(
111
  general_instruction,
112
  additional_args={"user_prompt": prompt}
113
  )
114
-
115
- #if isinstance(result, (list, tuple)):
116
- result = result[0]
117
-
118
  image = result.to_raw()
 
 
 
 
 
 
119
  except Exception as e:
120
  print(f"Agent failed on {time_period}: {e}")
121
  continue
122
-
123
- images.append(result.to_raw())
124
- image_filename = f"{object_name}_{time_period}.png"
125
- plot_and_save_agent_image(result, labels[time_period], save_path=image_filename)
126
 
127
  gif_path = f"{object_name}_evolution.gif"
128
- images[0].save(gif_path, save_all=True, append_images=images[1:], duration=1000, loop=0)
129
- #return [
130
- # f"{object_name}_past.png",
131
- # f"{object_name}_present.png",
132
- # f"{object_name}_future.png"], gif_path
133
- return [(f"{object_name}_past.png", labels["past"]),
134
- (f"{object_name}_present.png", labels["present"]),
135
- (f"{object_name}_future.png", labels["future"])], gif_path
136
- #return images, gif_path
137
 
138
  # =========================================================
139
  # Gradio Interface
@@ -148,32 +139,24 @@ def create_gradio_interface():
148
  """)
149
 
150
  default_images = [
151
- ("car_past.png", "Car - Past"),
152
- ("car_present.png", "Car - Present"),
153
- ("car_future.png", "Car - Future")
154
  ]
155
- #default_images = [
156
- # "car_past.png",
157
- # "car_present.png",
158
- # "car_future.png"
159
- # ]
160
  default_gif_path = "car_evolution.gif"
161
 
162
  with gr.Row():
163
  with gr.Column():
164
  object_name_input = gr.Textbox(label="Enter an object name", placeholder="e.g. bicycle, car, phone")
165
  generate_button = gr.Button("Generate Evolution")
166
- image_gallery = gr.Gallery(label="Generated Images", columns=3, rows=1, value=default_images, type="filepath" )
167
- #image_gallery = gr.Gallery(label="Generated Images", columns=3, rows=1, type="filepath")
168
- gif_output = gr.Image(label="Generated GIF", value=default_gif_path)
169
 
170
  generate_button.click(fn=generate_object_history, inputs=[object_name_input], outputs=[image_gallery, gif_output])
171
 
172
  return demo
173
 
174
- # =========================================================
175
- # Run the app
176
- # =========================================================
177
 
 
178
  demo = create_gradio_interface()
179
- demo.launch(share=True)
 
90
  def generate_object_history(object_name):
91
  images = []
92
  prompts = generate_prompts_for_object(object_name)
 
 
 
 
 
 
93
 
94
  general_instruction = (
95
  "Search the necessary information and features for the following prompt, "
96
  "then generate an image of it."
97
  )
98
 
99
+ image_paths = []
100
  for time_period, prompt in prompts.items():
101
  print(f"Generating {time_period} frame: {prompt}")
 
102
 
103
  try:
104
  result = agent.run(
105
  general_instruction,
106
  additional_args={"user_prompt": prompt}
107
  )
108
+
109
+ if isinstance(result, (list, tuple)):
110
+ result = result[0]
111
+
112
  image = result.to_raw()
113
+ image_filename = f"{object_name}_{time_period}.png"
114
+ image.save(image_filename)
115
+ plot_and_save_agent_image(image, f"{object_name} - {time_period.title()}", save_path=image_filename)
116
+
117
+ image_paths.append(image_filename)
118
+ images.append(image)
119
  except Exception as e:
120
  print(f"Agent failed on {time_period}: {e}")
121
  continue
 
 
 
 
122
 
123
  gif_path = f"{object_name}_evolution.gif"
124
+ if images:
125
+ images[0].save(gif_path, save_all=True, append_images=images[1:], duration=1000, loop=0)
126
+
127
+ return image_paths, gif_path
 
 
 
 
 
128
 
129
  # =========================================================
130
  # Gradio Interface
 
139
  """)
140
 
141
  default_images = [
142
+ "car_past.png",
143
+ "car_present.png",
144
+ "car_future.png"
145
  ]
 
 
 
 
 
146
  default_gif_path = "car_evolution.gif"
147
 
148
  with gr.Row():
149
  with gr.Column():
150
  object_name_input = gr.Textbox(label="Enter an object name", placeholder="e.g. bicycle, car, phone")
151
  generate_button = gr.Button("Generate Evolution")
152
+ image_gallery = gr.Gallery(label="Generated Images", columns=3, rows=1, value=default_images, type="filepath")
153
+ gif_output = gr.Image(label="Generated GIF", value=default_gif_path, type="filepath")
 
154
 
155
  generate_button.click(fn=generate_object_history, inputs=[object_name_input], outputs=[image_gallery, gif_output])
156
 
157
  return demo
158
 
 
 
 
159
 
160
+ # Launch the interface
161
  demo = create_gradio_interface()
162
+ demo.launch()