Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
from diffusers import StableDiffusion3Pipeline
|
| 6 |
+
|
| 7 |
+
model_path = "../Models/models--Salesforce--blip-image-captioning-base/snapshots/82a37760796d32b1411fe092ab5d4e227313294b"
|
| 8 |
+
|
| 9 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
+
|
| 11 |
+
caption_image = pipeline("image-to-text", model=model_path, device=device)
|
| 12 |
+
# caption_image = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base", device=device)
|
| 13 |
+
|
| 14 |
+
def image_generation(prompt):
|
| 15 |
+
# is_cuda = False
|
| 16 |
+
pipeline = StableDiffusion3Pipeline.from_pretrained("stabilityai/stable-diffusion-3-medium-diffusers",
|
| 17 |
+
torch_dtype=torch.float32,
|
| 18 |
+
text_encoder_3=None,
|
| 19 |
+
tokenizer_3=None)
|
| 20 |
+
# pipeline.enable_model_cpu_offload()
|
| 21 |
+
pipeline.to('cpu')
|
| 22 |
+
|
| 23 |
+
image = pipeline(
|
| 24 |
+
prompt=prompt,
|
| 25 |
+
negative_prompt="blurred, ugly, watermark, low resolution, blurry",
|
| 26 |
+
num_inference_steps=15,
|
| 27 |
+
height=192,
|
| 28 |
+
width=192,
|
| 29 |
+
guidance_scale=7.0
|
| 30 |
+
).images[0]
|
| 31 |
+
|
| 32 |
+
return image
|
| 33 |
+
|
| 34 |
+
def caption_my_image(pil_image):
|
| 35 |
+
semantics = caption_image(images=pil_image)[0]['generated_text']
|
| 36 |
+
image = image_generation(semantics)
|
| 37 |
+
return image
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
gr.close_all()
|
| 41 |
+
|
| 42 |
+
demo = gr.Interface(fn=caption_my_image,
|
| 43 |
+
inputs=[gr.Image(label="Select Image",type="pil")],
|
| 44 |
+
outputs=[gr.Image(label="New Generated Image using SD3", type="pil")],
|
| 45 |
+
title="@GenAILearniverse Project 10: Generate Similar image",
|
| 46 |
+
description="THIS APPLICATION WILL BE USED TO GENERATE SIMILAR IMAGE BASED ON IMAGE UPLOADED.")
|
| 47 |
+
|
| 48 |
+
demo.launch()
|