|
from transformers import load_tool, ReactCodeAgent, HfApiEngine |
|
from PIL import Image |
|
import torch |
|
import numpy as np |
|
import tempfile |
|
import os |
|
import uuid |
|
import gradio as gr |
|
|
|
|
|
|
|
def plot_and_save_agent_image(agent_image, save_path=None): |
|
|
|
pil_image = agent_image.to_raw() |
|
|
|
|
|
pil_image.show() |
|
|
|
|
|
if save_path: |
|
pil_image.save(save_path) |
|
print(f"Image saved to {save_path}") |
|
else: |
|
print("No save path provided. Image not saved.") |
|
|
|
|
|
def generate_prompts_for_object(object_name): |
|
prompts = { |
|
"past": f"Show an old version of a {object_name} from its early days.", |
|
"present": f"Show a modern {object_name} with its current design and technology.", |
|
"future": f"Show a futuristic version of a {object_name} with advanced features and futuristic design." |
|
} |
|
return prompts |
|
|
|
|
|
|
|
def generate_object_history(object_name): |
|
images = [] |
|
|
|
|
|
prompts = generate_prompts_for_object(object_name) |
|
|
|
|
|
for time_period, frame in prompts.items(): |
|
print(f"Generating {time_period} frame: {frame}") |
|
result = agent.run(frame) |
|
|
|
|
|
images.append(result.to_raw()) |
|
|
|
|
|
image_filename = f"{object_name}_{time_period}.png" |
|
plot_and_save_agent_image(result, save_path=image_filename) |
|
|
|
|
|
|
|
gif_path = f"{object_name}_evolution.gif" |
|
images[0].save( |
|
gif_path, |
|
save_all=True, |
|
append_images=images[1:], |
|
duration=1000, |
|
loop=0 |
|
) |
|
|
|
|
|
return images, gif_path |
|
|
|
|
|
|
|
image_generation_tool = load_tool("m-ric/text-to-image", cache=False) |
|
|
|
|
|
|
|
from transformers.agents.search import DuckDuckGoSearchTool |
|
|
|
search_tool = DuckDuckGoSearchTool() |
|
|
|
|
|
llm_engine = HfApiEngine("Qwen/Qwen2.5-72B-Instruct") |
|
|
|
agent = ReactCodeAgent(tools=[image_generation_tool, search_tool], llm_engine=llm_engine) |
|
|
|
|
|
|
|
|
|
|
|
def create_gradio_interface(): |
|
with gr.Blocks() as demo: |
|
gr.Markdown("# Object Evolution Generator") |
|
|
|
|
|
gr.Markdown(""" |
|
## Welcome to the Object Evolution Generator! |
|
|
|
This app allows you to generate visualizations of how an object, like a bicycle or a car, may have evolved over time. |
|
It generates images of the object in the past, present, and future based on your input. |
|
|
|
### How to use: |
|
- Enter the name of an object (e.g., "bicycle", "car", "phone"). |
|
- Click "Generate Evolution" to generate the evolution of the object across three time periods: past, present, and future. |
|
- View the generated images and a GIF showing the evolution of the object. |
|
|
|
### Example: |
|
Try entering an object name like "car" and see how it has evolved! |
|
""") |
|
|
|
with gr.Row(): |
|
with gr.Column(): |
|
|
|
object_name_input = gr.Textbox(label="Enter an object name (e.g., car)", |
|
placeholder="Enter an object name", |
|
lines=1) |
|
|
|
|
|
generate_button = gr.Button("Generate Evolution") |
|
|
|
|
|
image_gallery = gr.Gallery(label="Generated Images", show_label=True, columns=3, rows=1) |
|
|
|
|
|
gif_output = gr.Image(label="Generated GIF", show_label=True) |
|
|
|
|
|
generate_button.click(fn=generate_object_history, inputs=[object_name_input], outputs=[image_gallery, gif_output]) |
|
|
|
return demo |
|
|
|
|
|
demo = create_gradio_interface() |
|
|
|
|
|
demo.launch(share=True) |