Skip to content

Commit

Permalink
InitAPI dettached
Browse files Browse the repository at this point in the history
  • Loading branch information
Moibe committed Nov 25, 2024
1 parent 1f01c1d commit 6580836
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 76 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ bridges.py
/images/positions/
/data/__pycache__/
/configuracion/__pycache__/
/flagged/
/flagged/
**/__pycache__/
4 changes: 2 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ def iniciar():
#INTERFAZ
#Credit Related Elements
html_credits = gr.HTML(visible=True)
lbl_console = gr.Label(label="AI Terminal " + globales.version + " messages", value="AI Engine ready...", container=True)
lbl_console = gr.Label(label="AI Terminal " + globales.version + " messages", value="", container=True)
btn_buy = gr.Button("Get Credits", visible=False, size='lg')

#Customizable Inputs and Outputs
input1, result = inputs.inputs_selector(globales.seto)

with gr.Blocks(theme=globales.tema, css="footer {visibility: True}") as main:
#Cargado en Load: Función, input, output
main.load(sulkuFront.precarga, None, [lbl_console, html_credits])
main.load(sulkuFront.precarga, None, html_credits)
#main.load(sulkuFront.precarga, None, [lbl_console, html_credits])

with gr.Row():
Expand Down
7 changes: 5 additions & 2 deletions autorizador.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import ast
import globales
import sulkuPypi
import sulkuFront
import gradio as gr
import tools

def authenticate(username, password):
cadena_usuarios = sulkuPypi.getData(globales.aplicacion)
lista_usuarios = ast.literal_eval(cadena_usuarios)

lista_usuarios = ast.literal_eval(cadena_usuarios)
for u, p in lista_usuarios:
if username == u and password == p:
tools.initAPI()
return True
return False
Binary file removed configuracion/__pycache__/globales.cpython-310.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion configuracion/splashmix.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
selected_databank = Hotgirl
creacion = "Hotgirl" #"Superhero" o "Hotgirl"

positions_path = "superheroes"
positions_path = "girlsAllPositions"
# prob_position = 0.1
8 changes: 4 additions & 4 deletions funciones.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ def perform(input1, request: gr.Request):

#MASS es la que ejecuta la aplicación EXTERNA
def mass(input1):
("Entré a mass...")

client = gradio_client.Client(globales.api, hf_token=bridges.hug)
#client = gradio_client.Client("https://058d1a6dcdbaca0dcf.gradio.live/") #MiniProxy

imagenSource = gradio_client.handle_file(input1)
print("Ésto es imagenSource después de ser procesada: ", imagenSource)
imagenPosition = gradio_client.handle_file(splash_tools.getPosition())
creacion=splash_tools.creadorObjeto()
prompt = prompter.prompteador(creacion)

client = gradio_client.Client(globales.api, hf_token=bridges.hug)
#client = gradio_client.Client("https://058d1a6dcdbaca0dcf.gradio.live/") #MiniProxy

print("Llegué a la ejecución de la API dentro de Mass:")
try:
Expand Down
2 changes: 1 addition & 1 deletion globales.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import gradio as gr

#MAIN
version = "4.5.13"
version = "4.6.13"
env = "dev"
aplicacion = "superheroes-dev" #como se llama en tu repo y tu dominio.
#api = "Moibe/splashmix"
Expand Down
78 changes: 36 additions & 42 deletions sulkuFront.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,68 +7,39 @@
import threading
from huggingface_hub import HfApi

result_from_displayTokens = None
result_from_displayTokens = None
result_from_initAPI = None

def initAPI():
#PROCESO1
global result_from_initAPI

try:
repo_id = globales.api
api = HfApi(token=bridges.hug)
runtime = api.get_space_runtime(repo_id=repo_id)
print("Hardware: ", runtime.hardware)
print("Stage: ", runtime.stage)
#"RUNNING_BUILDING", "APP_STARTING", "SLEEPING", "RUNNING"
if runtime.stage == "SLEEPING":
print("Estoy durmiendo..., Despierta")
api.restart_space(repo_id=repo_id)
print("Desperando")
print("Hardware: ", runtime.hardware)
# #"cpu-basic"

# client = gradio_client.Client(globales.api, hf_token=bridges.hug)
# print("Éste cliente: ", client.api_url)
result_from_initAPI = runtime.stage
# client = None
except Exception as e:
print("No api, encendiendo: ", e)
result_from_initAPI = str(e)
def displayTokens(request: gr.Request):

print("API Result displayed: ", result_from_initAPI)
global result_from_displayTokens

