HAL1993 commited on
Commit
d785d43
·
verified ·
1 Parent(s): b24d120

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -19
app.py CHANGED
@@ -10,9 +10,6 @@ client = Client(
10
  hf_token=HF_TOKEN,
11
  )
12
 
13
- # Variable to hold result path
14
- result_path = ""
15
-
16
  def rename_to_image(input_path, output_dir="Uploads"):
17
  os.makedirs(output_dir, exist_ok=True)
18
  random_id = os.urandom(4).hex()
@@ -23,12 +20,11 @@ def rename_to_image(input_path, output_dir="Uploads"):
23
  img.convert('RGB').save(output_path, 'PNG', quality=100)
24
  return output_path
25
  except Exception as e:
26
- raise ValueError(f"Error renaming image: {str(e)}")
27
 
28
  def virtual_tryon(person_path, garment_path, garment_type):
29
- global result_path
30
  if not person_path or not garment_path:
31
- return None, gr.update(visible=False), "Ju lutem ngarkoni të dyja imazhet."
32
 
33
  # Rename uploads to Image_<random>.png
34
  person_png = rename_to_image(person_path)
@@ -43,27 +39,23 @@ def virtual_tryon(person_path, garment_path, garment_type):
43
  garment_file,
44
  ref_acceleration=False,
45
  step=40,
46
- scale=2.5,
47
  seed=42,
48
  vt_model_type="viton_hd",
49
  vt_garment_type=garment_type,
50
  vt_repaint=False,
51
  api_name="/leffa_predict_vt",
52
  )
53
- # Rename result to medyfytyra_<random>.png
54
  input_image_path = result[0]
55
  random_id = os.urandom(4).hex()
56
- output_image_path = os.path.join("Outputs", f"medyfytyra_{random_id}.png")
57
  os.makedirs("Outputs", exist_ok=True)
58
  with Image.open(input_image_path) as img:
59
  img.convert('RGB').save(output_image_path, 'PNG', quality=95)
60
- result_path = output_image_path
61
- return result_path, gr.update(visible=True), ""
62
  except Exception as e:
63
- return None, gr.update(visible=False), f"Error processing: {str(e)}"
64
-
65
- def trigger_download():
66
- return gr.update(value=result_path, visible=True, interactive=True)
67
 
68
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
69
  gr.Markdown("")
@@ -83,16 +75,13 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
83
  )
84
  with gr.Column():
85
  btn = gr.Button("VISHU")
86
- shkarko_btn = gr.Button("SHKARKO", visible=False)
87
- downloadable = gr.File(visible=False)
88
  out = gr.Image(type="filepath", label="Rezultati")
89
 
90
  btn.click(
91
  virtual_tryon,
92
  inputs=[src, ref, garment_type],
93
- outputs=[out, shkarko_btn, error_msg]
94
  )
95
- shkarko_btn.click(trigger_download, outputs=downloadable)
96
 
97
  demo.launch(
98
  share=True,
 
10
  hf_token=HF_TOKEN,
11
  )
12
 
 
 
 
13
  def rename_to_image(input_path, output_dir="Uploads"):
14
  os.makedirs(output_dir, exist_ok=True)
15
  random_id = os.urandom(4).hex()
 
20
  img.convert('RGB').save(output_path, 'PNG', quality=100)
21
  return output_path
22
  except Exception as e:
23
+ raise ValueError(f"Error renaming: {str(e)}")
24
 
25
  def virtual_tryon(person_path, garment_path, garment_type):
 
26
  if not person_path or not garment_path:
27
+ return None, "Ju lutem ngarkoni të dyja imazhet."
28
 
29
  # Rename uploads to Image_<random>.png
30
  person_png = rename_to_image(person_path)
 
39
  garment_file,
40
  ref_acceleration=False,
41
  step=40,
42
+ scale=2.5, # Corrected to 2.5 (was 42 in last version)
43
  seed=42,
44
  vt_model_type="viton_hd",
45
  vt_garment_type=garment_type,
46
  vt_repaint=False,
47
  api_name="/leffa_predict_vt",
48
  )
49
+ # Rename result to uniqueproc_<random>.png
50
  input_image_path = result[0]
51
  random_id = os.urandom(4).hex()
52
+ output_image_path = os.path.join("Outputs", f"uniqueproc_{random_id}.png")
53
  os.makedirs("Outputs", exist_ok=True)
54
  with Image.open(input_image_path) as img:
55
  img.convert('RGB').save(output_image_path, 'PNG', quality=95)
56
+ return output_image_path, ""
 
57
  except Exception as e:
58
+ return None, f"Error processing: {str(e)}"
 
 
 
59
 
60
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
61
  gr.Markdown("")
 
75
  )
76
  with gr.Column():
77
  btn = gr.Button("VISHU")
 
 
78
  out = gr.Image(type="filepath", label="Rezultati")
79
 
80
  btn.click(
81
  virtual_tryon,
82
  inputs=[src, ref, garment_type],
83
+ outputs=[out, error_msg]
84
  )
 
85
 
86
  demo.launch(
87
  share=True,