File size: 1,466 Bytes
d4004ce
e241a3a
038b110
 
 
 
fdeca56
e241a3a
 
 
 
d4004ce
e241a3a
 
d4004ce
 
038b110
d4004ce
 
 
 
 
 
 
038b110
 
d4004ce
038b110
 
 
 
 
 
 
 
 
 
 
d4004ce
038b110
 
d4004ce
 
 
 
 
 
 
038b110
 
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
import os 
from huggingface_hub import login
import torch
import gradio as gr
from diffusers import StableDiffusion3Pipeline

# Get Hugging Face token from environment variables
hf_token = os.getenv("HF_API_TOKEN")

if hf_token:
    login(token=hf_token)
    print("Login successful")
else:
    raise ValueError("Hugging Face token is missing. Please set it in the environment variables.")

def image_generator(prompt):
    device = "cuda" if torch.cuda.is_available() else "cpu"
    pipeline = StableDiffusion3Pipeline.from_pretrained(
        "stabilityai/stable-diffusion-3-medium-diffusers", 
        torch_dtype=torch.float16 if device == "cuda" else torch.float32,
        text_encoder_3=None,
        tokenizer_3=None
    )
    # Move the pipeline to the appropriate device
    pipeline.to(device)

    # Generate the image
    image = pipeline(
        prompt=prompt,
        negative_prompt="blurred, ugly, watermark, low, resolution, blurry",
        num_inference_steps=40,
        height=1024,
        width=1024,
        guidance_scale=9.0
    ).images[0]

    return image

# Create a Gradio interface
interface = gr.Interface(
    fn=image_generator, 
    inputs=gr.Textbox(lines=2, placeholder="Enter your prompt..."), 
    outputs=gr.Image(type="pil"),
    title="Image Generator App",
    description="This is a simple image generator app using HuggingFace's Stable Diffusion 3 model."
)

# Launch the interface
interface.launch()
print(interface)