Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ import logging
|
|
3 |
import asyncio
|
4 |
import uvicorn
|
5 |
import torch
|
|
|
6 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
7 |
from fastapi import FastAPI, Query, HTTPException
|
8 |
from fastapi.responses import HTMLResponse
|
@@ -23,9 +24,6 @@ message_history = []
|
|
23 |
# Lista para almacenar los tokens
|
24 |
tokens_history = []
|
25 |
|
26 |
-
# Bandera para verificar si los tokens han sido cargados
|
27 |
-
tokens_loaded = False
|
28 |
-
|
29 |
# Función para cargar modelos
|
30 |
async def load_models():
|
31 |
programming_models = [
|
@@ -178,35 +176,43 @@ async def main():
|
|
178 |
# Ruta para la generación de respuestas
|
179 |
@app.get('/autocomplete')
|
180 |
async def autocomplete(q: str = Query(...)):
|
181 |
-
global data_and_models_dict, message_history, tokens_history
|
182 |
|
183 |
# Verificar si hay modelos cargados
|
184 |
if 'model' not in data_and_models_dict:
|
185 |
await download_models()
|
186 |
|
187 |
-
# Cargar
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
210 |
|
211 |
# Guardar el mensaje del usuario en el historial
|
212 |
message_history.append(q)
|
|
|
3 |
import asyncio
|
4 |
import uvicorn
|
5 |
import torch
|
6 |
+
import random
|
7 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
8 |
from fastapi import FastAPI, Query, HTTPException
|
9 |
from fastapi.responses import HTMLResponse
|
|
|
24 |
# Lista para almacenar los tokens
|
25 |
tokens_history = []
|
26 |
|
|
|
|
|
|
|
27 |
# Función para cargar modelos
|
28 |
async def load_models():
|
29 |
programming_models = [
|
|
|
176 |
# Ruta para la generación de respuestas
|
177 |
@app.get('/autocomplete')
|
178 |
async def autocomplete(q: str = Query(...)):
|
179 |
+
global data_and_models_dict, message_history, tokens_history
|
180 |
|
181 |
# Verificar si hay modelos cargados
|
182 |
if 'model' not in data_and_models_dict:
|
183 |
await download_models()
|
184 |
|
185 |
+
# Cargar el modelo y el tokenizer
|
186 |
+
model, tokenizer = data_and_models_dict['model']
|
187 |
+
|
188 |
+
# Generar tokens de entrada
|
189 |
+
input_ids = tokenizer.encode(q, return_tensors="pt")
|
190 |
+
tokens_history.append({"input": input_ids.tolist()}) # Guardar tokens de entrada
|
191 |
+
|
192 |
+
# Generar parámetros aleatorios
|
193 |
+
top_k = random.randint(0, 50)
|
194 |
+
top_p = random.uniform(0.8, 1.0)
|
195 |
+
temperature = random.uniform(0.7, 1.5)
|
196 |
+
|
197 |
+
# Generar una respuesta utilizando el modelo
|
198 |
+
output = model.generate(
|
199 |
+
input_ids,
|
200 |
+
max_length=50,
|
201 |
+
top_k=top_k,
|
202 |
+
top_p=top_p,
|
203 |
+
temperature=temperature,
|
204 |
+
num_return_sequences=1
|
205 |
+
)
|
206 |
+
response_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
207 |
+
|
208 |
+
# Generar tokens de salida
|
209 |
+
output_ids = output[0].tolist()
|
210 |
+
tokens_history.append({"output": output_ids}) # Guardar tokens de salida
|
211 |
+
|
212 |
+
# Guardar eos y pad tokens
|
213 |
+
eos_token = tokenizer.eos_token_id
|
214 |
+
pad_token = tokenizer.pad_token_id
|
215 |
+
tokens_history.append({"eos_token": eos_token, "pad_token": pad_token})
|
216 |
|
217 |
# Guardar el mensaje del usuario en el historial
|
218 |
message_history.append(q)
|