Rooni commited on
Commit
3412fa4
·
verified ·
1 Parent(s): df949d3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -14
app.py CHANGED
@@ -16,32 +16,31 @@ def swap_face_api(source_img, target_img, doFaceEnhancer):
16
  api_key = get_random_api_key()
17
  client = Client("tuan2308/face-swap")
18
 
19
- # Конвертируем PIL изображения в байты, затем в base64
20
- source_bytes = BytesIO()
21
- source_img.save(source_bytes, format="JPEG")
22
- source_b64 = base64.b64encode(source_bytes.getvalue()).decode("utf-8")
23
-
24
- target_bytes = BytesIO()
25
- target_img.save(target_bytes, format="JPEG")
26
- target_b64 = base64.b64encode(target_bytes.getvalue()).decode("utf-8")
27
-
28
 
29
  result = client.predict(
30
- source_file=source_b64, # Передаем base64 строку
31
- target_file=target_b64, # Передаем base64 строку
32
  doFaceEnhancer=doFaceEnhancer,
33
  api_name="/predict",
34
- api_key=api_key # Передаем api_key в predict
35
  )
36
 
37
- # Конвертируем результат из байтов в PIL Image
38
- output_image = Image.open(BytesIO(result))
 
39
  return output_image
40
 
41
  except Exception as e:
42
  print(f"Ошибка при вызове API: {e}")
43
  return None
44
 
 
 
 
 
45
 
46
  iface = gr.Interface(
47
  fn=swap_face_api,
 
16
  api_key = get_random_api_key()
17
  client = Client("tuan2308/face-swap")
18
 
19
+ # Кодируем PIL изображения в base64, используя gr.processing_utils
20
+ source_b64 = gr.processing_utils.encode_pil_to_base64(source_img)
21
+ target_b64 = gr.processing_utils.encode_pil_to_base64(target_img)
 
 
 
 
 
 
22
 
23
  result = client.predict(
24
+ source_file=source_b64,
25
+ target_file=target_b64,
26
  doFaceEnhancer=doFaceEnhancer,
27
  api_name="/predict",
28
+ api_key=api_key # Передаем api_key в predict
29
  )
30
 
31
+ # Декодируем результат из base64 в PIL Image
32
+ result_decoded = base64.b64decode(result) # Декодируем результат
33
+ output_image = Image.open(BytesIO(result_decoded))
34
  return output_image
35
 
36
  except Exception as e:
37
  print(f"Ошибка при вызове API: {e}")
38
  return None
39
 
40
+ except Exception as e:
41
+ print(f"Ошибка при вызове API: {e}")
42
+ return None
43
+
44
 
45
  iface = gr.Interface(
46
  fn=swap_face_api,