Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,18 @@
|
|
1 |
import gradio as gr
|
2 |
from PIL import Image, ImageOps, ImageEnhance
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
def edit_image(img_data, operation, *args):
|
5 |
image = Image.open(img_data)
|
@@ -24,28 +37,53 @@ def edit_image(img_data, operation, *args):
|
|
24 |
|
25 |
return image
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
-
|
38 |
-
with gr.Column():
|
39 |
-
edit_operation = gr.Dropdown(choices=["rotate", "crop", "resize", "flip", "color"], label="Edit Operation")
|
40 |
-
edit_args = gr.Textbox(label="Edit Arguments (comma-separated)", placeholder="For rotate: angle, For crop: left,top,right,bottom, For resize: width,height, For flip: horizontal/vertical, For color: factor")
|
41 |
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
44 |
|
45 |
-
|
46 |
-
fn=lambda img_data, operation, args: edit_image(img_data, operation, *args.split(',')),
|
47 |
-
inputs=[edited_image, edit_operation, edit_args],
|
48 |
-
outputs=[edited_image]
|
49 |
-
)
|
50 |
|
|
|
51 |
demo.queue().launch()
|
|
|
1 |
import gradio as gr
|
2 |
from PIL import Image, ImageOps, ImageEnhance
|
3 |
+
import torch
|
4 |
+
from diffusers import DiffusionPipeline
|
5 |
+
|
6 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
7 |
+
|
8 |
+
if torch.cuda.is_available():
|
9 |
+
torch.cuda.max_memory_allocated(device=device)
|
10 |
+
pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
|
11 |
+
pipe.enable_xformers_memory_efficient_attention()
|
12 |
+
pipe = pipe.to(device)
|
13 |
+
else:
|
14 |
+
pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", use_safetensors=True)
|
15 |
+
pipe = pipe.to(device)
|
16 |
|
17 |
def edit_image(img_data, operation, *args):
|
18 |
image = Image.open(img_data)
|
|
|
37 |
|
38 |
return image
|
39 |
|
40 |
+
def create_demo():
|
41 |
+
with gr.Blocks() as demo:
|
42 |
+
with gr.Row():
|
43 |
+
gr.Markdown("# Image Editor")
|
44 |
+
|
45 |
+
with gr.Row():
|
46 |
+
with gr.Column():
|
47 |
+
edit_operation = gr.Dropdown(choices=["rotate", "crop", "resize", "flip", "color"], label="Edit Operation")
|
48 |
+
input_args = gr.State([])
|
49 |
+
|
50 |
+
angle = gr.Slider(0, 360, step=1, label="Angle", visible=False)
|
51 |
+
left = gr.Slider(0, 500, step=1, label="Left", visible=False)
|
52 |
+
top = gr.Slider(0, 500, step=1, label="Top", visible=False)
|
53 |
+
right = gr.Slider(0, 500, step=1, label="Right", visible=False)
|
54 |
+
bottom = gr.Slider(0, 500, step=1, label="Bottom", visible=False)
|
55 |
+
width = gr.Slider(50, 1000, step=1, label="Width", visible=False)
|
56 |
+
height = gr.Slider(50, 1000, step=1, label="Height", visible=False)
|
57 |
+
flip_direction = gr.Dropdown(choices=["horizontal", "vertical"], label="Direction", visible=False)
|
58 |
+
color_factor = gr.Slider(0.1, 2.0, step=0.1, label="Color Factor", visible=False)
|
59 |
+
|
60 |
+
edit_button = gr.Button("Edit Image")
|
61 |
+
uploaded_image = gr.Image(label="Upload Image", type="file")
|
62 |
+
edited_image = gr.Image(label="Edited Image", type="pil", interactive=True)
|
63 |
|
64 |
+
def update_inputs(operation):
|
65 |
+
if operation == "rotate":
|
66 |
+
return [gr.update(visible=True), angle]
|
67 |
+
elif operation == "crop":
|
68 |
+
return [gr.update(visible=True), left, top, right, bottom]
|
69 |
+
elif operation == "resize":
|
70 |
+
return [gr.update(visible=True), width, height]
|
71 |
+
elif operation == "flip":
|
72 |
+
return [gr.update(visible=True), flip_direction]
|
73 |
+
elif operation == "color":
|
74 |
+
return [gr.update(visible=True), color_factor]
|
75 |
+
else:
|
76 |
+
return []
|
77 |
|
78 |
+
edit_operation.change(fn=update_inputs, inputs=[edit_operation], outputs=[angle, left, top, right, bottom, width, height, flip_direction, color_factor])
|
|
|
|
|
|
|
79 |
|
80 |
+
edit_button.click(
|
81 |
+
fn=lambda img_data, operation, *args: edit_image(img_data, operation, *args),
|
82 |
+
inputs=[uploaded_image, edit_operation, angle, left, top, right, bottom, width, height, flip_direction, color_factor],
|
83 |
+
outputs=[edited_image]
|
84 |
+
)
|
85 |
|
86 |
+
return demo
|
|
|
|
|
|
|
|
|
87 |
|
88 |
+
demo = create_demo()
|
89 |
demo.queue().launch()
|