Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
from transformers import load_tool, ReactCodeAgent, HfApiEngine
|
2 |
from PIL import Image
|
3 |
import torch
|
@@ -6,9 +7,69 @@ import tempfile
|
|
6 |
import os
|
7 |
import uuid
|
8 |
import gradio as gr
|
9 |
-
from Methods import *
|
10 |
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
# Import text-to-image tool from Hub
|
13 |
# m-ric/text-to-image model generates images based on textual descriptions.
|
14 |
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.
|
|
|
1 |
+
#%% Import libraries
|
2 |
from transformers import load_tool, ReactCodeAgent, HfApiEngine
|
3 |
from PIL import Image
|
4 |
import torch
|
|
|
7 |
import os
|
8 |
import uuid
|
9 |
import gradio as gr
|
|
|
10 |
|
11 |
|
12 |
+
#%% Methods
|
13 |
+
# function to plot and save an AgentImage
|
14 |
+
def plot_and_save_agent_image(agent_image, save_path=None):
|
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 |
+
pil_image.show()
|
20 |
+
|
21 |
+
# If save_path is provided, save the image
|
22 |
+
if save_path:
|
23 |
+
pil_image.save(save_path)
|
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 from present with current features/design/technology.",
|
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():
|
47 |
+
print(f"Generating {time_period} frame: {frame}")
|
48 |
+
result = agent.run(frame) # The tool generates the image
|
49 |
+
|
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 (past, present, future)
|
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"
|
60 |
+
images[0].save(
|
61 |
+
gif_path,
|
62 |
+
save_all=True,
|
63 |
+
append_images=images[1:],
|
64 |
+
duration=1000, # Duration in milliseconds for each frame
|
65 |
+
loop=0 # Infinite loop
|
66 |
+
)
|
67 |
+
|
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 |
# m-ric/text-to-image model generates images based on textual descriptions.
|
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.
|