AMfeta99 commited on
Commit
84abbea
·
verified ·
1 Parent(s): 6968838

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -140
app.py CHANGED
@@ -1,167 +1,64 @@
1
- #from transformers import load_tool, ReactCodeAgent, HfApiEngine
2
- from transformers.Agent import ReactCodeAgent
3
- from transformers.Tools import load_tool
4
  from PIL import Image, ImageDraw, ImageFont
5
- import tempfile
6
  import gradio as gr
 
7
 
8
- #%% Methods
9
- # Function to add a label to an image
 
 
 
 
10
  def add_label_to_image(image, label):
11
- # Create a drawing context
12
  draw = ImageDraw.Draw(image)
13
-
14
- # Define font size and color (adjust font path for your environment)
15
- font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf" # Example font path
16
- font_size = 30 # Larger font size for better visibility
17
  try:
18
- font = ImageFont.truetype(font_path, font_size)
19
  except:
20
  font = ImageFont.load_default()
21
-
22
- # Calculate the size and position of the text (aligned to the left)
23
- text_bbox = draw.textbbox((0, 0), label, font=font)
24
- text_width, text_height = text_bbox[2] - text_bbox[0], text_bbox[3] - text_bbox[1]
25
- position = (image.width - text_width - 20, image.height - text_height - 20)# right-aligned with margin
26
-
27
- # Add a semi-transparent rectangle behind the text for better visibility
28
- rect_margin = 10
29
- rect_position = [
30
- position[0] - rect_margin,
31
- position[1] - rect_margin,
32
- position[0] + text_width + rect_margin,
33
- position[1] + text_height + rect_margin,
34
- ]
35
- draw.rectangle(rect_position, fill=(0, 0, 0, 128)) # Semi-transparent black
36
- draw.text(position, label, fill="white", font=font)
37
  return image
38
 
39
-
40
- # Function to plot, label, and save an image
41
- def plot_and_save_agent_image(agent_image, label, save_path=None):
42
- # Convert AgentImage to a raw PIL Image
43
- pil_image = agent_image.to_raw()
44
-
45
- # Add a label to the image
46
- labeled_image = add_label_to_image(pil_image, label)
47
-
48
- # Plot the image using PIL's show method
49
- labeled_image.show()
50
-
51
- # If save_path is provided, save the image
52
- if save_path:
53
- labeled_image.save(save_path)
54
- print(f"Image saved to {save_path}")
55
- else:
56
- print("No save path provided. Image not saved.")
57
-
58
- # Function to generate prompts for an object
59
- def generate_prompts_for_object(object_name):
60
  prompts = {
61
- "past": f"Show an old version of a {object_name} from its early days.",
62
- "present": f"Show a {object_name} with current features/design/technology.",
63
- "future": f"Show a futuristic version of a {object_name}, by predicting advanced features and futuristic design."
64
  }
65
- return prompts
66
 
67
- # Function to generate the object's history images and GIF
68
- def generate_object_history(object_name):
69
  images = []
70
-
71
- # Get prompts for the object
72
- prompts = generate_prompts_for_object(object_name)
73
- labels = {
74
- "past": f"{object_name} - Past",
75
- "present": f"{object_name} - Present",
76
- "future": f"{object_name} - Future"
77
- }
78
-
79
- # Generate sequential images and display them
80
- for time_period, frame in prompts.items():
81
- print(f"Generating {time_period} frame: {frame}")
82
- result = agent.run(frame) # The tool generates the image
83
-
84
- # Append the image to the list for GIF creation
85
- images.append(result.to_raw()) # Ensure we're using raw image for GIF
86
-
87
- # Save each image with the appropriate name and label
88
- image_filename = f"{object_name}_{time_period}.png"
89
- plot_and_save_agent_image(result, labels[time_period], save_path=image_filename)
90
-
91
- # Create GIF from images
92
- gif_path = f"{object_name}_evolution.gif"
93
- images[0].save(
94
- gif_path,
95
- save_all=True,
96
- append_images=images[1:],
97
- duration=1000, # Duration in milliseconds for each frame
98
- loop=0 # Infinite loop
99
- )
100
-
101
- # Return images and GIF path
102
- return images, gif_path
103
 
104
- #%% Initialization of tools and AI_Agent
105
- # Import text-to-image tool from Hub
106
- image_generation_tool = load_tool("m-ric/text-to-image", cache=False)
 
 
 
 
107
 
108
- # Import search tool from LangChain
109
- from transformers.agents.search import DuckDuckGoSearchTool
110
- search_tool = DuckDuckGoSearchTool()
111
-
112
- # Load the LLM engine
113
- llm_engine = HfApiEngine("Qwen/Qwen2.5-72B-Instruct")
114
 
115
- # Initialize the agent with both tools
116
- agent = ReactCodeAgent(tools=[image_generation_tool, search_tool], llm_engine=llm_engine)
117
 
118
- # Gradio interface
119
  def create_gradio_interface():
