Akbartus commited on
Commit
d09a710
·
verified ·
1 Parent(s): 1dff83d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +401 -0
app.py ADDED
@@ -0,0 +1,401 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import random
3
+ import os
4
+ import torch
5
+ import subprocess
6
+ import numpy as np
7
+ from PIL import Image
8
+ from transformers import AutoProcessor, AutoModelForCausalLM
9
+ from diffusers import DiffusionPipeline
10
+ import cv2
11
+ from datetime import datetime
12
+ from fastapi import FastAPI
13
+
14
+ app = FastAPI()
15
+
16
+
17
+ #----------Start of theme----------
18
+ theme = gr.themes.Soft(
19
+ primary_hue="zinc",
20
+ secondary_hue="stone",
21
+ font=[gr.themes.GoogleFont('Kavivanar'), gr.themes.GoogleFont('Kavivanar'), 'system-ui', 'sans-serif'],
22
+ font_mono=[gr.themes.GoogleFont('Source Code Pro'), gr.themes.GoogleFont('Inconsolata'), gr.themes.GoogleFont('Inconsolata'), 'monospace'],
23
+ ).set(
24
+ body_background_fill='*primary_100',
25
+ body_text_color='secondary_600',
26
+ body_text_color_subdued='*primary_500',
27
+ body_text_weight='500',
28
+ background_fill_primary='*primary_100',
29
+ background_fill_secondary='*secondary_200',
30
+ color_accent='*primary_300',
31
+ border_color_accent_subdued='*primary_400',
32
+ border_color_primary='*primary_400',
33
+ block_background_fill='*primary_300',
34
+ block_border_width='*panel_border_width',
35
+ block_info_text_color='*primary_700',
36
+ block_info_text_size='*text_md',
37
+ panel_background_fill='*primary_200',
38
+ accordion_text_color='*primary_600',
39
+ table_text_color='*primary_600',
40
+ input_background_fill='*primary_50',
41
+ input_background_fill_focus='*primary_100',
42
+ button_primary_background_fill='*primary_500',
43
+ button_primary_background_fill_hover='*primary_400',
44
+ button_primary_text_color='*primary_50',
45
+ button_primary_text_color_hover='*primary_100',
46
+ button_cancel_background_fill='*primary_500',
47
+ button_cancel_background_fill_hover='*primary_400'
48
+ )
49
+ #----------End of theme----------
50
+
51
+ def flip_image(x):
52
+ return np.fliplr(x)
53
+
54
+ def basic_filter(image, filter_type):
55
+ """Apply basic image filters"""
56
+ if filter_type == "Gray Toning":
57
+ return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
58
+ elif filter_type == "Sepia":
59
+ sepia_filter = np.array([
60
+ [0.272, 0.534, 0.131],
61
+ [0.349, 0.686, 0.168],
62
+ [0.393, 0.769, 0.189]
63
+ ])
64
+ return cv2.transform(image, sepia_filter)
65
+ elif filter_type == "X-ray":
66
+ # Improved X-ray effect
67
+ gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
68
+ inverted = cv2.bitwise_not(gray)
69
+ # Increase contrast
70
+ clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8))
71
+ enhanced = clahe.apply(inverted)
72
+ # Sharpen
73
+ kernel = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]])
74
+ sharpened = cv2.filter2D(enhanced, -1, kernel)
75
+ return cv2.cvtColor(sharpened, cv2.COLOR_GRAY2BGR)
76
+ elif filter_type == "Burn it":
77
+ return cv2.GaussianBlur(image, (15, 15), 0)
78
+
79
+ def classic_filter(image, filter_type):
80
+ """Classical display filters"""
81
+ if filter_type == "Charcoal Effect":
82
+ gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
83
+ inverted = cv2.bitwise_not(gray)
84
+ blurred = cv2.GaussianBlur(inverted, (21, 21), 0)
85
+ sketch = cv2.divide(gray, cv2.subtract(255, blurred), scale=256)
86
+ return cv2.cvtColor(sketch, cv2.COLOR_GRAY2BGR)
87
+
88
+ elif filter_type == "Sharpen":
89
+ kernel = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]])
90
+ return cv2.filter2D(image, -1, kernel)
91
+
92
+ elif filter_type == "Embossing":
93
+ kernel = np.array([[0,-1,-1], [1,0,-1], [1,1,0]])
94
+ gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
95
+ emboss = cv2.filter2D(gray, -1, kernel) + 128
96
+ return cv2.cvtColor(emboss, cv2.COLOR_GRAY2BGR)
97
+
98
+ elif filter_type == "Edge Detection":
99
+ edges = cv2.Canny(image, 100, 200)
100
+ return cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR)
101
+
102
+ def creative_filters(image, filter_type):
103
+ """Creative and unusual image filters"""
104
+ if filter_type == "Pixel Art":
105
+ h, w = image.shape[:2]
106
+ piksel_size = 20
107
+ small = cv2.resize(image, (w//piksel_size, h//piksel_size))
108
+ return cv2.resize(small, (w, h), interpolation=cv2.INTER_NEAREST)
109
+
110
+ elif filter_type == "Mosaic Effect":
111
+ h, w = image.shape[:2]
112
+ mosaic_size = 30
113
+ for i in range(0, h, mosaic_size):
114
+ for j in range(0, w, mosaic_size):
115
+ roi = image[i:i+mosaic_size, j:j+mosaic_size]
116
+ if roi.size > 0:
117
+ color = np.mean(roi, axis=(0,1))
118
+ image[i:i+mosaic_size, j:j+mosaic_size] = color
119
+ return image
120
+
121
+ elif filter_type == "Rainbow":
122
+ hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
123
+ h, w = image.shape[:2]
124
+ for i in range(h):
125
+ hsv[i, :, 0] = (hsv[i, :, 0] + i % 180).astype(np.uint8)
126
+ return cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
127
+
128
+ elif filter_type == "Night Vision":
129
+ green_image = image.copy()
130
+ green_image[:,:,0] = 0 # Blue channel
131
+ green_image[:,:,2] = 0 # Red channel
132
+ return cv2.addWeighted(green_image, 1.5, np.zeros(image.shape, image.dtype), 0, -50)
133
+
134
+ def special_effects(image, filter_type):
135
+ """Apply special effects"""
136
+ if filter_type == "Matrix Effect":
137
+ green_matrix = np.zeros_like(image)
138
+ green_matrix[:,:,1] = image[:,:,1] # Only green channel
139
+ random_brightness = np.random.randint(0, 255, size=image.shape[:2])
140
+ green_matrix[:,:,1] = np.minimum(green_matrix[:,:,1] + random_brightness, 255)
141
+ return green_matrix
142
+
143
+ elif filter_type == "Wave Effect":
144
+ rows, cols = image.shape[:2]
145
+ img_output = np.zeros(image.shape, dtype=image.dtype)
146
+
147
+ for i in range(rows):
148
+ for j in range(cols):
149
+ offset_x = int(25.0 * np.sin(2 * 3.14 * i / 180))
150
+ offset_y = int(25.0 * np.cos(2 * 3.14 * j / 180))
151
+ if i+offset_x < rows and j+offset_y < cols:
152
+ img_output[i,j] = image[(i+offset_x)%rows,(j+offset_y)%cols]
153
+ else:
154
+ img_output[i,j] = 0
155
+ return img_output
156
+
157
+ elif filter_type == "Time Stamp":
158
+ output = image.copy()
159
+ timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
160
+ font = cv2.FONT_HERSHEY_SIMPLEX
161
+ cv2.putText(output, timestamp, (10, 30), font, 1, (255, 255, 255), 2)
162
+ return output
163
+
164
+ elif filter_type == "Glitch Effect":
165
+ glitch = image.copy()
166
+ h, w = image.shape[:2]
167
+ for _ in range(10):
168
+ x1 = random.randint(0, w-50)
169
+ y1 = random.randint(0, h-50)
170
+ x2 = random.randint(x1, min(x1+50, w))
171
+ y2 = random.randint(y1, min(y1+50, h))
172
+ glitch[y1:y2, x1:x2] = np.roll(glitch[y1:y2, x1:x2],
173
+ random.randint(-20, 20),
174
+ axis=random.randint(0, 1))
175
+ return glitch
176
+
177
+ def artistic_filters(image, filter_type):
178
+ """Applies artistic image filters"""
179
+ if filter_type == "Pop Art":
180
+ img_small = cv2.resize(image, None, fx=0.5, fy=0.5)
181
+ img_color = cv2.resize(img_small, (image.shape[1], image.shape[0]))
182
+ for _ in range(2):
183
+ img_color = cv2.bilateralFilter(img_color, 9, 300, 300)
184
+ hsv = cv2.cvtColor(img_color, cv2.COLOR_BGR2HSV)
185
+ hsv[:,:,1] = hsv[:,:,1]*1.5
186
+ return cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
187
+
188
+ elif filter_type == "Oil Paint":
189
+ ret = np.float32(image.copy())
190
+ ret = cv2.bilateralFilter(ret, 9, 75, 75)
191
+ ret = cv2.detailEnhance(ret, sigma_s=15, sigma_r=0.15)
192
+ ret = cv2.edgePreservingFilter(ret, flags=1, sigma_s=60, sigma_r=0.4)
193
+ return np.uint8(ret)
194
+
195
+ elif filter_type == "Cartoon":
196
+ # Improved cartoon effect
197
+ color = image.copy()
198
+ gray = cv2.cvtColor(color, cv2.COLOR_BGR2GRAY)
199
+ gray = cv2.medianBlur(gray, 5)
200
+ edges = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 9, 9)
201
+ color = cv2.bilateralFilter(color, 9, 300, 300)
202
+ cartoon = cv2.bitwise_and(color, color, mask=edges)
203
+ # Increase color saturation
204
+ hsv = cv2.cvtColor(cartoon, cv2.COLOR_BGR2HSV)
205
+ hsv[:,:,1] = hsv[:,:,1]*1.4 # saturation increase
206
+ return cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
207
+
208
+ def atmospheric_filters(image, filter_type):
209
+ """atmospheric filters"""
210
+ if filter_type == "Autumn":
211
+ # Genhanced autumn effect
212
+ autumn_filter = np.array([
213
+ [0.393, 0.769, 0.189],
214
+ [0.349, 0.686, 0.168],
215
+ [0.272, 0.534, 0.131]
216
+ ])
217
+ autumn = cv2.transform(image, autumn_filter)
218
+ # Increase color temperature
219
+ hsv = cv2.cvtColor(autumn, cv2.COLOR_BGR2HSV)
220
+ hsv[:,:,0] = hsv[:,:,0]*0.8 # Shift to orange/yellow tones
221
+ hsv[:,:,1] = hsv[:,:,1]*1.2 # Increase saturation
222
+ return cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
223
+
224
+ elif filter_type == "Nostalgia":
225
+ # Improved nostalgia effect
226
+ # Reduce contrast and add yellowish tone
227
+ image = cv2.convertScaleAbs(image, alpha=0.9, beta=10)
228
+ sepia = cv2.transform(image, np.array([
229
+ [0.393, 0.769, 0.189],
230
+ [0.349, 0.686, 0.168],
231
+ [0.272, 0.534, 0.131]
232
+ ]))
233
+ # Darkening effect in corners
234
+ h, w = image.shape[:2]
235
+ kernel = np.zeros((h, w))
236
+ center = (h//2, w//2)
237
+ for i in range(h):
238
+ for j in range(w):
239
+ dist = np.sqrt((i-center[0])**2 + (j-center[1])**2)
240
+ kernel[i,j] = 1 - min(1, dist/(np.sqrt(h**2 + w**2)/2))
241
+ kernel = np.dstack([kernel]*3)
242
+ return cv2.multiply(sepia, kernel).astype(np.uint8)
243
+
244
+ elif filter_type == "Increase Brightness":
245
+ # Improved brightness boost
246
+ hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
247
+ # Increase brightness
248
+ hsv[:,:,2] = cv2.convertScaleAbs(hsv[:,:,2], alpha=1.2, beta=30)
249
+ # Also increase the contrast slightly
250
+ return cv2.convertScaleAbs(cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR), alpha=1.1, beta=0)
251
+
252
+ def image_processing(image, filter_type):
253
+ """Main image processing function"""
254
+ if image is None:
255
+ return None
256
+
257
+ image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
258
+
259
+ # Process by filter categories
260
+ basic_filter_list = ["Gray Toning", "Sepia", "X-ray", "Burn it"]
261
+ classic_filter_list = ["Charcoal Effect", "Sharpen", "Embossing", "Edge Detection"]
262
+ creative_filters_list = ["Rainbow", "Night Vision"]
263
+ special_effects_list = ["Matrix Effect", "Wave Effect", "Time Stamp", "Glitch Effect"]
264
+ artistic_filters_list = ["Pop Art", "Oil Paint", "Cartoon"]
265
+ atmospheric_filters_list = ["Autumn", "Increase Brightness"]
266
+
267
+ if filter_type in basic_filter_list:
268
+ output = basic_filter(image, filter_type)
269
+ elif filter_type in classic_filter_list:
270
+ output = classic_filter(image, filter_type)
271
+ elif filter_type in creative_filters_list:
272
+ output = creative_filters(image, filter_type)
273
+ elif filter_type in special_effects_list:
274
+ output = special_effects(image, filter_type)
275
+ elif filter_type in artistic_filters_list:
276
+ output = artistic_filters(image, filter_type)
277
+ elif filter_type in atmospheric_filters_list:
278
+ output = atmospheric_filters(image, filter_type)
279
+ else:
280
+ output = image
281
+
282
+ return cv2.cvtColor(output, cv2.COLOR_BGR2RGB) if len(output.shape) == 3 else output
283
+
284
+
285
+ css = """
286
+ #app-container {
287
+ max-width: 1200px;
288
+ margin-left: auto;
289
+ margin-right: auto;
290
+ }
291
+ """
292
+
293
+ # Gradio interface
294
+ with gr.Blocks(theme=theme, css=css) as app:
295
+ gr.HTML("<center><h6>🎨 Image Studio</h6></center>")
296
+
297
+ with gr.Tab("Image to Prompt"):
298
+ subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
299
+
300
+ # Initialize Florence model
301
+ device = "cuda" if torch.cuda.is_available() else "cpu"
302
+ florence_model = AutoModelForCausalLM.from_pretrained('microsoft/Florence-2-base', trust_remote_code=True).to(device).eval()
303
+ florence_processor = AutoProcessor.from_pretrained('microsoft/Florence-2-base', trust_remote_code=True)
304
+
305
+ # api_key = os.getenv("HF_READ_TOKEN")
306
+
307
+ def generate_caption(image):
308
+ if not isinstance(image, Image.Image):
309
+ image = Image.fromarray(image)
310
+
311
+ inputs = florence_processor(text="<MORE_DETAILED_CAPTION>", images=image, return_tensors="pt").to(device)
312
+ generated_ids = florence_model.generate(
313
+ input_ids=inputs["input_ids"],
314
+ pixel_values=inputs["pixel_values"],
315
+ max_new_tokens=1024,
316
+ early_stopping=False,
317
+ do_sample=False,
318
+ num_beams=3,
319
+ )
320
+ generated_text = florence_processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
321
+ parsed_answer = florence_processor.post_process_generation(
322
+ generated_text,
323
+ task="<MORE_DETAILED_CAPTION>",
324
+ image_size=(image.width, image.height)
325
+ )
326
+ prompt = parsed_answer["<MORE_DETAILED_CAPTION>"]
327
+ print("\n\nGeneration completed!:"+ prompt)
328
+ return prompt
329
+
330
+ io = gr.Interface(generate_caption,
331
+ inputs=[gr.Image(label="Input Image")],
332
+ outputs = [gr.Textbox(label="Output Prompt", lines=2, show_copy_button = True),
333
+ # gr.Image(label="Output Image")
334
+ ]
335
+ )
336
+
337
+ with gr.Tab("Text to Image"):
338
+ gr.HTML("<center><h6>ℹ️ Please do not run the models at the same time, the models are currently running on the CPU, which might affect performance.</h6></center>")
339
+ with gr.Accordion("Flux-RealismLora", open=False):
340
+ model1 = gr.load("models/XLabs-AI/flux-RealismLora")
341
+ with gr.Accordion("Flux--schnell-realism", open=False):
342
+ model2 = gr.load("models/hugovntr/flux-schnell-realism")
343
+ with gr.Accordion("Flux--schnell-LoRA", open=False):
344
+ model3 = gr.load("models/Octree/flux-schnell-lora")
345
+
346
+ with gr.Tab("Flip Image"):
347
+ with gr.Row():
348
+ image_input = gr.Image(type="numpy", label="Upload Image")
349
+ image_output = gr.Image(format="png")
350
+ with gr.Row():
351
+ image_button = gr.Button("Run", variant='primary')
352
+ image_button.click(flip_image, inputs=image_input, outputs=image_output)
353
+ with gr.Tab("Image Filters"):
354
+ with gr.Row():
355
+ with gr.Column():
356
+ image_input = gr.Image(type="numpy", label="Upload Image")
357
+ with gr.Accordion("ℹ️ Filter Categories", open=True):
358
+ filter_type = gr.Dropdown(
359
+ [
360
+ # Basic Filters
361
+ "Gray Toning", "Sepia", "X-ray", "Burn it",
362
+ # Classic Filter
363
+ "Charcoal Effect", "Sharpen", "Embossing", "Edge Detection",
364
+ # Creative Filters
365
+ "Rainbow", "Night Vision",
366
+ # Special Effects
367
+ "Matrix Effect", "Wave Effect", "Time Stamp", "Glitch Effect",
368
+ # Artistic Filters
369
+ "Pop Art", "Oil Paint", "Cartoon",
370
+ # Atmospheric Filters
371
+ "Autumn", "Increase Brightness"
372
+ ],
373
+ label="🎭 Select Filter",
374
+ info="Choose the effect you want"
375
+ )
376
+ submit_button = gr.Button("✨ Apply Filter", variant="primary")
377
+
378
+ with gr.Column():
379
+ image_output = gr.Image(label="🖼️ Filtered Image")
380
+
381
+ submit_button.click(
382
+ image_processing,
383
+ inputs=[image_input, filter_type],
384
+ outputs=image_output
385
+ )
386
+
387
+
388
+
389
+ with gr.Tab("Image Upscaler"):
390
+ with gr.Row():
391
+ with gr.Column():
392
+ def upscale_image(input_image, radio_input):
393
+ upscale_factor = radio_input
394
+ output_image = cv2.resize(input_image, None, fx = upscale_factor, fy = upscale_factor, interpolation = cv2.INTER_CUBIC)
395
+ return output_image
396
+
397
+ radio_input = gr.Radio(label="Upscale Levels", choices=[2, 4, 6, 8, 10], value=2)
398
+
399
+ iface = gr.Interface(fn=upscale_image, inputs = [gr.Image(label="Input Image", interactive=True), radio_input], outputs = gr.Image(label="Upscaled Image", format="png"), title="Image Upscaler")
400
+
401
+ app.launch(share=True)