Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,44 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import requests
|
| 3 |
-
import io
|
| 4 |
-
from PIL import Image
|
| 5 |
-
|
| 6 |
-
def remove_background(image):
|
| 7 |
-
try:
|
| 8 |
-
# Convert image to bytes
|
| 9 |
-
img_bytes = io.BytesIO()
|
| 10 |
-
image.save(img_bytes, format="PNG")
|
| 11 |
-
img_bytes = img_bytes.getvalue()
|
| 12 |
-
|
| 13 |
-
# Call your local API (will change to HF URL later)
|
| 14 |
-
response = requests.post(
|
| 15 |
-
"http://localhost:8001/api/v1/background-removal/remove-background",
|
| 16 |
-
files={"file": ("image.png", img_bytes, "image/png")}
|
| 17 |
-
)
|
| 18 |
-
|
| 19 |
-
if response.status_code == 200:
|
| 20 |
-
# Get the response and return image
|
| 21 |
-
result_data = response.json()
|
| 22 |
-
download_url = result_data["download_url"]
|
| 23 |
-
|
| 24 |
-
# Download the processed image
|
| 25 |
-
result_response = requests.get(f"http://localhost:8001{download_url}")
|
| 26 |
-
result_bytes = io.BytesIO(result_response.content)
|
| 27 |
-
return Image.open(result_bytes)
|
| 28 |
-
else:
|
| 29 |
-
raise Exception(f"API call failed: {response.status_code}")
|
| 30 |
-
|
| 31 |
-
except Exception as e:
|
| 32 |
-
raise gr.Error(f"Background removal failed: {str(e)}")
|
| 33 |
-
|
| 34 |
-
# Create Gradio interface
|
| 35 |
-
demo = gr.Interface(
|
| 36 |
-
fn=remove_background,
|
| 37 |
-
inputs=gr.Image(type="pil", label="📷 Upload Image"),
|
| 38 |
-
outputs=gr.Image(type="pil", label="🎨 Background Removed"),
|
| 39 |
-
title="🎨 Professional Background Remover",
|
| 40 |
-
description="Upload any image (JPG, PNG, etc) to remove background automatically with AI"
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
if __name__ == "__main__":
|
| 45 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import io
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
def remove_background(image):
|
| 7 |
+
try:
|
| 8 |
+
# Convert image to bytes
|
| 9 |
+
img_bytes = io.BytesIO()
|
| 10 |
+
image.save(img_bytes, format="PNG")
|
| 11 |
+
img_bytes = img_bytes.getvalue()
|
| 12 |
+
|
| 13 |
+
# Call your local API (will change to HF URL later)
|
| 14 |
+
response = requests.post(
|
| 15 |
+
"http://localhost:8001/api/v1/background-removal/remove-background",
|
| 16 |
+
files={"file": ("image.png", img_bytes, "image/png")}
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
if response.status_code == 200:
|
| 20 |
+
# Get the response and return image
|
| 21 |
+
result_data = response.json()
|
| 22 |
+
download_url = result_data["download_url"]
|
| 23 |
+
|
| 24 |
+
# Download the processed image
|
| 25 |
+
result_response = requests.get(f"http://localhost:8001{download_url}")
|
| 26 |
+
result_bytes = io.BytesIO(result_response.content)
|
| 27 |
+
return Image.open(result_bytes)
|
| 28 |
+
else:
|
| 29 |
+
raise Exception(f"API call failed: {response.status_code}")
|
| 30 |
+
|
| 31 |
+
except Exception as e:
|
| 32 |
+
raise gr.Error(f"Background removal failed: {str(e)}")
|
| 33 |
+
|
| 34 |
+
# Create Gradio interface
|
| 35 |
+
demo = gr.Interface(
|
| 36 |
+
fn=remove_background,
|
| 37 |
+
inputs=gr.Image(type="pil", label="📷 Upload Image"),
|
| 38 |
+
outputs=gr.Image(type="pil", label="🎨 Background Removed"),
|
| 39 |
+
title="🎨 Professional Background Remover",
|
| 40 |
+
description="Upload any image (JPG, PNG, etc) to remove background automatically with AI"
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
if __name__ == "__main__":
|
|
|
|
| 44 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|