File size: 3,888 Bytes
493749a
 
 
 
 
 
 
 
 
 
 
 
 
640ac93
 
 
33f307f
 
 
493749a
 
 
 
 
 
640ac93
493749a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
640ac93
 
 
33f307f
 
493749a
 
 
 
 
 
33f307f
493749a
 
 
 
 
640ac93
493749a
feb9e51
 
 
 
493749a
 
 
 
 
 
 
640ac93
493749a
 
 
 
 
 
 
 
 
 
 
640ac93
 
 
493749a
 
 
 
 
 
 
 
 
 
 
 
 
 
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
105
106
import gradio as gr
import sulkuPypi
from funciones import mass
from data import usuarios
import time
import nycklar.nodes
import tools
import auth
import ast

#Funciones

#Funci贸n principal
def perform(input1, input2, request: gr.Request):
    
    print("5.- Entr茅 a Perform, checando persistencia...")

    tokens = sulkuPypi.getTokens(sulkuPypi.encripta(request.username).decode("utf-8")) #Todo en una l铆nea.
    print("Ahora tienes 555: ", tokens) 
    
    #Despu茅s autoriza.
    #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.
    
    autorizacion = sulkuPypi.authorize(gr.State.tokens, 'picswap')
    print("La autorizaci贸n es: ", autorizacion)
      
    if autorizacion is True: 
    
        path = mass(input1,input2)

    else:
        info_window = "Out of credits..."
        path = 'no-credits.png'
        return path, info_window, html_credits, btn_buy

    print(f"El path final fue {path}, si es no-result, no debites y controla la info window.")
    print(f"El type de path es: ", type(path))    

    print("Convirtiendo path a string...")
    path_string = str(path)    
    
    print("Path_string = ", path_string)

    if "no-source-face" not in path_string:
        #Si el path NO tiene no-result, todo funcion贸 bien, por lo tanto debita.
        print("Se obtuvo un resultado, debitaremos.")
        #Y finalmente debita los tokens.
        #IMPORTANTE: Tienes que reconstruir capsule ahora que ya se obtiene del request, sino, capsule sera un State para el uso...
        #...de todos y es ah铆 donde radica el problema: 
        capsule = sulkuPypi.encripta(request.username).decode("utf-8") #decode es para quitarle el 'b
        tokens = sulkuPypi.debitTokens(capsule, "picswap")
        html_credits = tools.actualizar_creditos(tokens, request.username)
        print(f"html credits qued贸 como : {html_credits} y es del tipo: {type(html_credits)}")
        info_window = "Image ready!"
        
    else:
        print("No se detect贸 un rostro...")
        info_window = "No face in source path detected."
        html_credits = tools.actualizar_creditos(tokens, request.username)        
        #No se hizo un proceso, por lo tanto no debitaremos.
        #En el futuro, como regla de negocio, podr铆a cambiar y que si debitemos.  
    
    return path, info_window, html_credits, btn_buy

def display_tokens(request: gr.Request):

    print("182: Checando la persistencia de la informaci贸n cada vez...")        
    tokens = sulkuPypi.getTokens(sulkuPypi.encripta(request.username).decode("utf-8"))
    display = tools.actualizar_creditos(tokens, request.username)
    
    return display

#Inputs
source_image = gr.Image(label="Source")
destination_image = gr.Image(label="Destination")

#Outputs
print("1.- Entr茅 en app.py...")
creditos = None 
result_image = gr.Image(label="Blend Result")
txt_credits = gr.Textbox(label="Credits Available", value="", interactive=False)
html_credits = gr.HTML()
lbl_console = gr.Label(label="AI Terminal Messages", value="AI Engine ready...", container=True)
btn_buy = gr.Button("Buy More", visible=False, size='lg')

#valor = gr.State.tokens
valor = ""

with gr.Blocks(theme=gr.themes.Base(), css="footer {visibility: hidden}") as main:
      
    print("2.- Estoy imprimiendo desde blocks...")
    
    #Funci贸n, input, output
    main.load(display_tokens, None, html_credits) 
   
    with gr.Row():

        demo = gr.Interface(
            fn=perform,
            title="", 
            inputs=[source_image, destination_image], 
            outputs=[result_image, lbl_console, html_credits, btn_buy], 
            allow_flagging='never'
            )        
        
main.launch(auth=auth.authenticate)