Create app_init.py
Browse files- app_init.py +165 -0
app_init.py
ADDED
@@ -0,0 +1,165 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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,
|
29 |
+
position[1] - 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"),
|
138 |
+
("car_future.png", "Car - Future")
|
139 |
+
]
|
140 |
+
default_gif_path = "car_evolution.gif"
|
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)
|