Update app.py
Browse files
app.py
CHANGED
@@ -1,57 +1,44 @@
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
-
|
4 |
from PIL import Image
|
5 |
import io
|
6 |
-
import zipfile
|
7 |
|
8 |
-
def
|
9 |
"""
|
10 |
-
|
11 |
-
then compresses them into a ZIP file for download.
|
12 |
"""
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
fill_mode='nearest')
|
22 |
-
|
23 |
-
# Initialize ZIP file in memory
|
24 |
-
zip_buffer = io.BytesIO()
|
25 |
-
|
26 |
-
with zipfile.ZipFile(zip_buffer, 'a', zipfile.ZIP_DEFLATED) as zipf:
|
27 |
-
img = Image.open(image_file.name).convert('RGB')
|
28 |
-
img = img.resize((256, 256)) # Resize image
|
29 |
-
x = img_to_array(img) # Convert image to np.array
|
30 |
-
x = np.expand_dims(x, axis=0) # Add batch dimension
|
31 |
-
|
32 |
-
# Generate and save augmented images
|
33 |
-
for i in range(num_duplicates):
|
34 |
-
it = datagen.flow(x, batch_size=1)
|
35 |
-
batch = next(it)
|
36 |
-
image = Image.fromarray(batch[0].astype('uint8'), 'RGB')
|
37 |
-
img_byte_arr = io.BytesIO()
|
38 |
-
image.save(img_byte_arr, format='JPEG')
|
39 |
-
img_byte_arr.seek(0)
|
40 |
-
zipf.writestr(f"augmented_image_{i}.jpeg", img_byte_arr.getvalue())
|
41 |
-
|
42 |
-
# Prepare ZIP file for download
|
43 |
-
zip_buffer.seek(0)
|
44 |
-
|
45 |
-
return zip_buffer, 'augmented_images.zip'
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
)
|
55 |
|
56 |
if __name__ == "__main__":
|
57 |
-
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
+
import time
|
4 |
from PIL import Image
|
5 |
import io
|
|
|
6 |
|
7 |
+
def sepia(input_img):
|
8 |
"""
|
9 |
+
Applies a sepia filter to the input image.
|
|
|
10 |
"""
|
11 |
+
sepia_filter = np.array([
|
12 |
+
[0.393, 0.769, 0.189],
|
13 |
+
[0.349, 0.686, 0.168],
|
14 |
+
[0.272, 0.534, 0.131]
|
15 |
+
])
|
16 |
+
sepia_img = input_img.dot(sepia_filter.T)
|
17 |
+
sepia_img /= sepia_img.max()
|
18 |
+
return sepia_img
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
+
def fake_diffusion_and_sepia(steps):
|
21 |
+
"""
|
22 |
+
A generator function that simulates a fake diffusion process for a specified number of steps.
|
23 |
+
After the final step, applies a sepia filter to the image.
|
24 |
+
"""
|
25 |
+
rng = np.random.default_rng()
|
26 |
+
for i in range(steps):
|
27 |
+
# Simulate the diffusion process
|
28 |
+
time.sleep(1) # Wait to simulate processing time
|
29 |
+
image = rng.random(size=(256, 256, 3)) # Generate a random image
|
30 |
+
if i == steps - 1: # Apply sepia filter on the last step
|
31 |
+
image = sepia(image)
|
32 |
+
yield image
|
33 |
+
|
34 |
+
# Define Gradio interface
|
35 |
+
demo = gr.Interface(
|
36 |
+
fn=fake_diffusion_and_sepia,
|
37 |
+
inputs=gr.Slider(minimum=1, maximum=10, default=5, step=1, label="Number of Steps"),
|
38 |
+
outputs=gr.Image(type="numpy", label="Sepia Image"),
|
39 |
+
title="Fake Diffusion with Sepia Filter",
|
40 |
+
description="Generates a series of images simulating a diffusion process. The final image is processed with a sepia filter."
|
41 |
)
|
42 |
|
43 |
if __name__ == "__main__":
|
44 |
+
demo.launch()
|