image_gen / app.py
Walid-Ahmed's picture
Update app.py
b505020 verified
raw
history blame
No virus
2.13 kB
import gradio as gr
import torch
from diffusers import StableDiffusion3Pipeline
import os
from huggingface_hub import snapshot_download
# Retrieve the API token from the environment variable
huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
if huggingface_token is None:
raise ValueError("HUGGINGFACE_TOKEN environment variable is not set.")
# Check if CUDA is available
device = "cuda" if torch.cuda.is_available() else "cpu"
model_path = snapshot_download(
repo_id="stabilityai/stable-diffusion-3-medium",
revision="refs/pr/26",
repo_type="model",
ignore_patterns=["*.md", "*..gitattributes"],
local_dir="stable-diffusion-3-medium",
token=huggingface_token, # yeni bir token-id yazın.
)
# Load the Stable Diffusion model
repo = "stabilityai/stable-diffusion-3-medium-diffusers"
#image_gen = StableDiffusion3Pipeline.from_pretrained(repo, text_encoder_3=None, tokenizer_3=None, use_auth_token=huggingface_token)
image_gen = StableDiffusion3Pipeline.from_pretrained(repo, text_encoder_3=None, tokenizer_3=None)
#pipe = StableDiffusion3Pipeline.from_pretrained(model_path, torch_dtype=torch.float16)
image_gen = image_gen.to(device)
def generate_image(prompt, num_inference_steps=50, guidance_scale=7.5):
# Generate the image
result = image_gen(
prompt=prompt,
num_inference_steps=num_inference_steps,
guidance_scale=guidance_scale,
negative_prompt="blurred, ugly, watermark, low resolution, blurry",
height=512,
width=512
)
# Get the generated image
image = result.images[0]
return image
# Create the Gradio interface
iface = gr.Interface(
fn=generate_image,
inputs=[
gr.Textbox(label="Enter a prompt"),
gr.Slider(label="Number of inference steps", minimum=1, maximum=100, value=50),
gr.Slider(label="Guidance scale", minimum=1.0, maximum=20.0, value=7.5)
],
outputs=gr.Image(label="Generated Image"),
title="Stable Diffusion Image Generator",
description="Enter a prompt to generate an image using the Stable Diffusion model."
)
# Launch the Gradio app
iface.launch()