AMfeta99 commited on
Commit
fad0d14
·
verified ·
1 Parent(s): 61aa8cb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -33
app.py CHANGED
@@ -1,64 +1,119 @@
1
- import torch
2
  from PIL import Image, ImageDraw, ImageFont
3
  import gradio as gr
4
- from diffusers import StableDiffusionPipeline
5
 
6
- # Load Stable Diffusion model
7
- pipe = StableDiffusionPipeline.from_pretrained(
8
- "runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16
9
- ).to("cuda" if torch.cuda.is_available() else "cpu")
10
 
11
- # Function to add label
12
  def add_label_to_image(image, label):
13
  draw = ImageDraw.Draw(image)
 
 
14
  try:
15
- font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 30)
16
  except:
17
  font = ImageFont.load_default()
18
- position = (20, image.height - 50)
19
- draw.rectangle([position, (position[0]+400, position[1]+40)], fill=(0, 0, 0, 180))
20
- draw.text(position, label, font=font, fill="white")
 
 
 
 
 
 
 
 
 
 
 
21
  return image
22
 
23
- # Generate prompt images
24
- def generate_object_history(object_name):
25
- prompts = {
26
- "past": f"An old version of a {object_name}, vintage, old-fashioned",
27
- "present": f"A modern {object_name}, realistic, current design",
28
- "future": f"A futuristic {object_name}, sci-fi, advanced design"
 
 
 
 
 
 
29
  }
30
 
 
 
 
 
 
 
 
31
  images = []
32
- pil_images = []
 
 
 
 
 
 
 
 
 
33
 
34
  for period, prompt in prompts.items():
35
- image = pipe(prompt).images[0]
36
- labeled_image = add_label_to_image(image, f"{object_name.title()} - {period.title()}")
 
 
 
 
 
 
 
 
 
37
  filename = f"{object_name}_{period}.png"
38
  labeled_image.save(filename)
39
- images.append((filename, f"{object_name.title()} - {period.title()}"))
40
- pil_images.append(labeled_image)
41
 
 
42
  gif_path = f"{object_name}_evolution.gif"
43
- pil_images[0].save(gif_path, save_all=True, append_images=pil_images[1:], duration=1000, loop=0)
44
 
45
- return images, gif_path
 
 
46
 
47
- # Gradio Interface
48
  def create_gradio_interface():
49
  with gr.Blocks() as demo:
50
- gr.Markdown("# TimeMetamorphy: Object Evolution Visualizer")
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
- object_name_input = gr.Textbox(label="Enter an object name (e.g., bicycle, phone)")
53
- generate_button = gr.Button("Generate Evolution")
 
 
 
 
54
 
55
- image_gallery = gr.Gallery(label="Generated Images", columns=3, rows=1)
56
- gif_output = gr.Image(label="Generated GIF")
57
 
58
- generate_button.click(fn=generate_object_history,
59
- inputs=[object_name_input],
60
- outputs=[image_gallery, gif_output])
61
  return demo
62
 
 
63
  demo = create_gradio_interface()
64
  demo.launch(share=True)
 
1
+ from transformers import HfAgent
2
  from PIL import Image, ImageDraw, ImageFont
3
  import gradio as gr
4
+ import os
5
 
6
+ #%% Utility Functions
 
 
 
7
 
 
8
  def add_label_to_image(image, label):
9
  draw = ImageDraw.Draw(image)
10
+ font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
11
+ font_size = 30
12
  try:
13
+ font = ImageFont.truetype(font_path, font_size)
14
  except:
15
  font = ImageFont.load_default()
16
+
17
+ text_bbox = draw.textbbox((0, 0), label, font=font)
18
+ text_width, text_height = text_bbox[2] - text_bbox[0], text_bbox[3] - text_bbox[1]
19
+ position = (image.width - text_width - 20, image.height - text_height - 20)
20
+
21
+ rect_margin = 10
22
+ rect_position = [
23
+ position[0] - rect_margin,
24
+ position[1] - rect_margin,
25
+ position[0] + text_width + rect_margin,
26
+ position[1] + text_height + rect_margin,
27
+ ]
28
+ draw.rectangle(rect_position, fill=(0, 0, 0, 128))
29
+ draw.text(position, label, fill="white", font=font)
30
  return image
