OminduAnjana commited on
Commit
13275a0
·
verified ·
1 Parent(s): 35bc55a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -113
app.py CHANGED
@@ -7,49 +7,21 @@ import torch
7
  os.environ["OMP_NUM_THREADS"] = str(os.cpu_count())
8
  torch.set_num_threads(os.cpu_count())
9
 
10
- # Load the Turbo Realism model
11
  model = gr.load("models/prithivMLmods/SD3.5-Turbo-Realism-2.0-LoRA")
12
  stop_event = threading.Event()
13
 
14
- def generate_images(text, width, height, quality_level, num_inference_steps, guidance_scale):
15
  stop_event.clear()
16
  results = []
17
 
18
- # Create model parameters based on advanced settings
19
- model_params = {
20
- "width": width,
21
- "height": height,
22
- "num_inference_steps": num_inference_steps,
23
- "guidance_scale": guidance_scale
24
- }
25
-
26
- # Add quality enhancements based on selected level
27
- if quality_level == "HD (1080p)":
28
- model_params["upscaler"] = "esrgan_x2plus"
29
- elif quality_level == "4K":
30
- model_params["upscaler"] = "esrgan_x4plus"
31
- model_params["upscale_strength"] = 0.8
32
-
33
  for i in range(3):
34
  if stop_event.is_set():
35
  return ["Image generation stopped by user."] * 3
36
 
37
- # Add quality descriptors to prompt for better results
38
- quality_descriptors = ""
39
- if quality_level == "HD (1080p)":
40
- quality_descriptors = "high definition, detailed, sharp"
41
- elif quality_level == "4K":
42
- quality_descriptors = "ultra high definition, extremely detailed, crystal clear, 4k quality"
43
-
44
- # Create the enhanced prompt
45
- modified_text = f"{text}, {quality_descriptors}, variation {i+1}"
46
-
47
- # Generate image with advanced parameters
48
- try:
49
- result = model(modified_text, **model_params)
50
- results.append(result)
51
- except Exception as e:
52
- results.append(f"Error: {str(e)}")
53
 
54
  return results
55
 
@@ -58,110 +30,40 @@ def stop_generation():
58
  stop_event.set()
59
  return ["Generation stopped."] * 3
60
 
61
- # Preset resolution options
62
- def get_resolution_presets(choice):
63
- presets = {
64
- "SD (480p)": (640, 480),
65
- "HD (1080p)": (1920, 1080),
66
- "4K": (3840, 2160),
67
- "Square": (1024, 1024),
68
- "Portrait": (1080, 1920),
69
- "Landscape": (1920, 1080),
70
- "Custom": None # Will keep current values
71
- }
72
-
73
- if choice in presets and presets[choice] is not None:
74
- return presets[choice][0], presets[choice][1]
75
- return gr.update(), gr.update() # No change for custom
76
-
77
  with gr.Blocks() as interface:
78
  # Warning message
79
  gr.Markdown(
80
  "### ⚠ Sorry for the inconvenience. The Space is currently running on the CPU, which might affect performance. We appreciate your understanding."
81
  )
82
 
83
- # Title and description
84
- gr.Markdown("# Advanced Image Generator")
85
- gr.Markdown("Generate high-quality images with customizable settings")
86
-
87
  # Input area
88
- with gr.Group():
89
- text_input = gr.Textbox(
90
- label="Type here your imagination:",
91
- placeholder="Type your prompt...",
92
- lines=3
93
- )
94
-
95
- # Advanced settings in collapsible section
96
- with gr.Accordion("Advanced Settings", open=True):
97
- with gr.Row():
98
- resolution_preset = gr.Dropdown(
99
- choices=["SD (480p)", "HD (1080p)", "4K", "Square", "Portrait", "Landscape", "Custom"],
100
- value="HD (1080p)",
101
- label="Resolution Preset"
102
- )
103
-
104
- with gr.Row():
105
- width = gr.Slider(minimum=256, maximum=3840, value=1920, step=64, label="Width")
106
- height = gr.Slider(minimum=256, maximum=2160, value=1080, step=64, label="Height")
107
-
108
- with gr.Row():
109
- quality_level = gr.Radio(
110
- choices=["Standard", "HD (1080p)", "4K"],
111
- value="HD (1080p)",
112
- label="Quality Level"
113
- )
114
-
115
- with gr.Row():
116
- num_inference_steps = gr.Slider(
117
- minimum=20, maximum=150, value=50, step=5,
118
- label="Inference Steps (Higher = Better Quality, Slower)"
119
- )
120
- guidance_scale = gr.Slider(
121
- minimum=1.0, maximum=20.0, value=7.5, step=0.5,
122
- label="Guidance Scale (Higher = More Prompt Adherence)"
123
- )
124
-
125
- # Connect the resolution preset to the width and height sliders
126
- resolution_preset.change(get_resolution_presets, inputs=[resolution_preset], outputs=[width, height])
127
 
