coder160 commited on
Commit
594459e
·
1 Parent(s): d4812c0

update methods v0

Browse files
api/core/app.py CHANGED
@@ -1,4 +1,3 @@
1
-
2
  class Demos:
3
  def __init__(self):
4
  from fastapi import FastAPI, HTTPException
@@ -17,15 +16,17 @@ class Demos:
17
  return:
18
  _texto (str) : Texto extraído desde la fuente dada.
19
  """
 
20
  if from_url:
21
- _texto = str("URL")
 
22
  elif from_pdf:
23
  _texto = str("PDF")
24
  else:
25
  _texto = str("Ninguna opción seleccionada")
26
  return _texto
27
  @staticmethod
28
- def generar_bloques(texto:str=None,s:int=1500)->list:
29
  """Genera bloques de texto de longitud S, a partir de un texto plano.
30
 
31
  args:
@@ -34,7 +35,8 @@ class Demos:
34
  return:
35
  _bloques (list) : Lista de bloques de textos de longitud s.
36
  """
37
- _bloques = list()
 
38
  return _bloques
39
  @staticmethod
40
  def traducir(texto:str=None, idioma:str="EN/ES")->str:
@@ -46,13 +48,14 @@ class Demos:
46
  return:
47
  _traduccion (str) : Traducción del texto
48
  """
 
49
  _traduccion = str()
50
  if "EN/ES" in idioma.upper():
51
- _traduccion = "input inglés, output español"
52
  elif "ES/EN" in idioma.upper():
53
- _traduccion = "input español, output inglés"
54
  elif "AR/ES" in idioma.upper():
55
- _traduccion = "input arabe, output español"
56
  else:
57
  _traduccion = "Idioma no válido"
58
  return _traduccion
@@ -65,10 +68,11 @@ class Demos:
65
  return:
66
  _resumen (str) : Resumen generado
67
  """
68
- _bloques = list()
69
- return _bloques
 
70
  @staticmethod
71
- def text_to_img(texto:str=None)->str:
72
  """Genera un BITARRAY de la imagen con el texto dado.
73
 
74
  args:
@@ -76,8 +80,20 @@ class Demos:
76
  return:
77
  _img (str) : Imagen en BITARRAY
78
  """
79
- _img = str()
80
- return _img
 
 
 
 
 
 
 
 
 
 
 
 
81
  @staticmethod
82
  def text_to_video(texto:str=None)->str:
83
  """Genera un BITARRAY del video con el texto dado.
 
 
1
  class Demos:
2
  def __init__(self):
3
  from fastapi import FastAPI, HTTPException
 
16
  return:
17
  _texto (str) : Texto extraído desde la fuente dada.
18
  """
19
+ from api.core.controllers.extractor import TextFromURL
20
  if from_url:
21
+ with TextFromURL(url=from_url) as web_content:
22
+ _texto = web_content.get_raw().get_text() if web_content.is_ok() else web_content.get_error()
23
  elif from_pdf:
24
  _texto = str("PDF")
25
  else:
26
  _texto = str("Ninguna opción seleccionada")
27
  return _texto
28
  @staticmethod
29
+ def generar_bloques(texto:str=None,size:int=1500)->list:
30
  """Genera bloques de texto de longitud S, a partir de un texto plano.
31
 
32
  args:
 
35
  return:
36
  _bloques (list) : Lista de bloques de textos de longitud s.
37
  """
38
+ from api.core.controllers.handlers import Generator
39
+ _bloques = Generator.get_bloques(from_text=texto,size=size)
40
  return _bloques
41
  @staticmethod
42
  def traducir(texto:str=None, idioma:str="EN/ES")->str:
 
48
  return:
49
  _traduccion (str) : Traducción del texto
50
  """
51
+ from api.core.controllers.text2text import Traductor
52
  _traduccion = str()
53
  if "EN/ES" in idioma.upper():
54
+ _traduccion = Traductor.EN_ES(texto=texto)
55
  elif "ES/EN" in idioma.upper():
56
+ _traduccion = Traductor.ES_EN(texto=texto)
57
  elif "AR/ES" in idioma.upper():
58
+ _traduccion = Traductor.AR_ES(texto=texto)
59
  else:
60
  _traduccion = "Idioma no válido"
61
  return _traduccion
 
68
  return:
69
  _resumen (str) : Resumen generado
70
  """
71
+ from api.core.controllers.text2text import Abstractor
72
+ _resumen = Abstractor.resumen(texto=texto)
73
+ return _resumen
74
  @staticmethod
75
+ def text_to_img(texto:str=None, model:str="PROMPTHERO")->list:
76
  """Genera un BITARRAY de la imagen con el texto dado.
77
 
78
  args:
 
80
  return:
81
  _img (str) : Imagen en BITARRAY
