Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,59 +3,48 @@ import requests
|
|
| 3 |
from PIL import Image
|
| 4 |
from io import BytesIO
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
# -------------------------
|
| 9 |
-
BACKEND_URL = "http://10.10.11.115:5000/run_vton" # your friend's laptop URL
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
def vton_interface(person_image, cloth_image):
|
| 15 |
-
try:
|
| 16 |
-
# Convert PIL images to bytes
|
| 17 |
-
person_bytes = BytesIO()
|
| 18 |
-
person_image.save(person_bytes, format="PNG")
|
| 19 |
-
person_bytes = person_bytes.getvalue()
|
| 20 |
-
|
| 21 |
-
cloth_bytes = BytesIO()
|
| 22 |
-
cloth_image.save(cloth_bytes, format="PNG")
|
| 23 |
-
cloth_bytes = cloth_bytes.getvalue()
|
| 24 |
-
|
| 25 |
-
# Send POST request to backend server
|
| 26 |
-
response = requests.post(
|
| 27 |
-
BACKEND_URL,
|
| 28 |
-
files={
|
| 29 |
-
"person_image": ("person.png", person_bytes, "image/png"),
|
| 30 |
-
"cloth_image": ("cloth.png", cloth_bytes, "image/png")
|
| 31 |
-
},
|
| 32 |
-
)
|
| 33 |
-
|
| 34 |
-
# Check response
|
| 35 |
-
if response.status_code != 200:
|
| 36 |
-
return None
|
| 37 |
-
|
| 38 |
-
# Convert response bytes to image
|
| 39 |
-
result_image = Image.open(BytesIO(response.content))
|
| 40 |
-
return result_image
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
except Exception as e:
|
| 43 |
-
print("[ERROR]
|
| 44 |
return None
|
| 45 |
|
| 46 |
-
#
|
| 47 |
-
|
| 48 |
-
# -
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
)
|
| 59 |
|
| 60 |
if __name__ == "__main__":
|
| 61 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
from io import BytesIO
|
| 5 |
|
| 6 |
+
# Backend server URL (your friend’s laptop)
|
| 7 |
+
BACKEND_URL = "http://10.10.11.115:5000/run_vton"
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
def vton_via_backend(person_img, garment_img):
|
| 10 |
+
if person_img is None or garment_img is None:
|
| 11 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# Convert PIL images to bytes
|
| 14 |
+
person_bytes = BytesIO()
|
| 15 |
+
garment_bytes = BytesIO()
|
| 16 |
+
person_img.save(person_bytes, format="PNG")
|
| 17 |
+
garment_img.save(garment_bytes, format="PNG")
|
| 18 |
+
person_bytes.seek(0)
|
| 19 |
+
garment_bytes.seek(0)
|
| 20 |
+
|
| 21 |
+
# POST request to backend
|
| 22 |
+
files = {
|
| 23 |
+
"person": ("person.png", person_bytes, "image/png"),
|
| 24 |
+
"garment": ("garment.png", garment_bytes, "image/png")
|
| 25 |
+
}
|
| 26 |
+
try:
|
| 27 |
+
response = requests.post(BACKEND_URL, files=files)
|
| 28 |
+
response.raise_for_status() # Raise error if bad status
|
| 29 |
+
result_img = Image.open(BytesIO(response.content))
|
| 30 |
+
return result_img
|
| 31 |
except Exception as e:
|
| 32 |
+
print("[ERROR] Backend request failed:", e)
|
| 33 |
return None
|
| 34 |
|
| 35 |
+
# Gradio interface
|
| 36 |
+
with gr.Blocks() as demo:
|
| 37 |
+
gr.Markdown("## 👕 Virtual Try-On via Remote Backend")
|
| 38 |
+
with gr.Row():
|
| 39 |
+
person_input = gr.Image(type="pil", label="Upload Person Image")
|
| 40 |
+
garment_input = gr.Image(type="pil", label="Upload Garment Image")
|
| 41 |
+
output = gr.Image(type="pil", label="Result")
|
| 42 |
+
run_btn = gr.Button("Run Virtual Try-On")
|
| 43 |
+
run_btn.click(
|
| 44 |
+
fn=vton_via_backend,
|
| 45 |
+
inputs=[person_input, garment_input],
|
| 46 |
+
outputs=output
|
| 47 |
+
)
|
| 48 |
|
| 49 |
if __name__ == "__main__":
|
| 50 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|