Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from diffusers import StableDiffusion3Pipeline
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Retrieve the API token from the environment variable
|
| 7 |
+
huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
|
| 8 |
+
if huggingface_token is None:
|
| 9 |
+
raise ValueError("HUGGINGFACE_TOKEN environment variable is not set.")
|
| 10 |
+
|
| 11 |
+
# Check if CUDA is available
|
| 12 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 13 |
+
|
| 14 |
+
# Load the Stable Diffusion model
|
| 15 |
+
repo = "stabilityai/stable-diffusion-3-medium-diffusers"
|
| 16 |
+
image_gen = StableDiffusion3Pipeline.from_pretrained(repo, text_encoder_3=None, tokenizer_3=None, use_auth_token=huggingface_token)
|
| 17 |
+
image_gen = image_gen.to(device)
|
| 18 |
+
|
| 19 |
+
def generate_image(prompt, num_inference_steps=50, guidance_scale=7.5):
|
| 20 |
+
# Generate the image
|
| 21 |
+
result = image_gen(
|
| 22 |
+
prompt=prompt,
|
| 23 |
+
num_inference_steps=num_inference_steps,
|
| 24 |
+
guidance_scale=guidance_scale,
|
| 25 |
+
negative_prompt="blurred, ugly, watermark, low resolution, blurry",
|
| 26 |
+
height=512,
|
| 27 |
+
width=512
|
| 28 |
+
)
|
| 29 |
+
# Get the generated image
|
| 30 |
+
image = result.images[0]
|
| 31 |
+
return image
|
| 32 |
+
|
| 33 |
+
# Create the Gradio interface
|
| 34 |
+
iface = gr.Interface(
|
| 35 |
+
fn=generate_image,
|
| 36 |
+
inputs=[
|
| 37 |
+
gr.Textbox(label="Enter a prompt"),
|
| 38 |
+
gr.Slider(label="Number of inference steps", minimum=1, maximum=100, value=50),
|
| 39 |
+
gr.Slider(label="Guidance scale", minimum=1.0, maximum=20.0, value=7.5)
|
| 40 |
+
],
|
| 41 |
+
outputs=gr.Image(label="Generated Image"),
|
| 42 |
+
title="Stable Diffusion Image Generator",
|
| 43 |
+
description="Enter a prompt to generate an image using the Stable Diffusion model."
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
# Launch the Gradio app
|
| 47 |
+
iface.launch()
|