82
  """
83
+
84
+ from api.core.controllers.text2image import Generador
85
+ _imagen = str()
86
+ if "RUNWAY" in model.upper():
87
+ _imagen = Generador.using_runway_sd_15(prompt=texto)
88
+ elif "STABILITY" in model.upper():
89
+ _imagen = Generador.using_stability_sd_21(prompt=texto)
90
+ elif "REALISTIC" in model.upper():
91
+ _imagen = Generador.using_realistic_v14(prompt=texto)
92
+ elif "PROMPTHERO" in model.upper():
93
+ _imagen = Generador.using_prompthero_openjourney(prompt=texto)
94
+ else:
95
+ _imagen = list(["error"])
96
+ return _imagen
97
  @staticmethod
98
  def text_to_video(texto:str=None)->str:
99
  """Genera un BITARRAY del video con el texto dado.
api/core/controllers/extractor.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ class TextFromURL:
2
+ __raw = None
3
+ def __init__(self, url:str):
4
+ self.__source=url
5
+ def __enter__(self):
6
+ try:
7
+ import requests as REQS
8
+ import bs4 as BS4
9
+ self.__raw = BS4.BeautifulSoup(REQS.get(self.get_source()).content, "html.parser")
10
+ except Exception as er:
11
+ print(er)
12
+ self.__error = er
13
+ finally:
14
+ return self
15
+ def __exit__(self, *args):
16
+ [print(e) for e in args if e is not None]
17
+ def get_source(self):
18
+ return self.__source
19
+ def get_raw(self):
20
+ return self.__raw
21
+ def get_error(self):
22
+ return self.__error
23
+ def is_ok(self):
24
+ return True if self.get_raw() else False
api/core/controllers/handlers.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ class Generator:
2
+ def get_bloques(from_text:str, size:int=1500):
3
+ _bloques = list()
4
+ _t = str(from_text)
5
+ if len(_t)>size:
6
+ _ = int(len(_t)/2)
7
+ _ts = [_t[n:n+_] for n in range(0,len(_t),_)]
8
+ for m in [0,1]:
9
+ _np = [nt for nt in Generator.get_bloques(from_text=_ts[m],size=size)]
10
+ _bloques+=_np
11
+ else:
12
+ _bloques.append(_t)
13
+ return _bloques
api/core/controllers/text2image.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from diffusers import DiffusionPipeline as Pipe
2
+ import torch
3
+
4
+ class Generador:
5
+ def using_runway_sd_15(prompt:str)->list:
6
+ _generador = Pipe.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16)
7
+ _generador.to("cuda")
8
+ _imagen = _generador(prompt).images[0]
9
+ return list(_imagen.getdata())
10
+ def using_stability_sd_21(prompt:str)->list:
11
+ _generador = Pipe.from_pretrained("stabilityai/stable-diffusion-2-1", torch_dtype=torch.float16)
12
+ _generador.to("cuda")
13
+ _imagen = _generador(prompt).images[0]
14
+ return list(_imagen.getdata())
15
+ def using_realistic_v14(prompt:str)->list:
16
+ _generador = Pipe.from_pretrained("SG161222/Realistic_Vision_V1.4", torch_dtype=torch.float16)
17
+ _generador.to("cuda")
18
+ _imagen = _generador(prompt).images[0]
19
+ return list(_imagen.getdata())
20
+ def using_prompthero_openjourney(prompt:str)->list:
21
+ _generador = Pipe.from_pretrained("prompthero/openjourney", torch_dtype=torch.float16)
22
+ _generador.to("cuda")
23
+ _imagen = _generador(prompt).images[0]
24
+ return list(_imagen.getdata())
25
+
api/core/controllers/text2speach.py ADDED
File without changes
api/core/controllers/text2text.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline as Pipe
2
+
3
+ class Traductor:
4
+ def EN_ES(texto:str)->str:
5
+ _traductor = Pipe("text2text-generation", model="Helsinki-NLP/opus-mt-en-es")
6
+ _traduccion = _traductor(texto)[0]
7
+ return _traduccion.get('generated_text')
8
+
9
+ def ES_EN(texto:str)->str:
10
+ _traductor = Pipe("text2text-generation", model="Helsinki-NLP/opus-mt-es-en")
11
+ _traduccion = _traductor(texto)[0]
12
+ return _traduccion.get('generated_text')
13
+
14
+ def AR_ES(texto:str)->str:
15
+ _traductor = Pipe("text2text-generation", model="Helsinki-NLP/opus-mt-ar-es")
16
+ _traduccion = _traductor(texto)[0]
17
+ return _traduccion.get('generated_text')
18
+
19
+ class Abstractor:
20
+ def resumen(texto:str)->str:
21
+ _abstractor = Pipe("text2text-generation", model="facebook/bart-large-cnn")
22
+ _resumen = _abstractor(texto)[0]
23
+ return _resumen.get('generated_text')
api/core/controllers/text2video.py ADDED
File without changes
api/main.py CHANGED
@@ -1,20 +1,9 @@
1
  from api.core.app import Demos
2
  __main = Demos()
3
  api = __main.api()
4
- @api.get("/", status_code=201)
5
- def home() -> dict:
6
- __response=dict({"request_data":None})
7
- try:
8
- __response['message']= "Bienvenido"
9
- except Exception as e:
10
- print(e)
11
- #To-do ->agregar mas información en el error fecha, usuario, reqs
12
- raise __main.exception(status_code = 403, datail=e)
13
- finally:
14
- return __response
15
 
