Spaces:
Runtime error
Runtime error
from fastapi import FastAPI, HTTPException | |
from pydantic import BaseModel | |
from llama_cpp import Llama | |
from concurrent.futures import ProcessPoolExecutor, as_completed | |
import uvicorn | |
from dotenv import load_dotenv | |
from difflib import SequenceMatcher | |
from tqdm import tqdm | |
import multiprocessing | |
load_dotenv() | |
app = FastAPI() | |
# Configuraci贸n de los modelos | |
models = [ | |
{"repo_id": "Ffftdtd5dtft/gpt2-xl-Q2_K-GGUF", "filename": "gpt2-xl-q2_k.gguf"}, | |
{"repo_id": "Ffftdtd5dtft/Meta-Llama-3.1-8B-Instruct-Q2_K-GGUF", "filename": "meta-llama-3.1-8b-instruct-q2_k.gguf"}, | |
{"repo_id": "Ffftdtd5dtft/gemma-2-9b-it-Q2_K-GGUF", "filename": "gemma-2-9b-it-q2_k.gguf"}, | |
{"repo_id": "Ffftdtd5dtft/gemma-2-27b-Q2_K-GGUF", "filename": "gemma-2-27b-q2_k.gguf"}, | |
] | |
# Funci贸n para cargar un modelo | |
def load_model(model_config): | |
return Llama.from_pretrained(repo_id=model_config['repo_id'], filename=model_config['filename']) | |
# Cargar modelos en paralelo | |
def load_all_models(): | |
with ProcessPoolExecutor() as executor: | |
future_to_model = {executor.submit(load_model, model): model for model in models} | |
loaded_models = {} | |
for future in as_completed(future_to_model): | |
model = future_to_model[future] | |
try: | |
loaded_models[model['repo_id']] = future.result() | |
print(f"Modelo cargado en RAM: {model['repo_id']}") | |
except Exception as exc: | |
print(f"Error al cargar modelo {model['repo_id']}: {exc}") | |
return loaded_models | |
# Cargar modelos en memoria | |
llms = load_all_models() | |
class ChatRequest(BaseModel): | |
message: str | |
top_k: int = 50 | |
top_p: float = 0.95 | |
temperature: float = 0.7 | |
def generate_chat_response(request, llm): | |
try: | |
user_input = normalize_input(request.message) | |
response = llm.create_chat_completion( | |
messages=[{"role": "user", "content": user_input}], | |
top_k=request.top_k, | |
top_p=request.top_p, | |
temperature=request.temperature | |
) | |
reply = response['choices'][0]['message']['content'] | |
return {"response": reply, "literal": user_input} | |
except Exception as e: | |
return {"response": f"Error: {str(e)}", "literal": user_input} | |
def normalize_input(input_text): | |
return input_text.strip() | |
def filter_duplicates(responses): | |
seen = set() | |
unique_responses = [] | |
for response in responses: | |
lines = response.split('\n') | |
unique_lines = set() | |
for line in lines: | |
if line not in seen: | |
seen.add(line) | |
unique_lines.add(line) | |
unique_responses.append('\n'.join(unique_lines)) | |
return unique_responses | |
def select_best_response(responses): | |
# Eliminar respuestas duplicadas | |
unique_responses = filter_duplicates(responses) | |
# Deduplicar respuestas | |
unique_responses = list(set(unique_responses)) | |
# Filtrar respuestas coherentes | |
coherent_responses = filter_by_coherence(unique_responses) | |
# Seleccionar la mejor respuesta | |
best_response = filter_by_similarity(coherent_responses) | |
return best_response | |
def filter_by_coherence(responses): | |
# Implementa aqu铆 un filtro de coherencia si es necesario | |
return responses | |
def filter_by_similarity(responses): | |
responses.sort(key=len, reverse=True) | |
best_response = responses[0] | |
for i in range(1, len(responses)): | |
ratio = SequenceMatcher(None, best_response, responses[i]).ratio() | |
if ratio < 0.9: | |
best_response = responses[i] | |
break | |
return best_response | |
async def generate_chat(request: ChatRequest): | |
if not request.message.strip(): | |
raise HTTPException(status_code=400, detail="The message cannot be empty.") | |
print(f"Procesando solicitud: {request.message}") | |
# Utilizar un ProcessPoolExecutor para procesar los modelos en paralelo | |
def worker_function(llm): | |
return generate_chat_response(request, llm) | |
with ProcessPoolExecutor() as executor: | |
futures = [executor.submit(worker_function, llm) for llm in llms.values()] | |
responses = [] | |
for future in tqdm(as_completed(futures), total=len(futures), desc="Generando respuestas"): | |
response = future.result() | |
responses.append(response['response']) | |
print(f"Modelo procesado: {response['literal'][:30]}...") | |
# Seleccionar la mejor respuesta | |
best_response = select_best_response(responses) | |
print(f"Mejor respuesta seleccionada: {best_response}") | |
return { | |
"best_response": best_response, | |
"all_responses": responses | |
} | |
if __name__ == "__main__": | |
uvicorn.run(app, host="0.0.0.0", port=7860) | |