Rooni commited on
Commit
20379ba
·
verified ·
1 Parent(s): 0807e33

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -30
app.py CHANGED
@@ -2,9 +2,10 @@ import gradio as gr
2
  from gradio_client import Client, handle_file
3
  from PIL import Image
4
  import requests
5
- import io
 
6
 
7
- # Initialize the client
8
  client = Client("not-lain/background-removal")
9
 
10
  def process_image_via_api(image):
@@ -14,7 +15,11 @@ def process_image_via_api(image):
14
  )
15
  # Convert the output tuple to PIL images and return
16
  if result:
17
- return (Image.open(result[0]), Image.open(result[1]))
 
 
 
 
18
  return None, None
19
 
20
  def process_url_via_api(url):
@@ -24,7 +29,11 @@ def process_url_via_api(url):
24
  )
25
  # Convert the output tuple to PIL images and return
26
  if result:
27
- return (Image.open(result[0]), Image.open(result[1]))
 
 
 
 
28
  return None, None
29
 
30
  def process_file_via_api(f):
@@ -32,61 +41,55 @@ def process_file_via_api(f):
32
  f=handle_file(f),
33
  api_name="/png"
34
  )
35
- # Ensure the result is a valid file path
36
- if isinstance(result, str):
37
  return result
38
- elif isinstance(result, bytes):
39
- # If the result is bytes, convert to image and save to a file
40
- image = Image.open(io.BytesIO(result))
41
- output_path = "output.png"
42
- image.save(output_path)
43
- return output_path
44
  return None
45
 
46
- # Example images
47
  chameleon = "butterfly.jpg"
48
  url_example = "https://hips.hearstapps.com/hmg-prod/images/gettyimages-1229892983-square.jpg"
49
 
50
  # Tab 1: Image Upload
51
- sliders_processed_tab1 = gr.Image(label="Processed Image")
52
- sliders_origin_tab1 = gr.Image(label="Original Image")
53
- image_upload_tab1 = gr.Image(label="Upload an image")
54
  tab1 = gr.Interface(
55
  fn=process_image_via_api,
56
- inputs=image_upload_tab1,
57
- outputs=[sliders_processed_tab1, sliders_origin_tab1],
58
  examples=[chameleon],
59
  api_name="/image_api"
60
  )
61
 
62
  # Tab 2: URL Input
63
- sliders_processed_tab2 = gr.Image(label="Processed Image")
64
- sliders_origin_tab2 = gr.Image(label="Original Image")
65
- url_input_tab2 = gr.Textbox(label="Paste an image URL")
66
  tab2 = gr.Interface(
67
  fn=process_url_via_api,
68
- inputs=url_input_tab2,
69
- outputs=[sliders_processed_tab2, sliders_origin_tab2],
70
  examples=[url_example],
71
  api_name="/url_api"
72
  )
73
 
74
  # Tab 3: File Output
75
- output_file_tab3 = gr.File(label="Output PNG File")
76
- image_file_upload_tab3 = gr.Image(label="Upload an image", type="filepath")
77
  tab3 = gr.Interface(
78
  fn=process_file_via_api,
79
- inputs=image_file_upload_tab3,
80
- outputs=output_file_tab3,
81
- examples=[chameleon],
82
  api_name="/png_api"
83
  )
84
 
85
- # Create the Tabbed Interface
86
  demo = gr.TabbedInterface(
87
  [tab1, tab2, tab3],
88
  ["Image Upload", "URL Input", "File Output"],
89
- title="Background Removal Tool using API"
90
  )
91
 
92
  if __name__ == "__main__":
 
2
  from gradio_client import Client, handle_file
3
  from PIL import Image
4
  import requests
5
+ import tempfile
6
+ import os
7
 
8
+ # Инициализируем клиент
9
  client = Client("not-lain/background-removal")
10
 
11
  def process_image_via_api(image):
 
15
  )
16
  # Convert the output tuple to PIL images and return
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
+ return (processed_image, origin_image)
23
  return None, None
24
 
25
  def process_url_via_api(url):
 
29
  )
30
  # Convert the output tuple to PIL images and return
31
  if result:
32
+ processed_image_path = result[0]
33
+ origin_image_path = result[1]
34
+ processed_image = Image.open(processed_image_path)
35
+ origin_image = Image.open(origin_image_path)
36
+ return (processed_image, origin_image)
37
  return None, None
38
 
39
  def process_file_via_api(f):
 
41
  f=handle_file(f),
42
  api_name="/png"
43
  )
44
+ # Return the path to the saved PNG file
45
+ if result:
46
  return result
 
 
 
 
 
 
47
  return None
48
 
49
+ # Пример изображений
50
  chameleon = "butterfly.jpg"
51
  url_example = "https://hips.hearstapps.com/hmg-prod/images/gettyimages-1229892983-square.jpg"
52
 
53
  # Tab 1: Image Upload
54
+ slider1_processed = ImageSlider(label="Processed Image", type="pil")
55
+ slider1_origin = ImageSlider(label="Original Image", type="pil")
56
+ image_upload = gr.Image(label="Upload an image")
57
  tab1 = gr.Interface(
58
  fn=process_image_via_api,
59
+ inputs=image_upload,
60
+ outputs=[slider1_processed, slider1_origin],
61
  examples=[chameleon],
62
  api_name="/image_api"
63
  )
64
 
65
  # Tab 2: URL Input
66
+ slider2_processed = ImageSlider(label="Processed Image", type="pil")
67
+ slider2_origin = ImageSlider(label="Original Image", type="pil")
68
+ url_input = gr.Textbox(label="Paste an image URL")
69
  tab2 = gr.Interface(
70
  fn=process_url_via_api,
71
+ inputs=url_input,
72
+ outputs=[slider2_processed, slider2_origin],
73
  examples=[url_example],
74
  api_name="/url_api"
75
  )
76
 
77
  # Tab 3: File Output
78
+ output_file = gr.File(label="Output PNG File")
79
+ image_file_upload = gr.Image(label="Upload an image", type="filepath")
80
  tab3 = gr.Interface(
81
  fn=process_file_via_api,
82
+ inputs=image_file_upload,
83
+ outputs=output_file,
84
+ examples=["butterfly.jpg"],
85
  api_name="/png_api"
86
  )
87
 
88
+ # Создаем интерфейс с вкладками
89
  demo = gr.TabbedInterface(
90
  [tab1, tab2, tab3],
91
  ["Image Upload", "URL Input", "File Output"],
92
+ title="Background Removal Tool"
93
  )
94
 
95
  if __name__ == "__main__":