graphics / app.py
amirkhanbloch's picture
Update app.py
e241a3a verified
raw
history blame
1.33 kB
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()