Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,40 +1,73 @@
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
-
|
4 |
-
|
5 |
from PIL import Image
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
# Siapkan URL dan header untuk permintaan API
|
8 |
-
url_api = os.environ['url_api']
|
9 |
-
|
10 |
# Fungsi untuk menyimpan gambar sementara ke file
|
11 |
-
def save_temp_image(image_array
|
|
|
12 |
image = Image.fromarray(image_array.astype('uint8'))
|
13 |
-
image.save(
|
|
|
14 |
|
15 |
# Fungsi untuk memanggil API Virtual Try-On
|
16 |
def virtual_tryon(person_img, garment_img, seed, randomize_seed):
|
17 |
-
client = Client(url_api)
|
18 |
-
|
19 |
# Simpan gambar sementara
|
20 |
-
person_img_path =
|
21 |
-
garment_img_path =
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
randomize_seed=randomize_seed,
|
30 |
-
api_name="/tryon"
|
31 |
-
)
|
32 |
|
33 |
-
#
|
34 |
-
|
35 |
-
os.remove(garment_img_path)
|
36 |
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
# Fungsi untuk memuat deskripsi dari file
|
40 |
def load_description(fp):
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
+
import requests
|
4 |
+
import json
|
5 |
from PIL import Image
|
6 |
+
import tempfile
|
7 |
+
|
8 |
+
# Siapkan URL untuk permintaan API
|
9 |
+
api_url = "https://kwai-kolors-kolors-virtual-try-on.hf.space/queue/join?__theme=system"
|
10 |
|
|
|
|
|
|
|
11 |
# Fungsi untuk menyimpan gambar sementara ke file
|
12 |
+
def save_temp_image(image_array):
|
13 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
|
14 |
image = Image.fromarray(image_array.astype('uint8'))
|
15 |
+
image.save(temp_file.name)
|
16 |
+
return temp_file.name
|
17 |
|
18 |
# Fungsi untuk memanggil API Virtual Try-On
|
19 |
def virtual_tryon(person_img, garment_img, seed, randomize_seed):
|
|
|
|
|
20 |
# Simpan gambar sementara
|
21 |
+
person_img_path = save_temp_image(person_img)
|
22 |
+
garment_img_path = save_temp_image(garment_img)
|
23 |
+
|
24 |
+
payload = json.dumps({
|
25 |
+
"data": [
|
26 |
+
{
|
27 |
+
"path": person_img_path,
|
28 |
+
"url": f"https://deddy-toko-baju-virtual-gratis.hf.space/file={person_img_path}",
|
29 |
+
"size": None,
|
30 |
+
"orig_name": os.path.basename(person_img_path),
|
31 |
+
"mime_type": None,
|
32 |
+
"is_stream": False,
|
33 |
+
"meta": {
|
34 |
+
"_type": "gradio.FileData"
|
35 |
+
}
|
36 |
+
},
|
37 |
+
{
|
38 |
+
"path": garment_img_path,
|
39 |
+
"url": f"https://deddy-toko-baju-virtual-gratis.hf.space/file={garment_img_path}",
|
40 |
+
"size": None,
|
41 |
+
"orig_name": os.path.basename(garment_img_path),
|
42 |
+
"mime_type": None,
|
43 |
+
"is_stream": False,
|
44 |
+
"meta": {
|
45 |
+
"_type": "gradio.FileData"
|
46 |
+
}
|
47 |
+
},
|
48 |
+
seed,
|
49 |
+
randomize_seed
|
50 |
+
],
|
51 |
+
"event_data": None,
|
52 |
+
"fn_index": 2, # Indeks API yang sesuai
|
53 |
+
"trigger_id": 26,
|
54 |
+
"session_hash": "l8hba3fqoe8" # Hash session harus diperbarui jika berubah
|
55 |
+
})
|
56 |
|
57 |
+
headers = {
|
58 |
+
'content-type': 'application/json',
|
59 |
+
'User-Agent': 'Mozilla/5.0'
|
60 |
+
}
|
|
|
|
|
|
|
61 |
|
62 |
+
# Kirim permintaan API
|
63 |
+
response = requests.post(api_url, headers=headers, data=payload)
|
|
|
64 |
|
65 |
+
if response.status_code == 200:
|
66 |
+
result = response.json()
|
67 |
+
# Mengambil hasil dari API
|
68 |
+
return result['data'][0], seed, "Proses selesai."
|
69 |
+
else:
|
70 |
+
return None, seed, f"Error: {response.status_code}"
|
71 |
|
72 |
# Fungsi untuk memuat deskripsi dari file
|
73 |
def load_description(fp):
|