Spaces:
Running
Running
Update app.py
Browse filesAdd alert and info
app.py
CHANGED
@@ -12,16 +12,14 @@ Oleh :
|
|
12 |
|
13 |
"""
|
14 |
|
15 |
-
|
16 |
import os
|
17 |
import gradio as gr
|
18 |
from gradio_client import Client, handle_file
|
19 |
-
from themes import IndonesiaTheme
|
20 |
from PIL import Image
|
21 |
import random
|
22 |
-
import tempfile
|
23 |
-
import time
|
24 |
-
from gradio_client.exceptions import AppError # Import untuk menangkap kesalahan Gradio Client
|
25 |
|
26 |
# Siapkan URL untuk permintaan API Virtual Try-On
|
27 |
url_api = os.environ['url_api']
|
@@ -43,12 +41,11 @@ def save_temp_image(image_array):
|
|
43 |
image.save(temp_file.name)
|
44 |
return temp_file.name
|
45 |
|
46 |
-
|
47 |
# Fungsi untuk memanggil API Virtual Try-On
|
48 |
def virtual_tryon(person_img, garment_img, garment_des, seed, randomize_seed, denoise_steps, is_checked, is_checked_crop):
|
49 |
-
# Pilih
|
50 |
-
|
51 |
-
client = Client(
|
52 |
|
53 |
# Simpan gambar sementara
|
54 |
person_img_path = save_temp_image(person_img)
|
@@ -70,11 +67,7 @@ def virtual_tryon(person_img, garment_img, garment_des, seed, randomize_seed, de
|
|
70 |
seed=seed,
|
71 |
api_name="/tryon"
|
72 |
)
|
73 |
-
|
74 |
-
# Hapus file sementara setelah selesai
|
75 |
-
os.remove(person_img_path)
|
76 |
-
os.remove(garment_img_path)
|
77 |
-
|
78 |
# Daftar kalimat acak yang menarik dan lucu
|
79 |
compliments = [
|
80 |
"Anda tampak hebat dengan baju itu!",
|
@@ -88,18 +81,22 @@ def virtual_tryon(person_img, garment_img, garment_des, seed, randomize_seed, de
|
|
88 |
"Perfect match! Anda terlihat menawan!",
|
89 |
"Siap untuk sesi foto? Anda benar-benar keren!"
|
90 |
]
|
91 |
-
|
92 |
-
#
|
93 |
return result[0], random.choice(compliments), "Proses selesai."
|
94 |
|
95 |
-
except AppError as e:
|
96 |
-
#
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
#
|
101 |
-
|
102 |
-
|
|
|
|
|
|
|
|
|
103 |
|
104 |
|
105 |
# Fungsi untuk menampilkan pesan popup dan menunda eksekusi
|
|
|
12 |
|
13 |
"""
|
14 |
|
|
|
15 |
import os
|
16 |
import gradio as gr
|
17 |
from gradio_client import Client, handle_file
|
18 |
+
from themes import IndonesiaTheme
|
19 |
from PIL import Image
|
20 |
import random
|
21 |
+
import tempfile
|
22 |
+
import time
|
|
|
23 |
|
24 |
# Siapkan URL untuk permintaan API Virtual Try-On
|
25 |
url_api = os.environ['url_api']
|
|
|
41 |
image.save(temp_file.name)
|
42 |
return temp_file.name
|
43 |
|
|
|
44 |
# Fungsi untuk memanggil API Virtual Try-On
|
45 |
def virtual_tryon(person_img, garment_img, garment_des, seed, randomize_seed, denoise_steps, is_checked, is_checked_crop):
|
46 |
+
# Pilih API secara acak antara dua URL
|
47 |
+
url_api = random.choice(["Nymbo/Virtual-Try-On", "yisol/IDM-VTON"])
|
48 |
+
client = Client(url_api)
|
49 |
|
50 |
# Simpan gambar sementara
|
51 |
person_img_path = save_temp_image(person_img)
|
|
|
67 |
seed=seed,
|
68 |
api_name="/tryon"
|
69 |
)
|
70 |
+
|
|
|
|
|
|
|
|
|
71 |
# Daftar kalimat acak yang menarik dan lucu
|
72 |
compliments = [
|
73 |
"Anda tampak hebat dengan baju itu!",
|
|
|
81 |
"Perfect match! Anda terlihat menawan!",
|
82 |
"Siap untuk sesi foto? Anda benar-benar keren!"
|
83 |
]
|
84 |
+
|
85 |
+
# Tampilkan hasil dan kalimat acak yang menarik
|
86 |
return result[0], random.choice(compliments), "Proses selesai."
|
87 |
|
88 |
+
except gradio_client.exceptions.AppError as e:
|
89 |
+
# Tampilkan pesan error di result_info jika terjadi kesalahan API
|
90 |
+
return None, str(e), "Terjadi kesalahan selama proses."
|
91 |
+
|
92 |
+
finally:
|
93 |
+
# Hapus file sementara setelah selesai
|
94 |
+
os.remove(person_img_path)
|
95 |
+
os.remove(garment_img_path)
|
96 |
+
|
97 |
+
# Tampilkan pesan peringatan dan menunggu selama 1 menit sebelum proses baru dimulai
|
98 |
+
gr.Warning("Ruang ganti kami terbatas. Mohon menunggu antreannya.")
|
99 |
+
time.sleep(60)
|
100 |
|
101 |
|
102 |
# Fungsi untuk menampilkan pesan popup dan menunda eksekusi
|