Sal-ONE commited on
Commit
f04bc6b
·
verified ·
1 Parent(s): 2519ecd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -5
app.py CHANGED
@@ -2,10 +2,27 @@ import gradio as gr
2
  from diffusers import StableDiffusionPipeline
3
  import torch
4
 
5
- # Load the Stable Diffusion model
6
- model_id = "runwayml/stable-diffusion-v1-5"
7
- pipeline = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
8
- pipeline = pipeline.to("cuda")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  def generate_image(prompt, num_inference_steps=50, guidance_scale=7.5):
11
  """Generate an image from a text prompt using Stable Diffusion."""
@@ -14,10 +31,17 @@ def generate_image(prompt, num_inference_steps=50, guidance_scale=7.5):
14
 
15
  # Define the Gradio app layout
16
  with gr.Blocks() as app:
17
- gr.Markdown("# Stable Diffusion v1.5 Gradio App\nGenerate stunning images from text prompts!")
18
 
19
  with gr.Row():
20
  with gr.Column():
 
 
 
 
 
 
 
21
  prompt = gr.Textbox(label="Prompt", placeholder="Enter your prompt here", lines=2)
22
  num_inference_steps = gr.Slider(label="Inference Steps", minimum=10, maximum=100, value=50, step=5)
23
  guidance_scale = gr.Slider(label="Guidance Scale", minimum=1.0, maximum=20.0, value=7.5, step=0.5)
@@ -26,6 +50,12 @@ with gr.Blocks() as app:
26
  with gr.Column():
27
  output_image = gr.Image(label="Generated Image", type="pil")
28
 
 
 
 
 
 
 
29
  generate_btn.click(
30
  generate_image,
31
  inputs=[prompt, num_inference_steps, guidance_scale],
 
2
  from diffusers import StableDiffusionPipeline
3
  import torch
4
 
5
+ # Function to load the selected Stable Diffusion model
6
+ def load_model(model_id):
7
+ """Load the specified Stable Diffusion model."""
8
+ pipeline = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float32)
9
+ pipeline = pipeline.to("cpu")
10
+ return pipeline
11
+
12
+ # Initial model
13
+ model_options = [
14
+ "CompVis/stable-diffusion-v1-1",
15
+ "stable-diffusion-v1-5/stable-diffusion-v1-5",
16
+ "stabilityai/stable-diffusion-2-1"
17
+ ]
18
+ current_model_id = model_options[0]
19
+ pipeline = load_model(current_model_id)
20
+
21
+ def switch_model(selected_model):
22
+ """Switch the Stable Diffusion model."""
23
+ global pipeline
24
+ pipeline = load_model(selected_model)
25
+ return f"Model switched to: {selected_model}"
26
 
27
  def generate_image(prompt, num_inference_steps=50, guidance_scale=7.5):
28
  """Generate an image from a text prompt using Stable Diffusion."""
 
31
 
32
  # Define the Gradio app layout
33
  with gr.Blocks() as app:
34
+ gr.Markdown("# Stable Diffusion Gradio App\nGenerate stunning images from text prompts and switch between models!")
35
 
36
  with gr.Row():
37
  with gr.Column():
38
+ model_selector = gr.Dropdown(
39
+ label="Select Model",
40
+ choices=model_options,
41
+ value=current_model_id,
42
+ interactive=True
43
+ )
44
+ model_switch_status = gr.Textbox(label="Model Status", value=f"Current model: {current_model_id}", interactive=False)
45
  prompt = gr.Textbox(label="Prompt", placeholder="Enter your prompt here", lines=2)
46
  num_inference_steps = gr.Slider(label="Inference Steps", minimum=10, maximum=100, value=50, step=5)
47
  guidance_scale = gr.Slider(label="Guidance Scale", minimum=1.0, maximum=20.0, value=7.5, step=0.5)
 
50
  with gr.Column():
51
  output_image = gr.Image(label="Generated Image", type="pil")
52
 
53
+ model_selector.change(
54
+ switch_model,
55
+ inputs=[model_selector],
56
+ outputs=model_switch_status
57
+ )
58
+
59
  generate_btn.click(
60
  generate_image,
61
  inputs=[prompt, num_inference_steps, guidance_scale],