File size: 2,374 Bytes
84abbea 1fd7a59 5167fb6 84abbea 5167fb6 84abbea 1fd7a59 3a2a66c 84abbea 3a2a66c 84abbea 1fd7a59 84abbea 6d28680 84abbea 6d28680 84abbea 6d28680 84abbea 5167fb6 84abbea 1fd7a59 84abbea 5167fb6 84abbea 5167fb6 84abbea 5420ab6 84abbea 5167fb6 84abbea 5167fb6 84abbea 5167fb6 f7c1e90 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
import torch
from PIL import Image, ImageDraw, ImageFont
import gradio as gr
from diffusers import StableDiffusionPipeline
# Load Stable Diffusion model
pipe = StableDiffusionPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16
).to("cuda" if torch.cuda.is_available() else "cpu")
# Function to add label
def add_label_to_image(image, label):
draw = ImageDraw.Draw(image)
try:
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 30)
except:
font = ImageFont.load_default()
position = (20, image.height - 50)
draw.rectangle([position, (position[0]+400, position[1]+40)], fill=(0, 0, 0, 180))
draw.text(position, label, font=font, fill="white")
return image
# Generate prompt images
def generate_object_history(object_name):
prompts = {
"past": f"An old version of a {object_name}, vintage, old-fashioned",
"present": f"A modern {object_name}, realistic, current design",
"future": f"A futuristic {object_name}, sci-fi, advanced design"
}
images = []
pil_images = []
for period, prompt in prompts.items():
image = pipe(prompt).images[0]
labeled_image = add_label_to_image(image, f"{object_name.title()} - {period.title()}")
filename = f"{object_name}_{period}.png"
labeled_image.save(filename)
images.append((filename, f"{object_name.title()} - {period.title()}"))
pil_images.append(labeled_image)
gif_path = f"{object_name}_evolution.gif"
pil_images[0].save(gif_path, save_all=True, append_images=pil_images[1:], duration=1000, loop=0)
return images, gif_path
# Gradio Interface
def create_gradio_interface():
with gr.Blocks() as demo:
gr.Markdown("# TimeMetamorphy: Object Evolution Visualizer")
object_name_input = gr.Textbox(label="Enter an object name (e.g., bicycle, phone)")
generate_button = gr.Button("Generate Evolution")
image_gallery = gr.Gallery(label="Generated Images", columns=3, rows=1)
gif_output = gr.Image(label="Generated GIF")
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)
|