Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import io
|
| 3 |
+
# Workaround for PIL/Gradio bug :contentReference[oaicite:13]{index=13}
|
| 4 |
+
import PIL.Image
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from gradio_client import Client, handle_file
|
| 7 |
+
|
| 8 |
+
from gradio_client.client import re
|
| 9 |
+
from numpy import array
|
| 10 |
+
# 1. Load your HF token from env
|
| 11 |
+
HF_TOKEN = os.getenv("HF_TOKEN") # export HF_TOKEN="hf_..."
|
| 12 |
+
# 1) Connect to the Leffa Gradio app’s predict endpoint
|
| 13 |
+
# Use the full "/call/predict" API path as shown on the View API page
|
| 14 |
+
client = Client(
|
| 15 |
+
"franciszzj/Leffa",
|
| 16 |
+
hf_token=HF_TOKEN,
|
| 17 |
+
) # Gradio Python client
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def virtual_tryon(
|
| 21 |
+
person_path,
|
| 22 |
+
garment_path,
|
| 23 |
+
garment_type,
|
| 24 |
+
):
|
| 25 |
+
# 2) Wrap file inputs so Gradio client uploads them correctly
|
| 26 |
+
person_file = handle_file(
|
| 27 |
+
person_path
|
| 28 |
+
) # handle_file uploads the image :contentReference[oaicite:6]{index=6}
|
| 29 |
+
garment_file = handle_file(garment_path)
|
| 30 |
+
|
| 31 |
+
# 3) Build inputs in the exact order shown on the “Use via API” page :contentReference[oaicite:7]{index=7}
|
| 32 |
+
|
| 33 |
+
# 4) Call the named endpoint with handle_file inputs
|
| 34 |
+
result = client.predict(
|
| 35 |
+
person_file, # Person Image
|
| 36 |
+
garment_file, # Garment Image
|
| 37 |
+
ref_acceleration=False,
|
| 38 |
+
step=30,
|
| 39 |
+
scale=2.5,
|
| 40 |
+
seed=42,
|
| 41 |
+
vt_model_type="viton_hd",
|
| 42 |
+
vt_garment_type=garment_type,
|
| 43 |
+
vt_repaint=False,
|
| 44 |
+
api_name="/leffa_predict_vt",)
|
| 45 |
+
# result[0] is the generated image filepath on the server
|
| 46 |
+
return result[0] # Gradio will download & display this file
|
| 47 |
+
|
| 48 |
+
# 5) Gradio UI
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 52 |
+
gr.Markdown("## V_TRY DEMO")
|
| 53 |
+
with gr.Row():
|
| 54 |
+
with gr.Column():
|
| 55 |
+
gr.Markdown("####UPLOAD PERSON IMAGE")
|
| 56 |
+
src = gr.Image(sources="upload", type="filepath",
|
| 57 |
+
label="Person Image")
|
| 58 |
+
with gr.Column():
|
| 59 |
+
gr.Markdown("####UPLOAD GARMENT IMAGE")
|
| 60 |
+
ref = gr.Image(sources="upload", type="filepath",
|
| 61 |
+
label="Garment Image")
|
| 62 |
+
with gr.Column():
|
| 63 |
+
gr.Markdown("####Select the Garment type")
|
| 64 |
+
garment_type = gr.Radio(
|
| 65 |
+
choices=[("Upper", "upper_body"),
|
| 66 |
+
("Lower", "lower_body"), ("Dress", "dresses")],
|
| 67 |
+
value="upper_body",
|
| 68 |
+
label="Garment Type",
|
| 69 |
+
)
|
| 70 |
+
with gr.Column():
|
| 71 |
+
gr.Markdown("####Output Image")
|
| 72 |
+
out = gr.Image(type="filepath", label="Result",)
|
| 73 |
+
with gr.Row():
|
| 74 |
+
btn = gr.Button("Generate")
|
| 75 |
+
|
| 76 |
+
btn.click(virtual_tryon, [src, ref, garment_type], out)
|
| 77 |
+
|
| 78 |
+
demo.launch(
|
| 79 |
+
share=True,
|
| 80 |
+
show_error=True,
|
| 81 |
+
pwa=True,
|
| 82 |
+
)
|