def displayTokens(request: gr.Request):
#PROCESO2
print("Running 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)

global result_from_displayTokens

result_from_displayTokens = display
print("Tokens displayed: ", result_from_displayTokens)


def precarga(request: gr.Request):

global result_from_initAPI
global result_from_displayTokens
# global result_from_initAPI
# global result_from_displayTokens

thread1 = threading.Thread(target=initAPI)
#thread1 = threading.Thread(target=initAPI)
thread2 = threading.Thread(target=displayTokens, args=(request,))

thread1.start()
#thread1.start()
thread2.start()

thread1.join() # Espera a que el hilo 1 termine
#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_initAPI, result_from_displayTokens
return result_from_displayTokens

def visualizar_creditos(nuevos_creditos, usuario):

Expand All @@ -90,12 +61,35 @@ def noCredit(usuario):

def aError(usuario, tokens, excepcion):
#aError se usa para llenar todos los elementos visuales en el front.
info_window = tools.manejadorExcepciones(excepcion)
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 = "AI Engine Paused, ready soon."
elif excepcion == "RUNTIME_ERROR":
info_window = "Error building AI environment, please contact me."
elif excepcion == "STARTING":
info_window = "Server Powering UP, wait a few minutes and try again."
elif excepcion == "HANDSHAKE_ERROR":
info_window = "Connection error try again."
elif excepcion == "GENERAL":
info_window = "Network error, no credits were debited."
elif excepcion == "NO_FACE":
info_window = "Unable to detect a face in the image. Please upload a different photo with a clear face."
elif excepcion == "NO_FILE":
info_window = "No file, please add a valid archive."
elif "quota" in excepcion: #Caso especial porque el texto cambiará citando la cuota.
info_window = excepcion
else:
info_window = "Error. No credits were debited."

return info_window

def presentacionFinal(usuario, accion):

capsule = sulkuPypi.encripta(usuario).decode("utf-8") #decode es para quitarle el 'b
Expand Down
50 changes: 27 additions & 23 deletions tools.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import random
import traceback
import gradio as gr
import globales
from huggingface_hub import HfApi
import bridges

def theme_selector():
temas_posibles = [
Expand All @@ -14,9 +17,31 @@ def theme_selector():
print("Tema random: ", tema)
return tema

def titulizaExcepDeAPI(e):
#Resume una excepción a un título manejable.
def initAPI():

global result_from_initAPI

try:
repo_id = globales.api
api = HfApi(token=bridges.hug)
runtime = api.get_space_runtime(repo_id=repo_id)
print("Stage: ", runtime.stage)
#"RUNNING_BUILDING", "APP_STARTING", "SLEEPING", "RUNNING", "PAUSED", "RUNTIME_ERROR"
if runtime.stage == "SLEEPING":
api.restart_space(repo_id=repo_id)
print("Desperando")
print("Hardware: ", runtime.hardware)
result_from_initAPI = runtime.stage

except Exception as e:
#Creo que ya no debería de llegar aquí.
print("No api, encendiendo: ", e)
result_from_initAPI = str(e)

return result_from_initAPI

def titulizaExcepDeAPI(e):
#Resume una excepción a un título manejable.
if "RUNTIME_ERROR" in str(e):
resultado = "RUNTIME_ERROR" #api mal construida tiene error.
elif "PAUSED" in str(e):
Expand All @@ -36,28 +61,7 @@ def titulizaExcepDeAPI(e):

return resultado

def manejadorExcepciones(excepcion):
#El parámetro que recibe es el texto despliega ante determinada excepción:
if excepcion == "PAUSED":
info_window = "AI Engine Paused, ready soon."
elif excepcion == "RUNTIME_ERROR":
info_window = "Error building AI environment, please contact me."
elif excepcion == "STARTING":
info_window = "Server Powering UP, wait a few minutes and try again."
elif excepcion == "HANDSHAKE_ERROR":
info_window = "Connection error try again."
elif excepcion == "GENERAL":
info_window = "Network error, no credits were debited."
elif excepcion == "NO_FACE":
info_window = "Unable to detect a face in the image. Please upload a different photo with a clear face."
elif excepcion == "NO_FILE":
info_window = "No file, please add a valid archive."
elif "quota" in excepcion: #Caso especial porque el texto cambiará citando la cuota.
info_window = excepcion
else:
info_window = "Error. No credits were debited."

return info_window

def recortadorQuota(texto_quota):
# Encontrar el índice de inicio (después de "exception:")
Expand Down

0 comments on commit 6580836

Please sign in to comment.