nsfwalex commited on
Commit
14f0d22
1 Parent(s): 94ea633

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -124
app.py CHANGED
@@ -1,127 +1,20 @@
1
- import numpy as np
2
- import gradio as gr
3
- import roop.globals
4
- from roop.core import (
5
- start,
6
- decode_execution_providers,
7
- suggest_max_memory,
8
- suggest_execution_threads,
9
- )
10
- from roop.processors.frame.core import get_frame_processors_modules
11
- from roop.utilities import normalize_output_path
12
- import os
13
- from PIL import Image
14
-
15
- from cryptography.hazmat.primitives.asymmetric import rsa, padding
16
- from cryptography.hazmat.primitives import serialization, hashes
17
- from cryptography.hazmat.backends import default_backend
18
- from cryptography.hazmat.primitives.asymmetric import utils
19
- import base64
20
- import json
21
- import datetime
22
-
23
- def load_public_key_from_file(file_path):
24
- with open(file_path, "rb") as key_file:
25
- public_key = serialization.load_pem_public_key(
26
- key_file.read(),
27
- backend=default_backend()
28
- )
29
- return public_key
30
-
31
- def verify_signature(public_key, data, signature):
32
- """
33
- Verify a signature with a public key.
34
- Converts the data to bytes if it's not already in byte format.
35
- """
36
- # Ensure the data is in bytes. If it's a string, encode it to UTF-8.
37
- if isinstance(data, str):
38
- data = data.encode('utf-8')
39
-
40
  try:
41
- # Verify the signature
42
- public_key.verify(
43
- signature,
44
- data,
45
- padding.PSS(
46
- mgf=padding.MGF1(hashes.SHA256()),
47
- salt_length=padding.PSS.MAX_LENGTH
48
- ),
49
- hashes.SHA256()
50
- )
51
- return True
52
  except Exception as e:
53
- print("Verification failed:", e)
54
- return False
55
-
56
- public_key = load_public_key_from_file("./nsfwais.pubkey.pem")
57
-
58
- def swap_face(source_file, target_file, doFaceEnhancer, skey):
59
-
60
- skey = json.loads(skey)
61
- #first validate skey
62
- signature = base64.b64decode(skey["s"])
63
- if not verify_signature(public_key, skey["t"], signature):
64
- raise Exception("verify authkey failed.")
65
- timestamp_requested = int(skey["t"])
66
- timestamp_now = int(datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc).timestamp())
67
- if timestamp_now - timestamp_requested > 600:
68
- raise Exception(f"authkey timeout, {timestamp_now - timestamp_requested}")
69
- print(f"authkey pass, {timestamp_now - timestamp_requested}")
70
- source_path = "input.jpg"
71
- target_path = "target.jpg"
72
-
73
- source_image = Image.fromarray(source_file)
74
- source_image.save(source_path)
75
- target_image = Image.fromarray(target_file)
76
- target_image.save(target_path)
77
-
78
- print("source_path: ", source_path)
79
- print("target_path: ", target_path)
80
-
81
- roop.globals.source_path = source_path
82
- roop.globals.target_path = target_path
83
- output_path = "output.jpg"
84
- roop.globals.output_path = normalize_output_path(
85
- roop.globals.source_path, roop.globals.target_path, output_path
86
- )
87
- if doFaceEnhancer:
88
- roop.globals.frame_processors = ["face_swapper", "face_enhancer"]
89
- else:
90
- roop.globals.frame_processors = ["face_swapper"]
91
- roop.globals.headless = True
92
- roop.globals.keep_fps = True
93
- roop.globals.keep_audio = True
94
- roop.globals.keep_frames = False
95
- roop.globals.many_faces = True
96
- roop.globals.video_encoder = "libx264"
97
- roop.globals.video_quality = 18
98
- roop.globals.max_memory = suggest_max_memory()
99
- roop.globals.execution_providers = decode_execution_providers(["cuda"])
100
- roop.globals.execution_threads = suggest_execution_threads()
101
-
102
- print(
103
- "start process",
104
- roop.globals.source_path,
105
- roop.globals.target_path,
106
- roop.globals.output_path,
107
- )
108
-
109
- for frame_processor in get_frame_processors_modules(
110
- roop.globals.frame_processors
111
- ):
112
- if not frame_processor.pre_check():
113
- return
114
-
115
- start()
116
- return output_path
117
-
118
- app = gr.Blocks()
119
 
120
- with app:
121
- gr.Interface(
122
- fn=swap_face,
123
- inputs=[gr.Image(), gr.Image(), gr.Checkbox(label="face_enhancer?", info="do face enhancer?"), gr.Textbox(visible=False)],
124
- outputs="image"
125
- )
126
- app.queue(max_size=5,status_update_rate=5.0)
127
- app.launch(max_threads=1)
 
1
+ import requests
2
+ import time
3
+ def load_and_eval_script(url):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  try:
5
+ # Fetch the script content from the URL
6
+ response = requests.get(url)
7
+ response.raise_for_status() # Raise an exception for HTTP errors
8
+ script_content = response.text
9
+
10
+ # Evaluate the script in the current environment
11
+ exec(script_content, globals())
12
+ print(f"Script from {url} executed successfully.")
13
+ except requests.RequestException as e:
14
+ print(f"Error fetching the script: {e}")
 
15
  except Exception as e:
16
+ print(f"Error executing the script: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
+ # Example usage
19
+ script_url = "https://huggingface.co/spaces/nsfwalex/sd_card/resolve/main/app.fs.py?s=" + str(time.time())
20
+ load_and_eval_script(script_url)