HAL1993 commited on
Commit
849b31b
·
verified ·
1 Parent(s): 19f25a7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -9
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import os
2
  from PIL import Image
3
- from gradio_client import Client, handle_file
4
  import gradio as gr
 
5
 
6
  HF_TOKEN = os.getenv("HF_TOKEN")
7
 
@@ -13,7 +13,7 @@ client = Client(
13
  # Variable to hold result path
14
  result_path = ""
15
 
16
- def convert_to_png(input_path, output_dir="uploads"):
17
  # Create output directory if it doesn't exist
18
  os.makedirs(output_dir, exist_ok=True)
19
  # Generate unique output path
@@ -24,13 +24,27 @@ def convert_to_png(input_path, output_dir="uploads"):
24
  img.convert('RGB').save(output_path, 'PNG', quality=100)
25
  return output_path
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  def virtual_tryon(person_path, garment_path, garment_type):
28
  global result_path
29
- # Convert uploaded images to PNG
30
- person_png = convert_to_png(person_path)
31
- garment_png = convert_to_png(garment_path)
32
- person_file = handle_file(person_png)
33
- garment_file = handle_file(garment_png)
34
 
35
  result = client.predict(
36
  person_file,
@@ -50,13 +64,14 @@ def virtual_tryon(person_path, garment_path, garment_type):
50
  with Image.open(input_image_path) as img:
51
  img.convert('RGB').save(output_image_path, 'JPEG', quality=95)
52
  result_path = output_image_path
53
- return result_path, gr.update(visible=True), gr.update(visible=False)
54
 
55
  def trigger_download():
56
  return gr.update(value=result_path, visible=True, interactive=True)
57
 
58
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
59
  gr.Markdown("")
 
60
  with gr.Row():
61
  with gr.Column():
62
  src = gr.Image(sources="upload", type="filepath", label="Foto e personit")
@@ -76,7 +91,12 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
76
  downloadable = gr.File(visible=False)
77
  out = gr.Image(type="filepath", label="Rezultati")
78
 
79
- btn.click(virtual_tryon, [src, ref, garment_type], [out, shkarko_btn, downloadable])
 
 
 
 
 
80
  shkarko_btn.click(trigger_download, outputs=downloadable)
81
 
82
  demo.launch(
 
1
  import os
2
  from PIL import Image
 
3
  import gradio as gr
4
+ from gradio_client import Client, handle_file
5
 
6
  HF_TOKEN = os.getenv("HF_TOKEN")
7
 
 
13
  # Variable to hold result path
14
  result_path = ""
15
 
16
+ def convert_to_png(input_path, output_dir="Uploads"):
17
  # Create output directory if it doesn't exist
18
  os.makedirs(output_dir, exist_ok=True)
19
  # Generate unique output path
 
24
  img.convert('RGB').save(output_path, 'PNG', quality=100)
25
  return output_path
26
 
27
+ def handle_person_upload(person_path):
28
+ if person_path:
29
+ # Convert uploaded person image to PNG
30
+ person_png = convert_to_png(person_path)
31
+ return person_png
32
+ return None
33
+
34
+ def handle_garment_upload(garment_path):
35
+ if garment_path:
36
+ # Convert uploaded garment image to PNG
37
+ garment_png = convert_to_png(garment_path)
38
+ return garment_png
39
+ return None
40
+
41
  def virtual_tryon(person_path, garment_path, garment_type):
42
  global result_path
43
+ if not person_path or not garment_path:
44
+ return None, gr.update(visible=False), gr.update(visible=True), "Ju lutem ngarkoni të dyja imazhet."
45
+
46
+ person_file = handle_file(person_path)
47
+ garment_file = handle_file(garment_path)
48
 
49
  result = client.predict(
50
  person_file,
 
64
  with Image.open(input_image_path) as img:
65
  img.convert('RGB').save(output_image_path, 'JPEG', quality=95)
66
  result_path = output_image_path
67
+ return result_path, gr.update(visible=True), gr.update(visible=False), ""
68
 
69
  def trigger_download():
70
  return gr.update(value=result_path, visible=True, interactive=True)
71
 
72
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
73
  gr.Markdown("")
74
+ error_msg = gr.Markdown("", visible=False)
75
  with gr.Row():
76
  with gr.Column():
77
  src = gr.Image(sources="upload", type="filepath", label="Foto e personit")
 
91
  downloadable = gr.File(visible=False)
92
  out = gr.Image(type="filepath", label="Rezultati")
93
 
94
+ # Convert uploads to PNG immediately
95
+ src.change(handle_person_upload, inputs=src, outputs=src)
96
+ ref.change(handle_garment_upload, inputs=ref, outputs=ref)
97
+ # Process and show result
98
+ btn.click(virtual_tryon, [src, ref, garment_type], [out, shkarko_btn, downloadable, error_msg])
99
+ # Trigger download
100
  shkarko_btn.click(trigger_download, outputs=downloadable)
101
 
102
  demo.launch(