idk
Browse files- Dockerfile +0 -2
- api/core/app.py +2 -1
- api/core/controllers/text2image.py +29 -13
- api/main.py +7 -2
Dockerfile
CHANGED
|
@@ -14,8 +14,6 @@ RUN apt update && \
|
|
| 14 |
python3-pip \
|
| 15 |
python3.8-venv && \
|
| 16 |
rm -rf /var/lib/apt/lists
|
| 17 |
-
|
| 18 |
-
RUN python3 -m venv /opt/venv
|
| 19 |
WORKDIR /code
|
| 20 |
COPY ./requirements.txt /code/requirements.txt
|
| 21 |
RUN python3 -m pip install --no-cache-dir --upgrade pip && \
|
|
|
|
| 14 |
python3-pip \
|
| 15 |
python3.8-venv && \
|
| 16 |
rm -rf /var/lib/apt/lists
|
|
|
|
|
|
|
| 17 |
WORKDIR /code
|
| 18 |
COPY ./requirements.txt /code/requirements.txt
|
| 19 |
RUN python3 -m pip install --no-cache-dir --upgrade pip && \
|
api/core/app.py
CHANGED
|
@@ -1,8 +1,9 @@
|
|
| 1 |
class Demos:
|
| 2 |
def __init__(self):
|
| 3 |
-
from fastapi import FastAPI, HTTPException
|
| 4 |
self.api = FastAPI
|
| 5 |
self.exception = HTTPException
|
|
|
|
| 6 |
def validate_apikey(self,api_key)->bool:
|
| 7 |
__validation = True
|
| 8 |
return __validation
|
|
|
|
| 1 |
class Demos:
|
| 2 |
def __init__(self):
|
| 3 |
+
from fastapi import FastAPI, HTTPException, Response
|
| 4 |
self.api = FastAPI
|
| 5 |
self.exception = HTTPException
|
| 6 |
+
self.api_response = Response
|
| 7 |
def validate_apikey(self,api_key)->bool:
|
| 8 |
__validation = True
|
| 9 |
return __validation
|
api/core/controllers/text2image.py
CHANGED
|
@@ -1,45 +1,61 @@
|
|
| 1 |
from diffusers import DiffusionPipeline as Pipe
|
| 2 |
import torch
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
class Generador:
|
| 5 |
-
def
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
_generador = Pipe.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16)
|
| 8 |
_generador.to("cuda")
|
| 9 |
_imagen = _generador(prompt).images[0]
|
| 10 |
-
_response =
|
| 11 |
except Exception as e:
|
| 12 |
-
_response =
|
| 13 |
finally:
|
| 14 |
return _response
|
| 15 |
-
def using_stability_sd_21(prompt:str)->
|
| 16 |
try:
|
| 17 |
_generador = Pipe.from_pretrained("stabilityai/stable-diffusion-2-1", torch_dtype=torch.float16)
|
| 18 |
_generador.to("cuda")
|
| 19 |
_imagen = _generador(prompt).images[0]
|
| 20 |
-
_response =
|
| 21 |
except Exception as e:
|
| 22 |
-
_response =
|
| 23 |
finally:
|
| 24 |
return _response
|
| 25 |
-
def using_realistic_v14(prompt:str)->
|
| 26 |
try:
|
| 27 |
_generador = Pipe.from_pretrained("SG161222/Realistic_Vision_V1.4", torch_dtype=torch.float16)
|
| 28 |
_generador.to("cuda")
|
| 29 |
_imagen = _generador(prompt).images[0]
|
| 30 |
-
_response =
|
| 31 |
except Exception as e:
|
| 32 |
-
_response =
|
| 33 |
finally:
|
| 34 |
return _response
|
| 35 |
-
def using_prompthero_openjourney(prompt:str)->
|
| 36 |
try:
|
| 37 |
_generador = Pipe.from_pretrained("prompthero/openjourney", torch_dtype=torch.float16)
|
| 38 |
_generador.to("cuda")
|
| 39 |
_imagen = _generador(prompt).images[0]
|
| 40 |
-
_response =
|
| 41 |
except Exception as e:
|
| 42 |
-
_response =
|
| 43 |
finally:
|
| 44 |
return _response
|
| 45 |
|
|
|
|
| 1 |
from diffusers import DiffusionPipeline as Pipe
|
| 2 |
import torch
|
| 3 |
|
| 4 |
+
class Converter:
|
| 5 |
+
import io
|
| 6 |
+
from PIL import Image
|
| 7 |
+
|
| 8 |
+
img = Image.open(fh, mode='r')
|
| 9 |
+
roi_img = img.crop(box)
|
| 10 |
+
|
| 11 |
+
img_byte_arr = io.BytesIO()
|
| 12 |
+
roi_img.save(img_byte_arr, format='PNG')
|
| 13 |
+
img_byte_arr = img_byte_arr.getvalue()
|
| 14 |
+
|
| 15 |
class Generador:
|
| 16 |
+
def img_to_bytes(image) -> bytes:
|
| 17 |
+
import io
|
| 18 |
+
_imgByteArr = io.BytesIO()
|
| 19 |
+
image.save(_imgByteArr, format=image.format)
|
| 20 |
+
return _imgByteArr.getvalue()
|
| 21 |
+
def using_runway_sd_15(prompt:str)->bytes|None:
|
| 22 |
+
try:
|
| 23 |
_generador = Pipe.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16)
|
| 24 |
_generador.to("cuda")
|
| 25 |
_imagen = _generador(prompt).images[0]
|
| 26 |
+
_response = Generador.img_to_bytes(img=_imagen)
|
| 27 |
except Exception as e:
|
| 28 |
+
_response = None
|
| 29 |
finally:
|
| 30 |
return _response
|
| 31 |
+
def using_stability_sd_21(prompt:str)->bytes|None:
|
| 32 |
try:
|
| 33 |
_generador = Pipe.from_pretrained("stabilityai/stable-diffusion-2-1", torch_dtype=torch.float16)
|
| 34 |
_generador.to("cuda")
|
| 35 |
_imagen = _generador(prompt).images[0]
|
| 36 |
+
_response = Generador.img_to_bytes(img=_imagen)
|
| 37 |
except Exception as e:
|
| 38 |
+
_response = None
|
| 39 |
finally:
|
| 40 |
return _response
|
| 41 |
+
def using_realistic_v14(prompt:str)->bytes|None:
|
| 42 |
try:
|
| 43 |
_generador = Pipe.from_pretrained("SG161222/Realistic_Vision_V1.4", torch_dtype=torch.float16)
|
| 44 |
_generador.to("cuda")
|
| 45 |
_imagen = _generador(prompt).images[0]
|
| 46 |
+
_response = Generador.img_to_bytes(img=_imagen)
|
| 47 |
except Exception as e:
|
| 48 |
+
_response = None
|
| 49 |
finally:
|
| 50 |
return _response
|
| 51 |
+
def using_prompthero_openjourney(prompt:str)->bytes|None:
|
| 52 |
try:
|
| 53 |
_generador = Pipe.from_pretrained("prompthero/openjourney", torch_dtype=torch.float16)
|
| 54 |
_generador.to("cuda")
|
| 55 |
_imagen = _generador(prompt).images[0]
|
| 56 |
+
_response = Generador.img_to_bytes(img=_imagen)
|
| 57 |
except Exception as e:
|
| 58 |
+
_response = None
|
| 59 |
finally:
|
| 60 |
return _response
|
| 61 |
|
api/main.py
CHANGED
|
@@ -82,7 +82,7 @@ def get_resumen(data:dict) -> dict:
|
|
| 82 |
finally:
|
| 83 |
return __response
|
| 84 |
|
| 85 |
-
@api.post("/texto_a_imagen/", status_code=201)
|
| 86 |
def get_text2img(data:dict) -> dict:
|
| 87 |
__response=dict({"request_data":data})
|
| 88 |
try:
|
|
@@ -97,7 +97,7 @@ def get_text2img(data:dict) -> dict:
|
|
| 97 |
#To-do ->agregar mas información en el error fecha, usuario, reqs
|
| 98 |
raise __main.exception(status_code = 403, datail=e)
|
| 99 |
finally:
|
| 100 |
-
return __response
|
| 101 |
|
| 102 |
@api.post("/texto_a_video/", status_code=201)
|
| 103 |
def get_text2video(data:dict) -> dict:
|
|
@@ -130,3 +130,8 @@ def get_text2speach(data:dict) -> dict:
|
|
| 130 |
raise __main.exception(status_code = 403, datail=e)
|
| 131 |
finally:
|
| 132 |
return __response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
finally:
|
| 83 |
return __response
|
| 84 |
|
| 85 |
+
@api.post("/texto_a_imagen/", status_code=201, responses = {201: {"content": {"image/png": {}}}} ,response_class=__main.api_response)
|
| 86 |
def get_text2img(data:dict) -> dict:
|
| 87 |
__response=dict({"request_data":data})
|
| 88 |
try:
|
|
|
|
| 97 |
#To-do ->agregar mas información en el error fecha, usuario, reqs
|
| 98 |
raise __main.exception(status_code = 403, datail=e)
|
| 99 |
finally:
|
| 100 |
+
return __main.api_response(content=__response, media_type="image/png")
|
| 101 |
|
| 102 |
@api.post("/texto_a_video/", status_code=201)
|
| 103 |
def get_text2video(data:dict) -> dict:
|
|
|
|
| 130 |
raise __main.exception(status_code = 403, datail=e)
|
| 131 |
finally:
|
| 132 |
return __response
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
|