Sourudra commited on
Commit
9f1cf6b
·
verified ·
1 Parent(s): 3d6bddc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -72
app.py CHANGED
@@ -1,96 +1,66 @@
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()
 
1
  import gradio as gr
 
 
 
2
 
3
+ # CSS for aligning the UI elements in the middle
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  css = """
5
  #col-container {
 
 
 
 
 
6
  margin: 0 auto;
7
+ max-width: 640px;
8
+ text-align: center;
9
  }
10
  """
11
 
12
+ # Maximum image size
13
+ MAX_IMAGE_SIZE = 1024
14
+
15
+ # Load the ZB-Tech/Text-to-Image model
16
+ def infer(prompt, height, width):
17
+ model = gr.Interface.load("models/ZB-Tech/Text-to-Image")
18
+ return model(prompt, height=height, width=width)
19
+
20
  # Gradio app
21
  with gr.Blocks(css=css) as demo:
22
  with gr.Column(elem_id="col-container"):
23
+ # Title and description
24
  gr.Markdown(
25
  """
26
  # AI-Powered Text-to-Image Generator
27
+ *Generate stunning images from text prompts using the ZB-Tech/Text-to-Image model.*
28
  """
29
  )
30
 
31
+ # Input: Prompt
32
+ prompt = gr.Textbox(
33
+ label="Prompt",
34
+ placeholder="Enter your prompt here...",
35
+ lines=2,
 
 
 
36
  )
37
 
38
+ # Input: Height and Width sliders
39
+ height = gr.Slider(
40
+ label="Height",
41
+ minimum=256,
42
+ maximum=MAX_IMAGE_SIZE,
43
+ step=32,
44
+ value=1024,
45
+ )
 
 
46
 
47
+ width = gr.Slider(
48
+ label="Width",
49
+ minimum=256,
50
+ maximum=MAX_IMAGE_SIZE,
51
+ step=32,
52
+ value=1024,
53
  )
54
 
55
+ # Output: Generated Image
56
+ result = gr.Image(label="Generated Image", type="pil")
57
+
58
+ # Generate Button
59
+ generate_button = gr.Button("Generate")
60
+
61
+ # Button action: Call infer function
62
+ generate_button.click(infer, inputs=[prompt, height, width], outputs=result)
63
+
64
+ # Launch the app
65
  if __name__ == "__main__":
66
  demo.launch()