Update app.py
Browse files
app.py
CHANGED
@@ -1,38 +1,47 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
-
import
|
4 |
|
5 |
-
# Load the pipeline (lazy-load the model to save resources)
|
6 |
-
@spaces.GPU
|
7 |
def load_model():
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
torch_dtype=
|
|
|
|
|
12 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
# Initialize the
|
15 |
-
|
|
|
|
|
|
|
16 |
|
17 |
-
#
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
images = model(prompt, guidance_scale=guidance_scale)
|
22 |
return images[0]
|
23 |
|
24 |
-
# Gradio
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
gr.
|
29 |
-
gr.Slider(
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
)
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
1 |
import gradio as gr
|
2 |
+
from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
|
3 |
+
import torch
|
4 |
|
|
|
|
|
5 |
def load_model():
|
6 |
+
# Specify the Stable Diffusion pipeline with an appropriate model type
|
7 |
+
pipeline = StableDiffusionPipeline.from_pretrained(
|
8 |
+
"stabilityai/stable-diffusion-2-1",
|
9 |
+
torch_dtype=torch.float16,
|
10 |
+
revision="fp16",
|
11 |
+
safety_checker=None # Disable safety checker if necessary
|
12 |
)
|
13 |
+
|
14 |
+
# Set the scheduler (optional but recommended)
|
15 |
+
pipeline.scheduler = DPMSolverMultistepScheduler.from_config(pipeline.scheduler.config)
|
16 |
+
|
17 |
+
# Move pipeline to GPU or ZeroGPU
|
18 |
+
pipeline = pipeline.to("cuda") # or ZeroGPU-specific setup
|
19 |
+
|
20 |
+
return pipeline
|
21 |
|
22 |
+
# Initialize the model
|
23 |
+
try:
|
24 |
+
model = load_model()
|
25 |
+
except Exception as e:
|
26 |
+
print(f"Error loading the model: {e}")
|
27 |
|
28 |
+
# Define Gradio interface
|
29 |
+
def generate(prompt, guidance_scale=7.5, num_inference_steps=50):
|
30 |
+
# Generate the image
|
31 |
+
images = model(prompt, guidance_scale=guidance_scale, num_inference_steps=num_inference_steps).images
|
|
|
32 |
return images[0]
|
33 |
|
34 |
+
# Gradio Interface
|
35 |
+
with gr.Blocks() as demo:
|
36 |
+
with gr.Row():
|
37 |
+
prompt = gr.Textbox(label="Enter your prompt")
|
38 |
+
guidance_scale = gr.Slider(1.0, 10.0, value=7.5, label="Guidance Scale")
|
39 |
+
steps = gr.Slider(10, 100, value=50, label="Number of Inference Steps")
|
40 |
+
with gr.Row():
|
41 |
+
submit = gr.Button("Generate")
|
42 |
+
with gr.Row():
|
43 |
+
output = gr.Image()
|
|
|
44 |
|
45 |
+
submit.click(generate, inputs=[prompt, guidance_scale, steps], outputs=output)
|
46 |
+
|
47 |
+
demo.launch()
|