Update app.py
Browse files
app.py
CHANGED
@@ -15,36 +15,27 @@ result_path = ""
|
|
15 |
|
16 |
def rename_to_image(input_path, output_dir="Uploads"):
|
17 |
os.makedirs(output_dir, exist_ok=True)
|
18 |
-
# Rename to Image_<random>.png
|
19 |
random_id = os.urandom(4).hex()
|
20 |
output_filename = f"Image_{random_id}.png"
|
21 |
output_path = os.path.join(output_dir, output_filename)
|
22 |
try:
|
23 |
with Image.open(input_path) as img:
|
24 |
img.convert('RGB').save(output_path, 'PNG', quality=100)
|
25 |
-
return output_path
|
26 |
except Exception as e:
|
27 |
-
|
28 |
-
|
29 |
-
def store_person_upload(person_path):
|
30 |
-
if person_path:
|
31 |
-
person_png, error = rename_to_image(person_path)
|
32 |
-
return person_png, error
|
33 |
-
return None, ""
|
34 |
-
|
35 |
-
def store_garment_upload(garment_path):
|
36 |
-
if garment_path:
|
37 |
-
garment_png, error = rename_to_image(garment_path)
|
38 |
-
return garment_png, error
|
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),
|
|
|
|
|
|
|
|
|
45 |
|
46 |
-
person_file = handle_file(
|
47 |
-
garment_file = handle_file(
|
48 |
|
49 |
try:
|
50 |
result = client.predict(
|
@@ -67,9 +58,9 @@ def virtual_tryon(person_path, garment_path, garment_type):
|
|
67 |
with Image.open(input_image_path) as img:
|
68 |
img.convert('RGB').save(output_image_path, 'PNG', quality=95)
|
69 |
result_path = output_image_path
|
70 |
-
return result_path, gr.update(visible=True),
|
71 |
except Exception as e:
|
72 |
-
return None, gr.update(visible=False),
|
73 |
|
74 |
def trigger_download():
|
75 |
return gr.update(value=result_path, visible=True, interactive=True)
|
@@ -77,10 +68,6 @@ def trigger_download():
|
|
77 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
78 |
gr.Markdown("")
|
79 |
error_msg = gr.Markdown("", visible=False)
|
80 |
-
# States to store image paths
|
81 |
-
person_state = gr.State()
|
82 |
-
garment_state = gr.State()
|
83 |
-
|
84 |
with gr.Row():
|
85 |
with gr.Column():
|
86 |
src = gr.Image(sources="upload", type="filepath", label="Foto e personit")
|
@@ -100,24 +87,11 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
100 |
downloadable = gr.File(visible=False)
|
101 |
out = gr.Image(type="filepath", label="Rezultati")
|
102 |
|
103 |
-
# Store renamed images on upload
|
104 |
-
src.upload(
|
105 |
-
store_person_upload,
|
106 |
-
inputs=src,
|
107 |
-
outputs=[person_state, error_msg]
|
108 |
-
)
|
109 |
-
ref.upload(
|
110 |
-
store_garment_upload,
|
111 |
-
inputs=ref,
|
112 |
-
outputs=[garment_state, error_msg]
|
113 |
-
)
|
114 |
-
# Process and show result
|
115 |
btn.click(
|
116 |
virtual_tryon,
|
117 |
-
inputs=[
|
118 |
-
outputs=[out, shkarko_btn,
|
119 |
)
|
120 |
-
# Trigger download
|
121 |
shkarko_btn.click(trigger_download, outputs=downloadable)
|
122 |
|
123 |
demo.launch(
|
|
|
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()
|
19 |
output_filename = f"Image_{random_id}.png"
|
20 |
output_path = os.path.join(output_dir, output_filename)
|
21 |
try:
|
22 |
with Image.open(input_path) as img:
|
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)
|
35 |
+
garment_png = rename_to_image(garment_path)
|
36 |
|
37 |
+
person_file = handle_file(person_png)
|
38 |
+
garment_file = handle_file(garment_png)
|
39 |
|
40 |
try:
|
41 |
result = client.predict(
|
|
|
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)
|
|
|
68 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
69 |
gr.Markdown("")
|
70 |
error_msg = gr.Markdown("", visible=False)
|
|
|
|
|
|
|
|
|
71 |
with gr.Row():
|
72 |
with gr.Column():
|
73 |
src = gr.Image(sources="upload", type="filepath", label="Foto e personit")
|
|
|
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(
|