Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,13 +1,13 @@
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
-
import
|
4 |
-
import json
|
5 |
-
from PIL import Image
|
6 |
-
import tempfile
|
7 |
from themes import IndonesiaTheme # Impor tema custom dari themes.py
|
|
|
|
|
|
|
8 |
|
9 |
-
# Siapkan URL untuk permintaan API
|
10 |
-
|
11 |
|
12 |
# Fungsi untuk menyimpan gambar sementara ke file
|
13 |
def save_temp_image(image_array):
|
@@ -17,64 +17,49 @@ def save_temp_image(image_array):
|
|
17 |
return temp_file.name
|
18 |
|
19 |
# Fungsi untuk memanggil API Virtual Try-On
|
20 |
-
def virtual_tryon(person_img, garment_img, seed, randomize_seed):
|
|
|
|
|
21 |
# Simpan gambar sementara
|
22 |
person_img_path = save_temp_image(person_img)
|
23 |
garment_img_path = save_temp_image(garment_img)
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
"url": f"https://deddy-toko-baju-virtual-gratis.hf.space/file={garment_img_path}",
|
41 |
-
"size": None,
|
42 |
-
"orig_name": os.path.basename(garment_img_path),
|
43 |
-
"mime_type": None,
|
44 |
-
"is_stream": False,
|
45 |
-
"meta": {
|
46 |
-
"_type": "gradio.FileData"
|
47 |
-
}
|
48 |
-
},
|
49 |
-
seed,
|
50 |
-
randomize_seed
|
51 |
-
],
|
52 |
-
"event_data": None,
|
53 |
-
"fn_index": 2, # Indeks API yang sesuai
|
54 |
-
"trigger_id": 26,
|
55 |
-
"session_hash": "l8hba3fqoe8" # Hash session harus diperbarui jika berubah
|
56 |
-
})
|
57 |
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
}
|
62 |
|
63 |
-
#
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
|
|
|
|
|
|
72 |
|
73 |
-
#
|
74 |
-
|
75 |
-
with open(fp, 'r', encoding='utf-8') as f:
|
76 |
-
content = f.read()
|
77 |
-
return content
|
78 |
|
79 |
# Path untuk contoh gambar
|
80 |
example_path = os.path.join(os.path.dirname(__file__), 'assets')
|
@@ -87,6 +72,7 @@ garm_list_path = [os.path.join(example_path, "cloth", garm) for garm in garm_lis
|
|
87 |
human_list = os.listdir(os.path.join(example_path, "human"))
|
88 |
human_list_path = [os.path.join(example_path, "human", human) for human in human_list]
|
89 |
|
|
|
90 |
# CSS untuk styling antarmuka
|
91 |
css = """
|
92 |
#col-left, #col-mid, #col-right {
|
@@ -119,7 +105,7 @@ css = """
|
|
119 |
"""
|
120 |
|
121 |
# Membuat antarmuka Gradio dengan tema IndonesiaTheme
|
122 |
-
with gr.Blocks(css=css, theme=IndonesiaTheme()) as
|
123 |
# Tambahkan banner
|
124 |
gr.HTML("""
|
125 |
<div style='text-align: center;'>
|
@@ -127,7 +113,6 @@ with gr.Blocks(css=css, theme=IndonesiaTheme()) as app:
|
|
127 |
</div>
|
128 |
""")
|
129 |
|
130 |
-
|
131 |
# Ganti judul
|
132 |
gr.Markdown("<h1 style='text-align: center;'>Selamat Datang Pelanggan Tercinta!β€οΈ Senang dapat melayani Anda.. </h1>")
|
133 |
|
@@ -135,22 +120,32 @@ with gr.Blocks(css=css, theme=IndonesiaTheme()) as app:
|
|
135 |
with gr.Row():
|
136 |
with gr.Column(elem_id="col-left"):
|
137 |
gr.Markdown("### Unggah Foto Anda")
|
138 |
-
person_img = gr.Image(label="Foto
|
|
|
|
|
139 |
gr.Examples(inputs=person_img, examples=human_list_path, examples_per_page=8)
|
140 |
|
141 |
with gr.Column(elem_id="col-mid"):
|
142 |
gr.Markdown("### Unggah Foto Pakaian")
|
143 |
garment_img = gr.Image(label="Foto Pakaian", type="numpy")
|
144 |
-
gr.Examples(inputs=garment_img, examples=garm_list_path, examples_per_page=
|
145 |
|
146 |
with gr.Column(elem_id="col-right"):
|
147 |
result_img = gr.Image(label="Hasil", show_share_button=False)
|
148 |
-
seed = gr.Slider(label="Token-Slider", minimum=0, maximum=999999, step=1, value=
|
149 |
randomize_seed = gr.Checkbox(label="Random Token", value=True)
|
150 |
-
|
|
|
|
|
151 |
result_info = gr.Textbox(label="Jawaban CS Kami")
|
|
|
|
|
152 |
run_button = gr.Button(value="β Mulai Ganti Pakaian β", elem_id="run-button")
|
153 |
-
run_button.click(
|
|
|
|
|
|
|
|
|
154 |
|
155 |
# Tambahkan footer di bagian bawah
|
156 |
gr.HTML("""
|
@@ -161,4 +156,4 @@ with gr.Blocks(css=css, theme=IndonesiaTheme()) as app:
|
|
161 |
|
162 |
# Menjalankan aplikasi
|
163 |
if __name__ == "__main__":
|
164 |
-
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
+
from gradio_client import Client, handle_file
|
|
|
|
|
|
|
4 |
from themes import IndonesiaTheme # Impor tema custom dari themes.py
|
5 |
+
from PIL import Image
|
6 |
+
import random
|
7 |
+
import tempfile # Untuk membuat file sementara
|
8 |
|
9 |
+
# Siapkan URL dan header untuk permintaan API
|
10 |
+
url_api = os.environ['url_api']
|
11 |
|
12 |
# Fungsi untuk menyimpan gambar sementara ke file
|
13 |
def save_temp_image(image_array):
|
|
|
17 |
return temp_file.name
|
18 |
|
19 |
# Fungsi untuk memanggil API Virtual Try-On
|
20 |
+
def virtual_tryon(person_img, garment_img, garment_des, seed, randomize_seed, denoise_steps, is_checked, is_checked_crop):
|
21 |
+
client = Client(url_api)
|
22 |
+
|
23 |
# Simpan gambar sementara
|
24 |
person_img_path = save_temp_image(person_img)
|
25 |
garment_img_path = save_temp_image(garment_img)
|
26 |
|
27 |
+
# Pemanggilan API yang sudah disesuaikan dengan format baru
|
28 |
+
result = client.predict(
|
29 |
+
dict={
|
30 |
+
"background": handle_file(person_img_path),
|
31 |
+
"layers": [],
|
32 |
+
"composite": None # Menggunakan None sesuai dengan format yang diberikan
|
33 |
+
},
|
34 |
+
garm_img=handle_file(garment_img_path),
|
35 |
+
garment_des=garment_des, # Deskripsi pakaian dari input pengguna
|
36 |
+
is_checked=is_checked, # Checkbox apakah checked atau tidak
|
37 |
+
is_checked_crop=is_checked_crop, # Checkbox untuk cropping
|
38 |
+
denoise_steps=denoise_steps, # Denoise steps slider
|
39 |
+
seed=seed,
|
40 |
+
api_name="/tryon"
|
41 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
+
# Hapus file sementara setelah selesai
|
44 |
+
os.remove(person_img_path)
|
45 |
+
os.remove(garment_img_path)
|
|
|
46 |
|
47 |
+
# Daftar kalimat acak yang menarik dan lucu
|
48 |
+
compliments = [
|
49 |
+
"Anda tampak hebat dengan baju itu!",
|
50 |
+
"Wow, Anda terlihat sangat memukau sekarang!",
|
51 |
+
"Siap jadi pusat perhatian dengan outfit ini!",
|
52 |
+
"Anda terlihat seperti bintang di red carpet!",
|
53 |
+
"Baju ini benar-benar cocok dengan gaya Anda!",
|
54 |
+
"Luar biasa! Anda terlihat seperti seorang model profesional!",
|
55 |
+
"Siapa yang tidak terpesona melihat Anda sekarang?",
|
56 |
+
"Gaya Anda sangat menakjubkan dengan pakaian ini!",
|
57 |
+
"Perfect match! Anda terlihat menawan!",
|
58 |
+
"Siap untuk sesi foto? Anda benar-benar keren!"
|
59 |
+
]
|
60 |
|
61 |
+
# Ubah bagian return dengan memilih kalimat secara acak
|
62 |
+
return result[0], random.choice(compliments), "Proses selesai."
|
|
|
|
|
|
|
63 |
|
64 |
# Path untuk contoh gambar
|
65 |
example_path = os.path.join(os.path.dirname(__file__), 'assets')
|
|
|
72 |
human_list = os.listdir(os.path.join(example_path, "human"))
|
73 |
human_list_path = [os.path.join(example_path, "human", human) for human in human_list]
|
74 |
|
75 |
+
|
76 |
# CSS untuk styling antarmuka
|
77 |
css = """
|
78 |
#col-left, #col-mid, #col-right {
|
|
|
105 |
"""
|
106 |
|
107 |
# Membuat antarmuka Gradio dengan tema IndonesiaTheme
|
108 |
+
with gr.Blocks(css=css, theme=IndonesiaTheme()) as TokoBajuVirtual:
|
109 |
# Tambahkan banner
|
110 |
gr.HTML("""
|
111 |
<div style='text-align: center;'>
|
|
|
113 |
</div>
|
114 |
""")
|
115 |
|
|
|
116 |
# Ganti judul
|
117 |
gr.Markdown("<h1 style='text-align: center;'>Selamat Datang Pelanggan Tercinta!β€οΈ Senang dapat melayani Anda.. </h1>")
|
118 |
|
|
|
120 |
with gr.Row():
|
121 |
with gr.Column(elem_id="col-left"):
|
122 |
gr.Markdown("### Unggah Foto Anda")
|
123 |
+
person_img = gr.Image(label="Foto Anda", type="numpy")
|
124 |
+
gr.Markdown("### Deskripsi Pakaian")
|
125 |
+
garment_des = gr.Textbox(label="Deskripsi Pakaian", placeholder="Optional : Berikan deskripsi pakaian...")
|
126 |
gr.Examples(inputs=person_img, examples=human_list_path, examples_per_page=8)
|
127 |
|
128 |
with gr.Column(elem_id="col-mid"):
|
129 |
gr.Markdown("### Unggah Foto Pakaian")
|
130 |
garment_img = gr.Image(label="Foto Pakaian", type="numpy")
|
131 |
+
gr.Examples(inputs=garment_img, examples=garm_list_path, examples_per_page=14)
|
132 |
|
133 |
with gr.Column(elem_id="col-right"):
|
134 |
result_img = gr.Image(label="Hasil", show_share_button=False)
|
135 |
+
seed = gr.Slider(label="Token-Slider", minimum=0, maximum=999999, step=1, value=42)
|
136 |
randomize_seed = gr.Checkbox(label="Random Token", value=True)
|
137 |
+
denoise_steps = gr.Slider(label="Denoise Steps", minimum=1, maximum=50, step=1, value=30)
|
138 |
+
is_checked = gr.Checkbox(label="Nyalakan Virtual Try-On", value=True)
|
139 |
+
is_checked_crop = gr.Checkbox(label="Ukuran Disesuaikan", value=True)
|
140 |
result_info = gr.Textbox(label="Jawaban CS Kami")
|
141 |
+
|
142 |
+
# Tombol untuk memulai proses ganti pakaian virtual
|
143 |
run_button = gr.Button(value="β Mulai Ganti Pakaian β", elem_id="run-button")
|
144 |
+
run_button.click(
|
145 |
+
fn=virtual_tryon,
|
146 |
+
inputs=[person_img, garment_img, garment_des, seed, randomize_seed, denoise_steps, is_checked, is_checked_crop],
|
147 |
+
outputs=[result_img, result_info] # Menghapus string "Hasil"
|
148 |
+
)
|
149 |
|
150 |
# Tambahkan footer di bagian bawah
|
151 |
gr.HTML("""
|
|
|
156 |
|
157 |
# Menjalankan aplikasi
|
158 |
if __name__ == "__main__":
|
159 |
+
TokoBajuVirtual.queue(api_open=False).launch(show_api=False)
|