Spaces:
Runtime error
Runtime error
Update backend_server.py
Browse files- backend_server.py +64 -0
backend_server.py
CHANGED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import io
|
| 5 |
+
|
| 6 |
+
# Replace with your friend's laptop backend URL (use ngrok if needed)
|
| 7 |
+
BACKEND_URL = "http://<FRIEND_LAPTOP_IP>:5000/run_vton"
|
| 8 |
+
|
| 9 |
+
def virtual_try_on_frontend(person_img, cloth_img):
|
| 10 |
+
"""
|
| 11 |
+
Sends person and cloth images to the backend server and returns the try-on result.
|
| 12 |
+
"""
|
| 13 |
+
if person_img is None or cloth_img is None:
|
| 14 |
+
return None
|
| 15 |
+
|
| 16 |
+
# Convert PIL Images to bytes
|
| 17 |
+
person_bytes = io.BytesIO()
|
| 18 |
+
person_img.save(person_bytes, format="PNG")
|
| 19 |
+
person_bytes = person_bytes.getvalue()
|
| 20 |
+
|
| 21 |
+
cloth_bytes = io.BytesIO()
|
| 22 |
+
cloth_img.save(cloth_bytes, format="PNG")
|
| 23 |
+
cloth_bytes = cloth_bytes.getvalue()
|
| 24 |
+
|
| 25 |
+
try:
|
| 26 |
+
# Send POST request with images as files
|
| 27 |
+
response = requests.post(
|
| 28 |
+
BACKEND_URL,
|
| 29 |
+
files={
|
| 30 |
+
"person": ("person.png", person_bytes, "image/png"),
|
| 31 |
+
"cloth": ("cloth.png", cloth_bytes, "image/png")
|
| 32 |
+
},
|
| 33 |
+
timeout=60
|
| 34 |
+
)
|
| 35 |
+
response.raise_for_status()
|
| 36 |
+
|
| 37 |
+
# Convert returned bytes to PIL image
|
| 38 |
+
result_bytes = io.BytesIO(response.content)
|
| 39 |
+
result_img = Image.open(result_bytes).convert("RGB")
|
| 40 |
+
return result_img
|
| 41 |
+
|
| 42 |
+
except Exception as e:
|
| 43 |
+
print("[ERROR] Backend request failed:", e)
|
| 44 |
+
return None
|
| 45 |
+
|
| 46 |
+
# Gradio interface
|
| 47 |
+
with gr.Blocks() as demo:
|
| 48 |
+
gr.Markdown("## 👕 Virtual Try-On Demo (Frontend → Backend)")
|
| 49 |
+
|
| 50 |
+
with gr.Row():
|
| 51 |
+
person_input = gr.Image(type="pil", label="Upload Person Image")
|
| 52 |
+
cloth_input = gr.Image(type="pil", label="Upload Garment")
|
| 53 |
+
|
| 54 |
+
output = gr.Image(type="pil", label="Result")
|
| 55 |
+
|
| 56 |
+
run_btn = gr.Button("Run Virtual Try-On")
|
| 57 |
+
run_btn.click(
|
| 58 |
+
fn=virtual_try_on_frontend,
|
| 59 |
+
inputs=[person_input, cloth_input],
|
| 60 |
+
outputs=output
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
if __name__ == "__main__":
|
| 64 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|