Update app.py
Browse files
app.py
CHANGED
@@ -1,46 +1,76 @@
|
|
1 |
-
#%% Import libraries
|
2 |
from transformers import load_tool, ReactCodeAgent, HfApiEngine
|
3 |
-
from PIL import Image
|
4 |
-
import torch
|
5 |
-
import numpy as np
|
6 |
import tempfile
|
7 |
-
import os
|
8 |
-
import uuid
|
9 |
import gradio as gr
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
# Convert AgentImage to a raw PIL Image
|
16 |
pil_image = agent_image.to_raw()
|
17 |
|
|
|
|
|
|
|
18 |
# Plot the image using PIL's show method
|
19 |
-
|
20 |
|
21 |
# If save_path is provided, save the image
|
22 |
if save_path:
|
23 |
-
|
24 |
print(f"Image saved to {save_path}")
|
25 |
else:
|
26 |
print("No save path provided. Image not saved.")
|
27 |
|
28 |
-
|
29 |
def generate_prompts_for_object(object_name):
|
30 |
prompts = {
|
31 |
"past": f"Show an old version of a {object_name} from its early days.",
|
32 |
-
"present": f"Show a {object_name} with
|
33 |
"future": f"Show a futuristic version of a {object_name}, by predicting advanced features and futuristic design."
|
34 |
}
|
35 |
return prompts
|
36 |
|
37 |
-
|
38 |
-
# Function to generate the car industry history
|
39 |
def generate_object_history(object_name):
|
40 |
images = []
|
41 |
|
42 |
# Get prompts for the object
|
43 |
prompts = generate_prompts_for_object(object_name)
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
# Generate sequential images and display them
|
46 |
for time_period, frame in prompts.items():
|
@@ -50,10 +80,9 @@ def generate_object_history(object_name):
|
|
50 |
# Append the image to the list for GIF creation
|
51 |
images.append(result.to_raw()) # Ensure we're using raw image for GIF
|
52 |
|
53 |
-
# Save each image with the appropriate name
|
54 |
image_filename = f"{object_name}_{time_period}.png"
|
55 |
-
plot_and_save_agent_image(result, save_path=image_filename)
|
56 |
-
|
57 |
|
58 |
# Create GIF from images
|
59 |
gif_path = f"{object_name}_evolution.gif"
|
@@ -68,25 +97,20 @@ def generate_object_history(object_name):
|
|
68 |
# Return images and GIF path
|
69 |
return images, gif_path
|
70 |
|
71 |
-
|
72 |
#%% Initialization of tools and AI_Agent
|
73 |
-
# Import text-to-image tool from Hub
|
74 |
-
|
75 |
-
image_generation_tool = load_tool("m-ric/text-to-image", cache=False) #cache=False ensures it fetches the latest tool updates directly from the Hub.
|
76 |
|
77 |
# Import search tool from LangChain
|
78 |
-
#This tool allows the agent to search for and retrieve information from the web.
|
79 |
from transformers.agents.search import DuckDuckGoSearchTool
|
80 |
-
|
81 |
search_tool = DuckDuckGoSearchTool()
|
82 |
|
83 |
-
#
|
84 |
-
llm_engine = HfApiEngine("Qwen/Qwen2.5-72B-Instruct")
|
|
|
85 |
# Initialize the agent with both tools
|
86 |
agent = ReactCodeAgent(tools=[image_generation_tool, search_tool], llm_engine=llm_engine)
|
87 |
|
88 |
-
|
89 |
-
|
90 |
# Gradio interface
|
91 |
def create_gradio_interface():
|
92 |
with gr.Blocks() as demo:
|
@@ -124,11 +148,10 @@ def create_gradio_interface():
|
|
124 |
generate_button = gr.Button("Generate Evolution")
|
125 |
|
126 |
# Gradio Gallery component to display the images
|
127 |
-
image_gallery = gr.Gallery(label="Generated Images", show_label=True, columns=3, rows=1
|
128 |
-
value=default_images)
|
129 |
|
130 |
# Output for the generated GIF
|
131 |
-
gif_output = gr.Image(label="Generated GIF", show_label=True
|
132 |
|
133 |
# Set the action when the button is clicked
|
134 |
generate_button.click(fn=generate_object_history, inputs=[object_name_input], outputs=[image_gallery, gif_output])
|
@@ -137,6 +160,4 @@ def create_gradio_interface():
|
|
137 |
|
138 |
# Launch the Gradio app
|
139 |
demo = create_gradio_interface()
|
140 |
-
|
141 |
-
# To make it permanent and hosted, we can use Gradio's 'share' argument or host it on a server.
|
142 |
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 = 40
|
15 |
+
try:
|
16 |
+
font = ImageFont.truetype(font_path, font_size)
|
17 |
+
except:
|
18 |
+
font = ImageFont.load_default()
|
19 |
+
|
20 |
+
# Get the text size and position
|
21 |
+
text_size = draw.textsize(label, font=font)
|
22 |
+
position = ((image.width - text_size[0]) // 2, image.height - text_size[1] - 10) # Centered at the bottom
|
23 |
+
|
24 |
+
# Add a semi-transparent rectangle behind the text for better visibility
|
25 |
+
rect_margin = 10
|
26 |
+
rect_position = [
|
27 |
+
position[0] - rect_margin,
|
28 |
+
position[1] - rect_margin,
|
29 |
+
position[0] + text_size[0] + rect_margin,
|
30 |
+
position[1] + text_size[1] + rect_margin,
|
31 |
+
]
|
32 |
+
draw.rectangle(rect_position, fill=(0, 0, 0, 128)) # Semi-transparent black
|
33 |
+
draw.text(position, label, fill="white", font=font)
|
34 |
+
return image
|
35 |
+
|
36 |
+
# Function to plot, label, and save an image
|
37 |
+
def plot_and_save_agent_image(agent_image, label, save_path=None):
|
38 |
# Convert AgentImage to a raw PIL Image
|
39 |
pil_image = agent_image.to_raw()
|
40 |
|
41 |
+
# Add a label to the image
|
42 |
+
labeled_image = add_label_to_image(pil_image, label)
|
43 |
+
|
44 |
# Plot the image using PIL's show method
|
45 |
+
labeled_image.show()
|
46 |
|
47 |
# If save_path is provided, save the image
|
48 |
if save_path:
|
49 |
+
labeled_image.save(save_path)
|
50 |
print(f"Image saved to {save_path}")
|
51 |
else:
|
52 |
print("No save path provided. Image not saved.")
|
53 |
|
54 |
+
# Function to generate prompts for an object
|
55 |
def generate_prompts_for_object(object_name):
|
56 |
prompts = {
|
57 |
"past": f"Show an old version of a {object_name} from its early days.",
|
58 |
+
"present": f"Show a {object_name} with current features/design/technology.",
|
59 |
"future": f"Show a futuristic version of a {object_name}, by predicting advanced features and futuristic design."
|
60 |
}
|
61 |
return prompts
|
62 |
|
63 |
+
# Function to generate the object's history images and GIF
|
|
|
64 |
def generate_object_history(object_name):
|
65 |
images = []
|
66 |
|
67 |
# Get prompts for the object
|
68 |
prompts = generate_prompts_for_object(object_name)
|
69 |
+
labels = {
|
70 |
+
"past": "Past Concept",
|
71 |
+
"present": "Present Concept",
|
72 |
+
"future": "Future Concept"
|
73 |
+
}
|
74 |
|
75 |
# Generate sequential images and display them
|
76 |
for time_period, frame in prompts.items():
|
|
|
80 |
# Append the image to the list for GIF creation
|
81 |
images.append(result.to_raw()) # Ensure we're using raw image for GIF
|
82 |
|
83 |
+
# Save each image with the appropriate name and label
|
84 |
image_filename = f"{object_name}_{time_period}.png"
|
85 |
+
plot_and_save_agent_image(result, labels[time_period], save_path=image_filename)
|
|
|
86 |
|
87 |
# Create GIF from images
|
88 |
gif_path = f"{object_name}_evolution.gif"
|
|
|
97 |
# Return images and GIF path
|
98 |
return images, gif_path
|
99 |
|
|
|
100 |
#%% Initialization of tools and AI_Agent
|
101 |
+
# Import text-to-image tool from Hub
|
102 |
+
image_generation_tool = load_tool("m-ric/text-to-image", cache=False)
|
|
|
103 |
|
104 |
# Import search tool from LangChain
|
|
|
105 |
from transformers.agents.search import DuckDuckGoSearchTool
|
|
|
106 |
search_tool = DuckDuckGoSearchTool()
|
107 |
|
108 |
+
# Load the LLM engine
|
109 |
+
llm_engine = HfApiEngine("Qwen/Qwen2.5-72B-Instruct")
|
110 |
+
|
111 |
# Initialize the agent with both tools
|
112 |
agent = ReactCodeAgent(tools=[image_generation_tool, search_tool], llm_engine=llm_engine)
|
113 |
|
|
|
|
|
114 |
# Gradio interface
|
115 |
def create_gradio_interface():
|
116 |
with gr.Blocks() as demo:
|
|
|
148 |
generate_button = gr.Button("Generate Evolution")
|
149 |
|
150 |
# Gradio Gallery component to display the images
|
151 |
+
image_gallery = gr.Gallery(label="Generated Images", show_label=True, columns=3, rows=1)
|
|
|
152 |
|
153 |
# Output for the generated GIF
|
154 |
+
gif_output = gr.Image(label="Generated GIF", show_label=True)
|
155 |
|
156 |
# Set the action when the button is clicked
|
157 |
generate_button.click(fn=generate_object_history, inputs=[object_name_input], outputs=[image_gallery, gif_output])
|
|
|
160 |
|
161 |
# Launch the Gradio app
|
162 |
demo = create_gradio_interface()
|
|
|
|
|
163 |
demo.launch(share=True)
|