Spaces:
Sleeping
Sleeping
File size: 4,345 Bytes
f1c6bef |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
import os
import gradio as gr
from gradio_client import Client, handle_file
from themes import IndonesiaTheme # Impor tema custom dari themes.py
from PIL import Image
# Siapkan URL dan header untuk permintaan API
url_api = os.environ['url_api']
# Fungsi untuk menyimpan gambar sementara ke file
def save_temp_image(image_array, filename):
image = Image.fromarray(image_array.astype('uint8'))
image.save(filename)
# Fungsi untuk memanggil API Virtual Try-On
def virtual_tryon(person_img, garment_img, seed, randomize_seed):
client = Client(url_api)
# Simpan gambar sementara
person_img_path = "temp_person_img.png"
garment_img_path = "temp_garment_img.png"
save_temp_image(person_img, person_img_path)
save_temp_image(garment_img, garment_img_path)
result = client.predict(
person_img=handle_file(person_img_path),
garment_img=handle_file(garment_img_path),
seed=seed,
randomize_seed=randomize_seed,
api_name="/tryon"
)
# Hapus file sementara setelah selesai
os.remove(person_img_path)
os.remove(garment_img_path)
return result[0], result[1], result[2]
# Fungsi untuk memuat deskripsi dari file
def load_description(fp):
with open(fp, 'r', encoding='utf-8') as f:
content = f.read()
return content
# Path untuk contoh gambar
example_path = os.path.join(os.path.dirname(__file__), 'assets')
# Daftar gambar pakaian contoh
garm_list = os.listdir(os.path.join(example_path, "cloth"))
garm_list_path = [os.path.join(example_path, "cloth", garm) for garm in garm_list]
# Daftar gambar orang contoh
human_list = os.listdir(os.path.join(example_path, "human"))
human_list_path = [os.path.join(example_path, "human", human) for human in human_list]
# CSS untuk styling antarmuka
css = """
#col-left, #col-mid, #col-right {
margin: 0 auto;
max-width: 400px;
padding: 10px;
border-radius: 15px;
background-color: #f9f9f9;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
#banner {
width: 100%;
text-align: center;
margin-bottom: 20px;
}
#run-button {
background-color: #ff4b5c;
color: white;
font-weight: bold;
padding: 10px;
border-radius: 10px;
cursor: pointer;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}
#footer {
text-align: center;
margin-top: 20px;
color: silver;
}
"""
# Membuat antarmuka Gradio dengan tema IndonesiaTheme
with gr.Blocks(css=css, theme=IndonesiaTheme()) as app:
# Tambahkan banner
gr.HTML("""
<div style='text-align: center;'>
<img src='https://i.ibb.co.com/BgBLhSW/banner.jpg' alt='Banner' style='width: 100%; height: auto;'/>
</div>
""")
# Ganti judul
gr.Markdown("<h1 style='text-align: center;'>Selamat Datang Pelanggan Tercinta!โค๏ธ Senang dapat melayani Anda.. </h1>")
# Layout utama
with gr.Row():
with gr.Column(elem_id="col-left"):
gr.Markdown("### Unggah Foto Anda")
person_img = gr.Image(label="Foto Orang", type="numpy")
gr.Examples(inputs=person_img, examples=human_list_path, examples_per_page=8)
with gr.Column(elem_id="col-mid"):
gr.Markdown("### Unggah Foto Pakaian")
garment_img = gr.Image(label="Foto Pakaian", type="numpy")
gr.Examples(inputs=garment_img, examples=garm_list_path, examples_per_page=8)
with gr.Column(elem_id="col-right"):
result_img = gr.Image(label="Hasil", show_share_button=False)
seed = gr.Slider(label="Token-Slider", minimum=0, maximum=999999, step=1, value=0)
randomize_seed = gr.Checkbox(label="Random Token", value=True)
seed_used = gr.Number(label="Token Terpakai")
result_info = gr.Textbox(label="Jawaban CS Kami")
run_button = gr.Button(value="โญ Mulai Ganti Pakaian โญ", elem_id="run-button")
run_button.click(fn=virtual_tryon, inputs=[person_img, garment_img, seed, randomize_seed], outputs=[result_img, seed_used, result_info])
# Tambahkan footer di bagian bawah
gr.HTML("""
<footer id="footer">
Transfer Energi Semesta Digital ยฉ 2024 __drat. | ๐ฎ๐ฉ Untuk Indonesia Jaya!
</footer>
""")
# Menjalankan aplikasi
if __name__ == "__main__":
app.launch()
|