Update app.py
Browse files
app.py
CHANGED
@@ -1,28 +1,30 @@
|
|
1 |
-
from
|
|
|
|
|
|
|
|
|
2 |
from PIL import Image, ImageDraw, ImageFont
|
3 |
import tempfile
|
4 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
#%% Methods
|
7 |
-
|
8 |
def add_label_to_image(image, label):
|
9 |
-
# Create a drawing context
|
10 |
draw = ImageDraw.Draw(image)
|
11 |
-
|
12 |
-
|
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)
|
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,108 +32,129 @@ def add_label_to_image(image, label):
|
|
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))
|
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 |
-
#
|
41 |
-
pil_image = agent_image
|
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 |
-
|
57 |
def generate_prompts_for_object(object_name):
|
58 |
-
|
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 |
-
|
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 |
-
|
86 |
-
|
87 |
-
|
88 |
|
89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
gif_path = f"{object_name}_evolution.gif"
|
91 |
-
images
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
|
|
|
|
|
|
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 |
-
#
|
107 |
-
|
108 |
-
search_tool = DuckDuckGoSearchTool()
|
109 |
|
110 |
-
|
111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
|
113 |
-
#
|
114 |
-
|
|
|
|
|
115 |
|
116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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,25 +164,22 @@ def create_gradio_interface():
|
|
141 |
|
142 |
with gr.Row():
|
143 |
with gr.Column():
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
# Button to trigger the generation of images and GIF
|
150 |
generate_button = gr.Button("Generate Evolution")
|
151 |
-
|
152 |
-
|
153 |
-
|
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 |
-
|
|
|
164 |
demo = create_gradio_interface()
|
165 |
-
demo.launch(share=True)
|
|
|
1 |
+
from huggingface_hub import InferenceClient
|
2 |
+
from langchain_huggingface import HuggingFaceHub
|
3 |
+
from langchain.tools import DuckDuckGoSearchResults
|
4 |
+
from langchain.agents import create_react_agent
|
5 |
+
from langchain.tools import BaseTool
|
6 |
from PIL import Image, ImageDraw, ImageFont
|
7 |
import tempfile
|
8 |
import gradio as gr
|
9 |
+
import requests
|
10 |
+
from io import BytesIO
|
11 |
+
|
12 |
+
# Your HF API token here (set your actual token)
|
13 |
+
#HF_TOKEN
|
14 |
|
15 |
#%% Methods
|
16 |
+
|
17 |
def add_label_to_image(image, label):
|
|
|
18 |
draw = ImageDraw.Draw(image)
|
19 |
+
font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
|
20 |
+
font_size = 30
|
|
|
|
|
21 |
try:
|
22 |
font = ImageFont.truetype(font_path, font_size)
|
23 |
except:
|
24 |
font = ImageFont.load_default()
|
|
|
|
|
25 |
text_bbox = draw.textbbox((0, 0), label, font=font)
|
26 |
text_width, text_height = text_bbox[2] - text_bbox[0], text_bbox[3] - text_bbox[1]
|
27 |
+
position = (image.width - text_width - 20, image.height - text_height - 20)
|
|
|
|
|
28 |
rect_margin = 10
|
29 |
rect_position = [
|
30 |
position[0] - 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))
|
36 |
draw.text(position, label, fill="white", font=font)
|
37 |
return image
|
38 |
|
39 |
|
|
|
40 |
def plot_and_save_agent_image(agent_image, label, save_path=None):
|
41 |
+
# agent_image is a PIL Image already in this refactor
|
42 |
+
pil_image = agent_image
|
43 |
|
|
|
44 |
labeled_image = add_label_to_image(pil_image, label)
|
|
|
|
|
45 |
labeled_image.show()
|
46 |
|
|
|
47 |
if save_path:
|
48 |
labeled_image.save(save_path)
|
49 |
print(f"Image saved to {save_path}")
|
50 |
else:
|
51 |
print("No save path provided. Image not saved.")
|
52 |
|
53 |
+
|
54 |
def generate_prompts_for_object(object_name):
|
55 |
+
return {
|
56 |
"past": f"Show an old version of a {object_name} from its early days.",
|
57 |
"present": f"Show a {object_name} with current features/design/technology.",
|
58 |
"future": f"Show a futuristic version of a {object_name}, by predicting advanced features and futuristic design."
|
59 |
}
|
|
|
60 |
|
61 |
+
|
62 |
def generate_object_history(object_name):
|
63 |
images = []
|
|
|
|
|
64 |
prompts = generate_prompts_for_object(object_name)
|
65 |
labels = {
|
66 |
"past": f"{object_name} - Past",
|
67 |
"present": f"{object_name} - Present",
|
68 |
"future": f"{object_name} - Future"
|
69 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
+
for time_period, prompt in prompts.items():
|
72 |
+
print(f"Generating {time_period} frame: {prompt}")
|
73 |
+
result = agent.invoke(prompt) # returns PIL Image or string output
|
74 |
|
75 |
+
# result is a PIL Image from our tool, or fallback string - ensure PIL Image
|
76 |
+
if isinstance(result, Image.Image):
|
77 |
+
images.append(result)
|
78 |
+
image_filename = f"{object_name}_{time_period}.png"
|
79 |
+
plot_and_save_agent_image(result, labels[time_period], save_path=image_filename)
|
80 |
+
else:
|
81 |
+
print(f"Unexpected output for {time_period}: {result}")
|
82 |
+
|
83 |
gif_path = f"{object_name}_evolution.gif"
|
84 |
+
if images:
|
85 |
+
images[0].save(
|
86 |
+
gif_path,
|
87 |
+
save_all=True,
|
88 |
+
append_images=images[1:],
|
89 |
+
duration=1000,
|
90 |
+
loop=0
|
91 |
+
)
|
92 |
+
print(f"GIF saved to {gif_path}")
|
93 |
+
else:
|
94 |
+
print("No images generated, GIF not created.")
|
95 |
+
|
96 |
return images, gif_path
|
97 |
|
98 |
+
|
99 |
#%% Initialization of tools and AI_Agent
|
|
|
|
|
100 |
|
101 |
+
# Initialize HuggingFace Inference Client for text-to-image
|
102 |
+
text_to_image_client = InferenceClient(repo_id="m-ric/text-to-image")
|
|
|
103 |
|
104 |
+
def run_text_to_image(prompt: str) -> Image.Image:
|
105 |
+
outputs = text_to_image_client.text_to_image(prompt)
|
106 |
+
# Assuming outputs returns a list of URLs
|
107 |
+
image_url = outputs[0] if outputs else None
|
108 |
+
if image_url is None:
|
109 |
+
raise ValueError("No image URL returned from the model.")
|
110 |
+
response = requests.get(image_url)
|
111 |
+
img = Image.open(BytesIO(response.content)).convert("RGB")
|
112 |
+
return img
|
113 |
|
114 |
+
# Custom LangChain tool wrapper for text-to-image
|
115 |
+
class TextToImageTool(BaseTool):
|
116 |
+
name = "text-to-image"
|
117 |
+
description = "Generates an image from a prompt using HuggingFace model"
|
118 |
|
119 |
+
def _run(self, prompt: str):
|
120 |
+
return run_text_to_image(prompt)
|
121 |
+
|
122 |
+
async def _arun(self, prompt: str):
|
123 |
+
raise NotImplementedError()
|
124 |
+
|
125 |
+
image_generation_tool = TextToImageTool()
|
126 |
+
|
127 |
+
# DuckDuckGo Search Tool from LangChain
|
128 |
+
search_tool = DuckDuckGoSearchResults()
|
129 |
+
|
130 |
+
# HuggingFace LLM for Qwen2.5
|
131 |
+
llm_engine = HuggingFaceHub(
|
132 |
+
repo_id="Qwen/Qwen2.5-72B-Instruct",
|
133 |
+
huggingfacehub_api_token=HF_TOKEN,
|
134 |
+
model_kwargs={"temperature": 0.7}
|
135 |
+
)
|
136 |
+
|
137 |
+
# Create agent with the tools and LLM
|
138 |
+
agent = create_react_agent(llm_engine, tools=[image_generation_tool, search_tool])
|
139 |
+
|
140 |
+
|
141 |
+
#%% Gradio interface
|
142 |
def create_gradio_interface():
|
143 |
with gr.Blocks() as demo:
|
144 |
gr.Markdown("# TimeMetamorphy: an object Evolution Generator")
|
145 |
|
|
|
146 |
gr.Markdown("""
|
147 |
## Unlocking the secrets of time!
|
148 |
This app unveils these mysteries by offering a unique/magic lens that allows us "time travel".
|
149 |
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.
|
150 |
+
|
151 |
This system allows you to generate visualizations of how an object/concept, like a bicycle or a car, may have evolved over time.
|
152 |
It generates images of the object in the past, present, and future based on your input.
|
153 |
+
|
154 |
### Default Example: Evolution of a Car
|
155 |
Below, you can see a precomputed example of a "car" evolution. Enter another object to generate its evolution.
|
156 |
""")
|
157 |
|
|
|
158 |
default_images = [
|
159 |
("car_past.png", "Car - Past"),
|
160 |
("car_present.png", "Car - Present"),
|
|
|
164 |
|
165 |
with gr.Row():
|
166 |
with gr.Column():
|
167 |
+
object_name_input = gr.Textbox(
|
168 |
+
label="Enter an object name (e.g., bicycle, phone)",
|
169 |
+
placeholder="Enter an object name",
|
170 |
+
lines=1
|
171 |
+
)
|
|
|
172 |
generate_button = gr.Button("Generate Evolution")
|
173 |
+
image_gallery = gr.Gallery(
|
174 |
+
label="Generated Images", show_label=True, columns=3, rows=1, value=default_images
|
175 |
+
)
|
|
|
|
|
176 |
gif_output = gr.Image(label="Generated GIF", show_label=True, value=default_gif_path)
|
177 |
+
|
|
|
178 |
generate_button.click(fn=generate_object_history, inputs=[object_name_input], outputs=[image_gallery, gif_output])
|
179 |
|
180 |
return demo
|
181 |
|
182 |
+
|
183 |
+
# Launch the Gradio app
|
184 |
demo = create_gradio_interface()
|
185 |
+
demo.launch(share=True)
|