Muhammad Anas Akhtar
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,31 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
from diffusers import
|
|
|
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
def image_generation(prompt):
|
|
|
|
| 7 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
pipeline.enable_model_cpu_offload()
|
| 14 |
-
|
| 15 |
-
|
| 16 |
image = pipeline(
|
| 17 |
prompt=prompt,
|
| 18 |
negative_prompt="blurred, ugly, watermark, low resolution, blurry",
|
|
@@ -21,17 +34,17 @@ def image_generation(prompt):
|
|
| 21 |
width=1024,
|
| 22 |
guidance_scale=9.0
|
| 23 |
).images[0]
|
| 24 |
-
|
| 25 |
return image
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
interface= gr.Interface(
|
| 30 |
fn=image_generation,
|
| 31 |
-
inputs
|
| 32 |
-
outputs
|
| 33 |
-
title
|
| 34 |
-
description="This application
|
| 35 |
)
|
| 36 |
|
| 37 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
+
from diffusers import StableDiffusionPipeline
|
| 4 |
+
from huggingface_hub import hf_api
|
| 5 |
|
| 6 |
+
# Retrieve the Hugging Face token stored in Hugging Face Spaces secrets
|
| 7 |
+
HUGGINGFACE_TOKEN = hf_api.get_secret("keyss")
|
| 8 |
+
|
| 9 |
+
if not HUGGINGFACE_TOKEN:
|
| 10 |
+
raise ValueError("Hugging Face token not found! Make sure it's added in the Hugging Face Secrets.")
|
| 11 |
|
| 12 |
def image_generation(prompt):
|
| 13 |
+
# Check if GPU is available
|
| 14 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 15 |
+
|
| 16 |
+
# Load the Stable Diffusion 3 pipeline
|
| 17 |
+
pipeline = StableDiffusionPipeline.from_pretrained(
|
| 18 |
+
"stabilityai/stable-diffusion-3-medium-diffusers",
|
| 19 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32,
|
| 20 |
+
use_auth_token=HUGGINGFACE_TOKEN, # Use the Hugging Face token for authentication
|
| 21 |
+
text_encoder_3=None,
|
| 22 |
+
tokenizer_3=None
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
# Enable efficient model execution
|
| 26 |
pipeline.enable_model_cpu_offload()
|
| 27 |
+
|
| 28 |
+
# Generate an image based on the prompt
|
| 29 |
image = pipeline(
|
| 30 |
prompt=prompt,
|
| 31 |
negative_prompt="blurred, ugly, watermark, low resolution, blurry",
|
|
|
|
| 34 |
width=1024,
|
| 35 |
guidance_scale=9.0
|
| 36 |
).images[0]
|
| 37 |
+
|
| 38 |
return image
|
| 39 |
|
| 40 |
+
# Define the Gradio interface
|
| 41 |
+
interface = gr.Interface(
|
|
|
|
| 42 |
fn=image_generation,
|
| 43 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter your Prompt..."),
|
| 44 |
+
outputs=gr.Image(type="pil"),
|
| 45 |
+
title="Image Creation using Stable Diffusion 3 Model",
|
| 46 |
+
description="This application generates awesome images using the Stable Diffusion 3 model."
|
| 47 |
)
|
| 48 |
|
| 49 |
+
# Launch the Gradio app
|
| 50 |
+
interface.launch()
|