import os import gradio as gr import requests import json from PIL import Image import tempfile from themes import IndonesiaTheme # Impor tema custom dari themes.py # Siapkan URL untuk permintaan API api_url = "https://kwai-kolors-kolors-virtual-try-on.hf.space/queue/join?__theme=system" # Fungsi untuk menyimpan gambar sementara ke file def save_temp_image(image_array): temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png") image = Image.fromarray(image_array.astype('uint8')) image.save(temp_file.name) return temp_file.name # Fungsi untuk memanggil API Virtual Try-On def virtual_tryon(person_img, garment_img, seed, randomize_seed): # Simpan gambar sementara person_img_path = save_temp_image(person_img) garment_img_path = save_temp_image(garment_img) payload = json.dumps({ "data": [ { "path": person_img_path, "url": f"https://deddy-toko-baju-virtual-gratis.hf.space/file={person_img_path}", "size": None, "orig_name": os.path.basename(person_img_path), "mime_type": None, "is_stream": False, "meta": { "_type": "gradio.FileData" } }, { "path": garment_img_path, "url": f"https://deddy-toko-baju-virtual-gratis.hf.space/file={garment_img_path}", "size": None, "orig_name": os.path.basename(garment_img_path), "mime_type": None, "is_stream": False, "meta": { "_type": "gradio.FileData" } }, seed, randomize_seed ], "event_data": None, "fn_index": 2, # Indeks API yang sesuai "trigger_id": 26, "session_hash": "l8hba3fqoe8" # Hash session harus diperbarui jika berubah }) headers = { 'content-type': 'application/json', 'User-Agent': 'Mozilla/5.0' } # Kirim permintaan API response = requests.post(api_url, headers=headers, data=payload) if response.status_code == 200: result = response.json() # Mengambil hasil dari API return result['data'][0], seed, "Proses selesai." else: return None, seed, f"Error: {response.status_code}" # 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("""