Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,13 +9,7 @@ system_instructions = os.getenv("SYSTEM_INSTRUCTIONS", "Eres un asistente amable
|
|
| 9 |
knowledge_base = os.getenv("KNOWLEDGE_BASE", "No hay informaci贸n adicional.")
|
| 10 |
|
| 11 |
# 2. Uni贸n de las instrucciones y los datos
|
| 12 |
-
final_system_prompt = f"""
|
| 13 |
-
DIRECTRICES DEL ASISTENTE:
|
| 14 |
-
{system_instructions}
|
| 15 |
-
|
| 16 |
-
CONTEXTO Y BASE DE CONOCIMIENTO DE LA EMPRESA:
|
| 17 |
-
{knowledge_base}
|
| 18 |
-
"""
|
| 19 |
|
| 20 |
# Modelo para seguir instrucciones
|
| 21 |
pipe = pipeline("text-generation", model="Qwen/Qwen1.5-0.5B-Chat", device=-1)
|
|
@@ -23,33 +17,44 @@ pipe = pipeline("text-generation", model="Qwen/Qwen1.5-0.5B-Chat", device=-1)
|
|
| 23 |
def respond(message, history, max_tokens, temperature, top_p):
|
| 24 |
# Formato a la conversaci贸n
|
| 25 |
messages = [{"role": "system", "content": final_system_prompt}]
|
| 26 |
-
|
| 27 |
-
# Se procesa el historial de Gradio
|
| 28 |
-
for val in history:
|
| 29 |
-
if isinstance(val, dict):
|
| 30 |
-
messages.append(val)
|
| 31 |
-
else: # Por si Gradio usa el formato antiguo de listas
|
| 32 |
-
messages.append({"role": "user", "content": val[0]})
|
| 33 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 34 |
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
# Se
|
| 38 |
prompt = pipe.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 39 |
|
| 40 |
try:
|
| 41 |
-
#
|
| 42 |
outputs = pipe(
|
| 43 |
prompt,
|
| 44 |
max_new_tokens=max_tokens,
|
| 45 |
temperature=temperature,
|
| 46 |
top_p=top_p,
|
| 47 |
-
do_sample=True
|
|
|
|
| 48 |
)
|
| 49 |
|
| 50 |
-
# Se
|
| 51 |
-
|
| 52 |
-
yield respuesta
|
| 53 |
|
| 54 |
except Exception as e:
|
| 55 |
yield f"鈿狅笍 Error interno procesando el texto: {str(e)}"
|
|
@@ -61,7 +66,7 @@ chatbot = gr.ChatInterface(
|
|
| 61 |
description=bot_description,
|
| 62 |
additional_inputs=[
|
| 63 |
gr.Slider(1, 1024, 512, step=10, label="Max new tokens"),
|
| 64 |
-
gr.Slider(0.1,
|
| 65 |
gr.Slider(0.1, 1.0, 0.95, step=0.05, label="Top-p"),
|
| 66 |
],
|
| 67 |
examples=[
|
|
|
|
| 9 |
knowledge_base = os.getenv("KNOWLEDGE_BASE", "No hay informaci贸n adicional.")
|
| 10 |
|
| 11 |
# 2. Uni贸n de las instrucciones y los datos
|
| 12 |
+
final_system_prompt = f"""DIRECTRICES DEL ASISTENTE:\n{system_instructions}\n\nCONTEXTO Y BASE DE CONOCIMIENTO DE LA EMPRESA:\n{knowledge_base}"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# Modelo para seguir instrucciones
|
| 15 |
pipe = pipeline("text-generation", model="Qwen/Qwen1.5-0.5B-Chat", device=-1)
|
|
|
|
| 17 |
def respond(message, history, max_tokens, temperature, top_p):
|
| 18 |
# Formato a la conversaci贸n
|
| 19 |
messages = [{"role": "system", "content": final_system_prompt}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
# Se estrae estrictamente texto del historial
|
| 22 |
+
for turn in history:
|
| 23 |
+
if isinstance(turn, dict): # Si Gradio usa el formato moderno (diccionarios)
|
| 24 |
+
content = turn.get("content", "")
|
| 25 |
+
if isinstance(content, (list, tuple)): content = content[0]
|
| 26 |
+
messages.append({"role": turn.get("role", "user"), "content": str(content)})
|
| 27 |
+
|
| 28 |
+
elif isinstance(turn, (list, tuple)) and len(turn) == 2: # Si Gradio usa el formato cl谩sico (tuplas)
|
| 29 |
+
u_msg, b_msg = turn[0], turn[1]
|
| 30 |
+
if isinstance(u_msg, (list, tuple)): u_msg = u_msg[0]
|
| 31 |
+
if isinstance(b_msg, (list, tuple)): b_msg = b_msg[0]
|
| 32 |
+
if u_msg: messages.append({"role": "user", "content": str(u_msg)})
|
| 33 |
+
if b_msg: messages.append({"role": "assistant", "content": str(b_msg)})
|
| 34 |
+
|
| 35 |
+
# Se a帽ade el mensaje actual
|
| 36 |
+
current_msg = message
|
| 37 |
+
if isinstance(current_msg, dict): current_msg = current_msg.get("text", "")
|
| 38 |
+
elif isinstance(current_msg, (list, tuple)): current_msg = current_msg[0]
|
| 39 |
+
|
| 40 |
+
messages.append({"role": "user", "content": str(current_msg)})
|
| 41 |
|
| 42 |
+
# Se genera la plantilla para el modelo
|
| 43 |
prompt = pipe.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 44 |
|
| 45 |
try:
|
| 46 |
+
# Se ejecuta el modelo
|
| 47 |
outputs = pipe(
|
| 48 |
prompt,
|
| 49 |
max_new_tokens=max_tokens,
|
| 50 |
temperature=temperature,
|
| 51 |
top_p=top_p,
|
| 52 |
+
do_sample=True,
|
| 53 |
+
return_full_text=False
|
| 54 |
)
|
| 55 |
|
| 56 |
+
# Se entrega solo la respuesta limpia
|
| 57 |
+
yield outputs[0]["generated_text"]
|
|
|
|
| 58 |
|
| 59 |
except Exception as e:
|
| 60 |
yield f"鈿狅笍 Error interno procesando el texto: {str(e)}"
|
|
|
|
| 66 |
description=bot_description,
|
| 67 |
additional_inputs=[
|
| 68 |
gr.Slider(1, 1024, 512, step=10, label="Max new tokens"),
|
| 69 |
+
gr.Slider(0.1, 2.0, 0.7, step=0.1, label="Temperature"),
|
| 70 |
gr.Slider(0.1, 1.0, 0.95, step=0.05, label="Top-p"),
|
| 71 |
],
|
| 72 |
examples=[
|