Spaces:
Sleeping
Sleeping
Add Basic Files
Browse files- app.py +15 -3
- flux_capacitor.py +53 -0
- sulkuGateway.py +40 -0
- theApp.py +32 -0
app.py
CHANGED
@@ -10,15 +10,27 @@ def authenticate(username, password):
|
|
10 |
return False
|
11 |
|
12 |
#Función principal
|
13 |
-
def
|
14 |
|
15 |
var_secreta = os.environ.get("huggingface_key")
|
16 |
print("Esto es var_secreta: ", var_secreta)
|
17 |
|
18 |
return "Tervetuloa " + name + "!!"
|
19 |
|
20 |
-
iface = gr.Interface(fn=
|
21 |
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
#iface.launch(auth=("admin", "pass1234"))
|
24 |
#iface.launch(auth=authenticate)
|
|
|
10 |
return False
|
11 |
|
12 |
#Función principal
|
13 |
+
def runpy(access, name):
|
14 |
|
15 |
var_secreta = os.environ.get("huggingface_key")
|
16 |
print("Esto es var_secreta: ", var_secreta)
|
17 |
|
18 |
return "Tervetuloa " + name + "!!"
|
19 |
|
20 |
+
#iface = gr.Interface(fn=runpy, inputs="text", outputs="text")
|
21 |
|
22 |
+
with gr.Blocks() as demo:
|
23 |
+
|
24 |
+
input_userfile = gr.Text(label="Userfile")
|
25 |
+
input_content = gr.Text(label="content")
|
26 |
+
btn = gr.Button(value="Submit")
|
27 |
+
output_tokens = gr.Text(label="Tokens")
|
28 |
+
output_resultado = gr.Text(label="Resultado")
|
29 |
+
|
30 |
+
#Actions
|
31 |
+
btn.click(runpy, inputs=[input_userfile, input_content], outputs=[output_tokens, output_resultado])
|
32 |
+
|
33 |
+
demo.launch()
|
34 |
+
#iface.launch()
|
35 |
#iface.launch(auth=("admin", "pass1234"))
|
36 |
#iface.launch(auth=authenticate)
|
flux_capacitor.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import theApp
|
2 |
+
import nodes
|
3 |
+
import sulkuGateway
|
4 |
+
import time
|
5 |
+
|
6 |
+
#Flux capacitor une la Autenticación y la App.
|
7 |
+
def do(access, content):
|
8 |
+
|
9 |
+
print("Entré a flux_capacitor.do...")
|
10 |
+
time.sleep(8)
|
11 |
+
#AUTENTICACIÓN#
|
12 |
+
#Obten cantidad de tokens dispobibles vía Sulku para ese usuario.
|
13 |
+
resultado = sulkuGateway.getTokens(access)
|
14 |
+
|
15 |
+
#Un try para convertirlo en entero porque quizá bajo alguna circunstancia no sea un número lo que regresa y marcará error al aplicarle int().
|
16 |
+
try:
|
17 |
+
resultado = int(resultado)
|
18 |
+
except ValueError:
|
19 |
+
# Manejar el error de conversión a entero
|
20 |
+
print("Error 401: return value is not int.")
|
21 |
+
|
22 |
+
except Exception as e:
|
23 |
+
# Manejar otros errores genéricos
|
24 |
+
print("Error 402: Unexpected error convertirng the result:", e)
|
25 |
+
|
26 |
+
#Si el resultado es un entero, es token.
|
27 |
+
if isinstance(resultado, int):
|
28 |
+
#El númmero si es un entero.
|
29 |
+
tokens = resultado
|
30 |
+
continuar = True
|
31 |
+
print("Access granted.")
|
32 |
+
print("Your amount of available tokens is: ", tokens)
|
33 |
+
else:
|
34 |
+
print("Message:", resultado)
|
35 |
+
continuar = False
|
36 |
+
|
37 |
+
#ACCIÓN#
|
38 |
+
print("Performing action, processing...")
|
39 |
+
if continuar is True and theApp.saldoParaAccion(tokens):
|
40 |
+
resultado = theApp.getResult(content)
|
41 |
+
print("Process completed.")
|
42 |
+
else:
|
43 |
+
print(f"Message: Not enough tokens to perform {nodes.work} action.")
|
44 |
+
resultado = 0
|
45 |
+
|
46 |
+
#CHARGE TOKENS
|
47 |
+
#Si se generó un resultado de AI entonces si hay que debitar el token correspondiente basado en la regla propia de la app.
|
48 |
+
if resultado != 0:
|
49 |
+
sulkuGateway.debitTokens(access, nodes.work)
|
50 |
+
else:
|
51 |
+
"No tokens will be charged because no outcome was produced."
|
52 |
+
|
53 |
+
return tokens, resultado
|
sulkuGateway.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import time
|
2 |
+
import nodes
|
3 |
+
import requests
|
4 |
+
import gradio_client
|
5 |
+
|
6 |
+
def getTokens(sulkukey):
|
7 |
+
|
8 |
+
print("Entre a SulkiGateway...")
|
9 |
+
time.sleep(7)
|
10 |
+
|
11 |
+
print("A punto de hacer llamado a cliente de API via Sulku...")
|
12 |
+
time.sleep(3)
|
13 |
+
try:
|
14 |
+
client = gradio_client.Client("Moibe/sulku", verbose=False)
|
15 |
+
resultado = client.predict(sulkukey, api_name="/getTokens")
|
16 |
+
|
17 |
+
return resultado
|
18 |
+
|
19 |
+
# except gradio_client.exceptions.APIError as e:
|
20 |
+
# print(f"Error 404 calling the API: {e}")
|
21 |
+
# return None
|
22 |
+
except requests.exceptions.RequestException as e:
|
23 |
+
print(f"Error 405: Network error, {e}")
|
24 |
+
return None
|
25 |
+
|
26 |
+
|
27 |
+
def debitTokens(sulkukey, work):
|
28 |
+
|
29 |
+
client = gradio_client.Client(nodes.validator, nodes.hf_token, verbose=False)
|
30 |
+
|
31 |
+
tokens = client.predict(
|
32 |
+
sulkukey,
|
33 |
+
work,
|
34 |
+
api_name="/debitTokens"
|
35 |
+
)
|
36 |
+
|
37 |
+
print(f"Available tokens now: {tokens}.")
|
38 |
+
time.sleep(1)
|
39 |
+
|
40 |
+
return tokens
|
theApp.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import time
|
2 |
+
import nodes
|
3 |
+
import gradio_client
|
4 |
+
|
5 |
+
def saldoParaAccion(tokens):
|
6 |
+
#Donde 1 es el cobro por acción, es decir, tiene capacidad por lo menos de realizar una acción.
|
7 |
+
if tokens >= 1:
|
8 |
+
return True
|
9 |
+
|
10 |
+
else:
|
11 |
+
print("Not enough tokens to perform the action, add more.")
|
12 |
+
return False
|
13 |
+
|
14 |
+
|
15 |
+
def getResult(content):
|
16 |
+
|
17 |
+
print("Processing content: ", content)
|
18 |
+
time.sleep(1)
|
19 |
+
|
20 |
+
if nodes.work == "picswap":
|
21 |
+
print("Work specs ok, action: ", nodes.work)
|
22 |
+
time.sleep(1)
|
23 |
+
|
24 |
+
#Aquí es donde ya no será un llamado de api si no la app itself. Listo OK!.
|
25 |
+
result = f"y el resultado es: Tervetuluo {content} !"
|
26 |
+
#Revisa si la app en cuestión podría regresar algún resultado extraño además del esperado. Esto se revisa en la otra App.
|
27 |
+
return result
|
28 |
+
|
29 |
+
else:
|
30 |
+
print("Work specs aren't correct. Please fix or contact app.")
|
31 |
+
#Regresa cero si el trabajo no se realizó por haber especificado incorrectamente el Work.
|
32 |
+
return 0
|