Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
from gradio_client import Client, handle_file
|
3 |
from PIL import Image
|
|
|
4 |
import tempfile
|
5 |
import os
|
6 |
|
@@ -12,13 +13,20 @@ def process_image_via_api(image):
|
|
12 |
image=handle_file(image),
|
13 |
api_name="/image"
|
14 |
)
|
15 |
-
# Convert the output tuple to PIL images and
|
16 |
if result:
|
17 |
processed_image_path = result[0]
|
18 |
origin_image_path = result[1]
|
19 |
processed_image = Image.open(processed_image_path)
|
20 |
origin_image = Image.open(origin_image_path)
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
return None, None
|
23 |
|
24 |
def process_url_via_api(url):
|
@@ -26,13 +34,20 @@ def process_url_via_api(url):
|
|
26 |
image=url,
|
27 |
api_name="/text"
|
28 |
)
|
29 |
-
# Convert the output tuple to PIL images and
|
30 |
if result:
|
31 |
processed_image_path = result[0]
|
32 |
origin_image_path = result[1]
|
33 |
processed_image = Image.open(processed_image_path)
|
34 |
origin_image = Image.open(origin_image_path)
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
return None, None
|
37 |
|
38 |
def process_file_via_api(f):
|
@@ -50,8 +65,8 @@ chameleon = "butterfly.jpg"
|
|
50 |
url_example = "https://hips.hearstapps.com/hmg-prod/images/gettyimages-1229892983-square.jpg"
|
51 |
|
52 |
# Tab 1: Image Upload
|
53 |
-
slider1_processed = gr.Image(label="Processed Image", type="
|
54 |
-
slider1_origin = gr.Image(label="Original Image", type="
|
55 |
image_upload = gr.Image(label="Upload an image")
|
56 |
tab1 = gr.Interface(
|
57 |
fn=process_image_via_api,
|
@@ -62,8 +77,8 @@ tab1 = gr.Interface(
|
|
62 |
)
|
63 |
|
64 |
# Tab 2: URL Input
|
65 |
-
slider2_processed = gr.Image(label="Processed Image", type="
|
66 |
-
slider2_origin = gr.Image(label="Original Image", type="
|
67 |
url_input = gr.Textbox(label="Paste an image URL")
|
68 |
tab2 = gr.Interface(
|
69 |
fn=process_url_via_api,
|
|
|
1 |
import gradio as gr
|
2 |
from gradio_client import Client, handle_file
|
3 |
from PIL import Image
|
4 |
+
import requests
|
5 |
import tempfile
|
6 |
import os
|
7 |
|
|
|
13 |
image=handle_file(image),
|
14 |
api_name="/image"
|
15 |
)
|
16 |
+
# Convert the output tuple to PIL images and save them to temporary files
|
17 |
if result:
|
18 |
processed_image_path = result[0]
|
19 |
origin_image_path = result[1]
|
20 |
processed_image = Image.open(processed_image_path)
|
21 |
origin_image = Image.open(origin_image_path)
|
22 |
+
|
23 |
+
# Save images to temporary files
|
24 |
+
processed_temp_path = tempfile.NamedTemporaryFile(delete=False, suffix=".png").name
|
25 |
+
origin_temp_path = tempfile.NamedTemporaryFile(delete=False, suffix=".png").name
|
26 |
+
processed_image.save(processed_temp_path)
|
27 |
+
origin_image.save(origin_temp_path)
|
28 |
+
|
29 |
+
return (processed_temp_path, origin_temp_path)
|
30 |
return None, None
|
31 |
|
32 |
def process_url_via_api(url):
|
|
|
34 |
image=url,
|
35 |
api_name="/text"
|
36 |
)
|
37 |
+
# Convert the output tuple to PIL images and save them to temporary files
|
38 |
if result:
|
39 |
processed_image_path = result[0]
|
40 |
origin_image_path = result[1]
|
41 |
processed_image = Image.open(processed_image_path)
|
42 |
origin_image = Image.open(origin_image_path)
|
43 |
+
|
44 |
+
# Save images to temporary files
|
45 |
+
processed_temp_path = tempfile.NamedTemporaryFile(delete=False, suffix=".png").name
|
46 |
+
origin_temp_path = tempfile.NamedTemporaryFile(delete=False, suffix=".png").name
|
47 |
+
processed_image.save(processed_temp_path)
|
48 |
+
origin_image.save(origin_temp_path)
|
49 |
+
|
50 |
+
return (processed_temp_path, origin_temp_path)
|
51 |
return None, None
|
52 |
|
53 |
def process_file_via_api(f):
|
|
|
65 |
url_example = "https://hips.hearstapps.com/hmg-prod/images/gettyimages-1229892983-square.jpg"
|
66 |
|
67 |
# Tab 1: Image Upload
|
68 |
+
slider1_processed = gr.Image(label="Processed Image", type="filepath")
|
69 |
+
slider1_origin = gr.Image(label="Original Image", type="filepath")
|
70 |
image_upload = gr.Image(label="Upload an image")
|
71 |
tab1 = gr.Interface(
|
72 |
fn=process_image_via_api,
|
|
|
77 |
)
|
78 |
|
79 |
# Tab 2: URL Input
|
80 |
+
slider2_processed = gr.Image(label="Processed Image", type="filepath")
|
81 |
+
slider2_origin = gr.Image(label="Original Image", type="filepath")
|
82 |
url_input = gr.Textbox(label="Paste an image URL")
|
83 |
tab2 = gr.Interface(
|
84 |
fn=process_url_via_api,
|