File size: 1,325 Bytes
e241a3a
c51e0da
9530fd2
e241a3a
9530fd2
e241a3a
 
 
 
 
 
 
 
 
d881e99
9530fd2
 
e241a3a
 
 
 
 
 
9530fd2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e241a3a
 
 
 
 
9530fd2
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 os
import torch
import gradio as gr
from huggingface_hub import login
from diffusers import StableDiffusion3Pipeline

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

# Authenticate with the token
if hf_token:
    login(token=hf_token)
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
    )
    pipeline.to(device)

    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

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."
)
interface.launch()