-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsulkuFront.py
104 lines (81 loc) · 3.63 KB
/
sulkuFront.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import globales
import sulkuPypi
import gradio as gr
import threading
import sulkuMessages
result_from_displayTokens = None
result_from_initAPI = None
def displayTokens(request: gr.Request):
global result_from_displayTokens
novelty = sulkuPypi.getNovelty(sulkuPypi.encripta(request.username).decode("utf-8"), globales.aplicacion)
if novelty == "new_user":
display = gr.Textbox(visible=False)
else:
tokens = sulkuPypi.getTokens(sulkuPypi.encripta(request.username).decode("utf-8"), globales.env)
display = visualizar_creditos(tokens, request.username)
result_from_displayTokens = display
def precarga(request: gr.Request):
# global result_from_initAPI
# global result_from_displayTokens
#thread1 = threading.Thread(target=initAPI)
thread2 = threading.Thread(target=displayTokens, args=(request,))
#thread1.start()
thread2.start()
#thread1.join() # Espera a que el hilo 1 termine
thread2.join() # Espera a que el hilo 2 termine
#return result_from_initAPI, result_from_displayTokens
return result_from_displayTokens
def visualizar_creditos(nuevos_creditos, usuario):
html_credits = f"""
<div>
<div style="text-align: left;">👤<b>Username: </b> {usuario}</div><div style="text-align: right;">💶<b>Credits Available: </b> {nuevos_creditos}</div>
</div>
"""
return html_credits
#Controla lo que se depliega en el frontend y que tiene que ver con llamados a Sulku.
def noCredit(usuario):
info_window = sulkuMessages.out_of_credits
path = 'images/no-credits.png'
tokens = 0
html_credits = visualizar_creditos(tokens, usuario)
return info_window, path, html_credits
def aError(usuario, tokens, excepcion):
#aError se usa para llenar todos los elementos visuales en el front.
info_window = manejadorExcepciones(excepcion)
path = 'images/error.png'
tokens = tokens
html_credits = visualizar_creditos(tokens, usuario)
return info_window, path, html_credits
def manejadorExcepciones(excepcion):
#El parámetro que recibe es el texto despliega ante determinada excepción:
if excepcion == "PAUSED":
info_window = sulkuMessages.PAUSED
elif excepcion == "RUNTIME_ERROR":
info_window = sulkuMessages.RUNTIME_ERROR
elif excepcion == "STARTING":
info_window = sulkuMessages.STARTING
elif excepcion == "HANDSHAKE_ERROR":
info_window = sulkuMessages.HANDSHAKE_ERROR
elif excepcion == "GENERAL":
info_window = sulkuMessages.GENERAL
elif excepcion == "NO_FACE":
info_window = sulkuMessages.NO_FACE
elif excepcion == "NO_FILE":
info_window = sulkuMessages.NO_FILE
elif excepcion == "NO_POSITION": #Solo aplíca para Splashmix.
info_window = sulkuMessages.NO_POSITION
elif "quota" in excepcion: #Caso especial porque el texto cambiará citando la cuota.
info_window = excepcion
else:
info_window = sulkuMessages.ELSE
return info_window
def presentacionFinal(usuario, accion):
capsule = sulkuPypi.encripta(usuario).decode("utf-8") #decode es para quitarle el 'b
if accion == "debita":
tokens = sulkuPypi.debitTokens(capsule, globales.work, globales.env)
info_window = "Image ready!"
else:
info_window = "No face in source path detected."
tokens = sulkuPypi.getTokens(capsule, globales.env)
html_credits = visualizar_creditos(tokens, usuario)
return html_credits, info_window