lucky0146 commited on
Commit
e2a118e
·
verified ·
1 Parent(s): ad7a573

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +141 -2
app.py CHANGED
@@ -1,4 +1,73 @@
1
- import spaces
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import gradio as gr
3
  from transparent_background import Remover
4
  from PIL import Image
@@ -27,7 +96,7 @@ def remove_background(image):
27
  # 界面设置
28
  iface = gr.Interface(
29
  fn=remove_background,
30
- api_name="remove_background",
31
  analytics_enabled=False,
32
  inputs=gr.Image(label="Upload Image"),
33
  outputs=gr.Image(label="Output Image", type="filepath"),
@@ -50,3 +119,73 @@ iface = gr.Interface(
50
 
51
  if __name__ == "__main__":
52
  iface.launch(show_error=True, show_api=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Today 08:49
2
+
3
+ Avatar
4
+ This is my hf space, tell me which file I've to modify in order to use this space via api: https://huggingface.co/spaces/innoai/Background-Remover
5
+ Looking at your Hugging Face Space for Background Remover, to use it via API, you'll need to modify the app.py file (or whichever file contains your main Gradio interface).
6
+
7
+ Here's what you need to do:
8
+
9
+ Find your main application file - This is typically app.py where your Gradio interface is defined.
10
+
11
+ Add API configuration - You'll need to modify the gr.Interface.launch() or gr.Blocks.launch() call to enable the API:
12
+
13
+ Access the API documentation - Once you redeploy with show_api=True, you can access the API documentation at:
14
+
15
+ https://huggingface.co/spaces/innoai/Background-Remover/api/docs
16
+ API endpoints - Your space will automatically generate REST API endpoints for each function in your Gradio interface. You can then make HTTP POST requests to these endpoints.
17
+
18
+ The key change is adding the show_api=True parameter to your launch method in the main application file. This will expose your Gradio interface as a REST API that you can call programmatically.
19
+
20
+ Would you like me to help you with the specific API request format once you've made this change?
21
+
22
+ # Instead of just:
23
+ demo.launch()
24
+
25
+ # Use:
26
+ demo.launch(show_api=True)
27
+ I understand you want to modify your Hugging Face Space to use it via API. Let me get the specific details from your space to give you precise instructions.
28
+
29
+ Extract Webpage Text
30
+ Let me check the files in your space to see the current structure:
31
+
32
+ Extract Webpage Text
33
+ Now let me check the current app.py file to see how it's structured:
34
+
35
+ Extract Webpage Text
36
+ Perfect! I can see your current app.py file. To enable API access, you need to modify line 58 in your app.py file.
37
+
38
+ Current code:
39
+
40
+ iface.launch(show_error=True, show_api=False)
41
+ Change it to:
42
+
43
+ iface.launch(show_error=True, show_api=True)
44
+ That's it! Just change show_api=False to show_api=True on the last line.
45
+
46
+ After you make this change and the space rebuilds, you'll be able to access your API at:
47
+
48
+ API docs: https://huggingface.co/spaces/innoai/Background-Remover/api/docs
49
+ API endpoint: https://huggingface.co/spaces/innoai/Background-Remover/api/predict
50
+ The API will accept POST requests with your image data and return the processed image with transparent background.
51
+
52
+ Avatar
53
+ I did but I got this: No API Routes found for https://lucky0146-background-remover-medium.hf.space/
54
+
55
+ To expose an API endpoint of your app in this page, set the api_name parameter of the event listener.
56
+ For more information, visit the API Page guide . To hide the API documentation button and this page, set show_api=False in the Blocks.launch() method.
57
+ Start Computer
58
+ Computer started
59
+ I see the issue! The problem is that you have api_name=False in your gr.Interface definition, which prevents the API endpoint from being created. You need to modify two places in your app.py file:
60
+
61
+ Let me help you navigate to your Hugging Face space to make the necessary changes.
62
+
63
+ Navigate to the Hugging Face space to edit the app.py file
64
+ Request user to log in to Hugging Face
65
+ Use Computer
66
+ Click anywhere to take control
67
+ Please log in to your Hugging Face account so I can help you edit the app.py file. Enter your username/email and password in the login form.
68
+
69
+ Avatar
70
+ Here's my app.py : import spaces
71
  import gradio as gr
72
  from transparent_background import Remover
73
  from PIL import Image
 
96
  # 界面设置
97
  iface = gr.Interface(
98
  fn=remove_background,
99
+ api_name=False,
100
  analytics_enabled=False,
101
  inputs=gr.Image(label="Upload Image"),
102
  outputs=gr.Image(label="Output Image", type="filepath"),
 
119
 
120
  if __name__ == "__main__":
121
  iface.launch(show_error=True, show_api=True)
122
+ Perfect! I can see the issue now. You have show_api=True but you also have api_name=False in your gr.Interface definition. This is what's preventing the API endpoint from being created.
123
+
124
+ You need to change line 26 in your code:
125
+
126
+ Current:
127
+
128
+ Change to:
129
+
130
+ So your complete corrected app.py should be:
131
+
132
+ After you make this change and redeploy your space, you'll have:
133
+
134
+ API endpoint: /api/remove_background
135
+ API docs: Available at your space's /api/docs URL
136
+ The key issue was that api_name=False explicitly disables API endpoint creation, even when show_api=True.
137
+
138
+ api_name=False,
139
+ api_name="remove_background",
140
+ import spaces
141
+ import gradio as gr
142
+ from transparent_background import Remover
143
+ from PIL import Image
144
+ import numpy as np
145
+ import io
146
+ import tempfile
147
+
148
+ @spaces.GPU
149
+ def remove_background(image):
150
+ remover = Remover()
151
+ if isinstance(image, Image.Image):
152
+ output = remover.process(image)
153
+ elif isinstance(image, np.ndarray):
154
+ image_pil = Image.fromarray(image)
155
+ output = remover.process(image_pil)
156
+ else:
157
+ raise TypeError("Unsupported image type")
158
+
159
+ # 保存PNG图片到临时文件
160
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
161
+ output.save(temp_file, format="PNG")
162
+ temp_file.close()
163
+
164
+ return temp_file.name
165
+
166
+ # 界面设置
167
+ iface = gr.Interface(
168
+ fn=remove_background,
169
+ api_name="remove_background", # Changed from False to "remove_background"
170
+ analytics_enabled=False,
171
+ inputs=gr.Image(label="Upload Image"),
172
+ outputs=gr.Image(label="Output Image", type="filepath"),
173
+ title="AI Background Remover - Automatically Remove Image Backgrounds",
174
+ description="Upload an image and our AI-powered background remover will automatically make the background transparent. Perfect for various needs requiring transparent images.",
175
+ article="""
176
+ ## How to Use
177
+ 1. Click **Upload Image** or drag and drop an image into the upload area.
178
+ 2. Click the **Submit** button and wait for a moment. The processed image will appear in the output area on the right.
179
+ 3. The background of the processed image will be transparent, and the image will be in PNG format for easy use.
180
+
181
+ ### Application Scenarios
182
+ - **E-commerce**: Remove backgrounds from product images to create clean product photos.
183
+ - **Graphic Design**: Quickly get images with transparent backgrounds for composition and design.
184
+ - **Social Media**: Create avatars or icons with transparent backgrounds that blend perfectly on various backgrounds.
185
+
186
+ Our tool leverages the latest AI technology to efficiently and accurately remove image backgrounds, saving you time and effort.
187
+ """
188
+ )
189
+
190
+ if __name__ == "__main__":
191
+ iface.launch(show_error=True, show_api=True)