31
 
32
+ def plot_and_save_agent_image(image, label, save_path=None):
33
+ labeled_image = add_label_to_image(image, label)
34
+ labeled_image.show()
35
+ if save_path:
36
+ labeled_image.save(save_path)
37
+ print(f"Image saved to {save_path}")
38
+
39
+ def generate_prompts_for_object(object_name):
40
+ return {
41
+ "past": f"Show an old version of a {object_name} from its early days.",
42
+ "present": f"Show a {object_name} with current features/design/technology.",
43
+ "future": f"Show a futuristic version of a {object_name}, predicting advanced features and design."
44
  }
45
 
46
+ #%% HF Agent Initialization
47
+
48
+ agent = HfAgent("https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta")
49
+
50
+ #%% Core Generation Function
51
+
52
+ def generate_object_history(object_name):
53
  images = []
54
+ gif_frames = []
55
+
56
+ prompts = generate_prompts_for_object(object_name)
57
+ labels = {
58
+ "past": f"{object_name} - Past",
59
+ "present": f"{object_name} - Present",
60
+ "future": f"{object_name} - Future"
61
+ }
62
+
63
+ output_image_paths = []
64
 
65
  for period, prompt in prompts.items():
66
+ print(f"Generating image for: {prompt}")
67
+ result = agent.run(prompt)
68
+
69
+ # Find first image in result (agent returns a dict)
70
+ image_path = next((v for v in result.values() if isinstance(v, str) and v.endswith((".png", ".jpg"))), None)
71
+ if not image_path or not os.path.exists(image_path):
72
+ raise RuntimeError(f"No valid image generated for {prompt}")
73
+
74
+ image = Image.open(image_path).convert("RGB")
75
+ labeled_image = add_label_to_image(image, labels[period])
76
+
77
  filename = f"{object_name}_{period}.png"
78
  labeled_image.save(filename)
79
+ output_image_paths.append((filename, labels[period]))
80
+ gif_frames.append(labeled_image)
81
 
82
+ # Save animated GIF
83
  gif_path = f"{object_name}_evolution.gif"
84
+ gif_frames[0].save(gif_path, save_all=True, append_images=gif_frames[1:], duration=1000, loop=0)
85
 
86
+ return output_image_paths, gif_path
87
+
88
+ #%% Gradio Interface
89
 
 
90
  def create_gradio_interface():
91
  with gr.Blocks() as demo:
92
+ gr.Markdown("# TimeMetamorphy: An Object Evolution Generator")
93
+
94
+ gr.Markdown("""
95
+ Explore how objects change over time — from past, to present, to future.
96
+ Enter any object below and let AI visualize its transformation through the ages.
97
+ """)
98
+
99
+ default_images = [
100
+ ("car_past.png", "Car - Past"),
101
+ ("car_present.png", "Car - Present"),
102
+ ("car_future.png", "Car - Future")
103
+ ]
104
+ default_gif_path = "car_evolution.gif"
105
 
106
+ with gr.Row():
107
+ with gr.Column():
108
+ object_name_input = gr.Textbox(label="Enter an object name", placeholder="e.g., bicycle, phone")
109
+ generate_button = gr.Button("Generate Evolution")
110
+ image_gallery = gr.Gallery(label="Generated Images", show_label=True, columns=3, rows=1, value=default_images)
111
+ gif_output = gr.Image(label="Generated GIF", show_label=True, value=default_gif_path)
112
 
113
+ generate_button.click(fn=generate_object_history, inputs=[object_name_input], outputs=[image_gallery, gif_output])
 
114
 
 
 
 
115
  return demo
116
 
117
+ # Launch
118
  demo = create_gradio_interface()
119
  demo.launch(share=True)