Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import load_tool, ReactCodeAgent, HfApiEngine
|
2 |
+
from PIL import Image
|
3 |
+
import torch
|
4 |
+
import numpy as np
|
5 |
+
import tempfile
|
6 |
+
import os
|
7 |
+
import uuid
|
8 |
+
import gradio as gr
|
9 |
+
|
10 |
+
|
11 |
+
# function to plot and save an AgentImage
|
12 |
+
def plot_and_save_agent_image(agent_image, save_path=None):
|
13 |
+
# Convert AgentImage to a raw PIL Image
|
14 |
+
pil_image = agent_image.to_raw()
|
15 |
+
|
16 |
+
# Plot the image using PIL's show method
|
17 |
+
pil_image.show()
|
18 |
+
|
19 |
+
# If save_path is provided, save the image
|
20 |
+
if save_path:
|
21 |
+
pil_image.save(save_path)
|
22 |
+
print(f"Image saved to {save_path}")
|
23 |
+
else:
|
24 |
+
print("No save path provided. Image not saved.")
|
25 |
+
|
26 |
+
|
27 |
+
def generate_prompts_for_object(object_name):
|
28 |
+
prompts = {
|
29 |
+
"past": f"Show an old version of a {object_name} from its early days.",
|
30 |
+
"present": f"Show a modern {object_name} with its current design and technology.",
|
31 |
+
"future": f"Show a futuristic version of a {object_name} with advanced features and futuristic design."
|
32 |
+
}
|
33 |
+
return prompts
|
34 |
+
|
35 |
+
|
36 |
+
# Function to generate the car industry history
|
37 |
+
def generate_object_history(object_name):
|
38 |
+
images = []
|
39 |
+
|
40 |
+
# Get prompts for the object
|
41 |
+
prompts = generate_prompts_for_object(object_name)
|
42 |
+
|
43 |
+
# Generate sequential images and display them
|
44 |
+
for time_period, frame in prompts.items():
|
45 |
+
print(f"Generating {time_period} frame: {frame}")
|
46 |
+
result = agent.run(frame) # The tool generates the image
|
47 |
+
|
48 |
+
# Append the image to the list for GIF creation
|
49 |
+
images.append(result.to_raw()) # Ensure we're using raw image for GIF
|
50 |
+
|
51 |
+
# Save each image with the appropriate name (past, present, future)
|
52 |
+
image_filename = f"{object_name}_{time_period}.png"
|
53 |
+
plot_and_save_agent_image(result, save_path=image_filename)
|
54 |
+
|
55 |
+
|
56 |
+
# Create GIF from images
|
57 |
+
gif_path = f"{object_name}_evolution.gif"
|
58 |
+
images[0].save(
|
59 |
+
gif_path,
|
60 |
+
save_all=True,
|
61 |
+
append_images=images[1:],
|
62 |
+
duration=1000, # Duration in milliseconds for each frame
|
63 |
+
loop=0 # Infinite loop
|
64 |
+
)
|
65 |
+
|
66 |
+
# Return images and GIF path
|
67 |
+
return images, gif_path
|
68 |
+
|
69 |
+
# Import text-to-image tool from Hub
|
70 |
+
# m-ric/text-to-image model generates images based on textual descriptions.
|
71 |
+
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.
|
72 |
+
|
73 |
+
# Import search tool from LangChain
|
74 |
+
#This tool allows the agent to search for and retrieve information from the web.
|
75 |
+
from transformers.agents.search import DuckDuckGoSearchTool
|
76 |
+
|
77 |
+
search_tool = DuckDuckGoSearchTool()
|
78 |
+
|
79 |
+
# Qwen2.5-72B-Instruct is a specific, a LLM fine-tuned for instruction-following tasks.
|
80 |
+
llm_engine = HfApiEngine("Qwen/Qwen2.5-72B-Instruct")
|
81 |
+
# Initialize the agent with both tools
|
82 |
+
agent = ReactCodeAgent(tools=[image_generation_tool, search_tool], llm_engine=llm_engine)
|
83 |
+
|
84 |
+
|
85 |
+
# Your existing generate_object_history function goes here
|
86 |
+
|
87 |
+
# Gradio interface
|
88 |
+
def create_gradio_interface():
|
89 |
+
with gr.Blocks() as demo:
|
90 |
+
gr.Markdown("# Object Evolution Generator")
|
91 |
+
|
92 |
+
# Add a section for instructions
|
93 |
+
gr.Markdown("""
|
94 |
+
## Welcome to the Object Evolution Generator!
|
95 |
+
|
96 |
+
This app allows you to generate visualizations of how an object, like a bicycle or a car, may have evolved over time.
|
97 |
+
It generates images of the object in the past, present, and future based on your input.
|
98 |
+
|
99 |
+
### How to use:
|
100 |
+
- Enter the name of an object (e.g., "bicycle", "car", "phone").
|
101 |
+
- Click "Generate Evolution" to generate the evolution of the object across three time periods: past, present, and future.
|
102 |
+
- View the generated images and a GIF showing the evolution of the object.
|
103 |
+
|
104 |
+
### Example:
|
105 |
+
Try entering an object name like "car" and see how it has evolved!
|
106 |
+
""")
|
107 |
+
|
108 |
+
with gr.Row():
|
109 |
+
with gr.Column():
|
110 |
+
# Textbox for user to input an object name
|
111 |
+
object_name_input = gr.Textbox(label="Enter an object name (e.g., car)",
|
112 |
+
placeholder="Enter an object name",
|
113 |
+
lines=1)
|
114 |
+
|
115 |
+
# Button to trigger the generation of images and GIF
|
116 |
+
generate_button = gr.Button("Generate Evolution")
|
117 |
+
|
118 |
+
# Gradio Gallery component to display the images
|
119 |
+
image_gallery = gr.Gallery(label="Generated Images", show_label=True, columns=3, rows=1)
|
120 |
+
|
121 |
+
# Output for the generated GIF
|
122 |
+
gif_output = gr.Image(label="Generated GIF", show_label=True)
|
123 |
+
|
124 |
+
# Set the action when the button is clicked
|
125 |
+
generate_button.click(fn=generate_object_history, inputs=[object_name_input], outputs=[image_gallery, gif_output])
|
126 |
+
|
127 |
+
return demo
|
128 |
+
|
129 |
+
# Launch the Gradio app (permanently)
|
130 |
+
demo = create_gradio_interface()
|
131 |
+
|
132 |
+
# To make it permanent and hosted, we can use Gradio's 'share' argument or host it on a server.
|
133 |
+
demo.launch(share=True)
|