128
  # Buttons
129
  with gr.Row():
130
- generate_button = gr.Button("Generate 3 Images 🎨", variant="primary")
131
- stop_button = gr.Button("Stop Image Generation", variant="secondary")
132
 
133
  # Output images
134
  with gr.Row():
135
- output1 = gr.Image(label="Generated Image 1", type="pil")
136
- output2 = gr.Image(label="Generated Image 2", type="pil")
137
- output3 = gr.Image(label="Generated Image 3", type="pil")
138
-
139
- # Generation status
140
- status = gr.Textbox(label="Status", value="Ready", interactive=False)
141
 
142
  # Set up the event handlers
143
  generate_button.click(
144
- fn=lambda: "Generating images... Please wait.",
145
- inputs=None,
146
- outputs=status
147
- ).then(
148
  generate_images,
149
- inputs=[text_input, width, height, quality_level, num_inference_steps, guidance_scale],
150
  outputs=[output1, output2, output3]
151
- ).then(
152
- fn=lambda: "Generation complete!",
153
- inputs=None,
154
- outputs=status
155
  )
156
 
157
  stop_button.click(
158
  stop_generation,
159
  inputs=[],
160
  outputs=[output1, output2, output3]
161
- ).then(
162
- fn=lambda: "Generation stopped by user.",
163
- inputs=None,
164
- outputs=status
165
  )
166
 
167
  interface.launch()
 
7
  os.environ["OMP_NUM_THREADS"] = str(os.cpu_count())
8
  torch.set_num_threads(os.cpu_count())
9
 
10
+ # Load only the Turbo Realism model
11
  model = gr.load("models/prithivMLmods/SD3.5-Turbo-Realism-2.0-LoRA")
12
  stop_event = threading.Event()
13
 
14
+ def generate_images(text):
15
  stop_event.clear()
16
  results = []
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  for i in range(3):
19
  if stop_event.is_set():
20
  return ["Image generation stopped by user."] * 3
21
 
22
+ modified_text = f"{text} variation {i+1}"
23
+ result = model(modified_text)
24
+ results.append(result)
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
  return results
27
 
 
30
  stop_event.set()
31
  return ["Generation stopped."] * 3
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  with gr.Blocks() as interface:
34
  # Warning message
35
  gr.Markdown(
36
  "### ⚠ Sorry for the inconvenience. The Space is currently running on the CPU, which might affect performance. We appreciate your understanding."
37
  )
38
 
 
 
 
 
39
  # Input area
40
+ text_input = gr.Textbox(
41
+ label="Type here your imagination:",
42
+ placeholder="Type your prompt..."
43
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
  # Buttons
46
  with gr.Row():
47
+ generate_button = gr.Button("Generate 3 Images 🎨")
48
+ stop_button = gr.Button("Stop Image Generation")
49
 
50
  # Output images
51
  with gr.Row():
52
+ output1 = gr.Image(label="Generated Image 1")
53
+ output2 = gr.Image(label="Generated Image 2")
54
+ output3 = gr.Image(label="Generated Image 3")
 
 
 
55
 
56
  # Set up the event handlers
57
  generate_button.click(
 
 
 
 
58
  generate_images,
59
+ inputs=[text_input],
60
  outputs=[output1, output2, output3]
 
 
 
 
61
  )
62
 
63
  stop_button.click(
64
  stop_generation,
65
  inputs=[],
66
  outputs=[output1, output2, output3]
 
 
 
 
67
  )
68
 
69
  interface.launch()