120
  with gr.Blocks() as demo:
121
- gr.Markdown("# TimeMetamorphy: an object Evolution Generator")
122
-
123
- # Add a section for instructions
124
- gr.Markdown("""
125
- ## Unlocking the secrets of time!
126
- This app unveils these mysteries by offering a unique/magic lens that allows us "time travel".
127
- Powered by AI agents equipped with cutting-edge tools, it provides the superpower to explore the past, witness the present, and dream up the future like never before.
128
-
129
- This system allows you to generate visualizations of how an object/concept, like a bicycle or a car, may have evolved over time.
130
- It generates images of the object in the past, present, and future based on your input.
131
-
132
- ### Default Example: Evolution of a Car
133
- Below, you can see a precomputed example of a "car" evolution. Enter another object to generate its evolution.
134
- """)
135
-
136
- # Paths to the precomputed files
137
- default_images = [
138
- ("car_past.png", "Car - Past"),
139
- ("car_present.png", "Car - Present"),
140
- ("car_future.png", "Car - Future")
141
- ]
142
- default_gif_path = "car_evolution.gif"
143
 
144
- with gr.Row():
145
- with gr.Column():
146
- # Textbox for user to input an object name
147
- object_name_input = gr.Textbox(label="Enter an object name (e.g., bicycle, phone)",
148
- placeholder="Enter an object name",
149
- lines=1)
150
 
151
- # Button to trigger the generation of images and GIF
152
- generate_button = gr.Button("Generate Evolution")
153
-
154
- # Gradio Gallery component to display the images
155
- image_gallery = gr.Gallery(label="Generated Images", show_label=True, columns=3, rows=1, value=default_images)
156
 
157
- # Output for the generated GIF
158
- gif_output = gr.Image(label="Generated GIF", show_label=True, value=default_gif_path)
159
-
160
- # Set the action when the button is clicked
161
- generate_button.click(fn=generate_object_history, inputs=[object_name_input], outputs=[image_gallery, gif_output])
162
-
163
  return demo
164
 
165
- # Launch the Gradio app
166
  demo = create_gradio_interface()
167
  demo.launch(share=True)
 
1
+ import torch
 
 
2
  from PIL import Image, ImageDraw, ImageFont
 
3
  import gradio as gr
4
+ from diffusers import StableDiffusionPipeline
5
 
6
+ # Load Stable Diffusion model
7
+ pipe = StableDiffusionPipeline.from_pretrained(
8
+ "runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16
9
+ ).to("cuda" if torch.cuda.is_available() else "cpu")
10
+
11
+ # Function to add label
12
  def add_label_to_image(image, label):
 
13
  draw = ImageDraw.Draw(image)
 
 
 
 
14
  try:
15
+ font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 30)
16
  except:
17
  font = ImageFont.load_default()
18
+ position = (20, image.height - 50)
19
+ draw.rectangle([position, (position[0]+400, position[1]+40)], fill=(0, 0, 0, 180))
20
+ draw.text(position, label, font=font, fill="white")
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  return image
22
 
23
+ # Generate prompt images
24
+ def generate_object_history(object_name):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  prompts = {
26
+ "past": f"An old version of a {object_name}, vintage, old-fashioned",
27
+ "present": f"A modern {object_name}, realistic, current design",
28
+ "future": f"A futuristic {object_name}, sci-fi, advanced design"
29
  }
 
30
 
 
 
31
  images = []
32
+ pil_images = []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
+ for period, prompt in prompts.items():
35
+ image = pipe(prompt).images[0]
36
+ labeled_image = add_label_to_image(image, f"{object_name.title()} - {period.title()}")
37
+ filename = f"{object_name}_{period}.png"
38
+ labeled_image.save(filename)
39
+ images.append((filename, f"{object_name.title()} - {period.title()}"))
40
+ pil_images.append(labeled_image)
41
 
42
+ gif_path = f"{object_name}_evolution.gif"
43
+ pil_images[0].save(gif_path, save_all=True, append_images=pil_images[1:], duration=1000, loop=0)
 
 
 
 
44
 
45
+ return images, gif_path
 
46
 
47
+ # Gradio Interface
48
  def create_gradio_interface():
49
  with gr.Blocks() as demo:
50
+ gr.Markdown("# TimeMetamorphy: Object Evolution Visualizer")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
+ object_name_input = gr.Textbox(label="Enter an object name (e.g., bicycle, phone)")
53
+ generate_button = gr.Button("Generate Evolution")
 
 
 
 
54
 
55
+ image_gallery = gr.Gallery(label="Generated Images", columns=3, rows=1)
56
+ gif_output = gr.Image(label="Generated GIF")
 
 
 
57
 
58
+ generate_button.click(fn=generate_object_history,
59
+ inputs=[object_name_input],
60
+ outputs=[image_gallery, gif_output])
 
 
 
61
  return demo
62
 
 
63
  demo = create_gradio_interface()
64
  demo.launch(share=True)