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