Sourudra commited on
Commit
dcf1a60
·
verified ·
1 Parent(s): 0caa8ea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -68
app.py CHANGED
@@ -1,85 +1,96 @@
1
  import gradio as gr
2
- import numpy as np
3
  import random
4
  import torch
5
  from diffusers import DiffusionPipeline
6
 
7
- # Check for GPU availability
8
  device = "cuda" if torch.cuda.is_available() else "cpu"
9
- torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
 
10
 
11
- # Load your DiffusionPipeline model
12
- model_repo_id = "stabilityai/sdxl-turbo"
13
- pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
14
- pipe = pipe.to(device)
15
 
16
- MAX_SEED = np.iinfo(np.int32).max
17
- MAX_IMAGE_SIZE = 1024
18
-
19
- # Define the custom model inference function
20
  def custom_infer(
21
- prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps
22
  ):
23
- if randomize_seed:
24
- seed = random.randint(0, MAX_SEED)
25
-
26
- generator = torch.Generator().manual_seed(seed)
27
-
28
- image = pipe(
29
- prompt=prompt,
30
- negative_prompt=negative_prompt,
31
- guidance_scale=guidance_scale,
32
- num_inference_steps=num_inference_steps,
33
- width=width,
34
- height=height,
35
- generator=generator,
36
- ).images[0]
37
-
38
- return image, seed
39
-
40
-
41
- # Gradio interface for custom model
42
- def custom_model_ui():
43
- with gr.Blocks() as custom_demo:
44
- gr.Markdown("## Custom Model: Stability AI SDXL")
45
- with gr.Row():
46
- prompt = gr.Text(label="Prompt")
47
- run_button = gr.Button("Generate")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
- result = gr.Image(label="Generated Image")
50
- negative_prompt = gr.Text(label="Negative Prompt", placeholder="Optional")
51
- seed = gr.Slider(0, MAX_SEED, label="Seed", step=1, value=0)
52
- randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
53
- width = gr.Slider(256, MAX_IMAGE_SIZE, step=32, value=1024, label="Width")
54
- height = gr.Slider(256, MAX_IMAGE_SIZE, step=32, value=1024, label="Height")
55
- guidance_scale = gr.Slider(0, 10, step=0.1, value=7.5, label="Guidance Scale")
56
- num_inference_steps = gr.Slider(1, 50, step=1, value=30, label="Inference Steps")
 
57
 
58
- run_button.click(
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  custom_infer,
60
- inputs=[prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
61
- outputs=[result, seed],
62
  )
63
 
64
- return custom_demo
65
-
66
-
67
- # Preloaded Gradio model
68
- def preloaded_model_ui():
69
- with gr.Blocks() as preloaded_demo:
70
- gr.Markdown("## Preloaded Model: ZB-Tech Text-to-Image")
71
- preloaded_demo = gr.load("models/ZB-Tech/Text-to-Image")
72
-
73
- return preloaded_demo
74
-
75
-
76
- # Combine both interfaces in tabs
77
- with gr.Blocks() as demo:
78
- with gr.Tab("Custom Model"):
79
- custom_ui = custom_model_ui()
80
-
81
- with gr.Tab("Preloaded Model"):
82
- preloaded_ui = preloaded_model_ui()
83
-
84
  if __name__ == "__main__":
85
  demo.launch()
 
1
  import gradio as gr
 
2
  import random
3
  import torch
4
  from diffusers import DiffusionPipeline
5
 
6
+ # Device setup
7
  device = "cuda" if torch.cuda.is_available() else "cpu"
8
+ model_repo_id_turbo = "stabilityai/sdxl-turbo" # Stability AI Model
9
+ pipe_turbo = DiffusionPipeline.from_pretrained(model_repo_id_turbo, torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32).to(device)
10
 
11
+ # Placeholder for ZB-Tech model
12
+ def load_zb_model():
13
+ return gr.Interface.load("models/ZB-Tech/Text-to-Image")
 
14
 
15
+ # Inference function
 
 
 
16
  def custom_infer(
17
+ model_choice, prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps
18
  ):
19
+ # Load the selected model
20
+ if model_choice == "Faster image generation (suitable for CPUs)":
21
+ model = load_zb_model()
22
+ return model(prompt)
23
+ else:
24
+ default_negative_prompt = "no watermark, hezzy, blurry"
25
+ combined_negative_prompt = f"{default_negative_prompt}, {negative_prompt}" if negative_prompt else default_negative_prompt
26
+
27
+ if randomize_seed:
28
+ seed = random.randint(0, np.iinfo(np.int32).max)
29
+
30
+ generator = torch.Generator().manual_seed(seed)
31
+ image = pipe_turbo(
32
+ prompt=prompt,
33
+ negative_prompt=combined_negative_prompt,
34
+ guidance_scale=guidance_scale,
35
+ num_inference_steps=num_inference_steps,
36
+ width=width,
37
+ height=height,
38
+ generator=generator,
39
+ ).images[0]
40
+ return image, seed
41
+
42
+ # CSS for centering UI
43
+ css = """
44
+ #col-container {
45
+ display: flex;
46
+ flex-direction: column;
47
+ align-items: center;
48
+ justify-content: center;
49
+ text-align: center;
50
+ margin: 0 auto;
51
+ }
52
+ """
53
+
54
+ # Gradio app
55
+ with gr.Blocks(css=css) as demo:
56
+ with gr.Column(elem_id="col-container"):
57
+ # App name and description
58
+ gr.Markdown(
59
+ """
60
+ # AI-Powered Text-to-Image Generator
61
+ *Generate stunning images from text prompts using advanced AI models.*
62
+ """
63
+ )
64
 
65
+ # Dropdown for model selection
66
+ model_choice = gr.Dropdown(
67
+ label="Select Model",
68
+ choices=[
69
+ "Faster image generation (suitable for CPUs)",
70
+ "More customizable option (slower, suitable for GPUs)"
71
+ ],
72
+ value="Faster image generation (suitable for CPUs)",
73
+ )
74
 
75
+ # Input section
76
+ prompt = gr.Textbox(label="Prompt", placeholder="Enter your prompt here...")
77
+ with gr.Accordion("Advanced Settings", open=False):
78
+ negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="Enter a negative prompt here...")
79
+ seed = gr.Slider(label="Seed", minimum=0, maximum=2147483647, step=1, value=0)
80
+ randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
81
+ width = gr.Slider(label="Width", minimum=256, maximum=1024, step=32, value=512)
82
+ height = gr.Slider(label="Height", minimum=256, maximum=1024, step=32, value=512)
83
+ guidance_scale = gr.Slider(label="Guidance Scale", minimum=0.0, maximum=10.0, step=0.1, value=7.5)
84
+ num_inference_steps = gr.Slider(label="Inference Steps", minimum=1, maximum=50, step=1, value=25)
85
+
86
+ # Output section
87
+ result = gr.Image(label="Generated Image", type="pil")
88
+ gr.Button("Generate").click(
89
  custom_infer,
90
+ inputs=[model_choice, prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
91
+ outputs=result
92
  )
93
 
94
+ # Launch app
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
  if __name__ == "__main__":
96
  demo.launch()