Spaces:
Sleeping
Sleeping
Reauth
Browse files- app.py +157 -0
- funciones.py +124 -0
app.py
CHANGED
@@ -0,0 +1,157 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import sulkuPypi
|
3 |
+
from funciones import mass
|
4 |
+
from data import usuarios
|
5 |
+
import encrypter
|
6 |
+
import time
|
7 |
+
import nycklar.nodes
|
8 |
+
|
9 |
+
#Funciones
|
10 |
+
def authenticate(username, password):
|
11 |
+
|
12 |
+
for u, p in usuarios:
|
13 |
+
#Si el usuario y la contraseña son correctas...
|
14 |
+
if username == u and password == p:
|
15 |
+
#Agrego el nombre del usuario al estado general.
|
16 |
+
gr.State.usuario = username
|
17 |
+
#Bienvenida al usuario...
|
18 |
+
print("Welcome ", gr.State.usuario)
|
19 |
+
|
20 |
+
#Capsule es el usuario encriptado que enviarás a la API de Sulku.
|
21 |
+
capsule = encrypter.encripta(gr.State.usuario).decode("utf-8") #decode es para quitarle el 'b
|
22 |
+
gr.State.capsule = capsule
|
23 |
+
|
24 |
+
#Checa cuantos tokens tiene ese usuario via la API de Sulku:
|
25 |
+
gr.State.tokens = sulkuPypi.getTokens(capsule)
|
26 |
+
|
27 |
+
print(f"Tienes: {gr.State.tokens} tokens. ")
|
28 |
+
|
29 |
+
return True
|
30 |
+
#Si no hubo coincidencia regresas un false.
|
31 |
+
return False
|
32 |
+
|
33 |
+
#Función principal
|
34 |
+
def perform(input1, input2):
|
35 |
+
|
36 |
+
print("Estando en perform182, la cantidad de tokens es: ", gr.State.tokens)
|
37 |
+
#Revisaremos de nuevo:
|
38 |
+
gr.State.tokens = sulkuPypi.getTokens(encrypter.encripta(gr.State.usuario).decode("utf-8")) #Todo en una línea.
|
39 |
+
print("Ahora tienes: ", gr.State.tokens)
|
40 |
+
|
41 |
+
#Después autoriza.
|
42 |
+
#Si está autorizada puede ejecutar la tarea, ésta lógica si está a cargo aquí, por parte de la app y su desarrollador, no de Sulku.
|
43 |
+
autorizacion = sulkuPypi.authorize(gr.State.tokens, 'picswap')
|
44 |
+
print("La autorización es: ", autorizacion)
|
45 |
+
|
46 |
+
info_window = ""
|
47 |
+
|
48 |
+
#Después ejecuta la masa.
|
49 |
+
if autorizacion is True:
|
50 |
+
|
51 |
+
path = mass(input1,input2)
|
52 |
+
|
53 |
+
else:
|
54 |
+
info_window = "Out of credits..."
|
55 |
+
path = 'no-result.png'
|
56 |
+
return path, info_window, btn_buy
|
57 |
+
|
58 |
+
print(f"El path final fue {path}, si es no-result, no debites y controla la info window.")
|
59 |
+
print(f"El type de path es: ", type(path))
|
60 |
+
|
61 |
+
|
62 |
+
print("Convirtiendo path a string...")
|
63 |
+
path_string = str(path)
|
64 |
+
|
65 |
+
|
66 |
+
print("Path_string = ", path_string)
|
67 |
+
|
68 |
+
if "no-result" not in path_string:
|
69 |
+
#Si el path NO tiene no-result, todo funcionó bien, por lo tanto debita.
|
70 |
+
print("Se obtuvo un resultado, debitaremos.")
|
71 |
+
#Y finalmente debita los tokens.
|
72 |
+
gr.State.tokens = sulkuPypi.debitTokens(gr.State.capsule, "picswap")
|
73 |
+
print(f"Y ahora tienes: {gr.State.tokens} tokens.")
|
74 |
+
info_window = "Image ready!"
|
75 |
+
|
76 |
+
else:
|
77 |
+
print("No se detectó un rostro...")
|
78 |
+
info_window = "No face in source path detected."
|
79 |
+
print(f"Y ahora tienes: {gr.State.tokens} tokens.")
|
80 |
+
lbl_credits = "Nuevo texto..."
|
81 |
+
#No se hizo un proceso, por lo tanto no debitaremos.
|
82 |
+
#En el futuro, como regla de negocio, podría cambiar y que si debitemos.
|
83 |
+
|
84 |
+
return path, info_window, btn_buy
|
85 |
+
|
86 |
+
def click_buy():
|
87 |
+
|
88 |
+
print("Ejecutando change...")
|
89 |
+
|
90 |
+
nuevo_markdown = """
|
91 |
+
|
92 |
+
"""
|
93 |
+
|
94 |
+
return nuevo_markdown
|
95 |
+
|
96 |
+
def display_tokens():
|
97 |
+
|
98 |
+
print("Ejecutando display_tokens...")
|
99 |
+
|
100 |
+
return gr.State.tokens
|
101 |
+
|
102 |
+
#LOCAL VARIABLES
|
103 |
+
# gr.State.usuario = "briggsboardman"
|
104 |
+
# Capsule es el usuario encriptado que enviarás a la API de Sulku.
|
105 |
+
# capsule = encrypter.encripta(gr.State.usuario).decode("utf-8") #decode es para quitarle el 'b
|
106 |
+
# gr.State.capsule = capsule
|
107 |
+
# gr.State.tokens = 20
|
108 |
+
|
109 |
+
|
110 |
+
#Inputs
|
111 |
+
source_image = gr.Image(label="Source")
|
112 |
+
destination_image = gr.Image(label="Destination")
|
113 |
+
|
114 |
+
#Outputs
|
115 |
+
result_image = gr.Image(label="Blend Result")
|
116 |
+
txt_credits = gr.Textbox(label="Credits Available", value="205", interactive=False)
|
117 |
+
html_credits = gr.HTML("<div>Credits = 205</div>")
|
118 |
+
lbl_console = gr.Label(label="AI Terminal Messages", value="AI Engine ready...", container=True)
|
119 |
+
btn_buy = gr.Button("Buy More", visible=True, size='lg')
|
120 |
+
#btn_buy.click(fn=click_buy, outputs=mrk_title)
|
121 |
+
|
122 |
+
#Gradio themes:
|
123 |
+
# — gr.themes.Base()
|
124 |
+
# — gr.themes.Default()
|
125 |
+
# — gr.themes.Glass()
|
126 |
+
# — gr.themes.Monochrome()
|
127 |
+
# — gr.themes.Soft()
|
128 |
+
|
129 |
+
#valor = gr.State.tokens
|
130 |
+
valor = "205"
|
131 |
+
|
132 |
+
with gr.Blocks(theme=gr.themes.Base(), css="footer {visibility: hidden}") as main:
|
133 |
+
|
134 |
+
with gr.Row():
|
135 |
+
gr.Markdown(visible=True)
|
136 |
+
gr.Markdown(visible=True)
|
137 |
+
gr.Markdown(visible=True)
|
138 |
+
lbl_credits = gr.Label(label="Credits Available", value=valor, scale=1)
|
139 |
+
result_image.change(fn=display_tokens, outputs=lbl_credits)
|
140 |
+
|
141 |
+
with gr.Row():
|
142 |
+
|
143 |
+
demo = gr.Interface(
|
144 |
+
fn=perform,
|
145 |
+
title="",
|
146 |
+
inputs=[source_image, destination_image],
|
147 |
+
outputs=[result_image, lbl_console, txt_credits, btn_buy],
|
148 |
+
allow_flagging='auto'
|
149 |
+
)
|
150 |
+
|
151 |
+
with gr.Row():
|
152 |
+
gr.Markdown(visible=True)
|
153 |
+
gr.Markdown(visible=True)
|
154 |
+
gr.Markdown(visible=True)
|
155 |
+
lbl_credits = gr.Textbox(label="Credits Available", value="205", interactive=False, scale=1)
|
156 |
+
|
157 |
+
main.launch(auth=authenticate)
|
funciones.py
CHANGED
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import time
|
3 |
+
import pathlib
|
4 |
+
from PIL import Image
|
5 |
+
import envCharger
|
6 |
+
|
7 |
+
|
8 |
+
def mass(input1, input2):
|
9 |
+
|
10 |
+
# envCharger.load_env("local")
|
11 |
+
# PLATAFORMA = os.getenv('plataforma')
|
12 |
+
# print("PLATAFORMA: ", PLATAFORMA)
|
13 |
+
# time.sleep(5)
|
14 |
+
|
15 |
+
#video o cualquier otro sería para imagenes.
|
16 |
+
modo = "pic"
|
17 |
+
#local o huggingface
|
18 |
+
plataforma = "huggingface"
|
19 |
+
#face_swapper o face_enhancer o la combinación de ellos.
|
20 |
+
procesador = "face_swapper"
|
21 |
+
|
22 |
+
print(f"Inicio: Estamos en modo {modo}, plataforma: {plataforma} y procesador: {procesador}.")
|
23 |
+
|
24 |
+
path_video = input2
|
25 |
+
print("Path_video es:", path_video)
|
26 |
+
|
27 |
+
if modo == "video":
|
28 |
+
|
29 |
+
if plataforma == "local":
|
30 |
+
#Para local.
|
31 |
+
path_parts = path_video.split("\\")
|
32 |
+
else:
|
33 |
+
#Para HuggingFace
|
34 |
+
#Creo que no va en imagen.
|
35 |
+
print("La plataforma en la que basaremos la división es HuggingFace.")
|
36 |
+
path_parts = path_video.split("/")
|
37 |
+
|
38 |
+
#Aquí obtendremos nom_video
|
39 |
+
#Creo no va en imagen
|
40 |
+
filename = path_parts[-1]
|
41 |
+
nom_video = filename[:-4]
|
42 |
+
print("Esto es filename alias nom_video: ", nom_video)
|
43 |
+
path_particular = "/".join(path_parts[0:len(path_parts) - 1])
|
44 |
+
path_general = "/".join(path_parts[0:len(path_parts) - 2])
|
45 |
+
path_general = path_general.replace("\\", "/")
|
46 |
+
path_particular = path_particular.replace("\\", "/")
|
47 |
+
print("Path general: ", path_general)
|
48 |
+
print("Path general: ", path_particular)
|
49 |
+
path = pathlib.Path("result.mp4")
|
50 |
+
files = os.listdir(path_general)
|
51 |
+
|
52 |
+
print("Estos son los files que hay:")
|
53 |
+
print(files)
|
54 |
+
|
55 |
+
ext_imagen = "png"
|
56 |
+
ext_video = "mp4"
|
57 |
+
|
58 |
+
#Selector de modo.
|
59 |
+
if modo == "video":
|
60 |
+
print("Se asigno la extensión de video:", ext_video)
|
61 |
+
extension = ext_video
|
62 |
+
else:
|
63 |
+
print("Se asigno la extensión de imagen:", ext_imagen)
|
64 |
+
extension = ext_imagen
|
65 |
+
|
66 |
+
#El source siempre es una imagen.
|
67 |
+
source_path = "source.png"
|
68 |
+
target_path = "target." + extension
|
69 |
+
result_path = "result." + extension
|
70 |
+
|
71 |
+
#La primera siempre será una imagen, por eso no entra en el modo selector.
|
72 |
+
source_image = Image.fromarray(input1)
|
73 |
+
print("Esto es source_image: ", source_image)
|
74 |
+
source_image.save(source_path)
|
75 |
+
|
76 |
+
#Aquí trabajaremos solo el target.
|
77 |
+
if modo == "video":
|
78 |
+
#Para Video
|
79 |
+
target_path = input2
|
80 |
+
else:
|
81 |
+
#Es decir si es modo imagen
|
82 |
+
#Para Imagenes
|
83 |
+
target_image = Image.fromarray(input2)
|
84 |
+
print("Esto es target_image: ", target_image)
|
85 |
+
target_image.save(target_path)
|
86 |
+
|
87 |
+
print("Después de los selectores de modo los paths quedaron así:")
|
88 |
+
print("source_path: ", source_path)
|
89 |
+
print("target_path: ", target_path)
|
90 |
+
#FUTURE: Agrega por parámetro o mejor aún por enviroment el hecho de si es compu para usar cpu o si es hf para usar cuda o azure?
|
91 |
+
#(choose from 'tensorrt', 'cuda', 'cpu')
|
92 |
+
command = f"python run.py -s {source_path} -t {target_path} -o {result_path} --frame-processor {procesador} --execution-provider cpu"
|
93 |
+
print(command)
|
94 |
+
time.sleep(1)
|
95 |
+
proc = os.popen(command)
|
96 |
+
output = proc.read()
|
97 |
+
|
98 |
+
print("Output (resultado de la ejecución del código):")
|
99 |
+
print(output)
|
100 |
+
print("Y el tipo del output es: ", type(output))
|
101 |
+
time.sleep(1)
|
102 |
+
|
103 |
+
print("Terminó la impresión del output...")
|
104 |
+
|
105 |
+
if "No face in source path detected" in output:
|
106 |
+
#Si no se detecta un rostro, pondremos un placeholder, ésto evita que se despliegue el último result obtenido antes...
|
107 |
+
#...de la operación fallida.
|
108 |
+
print("No se detectó ninguna cara en la ruta de origen.")
|
109 |
+
#result_path = "no-result.png"
|
110 |
+
|
111 |
+
else:
|
112 |
+
print("Si se detecto un rostro...")
|
113 |
+
#Si sí se detectó un rostro, sigue su camino normal.
|
114 |
+
|
115 |
+
print("Éste es el momento en el que se creo result, revisar...")
|
116 |
+
|
117 |
+
path = pathlib.Path(result_path)
|
118 |
+
path_abs = os.path.abspath(path)
|
119 |
+
print("Éste es el path:", path)
|
120 |
+
print("Y su ruta absoluta es: ", path_abs)
|
121 |
+
print("Listo! Gracias!")
|
122 |
+
return path
|
123 |
+
|
124 |
+
|