HAL1993 commited on
Commit
f7e511d
·
verified ·
1 Parent(s): 8b04cff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -39
app.py CHANGED
@@ -1,28 +1,36 @@
1
  import os
2
  import io
 
3
  import PIL.Image
4
  import gradio as gr
5
  from gradio_client import Client, handle_file
 
6
  from gradio_client.client import re
7
  from numpy import array
8
-
9
  # 1. Load your HF token from env
10
  HF_TOKEN = os.getenv("HF_TOKEN") # export HF_TOKEN="hf_..."
 
 
11
  client = Client(
12
  "franciszzj/Leffa",
13
  hf_token=HF_TOKEN,
14
  ) # Gradio Python client
15
 
 
16
  def virtual_tryon(
17
  person_path,
18
  garment_path,
19
  garment_type,
20
  ):
21
- # Wrap file inputs so Gradio client uploads them correctly
22
- person_file = handle_file(person_path)
 
 
23
  garment_file = handle_file(garment_path)
24
 
25
- # Call the named endpoint with handle_file inputs
 
 
26
  result = client.predict(
27
  person_file, # Person Image
28
  garment_file, # Garment Image
@@ -33,50 +41,43 @@ def virtual_tryon(
33
  vt_model_type="viton_hd",
34
  vt_garment_type=garment_type,
35
  vt_repaint=False,
36
- api_name="/leffa_predict_vt",
37
- )
38
  return result[0] # Gradio will download & display this file
39
 
40
- # Password protection
41
- def check_password(pw):
42
- return gr.update(visible=(pw == "supersecretpassword"))
43
 
44
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
45
- gr.Markdown("## VIRTUAL TRY-ON DEMO")
46
-
47
- # Invisible password prompt and access button (minimized to zero size)
48
- with gr.Row(visible=False): # Invisible container
49
- password_box = gr.Textbox(type="password", label="Enter access code", visible=False, elem_id="password_box") # Invisible password field
50
- access_button = gr.Button("Unlock", visible=False, elem_id="access_button") # Invisible button
51
 
52
- # Main UI elements (hidden until password is entered)
53
- with gr.Column(visible=False) as main_ui:
54
- with gr.Row():
55
- with gr.Column():
56
- gr.Markdown("### Upload Person Image")
57
- src = gr.Image(sources="upload", type="filepath", label="Person Image")
58
- with gr.Column():
59
- gr.Markdown("### Upload Garment Image")
60
- ref = gr.Image(sources="upload", type="filepath", label="Garment Image")
61
- with gr.Column():
62
- gr.Markdown("### Select Garment Type")
63
- garment_type = gr.Radio(
64
- choices=[("Upper", "upper_body"), ("Lower", "lower_body"), ("Dress", "dresses")],
65
- value="upper_body",
66
- label="Garment Type",
67
- )
68
- with gr.Column():
69
- gr.Markdown("### Output Image")
70
- out = gr.Image(type="filepath", label="Result")
71
- with gr.Row():
72
- btn = gr.Button("Generate")
 
 
 
73
 
74
  btn.click(virtual_tryon, [src, ref, garment_type], out)
75
 
76
- access_button.click(fn=check_password, inputs=password_box, outputs=main_ui)
77
-
78
  demo.launch(
79
  share=True,
80
  show_error=True,
81
  pwa=True,
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
 
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("")
53
+ with gr.Row():
54
+ with gr.Column():
55
+ gr.Markdown("")
56
+ src = gr.Image(sources="upload", type="filepath",
57
+ label="Foto e personit")
58
+ with gr.Column():
59
+ gr.Markdown("")
60
+ ref = gr.Image(sources="upload", type="filepath",
61
+ label="Foto e veshjes")
62
+ with gr.Column():
63
+ gr.Markdown("")
64
+ garment_type = gr.Radio(
65
+ choices=[("Siper", "upper_body"),
66
+ ("Posht", "lower_body"), ("Komplet", "dresses")],
67
+ value="upper_body",
68
+ label="Lloji i veshjes",
69
+ )
70
+ with gr.Column():
71
+ gr.Markdown("")
72
+ out = gr.Image(type="filepath", label="Rezultati",)
73
+ with gr.Row():
74
+ btn = gr.Button("VISHU")
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
  )
83
+