File size: 1,530 Bytes
4b4717e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
import gradio as gr
from PIL import Image
from diffusers import StableDiffusionPipeline

# Use a pipeline as a high-level helper
from transformers import pipeline

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

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


def image_generation(prompt):
    device = "cuda" if torch.cuda.is_available() else "cpu"
    pipeline = StableDiffusionPipeline.from_pretrained(
        "stabilityai/stable-diffusion-3-medium",
        torch_dtype=torch.float16 if device == "cuda" else torch.float32,
    )
    #pipeline.to(device)
    pipeline.enable_model_cpu_offload()
    
    image = pipeline(
        prompt=prompt + " 8K, Ultra HD",
        negative_prompt="blurred, ugly, watermark, low resolution, blurry, nude",
        num_inference_steps=40,
        height=1024,
        width=1024,
        guidance_scale=9.0
    ).images[0]

    return image

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

demo = gr.Interface(fn=caption_my_image,
                    inputs=[gr.Image(label="Select Image",type="pil")],
                    outputs=[gr.Image(label="New Image genrated using SD3",type="pil")],
                    title="PicTalker | ImageNarrator | SnapSpeech | SpeakScene",
                    description="🌟 Transform Ordinary Photos into Extraordinary Art!")
demo.launch()