nsfwalex commited on
Commit
9be7c33
1 Parent(s): 2475b75

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -2
app.py CHANGED
@@ -18,8 +18,63 @@ import onnxruntime as ort
18
  import cv2
19
  from roop.face_analyser import get_one_face
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  @spaces.GPU
22
- def swap_face(source_file, target_file, doFaceEnhancer):
 
 
 
 
 
 
 
 
 
 
 
 
23
  session_id = str(uuid.uuid4()) # Tạo một UUID duy nhất cho mỗi phiên làm việc
24
  session_dir = f"temp/{session_id}"
25
  os.makedirs(session_dir, exist_ok=True)
@@ -81,7 +136,8 @@ app = gr.Interface(
81
  inputs=[
82
  gr.Image(),
83
  gr.Image(),
84
- gr.Checkbox(label="Face Enhancer?", info="Do face enhancement?")
 
85
  ],
86
  outputs="image"
87
  )
 
18
  import cv2
19
  from roop.face_analyser import get_one_face
20
 
21
+ from cryptography.hazmat.primitives.asymmetric import rsa, padding
22
+ from cryptography.hazmat.primitives import serialization, hashes
23
+ from cryptography.hazmat.backends import default_backend
24
+ from cryptography.hazmat.primitives.asymmetric import utils
25
+ import base64
26
+ import json
27
+ import datetime
28
+
29
+ def load_public_key_from_file(file_path):
30
+ with open(file_path, "rb") as key_file:
31
+ public_key = serialization.load_pem_public_key(
32
+ key_file.read(),
33
+ backend=default_backend()
34
+ )
35
+ return public_key
36
+
37
+ def verify_signature(public_key, data, signature):
38
+ """
39
+ Verify a signature with a public key.
40
+ Converts the data to bytes if it's not already in byte format.
41
+ """
42
+ # Ensure the data is in bytes. If it's a string, encode it to UTF-8.
43
+ if isinstance(data, str):
44
+ data = data.encode('utf-8')
45
+
46
+ try:
47
+ # Verify the signature
48
+ public_key.verify(
49
+ signature,
50
+ data,
51
+ padding.PSS(
52
+ mgf=padding.MGF1(hashes.SHA256()),
53
+ salt_length=padding.PSS.MAX_LENGTH
54
+ ),
55
+ hashes.SHA256()
56
+ )
57
+ return True
58
+ except Exception as e:
59
+ print("Verification failed:", e)
60
+ return False
61
+
62
+ public_key = load_public_key_from_file("./nsfwais.pubkey.pem")
63
+
64
  @spaces.GPU
65
+ def swap_face(source_file, target_file, doFaceEnhancer, skey):
66
+
67
+ skey = json.loads(skey)
68
+ #first validate skey
69
+ signature = base64.b64decode(skey["s"])
70
+ if not verify_signature(public_key, skey["t"], signature):
71
+ raise Exception("verify authkey failed.")
72
+ timestamp_requested = int(skey["t"])
73
+ timestamp_now = int(datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc).timestamp())
74
+ if timestamp_now - timestamp_requested > 600:
75
+ raise Exception(f"authkey timeout, {timestamp_now - timestamp_requested}")
76
+ print(f"authkey pass, {timestamp_now - timestamp_requested}")
77
+
78
  session_id = str(uuid.uuid4()) # Tạo một UUID duy nhất cho mỗi phiên làm việc
79
  session_dir = f"temp/{session_id}"
80
  os.makedirs(session_dir, exist_ok=True)
 
136
  inputs=[
137
  gr.Image(),
138
  gr.Image(),
139
+ gr.Checkbox(label="Face Enhancer?", info="Do face enhancement?"),
140
+ gr.Textbox(visible=False)
141
  ],
142
  outputs="image"
143
  )