Rooni commited on
Commit
737f1ab
·
verified ·
1 Parent(s): 1fae4f7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -29
app.py CHANGED
@@ -1,42 +1,81 @@
1
  import gradio as gr
2
- from PIL import Image # Needed create blank "proxy image in the functions below
 
 
3
 
 
 
4
 
5
- def dummy_process_image(image):
6
- """Заглушка для обработки изображения. Возвращает пустое изображение."""
7
- return Image.new("RGB", (256, 256), "white"), Image.new("RGB", (256, 256), "black")
 
 
 
 
 
 
8
 
 
 
 
 
 
 
 
 
 
9
 
10
- def dummy_process_url(url):
 
 
 
 
 
 
 
 
11
 
12
- """Заглушка для обработки URL. Возвращает пустое изображение."""
13
- return Image.new("RGB", (256, 256), "white"), Image.new("RGB", (256, 256), "black")
 
14
 
15
- def dummy_process_file(filepath):
16
- """Заглушка для обработки файла. Возвращает имя фиктивного файла."""
17
- return "dummy_output.png"
 
 
 
18
 
 
 
 
 
 
 
 
19
 
20
- with gr.Blocks() as demo:
21
- with gr.Tab("Image Upload"):
22
- with gr.Row():
23
- image_upload = gr.Image(label="Upload an image")
24
- output_image = gr.Image(label="Processed Image", type="pil")
25
- image_upload.change(dummy_process_image, image_upload, output_image)
 
26
 
 
 
 
 
 
 
 
27
 
28
- with gr.Tab("URL Input"):
29
- with gr.Row():
30
- url_input = gr.Textbox(label="Paste an image URL")
31
- output_url = gr.Image(label="Processed Image from URL", type="pil")
32
- url_input.submit(dummy_process_url, url_input, output_url) # Or "submit" is more appropriate for text inputs and not change.
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)