Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -18,7 +18,7 @@ models = [
|
|
18 |
{"repo_id": "Ffftdtd5dtft/gemma-2-27b-Q2_K-GGUF", "filename": "gemma-2-27b-q2_k.gguf"},
|
19 |
]
|
20 |
|
21 |
-
# Cargar modelos en memoria
|
22 |
llms = [Llama.from_pretrained(repo_id=model['repo_id'], filename=model['filename']) for model in models]
|
23 |
|
24 |
class ChatRequest(BaseModel):
|
@@ -29,7 +29,8 @@ class ChatRequest(BaseModel):
|
|
29 |
|
30 |
def generate_chat_response(request, llm):
|
31 |
try:
|
32 |
-
|
|
|
33 |
response = llm.create_chat_completion(
|
34 |
messages=[{"role": "user", "content": user_input}],
|
35 |
top_k=request.top_k,
|
@@ -41,6 +42,10 @@ def generate_chat_response(request, llm):
|
|
41 |
except Exception as e:
|
42 |
return {"response": f"Error: {str(e)}", "literal": user_input}
|
43 |
|
|
|
|
|
|
|
|
|
44 |
def select_best_response(responses, request):
|
45 |
coherent_responses = filter_by_coherence([resp['response'] for resp in responses], request)
|
46 |
best_response = filter_by_similarity(coherent_responses)
|
@@ -62,6 +67,9 @@ def filter_by_similarity(responses):
|
|
62 |
|
63 |
@app.post("/generate_chat")
|
64 |
async def generate_chat(request: ChatRequest):
|
|
|
|
|
|
|
65 |
with ThreadPoolExecutor(max_workers=None) as executor:
|
66 |
futures = [executor.submit(generate_chat_response, request, llm) for llm in llms]
|
67 |
responses = []
|
|
|
18 |
{"repo_id": "Ffftdtd5dtft/gemma-2-27b-Q2_K-GGUF", "filename": "gemma-2-27b-q2_k.gguf"},
|
19 |
]
|
20 |
|
21 |
+
# Cargar modelos en memoria solo una vez
|
22 |
llms = [Llama.from_pretrained(repo_id=model['repo_id'], filename=model['filename']) for model in models]
|
23 |
|
24 |
class ChatRequest(BaseModel):
|
|
|
29 |
|
30 |
def generate_chat_response(request, llm):
|
31 |
try:
|
32 |
+
# Normalización del mensaje para manejo robusto
|
33 |
+
user_input = normalize_input(request.message)
|
34 |
response = llm.create_chat_completion(
|
35 |
messages=[{"role": "user", "content": user_input}],
|
36 |
top_k=request.top_k,
|
|
|
42 |
except Exception as e:
|
43 |
return {"response": f"Error: {str(e)}", "literal": user_input}
|
44 |
|
45 |
+
def normalize_input(input_text):
|
46 |
+
# Implementar aquí cualquier lógica de normalización que sea necesaria
|
47 |
+
return input_text.strip()
|
48 |
+
|
49 |
def select_best_response(responses, request):
|
50 |
coherent_responses = filter_by_coherence([resp['response'] for resp in responses], request)
|
51 |
best_response = filter_by_similarity(coherent_responses)
|
|
|
67 |
|
68 |
@app.post("/generate_chat")
|
69 |
async def generate_chat(request: ChatRequest):
|
70 |
+
if not request.message.strip():
|
71 |
+
raise HTTPException(status_code=400, detail="The message cannot be empty.")
|
72 |
+
|
73 |
with ThreadPoolExecutor(max_workers=None) as executor:
|
74 |
futures = [executor.submit(generate_chat_response, request, llm) for llm in llms]
|
75 |
responses = []
|