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