Hjgugugjhuhjggg
commited on
Commit
•
f1a08ce
1
Parent(s):
5213686
Update app.py
Browse files
app.py
CHANGED
@@ -1,128 +1,161 @@
|
|
1 |
-
from
|
2 |
-
from
|
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
|
9 |
-
import time
|
10 |
-
import nltk
|
11 |
-
from sklearn.feature_extraction.text import TfidfVectorizer
|
12 |
-
from sklearn.metrics.pairwise import cosine_similarity
|
13 |
-
import psutil
|
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 |
-
|
22 |
-
app = FastAPI()
|
23 |
-
|
24 |
-
device = torch.device("cpu")
|
25 |
|
|
|
|
|
|
|
26 |
modelos = {
|
27 |
-
"
|
28 |
-
"
|
29 |
-
"
|
30 |
}
|
31 |
|
|
|
32 |
caches = {
|
33 |
nombre: GPTCache(modelo, max_size=1000)
|
34 |
for nombre, modelo in modelos.items()
|
35 |
}
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify, render_template_string
|
2 |
+
from vllm import LLM, SamplingParams
|
3 |
from langchain_community.cache import GPTCache
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
+
app = Flask(__name__)
|
6 |
+
|
7 |
+
Configuración de los modelos de lenguaje
|
8 |
modelos = {
|
9 |
+
"facebook/opt-125m": LLM(model="facebook/opt-125m"),
|
10 |
+
"llama-3.2-1B": LLM(model="Hjgugugjhuhjggg/llama-3.2-1B-spinquant-hf"),
|
11 |
+
"gpt2": LLM(model="gpt2")
|
12 |
}
|
13 |
|
14 |
+
Configuración de los caches
|
15 |
caches = {
|
16 |
nombre: GPTCache(modelo, max_size=1000)
|
17 |
for nombre, modelo in modelos.items()
|
18 |
}
|
19 |
|
20 |
+
Configuración de muestreo
|
21 |
+
sampling_params = SamplingParams(temperature=0.8, top_p=0.95)
|
22 |
+
|
23 |
+
html_code_docs = """
|
24 |
+
<!DOCTYPE html>
|
25 |
+
<html>
|
26 |
+
<head>
|
27 |
+
<title>Documentación de la API</title>
|
28 |
+
</head>
|
29 |
+
<body>
|
30 |
+
<h1>API de Generación de Texto</h1>
|
31 |
+
<h2>Endpoints</h2>
|
32 |
+
<ul>
|
33 |
+
<li>
|
34 |
+
<h3>Generar texto</h3>
|
35 |
+
<p>Método: POST</p>
|
36 |
+
<p>Ruta: /generate</p>
|
37 |
+
<p>Parámetros:</p>
|
38 |
+
<ul>
|
39 |
+
<li>prompts: Lista de prompts para generar texto</li>
|
40 |
+
<li>modelo: Nombre del modelo a utilizar</li>
|
41 |
+
</ul>
|
42 |
+
<p>Ejemplo:</p>
|
43 |
+
<pre>curl -X POST -H "Content-Type: application/json" -d '{"prompts": ["Hola, cómo estás?"], "modelo": "facebook/opt-125m"}' http://localhost:5000/generate</pre>
|
44 |
+
</li>
|
45 |
+
<li>
|
46 |
+
<h3>Obtener lista de modelos</h3>
|
47 |
+
<p>Método: GET</p>
|
48 |
+
<p>Ruta: /modelos</p>
|
49 |
+
<p>Ejemplo:</p>
|
50 |
+
<pre>curl -X GET http://localhost:5000/modelos</pre>
|
51 |
+
</li>
|
52 |
+
<li>
|
53 |
+
<h3>Chatbot</h3>
|
54 |
+
<p>Método: POST</p>
|
55 |
+
<p>Ruta: /chatbot</p>
|
56 |
+
<p>Parámetros:</p>
|
57 |
+
<ul>
|
58 |
+
<li>mensaje: Mensaje para el chatbot</li>
|
59 |
+
<li>modelo: Nombre del modelo a utilizar</li>
|
60 |
+
</ul>
|
61 |
+
<p>Ejemplo:</p>
|
62 |
+
<pre>curl -X POST -H "Content-Type: application/json" -d '{"mensaje": "Hola, cómo estás?", "modelo": "facebook/opt-125m"}' http://localhost:5000/chatbot</pre>
|
63 |
+
</li>
|
64 |
+
</ul>
|
65 |
+
</body>
|
66 |
+
</html>
|
67 |
+
"""
|
68 |
+
|
69 |
+
html_code_chatbot = """
|
70 |
+
<!DOCTYPE html>
|
71 |
+
<html>
|
72 |
+
<head>
|
73 |
+
<title>Chatbot</title>
|
74 |
+
</head>
|
75 |
+
<body>
|
76 |
+
<h1>Chatbot</h1>
|
77 |
+
<form id="chat-form">
|
78 |
+
<input type="text" id="mensaje" placeholder="Escribe un mensaje">
|
79 |
+
<button type="submit">Enviar</button>
|
80 |
+
</form>
|
81 |
+
<div id="respuestas"></div>
|
82 |
+
|
83 |
+
<script>
|
84 |
+
const form = document.getElementById('chat-form');
|
85 |
+
const mensajeInput = document.getElementById('mensaje');
|
86 |
+
const respuestasDiv = document.getElementById('respuestas');
|
87 |
+
|
88 |
+
form.addEventListener('submit', (e) => {
|
89 |
+
e.preventDefault();
|
90 |
+
const mensaje = mensajeInput.value;
|
91 |
+
fetch('/chatbot', {
|
92 |
+
method: 'POST',
|
93 |
+
headers: {
|
94 |
+
'Content-Type': 'application/json'
|
95 |
+
},
|
96 |
+
body: JSON.stringify({ mensaje })
|
97 |
+
})
|
98 |
+
.then((res) => res.json())
|
99 |
+
.then((data) => {
|
100 |
+
const respuesta = data.respuesta;
|
101 |
+
const respuestaHTML = `<p>Tú: ${mensaje}</p><p>Chatbot: ${respuesta}</p>`;
|
102 |
+
respuestasDiv.innerHTML += respuestaHTML;
|
103 |
+
mensajeInput.value = '';
|
104 |
+
});
|
105 |
+
});
|
106 |
+
</script>
|
107 |
+
</body>
|
108 |
+
</html>
|
109 |
+
"""
|
110 |
+
|
111 |
+
@app.route('/generate', methods=['POST'])
|
112 |
+
def generate():
|
113 |
+
data = request.get_json()
|
114 |
+
prompts = data.get('prompts', [])
|
115 |
+
modelo_seleccionado = data.get('modelo', "facebook/opt-125m")
|
116 |
+
|
117 |
+
if modelo_seleccionado not in modelos:
|
118 |
+
return jsonify({"error": "Modelo no encontrado"}), 404
|
119 |
+
|
120 |
+
outputs = caches[modelo_seleccionado].generate(prompts, sampling_params)
|
121 |
+
|
122 |
+
results = []
|
123 |
+
for output in outputs:
|
124 |
+
prompt = output.prompt
|
125 |
+
generated_text = output.outputs[0].text
|
126 |
+
results.append({
|
127 |
+
'prompt': prompt,
|
128 |
+
'generated_text': generated_text
|
129 |
+
})
|
130 |
+
|
131 |
+
return jsonify(results)
|
132 |
+
|
133 |
+
@app.route('/modelos', methods=['GET'])
|
134 |
+
def get_modelos():
|
135 |
+
return jsonify({"modelos": list(modelos.keys())})
|
136 |
+
|
137 |
+
@app.route('/docs', methods=['GET'])
|
138 |
+
def docs():
|
139 |
+
return render_template_string(html_code_docs)
|
140 |
+
|
141 |
+
@app.route('/chatbot', methods=['POST'])
|
142 |
+
def chatbot():
|
143 |
+
data = request.get_json()
|
144 |
+
mensaje = data.get('mensaje', '')
|
145 |
+
modelo_seleccionado = data.get('modelo', "facebook/opt-125m")
|
146 |
+
|
147 |
+
if modelo_seleccionado not in modelos:
|
148 |
+
return jsonify({"error": "Modelo no encontrado"}), 404
|
149 |
+
|
150 |
+
outputs = caches[modelo_seleccionado].generate([mensaje], sampling_params)
|
151 |
+
|
152 |
+
respuesta = outputs[0].outputs[0].text
|
153 |
+
|
154 |
+
return jsonify({"respuesta": respuesta})
|
155 |
+
|
156 |
+
@app.route('/chat', methods=['GET'])
|
157 |
+
def chat():
|
158 |
+
return render_template_string(html_code_chatbot)
|
159 |
+
|
160 |
+
if __name__ == '__main__':
|
161 |
+
app.run(host='0.0.0.0', port=7860)
|