File size: 1,806 Bytes
16f0866
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
import gradio as gr
from PIL import Image
from transformers import pipeline
from diffusers import StableDiffusion3Pipeline

model_path = "../Models/models--Salesforce--blip-image-captioning-base/snapshots/82a37760796d32b1411fe092ab5d4e227313294b"

device = "cuda" if torch.cuda.is_available() else "cpu"

caption_image = pipeline("image-to-text", model=model_path, device=device)
# caption_image = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base", device=device)

def image_generation(prompt):
    # is_cuda = False
    pipeline = StableDiffusion3Pipeline.from_pretrained("stabilityai/stable-diffusion-3-medium-diffusers",
                                                        torch_dtype=torch.float32,
                                                        text_encoder_3=None,
                                                        tokenizer_3=None)
    # pipeline.enable_model_cpu_offload()
    pipeline.to('cpu')

    image = pipeline(
        prompt=prompt,
        negative_prompt="blurred, ugly, watermark, low resolution, blurry",
        num_inference_steps=15,
        height=192,
        width=192,
        guidance_scale=7.0
    ).images[0]

    return image

def caption_my_image(pil_image):
    semantics = caption_image(images=pil_image)[0]['generated_text']
    image = image_generation(semantics)
    return image


gr.close_all()

demo = gr.Interface(fn=caption_my_image,
                    inputs=[gr.Image(label="Select Image",type="pil")],
                    outputs=[gr.Image(label="New Generated Image using SD3", type="pil")],
                    title="@GenAILearniverse Project 10: Generate Similar image",
                    description="THIS APPLICATION WILL BE USED TO GENERATE SIMILAR IMAGE BASED ON IMAGE UPLOADED.")

demo.launch()