Spaces:
Running
Running
Prod Ready
Browse files- app.py +7 -1
- archivos/last_timestamp.txt +1 -0
- archivos/seconds_available.txt +1 -0
- config.ini +0 -1
- funciones.py +4 -0
- globales.py +3 -3
- herramientas.py +111 -2
- test.py +3 -0
app.py
CHANGED
@@ -3,6 +3,7 @@ from fastapi import FastAPI, File, UploadFile
|
|
3 |
from fastapi.responses import StreamingResponse, FileResponse
|
4 |
from io import BytesIO
|
5 |
import funciones, globales
|
|
|
6 |
|
7 |
app = FastAPI()
|
8 |
|
@@ -17,7 +18,12 @@ async def echo_image(image: UploadFile = File(...)):
|
|
17 |
@app.post("/genera-imagen/")
|
18 |
async def genera_imagen(platillo: str = Form(...)):
|
19 |
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
21 |
print("GPU...")
|
22 |
resultado = funciones.genera_platillo_gpu(platillo)
|
23 |
return FileResponse(resultado, media_type="image/png", filename="imagen.png")
|
|
|
3 |
from fastapi.responses import StreamingResponse, FileResponse
|
4 |
from io import BytesIO
|
5 |
import funciones, globales
|
6 |
+
import herramientas
|
7 |
|
8 |
app = FastAPI()
|
9 |
|
|
|
18 |
@app.post("/genera-imagen/")
|
19 |
async def genera_imagen(platillo: str = Form(...)):
|
20 |
|
21 |
+
#Obtengo antes los segundos disponibles para saber si tengo GPU disponible o me voy a inferencia.
|
22 |
+
seconds_available = herramientas.obtenSegundosDisponibles()
|
23 |
+
print("Los segundos disponibles obtenidos son: ", seconds_available)
|
24 |
+
|
25 |
+
|
26 |
+
if seconds_available > globales.work_cost:
|
27 |
print("GPU...")
|
28 |
resultado = funciones.genera_platillo_gpu(platillo)
|
29 |
return FileResponse(resultado, media_type="image/png", filename="imagen.png")
|
archivos/last_timestamp.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
1744393289
|
archivos/seconds_available.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
1488
|
config.ini
CHANGED
@@ -3,7 +3,6 @@ llave = herramientas.obtenAccesoHF()
|
|
3 |
|
4 |
[hugging_face]
|
5 |
previo = "Una fotografía de un plato blanco con "
|
6 |
-
seconds_available = 1500
|
7 |
espacio = "black-forest-labs/FLUX.1-schnell"
|
8 |
inferencia = "black-forest-labs/FLUX.1-dev"
|
9 |
proveedor = "hf-inference"
|
|
|
3 |
|
4 |
[hugging_face]
|
5 |
previo = "Una fotografía de un plato blanco con "
|
|
|
6 |
espacio = "black-forest-labs/FLUX.1-schnell"
|
7 |
inferencia = "black-forest-labs/FLUX.1-dev"
|
8 |
proveedor = "hf-inference"
|
funciones.py
CHANGED
@@ -2,6 +2,7 @@ from huggingface_hub import InferenceClient
|
|
2 |
import gradio_client
|
3 |
import io
|
4 |
import globales
|
|
|
5 |
|
6 |
|
7 |
def genera_platillo_gpu(platillo):
|
@@ -29,6 +30,9 @@ def genera_platillo_gpu(platillo):
|
|
29 |
# num_inference_steps=28,
|
30 |
# api_name="/infer"
|
31 |
)
|
|
|
|
|
|
|
32 |
|
33 |
return result[0]
|
34 |
|
|
|
2 |
import gradio_client
|
3 |
import io
|
4 |
import globales
|
5 |
+
import herramientas
|
6 |
|
7 |
|
8 |
def genera_platillo_gpu(platillo):
|
|
|
30 |
# num_inference_steps=28,
|
31 |
# api_name="/infer"
|
32 |
)
|
33 |
+
|
34 |
+
#Cuando es GPU, debe de restar segundos disponibles de HF
|
35 |
+
herramientas.restaSegundosGPU(globales.work_cost)
|
36 |
|
37 |
return result[0]
|
38 |
|
globales.py
CHANGED
@@ -3,10 +3,10 @@ import herramientas
|
|
3 |
previo = "Una fotografía de un plato blanco con "
|
4 |
llave = herramientas.obtenAccesoHF()
|
5 |
|
6 |
-
seconds_available = 1500
|
7 |
-
|
8 |
espacio = "black-forest-labs/FLUX.1-schnell"
|
9 |
#espacio = "black-forest-labs/FLUX.1-dev"
|
10 |
inferencia = "black-forest-labs/FLUX.1-dev"
|
11 |
|
12 |
-
proveedor = "hf-inference"
|
|
|
|
|
|
3 |
previo = "Una fotografía de un plato blanco con "
|
4 |
llave = herramientas.obtenAccesoHF()
|
5 |
|
|
|
|
|
6 |
espacio = "black-forest-labs/FLUX.1-schnell"
|
7 |
#espacio = "black-forest-labs/FLUX.1-dev"
|
8 |
inferencia = "black-forest-labs/FLUX.1-dev"
|
9 |
|
10 |
+
proveedor = "hf-inference"
|
11 |
+
|
12 |
+
work_cost = 12
|
herramientas.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import os
|
2 |
import socket
|
|
|
3 |
|
4 |
def obtenAccesoHF():
|
5 |
if local_check():
|
@@ -12,7 +13,6 @@ def obtenAccesoHF():
|
|
12 |
|
13 |
print("Ésto es llave:", llave)
|
14 |
|
15 |
-
|
16 |
return llave
|
17 |
|
18 |
def local_check():
|
@@ -26,4 +26,113 @@ def local_check():
|
|
26 |
return False
|
27 |
else:
|
28 |
print("Ejecutando en local")
|
29 |
-
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
import socket
|
3 |
+
import time
|
4 |
|
5 |
def obtenAccesoHF():
|
6 |
if local_check():
|
|
|
13 |
|
14 |
print("Ésto es llave:", llave)
|
15 |
|
|
|
16 |
return llave
|
17 |
|
18 |
def local_check():
|
|
|
26 |
return False
|
27 |
else:
|
28 |
print("Ejecutando en local")
|
29 |
+
return True
|
30 |
+
|
31 |
+
def obtenUltimoTimestamp():
|
32 |
+
"""
|
33 |
+
Obtiene el último timestamp de renovación guardado.
|
34 |
+
"""
|
35 |
+
|
36 |
+
archivo_ruta = "archivos/last_timestamp.txt"
|
37 |
+
|
38 |
+
try:
|
39 |
+
with open(archivo_ruta, 'r') as archivo:
|
40 |
+
contenido = archivo.readline().strip()
|
41 |
+
ultimo_timestamp = int(contenido)
|
42 |
+
return ultimo_timestamp
|
43 |
+
except FileNotFoundError:
|
44 |
+
print(f"Error: El archivo '{archivo_ruta}' no fue encontrado.")
|
45 |
+
return
|
46 |
+
except ValueError:
|
47 |
+
print(f"Error: El contenido del archivo '{archivo_ruta}' no es un número entero válido.")
|
48 |
+
return
|
49 |
+
|
50 |
+
def nuevoDia():
|
51 |
+
"""
|
52 |
+
Compara dos timestamps (en formato string ISO) y devuelve True si han
|
53 |
+
pasado más de 24 horas entre ellos, False en caso contrario.
|
54 |
+
"""
|
55 |
+
|
56 |
+
timestamp_original = obtenUltimoTimestamp()
|
57 |
+
timestamp_actual = int(time.time())
|
58 |
+
|
59 |
+
try:
|
60 |
+
segundos_en_24_horas = 24 * 60 * 60
|
61 |
+
diferencia_en_segundos = abs(timestamp_actual - timestamp_original)
|
62 |
+
return diferencia_en_segundos > segundos_en_24_horas
|
63 |
+
except ValueError:
|
64 |
+
print("Error: Formato de timestamp incorrecto.")
|
65 |
+
return False
|
66 |
+
|
67 |
+
def obtenSegundosDisponibles():
|
68 |
+
|
69 |
+
print("Estoy en obten segundos disponibles...")
|
70 |
+
|
71 |
+
if nuevoDia() == True:
|
72 |
+
renuevaSegundosDisponibles()
|
73 |
+
|
74 |
+
archivo_ruta = "archivos/seconds_available.txt"
|
75 |
+
|
76 |
+
try:
|
77 |
+
# Leer el número actual de segundos disponibles
|
78 |
+
with open(archivo_ruta, 'r') as archivo:
|
79 |
+
contenido = archivo.readline().strip()
|
80 |
+
segundos_disponibles = int(contenido)
|
81 |
+
return segundos_disponibles
|
82 |
+
except FileNotFoundError:
|
83 |
+
print(f"Error: El archivo '{archivo_ruta}' no fue encontrado.")
|
84 |
+
return
|
85 |
+
except ValueError:
|
86 |
+
print(f"Error: El contenido del archivo '{archivo_ruta}' no es un número entero válido.")
|
87 |
+
return
|
88 |
+
|
89 |
+
def renuevaSegundosDisponibles():
|
90 |
+
|
91 |
+
print("Estoy en renuevaSegundosDisponibles...")
|
92 |
+
|
93 |
+
archivo_ruta = "archivos/seconds_available.txt"
|
94 |
+
|
95 |
+
# Guardar el nuevo número en el archivo
|
96 |
+
try:
|
97 |
+
with open(archivo_ruta, 'w') as archivo:
|
98 |
+
archivo.write(str(1500))
|
99 |
+
print(f"Se renovaron los 1500 segundos disponibles.")
|
100 |
+
renuevaTimestampActual()
|
101 |
+
except Exception as e:
|
102 |
+
print(f"Error al escribir en el archivo '{archivo_ruta}': {e}")
|
103 |
+
|
104 |
+
def renuevaTimestampActual():
|
105 |
+
|
106 |
+
print("Estoy en renuevatimestmap actual...")
|
107 |
+
archivo_ruta = "archivos/last_timestamp.txt"
|
108 |
+
|
109 |
+
timestamp_actual = int(time.time())
|
110 |
+
|
111 |
+
# Guardar el nuevo número en el archivo
|
112 |
+
try:
|
113 |
+
with open(archivo_ruta, 'w') as archivo:
|
114 |
+
archivo.write(str(timestamp_actual))
|
115 |
+
print(f"Se renovó por el timestamp de éste momento.")
|
116 |
+
except Exception as e:
|
117 |
+
print(f"Error al escribir en el archivo '{archivo_ruta}': {e}")
|
118 |
+
|
119 |
+
def restaSegundosGPU(segundos):
|
120 |
+
"""
|
121 |
+
Lee el número de segundos disponibles desde seconds_available.txt,
|
122 |
+
resta los segundos dados como parámetro y guarda el nuevo valor en el archivo.
|
123 |
+
"""
|
124 |
+
|
125 |
+
print("Estoy en resta segundos...")
|
126 |
+
archivo_ruta = "archivos/seconds_available.txt"
|
127 |
+
segundos_disponibles = obtenSegundosDisponibles()
|
128 |
+
|
129 |
+
# Restar los segundos
|
130 |
+
nuevos_segundos_disponibles = segundos_disponibles - segundos
|
131 |
+
|
132 |
+
# Guardar el nuevo número en el archivo
|
133 |
+
try:
|
134 |
+
with open(archivo_ruta, 'w') as archivo:
|
135 |
+
archivo.write(str(nuevos_segundos_disponibles))
|
136 |
+
print(f"Se restaron {segundos} segundos. Ahora quedan {nuevos_segundos_disponibles} segundos disponibles.")
|
137 |
+
except Exception as e:
|
138 |
+
print(f"Error al escribir en el archivo '{archivo_ruta}': {e}")
|
test.py
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
import herramientas
|
2 |
+
|
3 |
+
print(herramientas.nuevoDia())
|