Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,42 +1,81 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
|
|
|
| 3 |
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
def
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
with gr.Tab("File Output"):
|
| 36 |
-
with gr.Row():
|
| 37 |
-
image_file_upload = gr.Image(label="Upload an image", type ="filepath")
|
| 38 |
-
output_file = gr.File(label="Output PNG File")
|
| 39 |
-
image_file_upload.change(dummy_process_file, image_file_upload, output_file)
|
| 40 |
|
| 41 |
if __name__ == "__main__":
|
| 42 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from gradio_client import Client, handle_file
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import requests
|
| 5 |
|
| 6 |
+
# Initialize the client
|
| 7 |
+
client = Client("not-lain/background-removal")
|
| 8 |
|
| 9 |
+
def process_image_via_api(image):
|
| 10 |
+
result = client.predict(
|
| 11 |
+
image=handle_file(image),
|
| 12 |
+
api_name="/image"
|
| 13 |
+
)
|
| 14 |
+
# Convert the output tuple to PIL images and return
|
| 15 |
+
if result:
|
| 16 |
+
return (Image.open(result[0]), Image.open(result[1]))
|
| 17 |
+
return None, None
|
| 18 |
|
| 19 |
+
def process_url_via_api(url):
|
| 20 |
+
result = client.predict(
|
| 21 |
+
image=url,
|
| 22 |
+
api_name="/text"
|
| 23 |
+
)
|
| 24 |
+
# Convert the output tuple to PIL images and return
|
| 25 |
+
if result:
|
| 26 |
+
return (Image.open(result[0]), Image.open(result[1]))
|
| 27 |
+
return None, None
|
| 28 |
|
| 29 |
+
def process_file_via_api(f):
|
| 30 |
+
result = client.predict(
|
| 31 |
+
f=handle_file(f),
|
| 32 |
+
api_name="/png"
|
| 33 |
+
)
|
| 34 |
+
# Return the path to the saved PNG file
|
| 35 |
+
if result:
|
| 36 |
+
return result
|
| 37 |
+
return None
|
| 38 |
|
| 39 |
+
# Example images
|
| 40 |
+
chameleon = "butterfly.jpg"
|
| 41 |
+
url_example = "https://hips.hearstapps.com/hmg-prod/images/gettyimages-1229892983-square.jpg"
|
| 42 |
|
| 43 |
+
image_upload = gr.Image(label="Upload an image")
|
| 44 |
+
url_input = gr.Textbox(label="Paste an image URL")
|
| 45 |
+
image_file_upload = gr.Image(label="Upload an image", type="filepath")
|
| 46 |
+
output_file = gr.File(label="Output PNG File")
|
| 47 |
+
sliders_processed = gr.Image(label="Processed Image")
|
| 48 |
+
slider_origin = gr.Image(label="Original Image")
|
| 49 |
|
| 50 |
+
tab1 = gr.Interface(
|
| 51 |
+
fn=process_image_via_api,
|
| 52 |
+
inputs=image_upload,
|
| 53 |
+
outputs=[sliders_processed, slider_origin],
|
| 54 |
+
examples=[chameleon],
|
| 55 |
+
api_name="/image_api"
|
| 56 |
+
)
|
| 57 |
|
| 58 |
+
tab2 = gr.Interface(
|
| 59 |
+
fn=process_url_via_api,
|
| 60 |
+
inputs=url_input,
|
| 61 |
+
outputs=[sliders_processed, slider_origin],
|
| 62 |
+
examples=[url_example],
|
| 63 |
+
api_name="/url_api"
|
| 64 |
+
)
|
| 65 |
|
| 66 |
+
tab3 = gr.Interface(
|
| 67 |
+
fn=process_file_via_api,
|
| 68 |
+
inputs=image_file_upload,
|
| 69 |
+
outputs=output_file,
|
| 70 |
+
examples=[chameleon],
|
| 71 |
+
api_name="/png_api"
|
| 72 |
+
)
|
| 73 |
|
| 74 |
+
demo = gr.TabbedInterface(
|
| 75 |
+
[tab1, tab2, tab3],
|
| 76 |
+
["Image Upload", "URL Input", "File Output"],
|
| 77 |
+
title="Background Removal Tool using API"
|
| 78 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
if __name__ == "__main__":
|
| 81 |
+
demo.launch(show_error=True)
|