Moibe commited on
Commit
d90d6a6
·
1 Parent(s): 5b38336

Distribución de poder de procesamiento

Browse files
Files changed (5) hide show
  1. app.py +14 -19
  2. funciones.py +50 -10
  3. globales.py +7 -0
  4. requirements.txt +1 -0
  5. tester.py +0 -3
app.py CHANGED
@@ -1,12 +1,8 @@
 
1
  from fastapi import FastAPI, File, UploadFile
2
- from fastapi.responses import StreamingResponse
3
-
4
- import io
5
  from io import BytesIO
6
-
7
- from fastapi import FastAPI, Form
8
-
9
- import funciones
10
 
11
  app = FastAPI()
12
 
@@ -18,15 +14,14 @@ async def echo_image(image: UploadFile = File(...)):
18
  contents = await image.read()
19
  return StreamingResponse(BytesIO(contents), media_type=image.content_type)
20
 
21
- @app.post("/get-platillo/")
22
- async def get_platillo_image(prompt: str = Form(...)):
23
-
24
- imagen_pil = funciones.genera_platillo(prompt)
25
-
26
- img_io = io.BytesIO()
27
- imagen_pil.save(img_io, "PNG")
28
- img_io.seek(0)
29
-
30
- #Ver cual es el mejor resultado para backend, si forzar la disposición de archivo o permitir que el navegador decida (puede que sea irrelevante para un consumo de la api directo)
31
- #return StreamingResponse(content=img_io, media_type="image/png", headers={"Content-Disposition": "attachment; filename=platillo.png"})
32
- return StreamingResponse(content=img_io, media_type="image/png")
 
1
+ from fastapi import FastAPI, Form
2
  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
 
 
14
  contents = await image.read()
15
  return StreamingResponse(BytesIO(contents), media_type=image.content_type)
16
 
17
+ @app.post("/genera-imagen/")
18
+ async def genera_imagen(platillo: str = Form(...)):
19
+
20
+ if globales.seconds_available > 25:
21
+ print("GPU...")
22
+ resultado = funciones.genera_platillo_gpu(platillo)
23
+ return FileResponse(resultado, media_type="image/png", filename="imagen.png")
24
+ else:
25
+ print("Inference...")
26
+ resultado = funciones.genera_platillo_inference(platillo)
27
+ return StreamingResponse(content=resultado, media_type="image/png")
 
funciones.py CHANGED
@@ -1,22 +1,58 @@
1
  import bridges
2
  from huggingface_hub import InferenceClient
3
- from PIL import Image
 
 
4
 
5
- def genera_platillo(prompt):
6
- enlace = "black-forest-labs/FLUX.1-dev"
7
- proveedor = "hf-inference"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  client = InferenceClient(
10
- provider= proveedor,
11
  api_key=bridges.hug
12
- )
13
-
14
- #Testing change,
15
 
16
  try:
17
  image = client.text_to_image(
18
  prompt,
19
- model=enlace,
20
  #seed=42, #default varía pero el default es que siempre sea la misma.
21
  #guidance_scale=7.5,
22
  #num_inference_steps=50,
@@ -24,7 +60,11 @@ def genera_platillo(prompt):
24
  #height=1024 #El límite de replicate es 1024.
25
  )
26
 
27
- return image
 
 
 
 
28
 
29
  except Exception as e:
30
  print("Excepción es: ", e)
 
1
  import bridges
2
  from huggingface_hub import InferenceClient
3
+ import gradio_client
4
+ import io
5
+ import globales
6
 
7
+
8
+ previo = "Una fotografía de un plato blanco con "
9
+
10
+ def genera_platillo_gpu(platillo):
11
+
12
+ client = gradio_client.Client(globales.espacio, hf_token=bridges.hug)
13
+
14
+ prompt = previo + platillo
15
+
16
+ print("Eso es el prompt final:", prompt)
17
+
18
+ kwargs = {
19
+ "prompt": prompt,
20
+ "api_name": "/infer"
21
+ }
22
+
23
+
24
+ try:
25
+ result = client.predict(**kwargs
26
+ # prompt=prompt,
27
+ # negative_prompt="",
28
+ # seed=42,
29
+ # randomize_seed=True,
30
+ # width=1024,
31
+ # height=1024,
32
+ # guidance_scale=3.5,
33
+ # num_inference_steps=28,
34
+ # api_name="/infer"
35
+ )
36
+
37
+ return result[0]
38
+
39
+ except Exception as e:
40
+ print("Excepción es: ", e)
41
+
42
+
43
+ def genera_platillo_inference(platillo):
44
 
45
  client = InferenceClient(
46
+ provider= globales.proveedor,
47
  api_key=bridges.hug
48
+ )
49
+
50
+ prompt = previo + platillo
51
 
52
  try:
53
  image = client.text_to_image(
54
  prompt,
55
+ model=globales.inferencia,
56
  #seed=42, #default varía pero el default es que siempre sea la misma.
57
  #guidance_scale=7.5,
58
  #num_inference_steps=50,
 
60
  #height=1024 #El límite de replicate es 1024.
61
  )
62
 
63
+ img_io = io.BytesIO()
64
+ image.save(img_io, "PNG")
65
+ img_io.seek(0)
66
+
67
+ return img_io
68
 
69
  except Exception as e:
70
  print("Excepción es: ", e)
globales.py ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ seconds_available = 0
2
+
3
+ #espacio = "black-forest-labs/FLUX.1-schnell"
4
+ espacio = "black-forest-labs/FLUX.1-dev"
5
+ inferencia = "black-forest-labs/FLUX.1-dev"
6
+
7
+ proveedor = "hf-inference"
requirements.txt CHANGED
@@ -2,5 +2,6 @@ fastapi
2
  fastapi[standard]
3
 
4
  huggingface_hub
 
5
 
6
  Pillow
 
2
  fastapi[standard]
3
 
4
  huggingface_hub
5
+ gradio_client
6
 
7
  Pillow
tester.py DELETED
@@ -1,3 +0,0 @@
1
- import funciones
2
-
3
- funciones.genera_platillo()