16
  @api.post("/texto_desde_web/", status_code=201)
17
- def __get_text_from_url(data:dict) -> dict:
18
  __response=dict({"request_data":data})
19
  try:
20
  if data:
@@ -29,7 +18,7 @@ def __get_text_from_url(data:dict) -> dict:
29
  return __response
30
 
31
  @api.post("/texto_desde_pdf/", status_code=201)
32
- def __get_text_from_pdf(data:dict) -> dict:
33
  __response=dict({"request_data":data})
34
  try:
35
  if data:
@@ -44,13 +33,13 @@ def __get_text_from_pdf(data:dict) -> dict:
44
  return __response
45
 
46
  @api.post("/generar_bloques/", status_code=201)
47
- def __get_blocks(data:dict) -> dict:
48
  __response=dict({"request_data":data})
49
  try:
50
  if data:
51
  __response['original']=data.get('texto')
52
  __response['bloques']=__main.generar_bloques(texto=data.get('texto'),
53
- s=data.get('size'))
54
  else:
55
  raise __main.exception(status_code = 401, datail=f"Datos mal formados:\n{data}")
56
  except Exception as e:
@@ -61,7 +50,7 @@ def __get_blocks(data:dict) -> dict:
61
  return __response
62
 
63
  @api.post("/traducir/", status_code=201)
64
- def __get_traduccion(data:dict) -> dict:
65
  __response=dict({"request_data":data})
66
  try:
67
  if data:
@@ -78,7 +67,7 @@ def __get_traduccion(data:dict) -> dict:
78
  return __response
79
 
80
  @api.post("/resumir/", status_code=201)
81
- def __get_resumen(data:dict) -> dict:
82
  __response=dict({"request_data":data})
83
  try:
84
  if data:
@@ -94,7 +83,7 @@ def __get_resumen(data:dict) -> dict:
94
  return __response
95
 
96
  @api.post("/texto_a_imagen/", status_code=201)
97
- def __get_text2img(data:dict) -> dict:
98
  __response=dict({"request_data":data})
99
  try:
100
  if data:
@@ -110,7 +99,7 @@ def __get_text2img(data:dict) -> dict:
110
  return __response
111
 
112
  @api.post("/texto_a_video/", status_code=201)
113
- def __get_text2video(data:dict) -> dict:
114
  __response=dict({"request_data":data})
115
  try:
116
  if data:
@@ -126,7 +115,7 @@ def __get_text2video(data:dict) -> dict:
126
  return __response
127
 
128
  @api.post("/texto_a_audio/", status_code=201)
129
- def __get_text2speach(data:dict) -> dict:
130
  __response=dict({"request_data":data})
131
  try:
132
  if data:
 
1
  from api.core.app import Demos
2
  __main = Demos()
3
  api = __main.api()
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  @api.post("/texto_desde_web/", status_code=201)
6
+ def get_text_from_url(data:dict) -> dict:
7
  __response=dict({"request_data":data})
8
  try:
9
  if data:
 
18
  return __response
19
 
20
  @api.post("/texto_desde_pdf/", status_code=201)
21
+ def get_text_from_pdf(data:dict) -> dict:
22
  __response=dict({"request_data":data})
23
  try:
24
  if data:
 
33
  return __response
34
 
35
  @api.post("/generar_bloques/", status_code=201)
36
+ def get_blocks(data:dict) -> dict:
37
  __response=dict({"request_data":data})
38
  try:
39
  if data:
40
  __response['original']=data.get('texto')
41
  __response['bloques']=__main.generar_bloques(texto=data.get('texto'),
42
+ size=data.get('size'))
43
  else:
44
  raise __main.exception(status_code = 401, datail=f"Datos mal formados:\n{data}")
45
  except Exception as e:
 
50
  return __response
51
 
52
  @api.post("/traducir/", status_code=201)
53
+ def get_traduccion(data:dict) -> dict:
54
  __response=dict({"request_data":data})
55
  try:
56
  if data:
 
67
  return __response
68
 
69
  @api.post("/resumir/", status_code=201)
70
+ def get_resumen(data:dict) -> dict:
71
  __response=dict({"request_data":data})
72
  try:
73
  if data:
 
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:
89
  if data:
 
99
  return __response
100
 
101
  @api.post("/texto_a_video/", status_code=201)
102
+ def get_text2video(data:dict) -> dict:
103
  __response=dict({"request_data":data})
104
  try:
105
  if data:
 
115
  return __response
116
 
117
  @api.post("/texto_a_audio/", status_code=201)
118
+ def get_text2speach(data:dict) -> dict:
119
  __response=dict({"request_data":data})
120
  try:
121
  if data:
requirements.txt CHANGED
@@ -1,4 +1,7 @@
1
  fastapi
2
  pydantic
3
  uvicorn
4
- typing
 
 
 
 
1
  fastapi
2
  pydantic
3
  uvicorn
4
+ typing
5
+ diffusers
6
+ diffusers[torch]
7
+ diffusers[flax]