Hjgugugjhuhjggg
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
from fastapi import FastAPI
|
|
|
|
|
2 |
import torch
|
3 |
from langchain.chains.llm import LLMChain
|
4 |
-
from langchain.llms import VLLM
|
5 |
-
from langchain.cache import GPTCache
|
6 |
from transformers import pipeline
|
7 |
import uvicorn
|
8 |
import threading
|
@@ -14,10 +14,8 @@ import psutil
|
|
14 |
import os
|
15 |
import gc
|
16 |
import logging
|
17 |
-
from tqdm import tqdm
|
18 |
|
19 |
logging.basicConfig(level=logging.INFO)
|
20 |
-
|
21 |
nltk.download('punkt')
|
22 |
nltk.download('stopwords')
|
23 |
|
@@ -29,24 +27,22 @@ else:
|
|
29 |
device = torch.device("cpu")
|
30 |
|
31 |
modelos = {
|
32 |
-
"gpt2-medium": VLLM(model="gpt2-medium"),
|
33 |
-
"qwen2.5-0.5b": VLLM(model="Qwen/Qwen2.5-0.5B-Instruct"),
|
34 |
-
"llamaxd": VLLM(model="Hjgugugjhuhjggg/llama-3.2-1B-spinquant-hf")
|
35 |
}
|
36 |
|
37 |
-
for nombre, modelo in tqdm(modelos.items()):
|
38 |
-
modelos[nombre] = modelo(to=device)
|
39 |
-
|
40 |
caches = {
|
41 |
-
nombre: GPTCache(modelo, max_size=1000)
|
|
|
42 |
}
|
43 |
|
44 |
cadenas = {
|
45 |
-
nombre: LLMChain(modelo,
|
|
|
46 |
}
|
47 |
|
48 |
summarizer = pipeline("summarization", device=device)
|
49 |
-
|
50 |
vectorizer = TfidfVectorizer()
|
51 |
|
52 |
def keep_alive():
|
@@ -56,7 +52,6 @@ def keep_alive():
|
|
56 |
cadena.ask("ΒΏCuΓ‘l es el sentido de la vida?")
|
57 |
except Exception as e:
|
58 |
logging.error(f"Error en modelo {cadena}: {e}")
|
59 |
-
cadenas.pop(cadena)
|
60 |
time.sleep(300)
|
61 |
|
62 |
def liberar_recursos():
|
@@ -70,7 +65,7 @@ def liberar_recursos():
|
|
70 |
os.kill(proc.info['pid'], 9)
|
71 |
time.sleep(60)
|
72 |
|
73 |
-
#
|
74 |
threading.Thread(target=keep_alive, daemon=True).start()
|
75 |
threading.Thread(target=liberar_recursos, daemon=True).start()
|
76 |
|
@@ -82,7 +77,7 @@ async def pregunta(pregunta: str, modelo: str):
|
|
82 |
mensajes = []
|
83 |
palabras = respuesta.split()
|
84 |
mensaje_actual = ""
|
85 |
-
for palabra in
|
86 |
if len(mensaje_actual.split()) + len(palabra.split()) > 2048:
|
87 |
mensajes.append(mensaje_actual)
|
88 |
mensaje_actual = palabra
|
@@ -96,8 +91,8 @@ async def pregunta(pregunta: str, modelo: str):
|
|
96 |
respuesta_vec = vectorizer.transform([respuesta])
|
97 |
similitud = cosine_similarity(pregunta_vec, respuesta_vec)
|
98 |
return {
|
99 |
-
"respuesta": respuesta,
|
100 |
-
"resumen": resumen[0]["summary_text"],
|
101 |
"similitud": similitud[0][0]
|
102 |
}
|
103 |
except Exception as e:
|
|
|
1 |
from fastapi import FastAPI
|
2 |
+
from langchain_community.llms import VLLM
|
3 |
+
from langchain_community.cache import GPTCache
|
4 |
import torch
|
5 |
from langchain.chains.llm import LLMChain
|
|
|
|
|
6 |
from transformers import pipeline
|
7 |
import uvicorn
|
8 |
import threading
|
|
|
14 |
import os
|
15 |
import gc
|
16 |
import logging
|
|
|
17 |
|
18 |
logging.basicConfig(level=logging.INFO)
|
|
|
19 |
nltk.download('punkt')
|
20 |
nltk.download('stopwords')
|
21 |
|
|
|
27 |
device = torch.device("cpu")
|
28 |
|
29 |
modelos = {
|
30 |
+
"gpt2-medium": VLLM(model="gpt2-medium", device=device),
|
31 |
+
"qwen2.5-0.5b": VLLM(model="Qwen/Qwen2.5-0.5B-Instruct", device=device),
|
32 |
+
"llamaxd": VLLM(model="Hjgugugjhuhjggg/llama-3.2-1B-spinquant-hf", device=device)
|
33 |
}
|
34 |
|
|
|
|
|
|
|
35 |
caches = {
|
36 |
+
nombre: GPTCache(modelo, max_size=1000)
|
37 |
+
for nombre, modelo in modelos.items()
|
38 |
}
|
39 |
|
40 |
cadenas = {
|
41 |
+
nombre: LLMChain(modelo, cache)
|
42 |
+
for nombre, modelo, cache in zip(modelos.keys(), modelos.values(), caches.values())
|
43 |
}
|
44 |
|
45 |
summarizer = pipeline("summarization", device=device)
|
|
|
46 |
vectorizer = TfidfVectorizer()
|
47 |
|
48 |
def keep_alive():
|
|
|
52 |
cadena.ask("ΒΏCuΓ‘l es el sentido de la vida?")
|
53 |
except Exception as e:
|
54 |
logging.error(f"Error en modelo {cadena}: {e}")
|
|
|
55 |
time.sleep(300)
|
56 |
|
57 |
def liberar_recursos():
|
|
|
65 |
os.kill(proc.info['pid'], 9)
|
66 |
time.sleep(60)
|
67 |
|
68 |
+
# Iniciar hilos en segundo plano
|
69 |
threading.Thread(target=keep_alive, daemon=True).start()
|
70 |
threading.Thread(target=liberar_recursos, daemon=True).start()
|
71 |
|
|
|
77 |
mensajes = []
|
78 |
palabras = respuesta.split()
|
79 |
mensaje_actual = ""
|
80 |
+
for palabra in palabras:
|
81 |
if len(mensaje_actual.split()) + len(palabra.split()) > 2048:
|
82 |
mensajes.append(mensaje_actual)
|
83 |
mensaje_actual = palabra
|
|
|
91 |
respuesta_vec = vectorizer.transform([respuesta])
|
92 |
similitud = cosine_similarity(pregunta_vec, respuesta_vec)
|
93 |
return {
|
94 |
+
"respuesta": respuesta,
|
95 |
+
"resumen": resumen[0]["summary_text"],
|
96 |
"similitud": similitud[0][0]
|
97 |
}
|
98 |
except Exception as e:
|