Rooni commited on
Commit
0ffaf55
·
verified ·
1 Parent(s): 22a4581

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -10
app.py CHANGED
@@ -2,6 +2,8 @@ import gradio as gr
2
  from gradio_client import Client, handle_file
3
  import os
4
  import random
 
 
5
 
6
  KEYS = os.getenv("KEYS").split(",")
7
 
@@ -11,31 +13,35 @@ def get_random_api_key():
11
  def swap_face_api(source_img, target_img, doFaceEnhancer):
12
  try:
13
  api_key = get_random_api_key()
14
-
15
  client = Client("tuan2308/face-swap")
16
 
17
- source_filename = f"source_{random.randint(1000, 9999)}.jpg"
18
- target_filename = f"target_{random.randint(1000, 9999)}.jpg"
 
 
19
 
20
- source_img.save(source_filename)
21
- target_img.save(target_filename)
 
22
 
23
  result = client.predict(
24
- source_file=handle_file(source_filename),
25
- target_file=handle_file(target_filename),
26
  doFaceEnhancer=doFaceEnhancer,
27
  api_name="/predict",
28
  api_key=api_key
29
  )
30
 
31
- os.remove(source_filename)
32
- os.remove(target_filename)
 
 
33
 
34
- return result
35
  except Exception as e:
36
  print(f"Ошибка при вызове API: {e}")
37
  return None
38
 
 
39
  iface = gr.Interface(
40
  fn=swap_face_api,
41
  inputs=[
 
2
  from gradio_client import Client, handle_file
3
  import os
4
  import random
5
+ from io import BytesIO
6
+ from PIL import Image
7
 
8
  KEYS = os.getenv("KEYS").split(",")
9
 
 
13
  def swap_face_api(source_img, target_img, doFaceEnhancer):
14
  try:
15
  api_key = get_random_api_key()
 
16
  client = Client("tuan2308/face-swap")
17
 
18
+ # Конвертируем PIL изображения в байты
19
+ source_bytes = BytesIO()
20
+ source_img.save(source_bytes, format="JPEG") # или PNG, в зависимости от формата
21
+ source_bytes.seek(0)
22
 
23
+ target_bytes = BytesIO()
24
+ target_img.save(target_bytes, format="JPEG") # или PNG, в зависимости от формата
25
+ target_bytes.seek(0)
26
 
27
  result = client.predict(
28
+ source_file=handle_file(source_bytes.read(), filename="source.jpg"), # Добавляем имя файла
29
+ target_file=handle_file(target_bytes.read(), filename="target.jpg"), # Добавляем имя файла
30
  doFaceEnhancer=doFaceEnhancer,
31
  api_name="/predict",
32
  api_key=api_key
33
  )
34
 
35
+ # Конвертируем результат из байтов обратно в PIL Image
36
+ output_image = Image.open(BytesIO(result))
37
+
38
+ return output_image
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,
47
  inputs=[