Spaces:
Sleeping
Sleeping
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() | |