Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,28 +1,77 @@
|
|
1 |
-
import os
|
2 |
-
import torch
|
3 |
-
from transformers import pipeline
|
4 |
import streamlit as st
|
|
|
|
|
5 |
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
|
9 |
-
|
|
|
10 |
|
11 |
-
|
|
|
|
|
|
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
)
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import torch
|
3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
4 |
|
5 |
+
# Cargar modelo
|
6 |
+
model_name = "Qwen/Qwen3-1.7B"
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
8 |
+
model = AutoModelForCausalLM.from_pretrained(
|
9 |
+
model_name,
|
10 |
+
torch_dtype="auto",
|
11 |
+
device_map="auto",
|
12 |
+
trust_remote_code=True
|
13 |
+
)
|
14 |
|
15 |
+
# Cargar prompt base
|
16 |
+
with open("SeguroCargo.txt", "r", encoding="utf-8") as f:
|
17 |
+
prompt_base = f.read().strip()
|
18 |
|
19 |
+
# Funci贸n para generar respuesta con "thinking" opcional
|
20 |
+
def generar_respuesta(pregunta):
|
21 |
+
contexto = f"{prompt_base}\nUsuario: {pregunta}\nAsistente:"
|
22 |
+
messages = [{"role": "user", "content": contexto}]
|
23 |
|
24 |
+
text = tokenizer.apply_chat_template(
|
25 |
+
messages,
|
26 |
+
tokenize=False,
|
27 |
+
add_generation_prompt=True,
|
28 |
+
enable_thinking=False
|
29 |
+
)
|
30 |
+
|
31 |
+
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
32 |
+
generated_ids = model.generate(
|
33 |
+
**model_inputs,
|
34 |
+
max_new_tokens=1024,
|
35 |
+
do_sample=True,
|
36 |
+
temperature=0.7,
|
37 |
+
top_p=0.8
|
38 |
+
)
|
39 |
+
|
40 |
+
output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()
|
41 |
+
|
42 |
+
try:
|
43 |
+
index = len(output_ids) - output_ids[::-1].index(151668)
|
44 |
+
except ValueError:
|
45 |
+
index = 0
|
46 |
+
|
47 |
+
content = tokenizer.decode(output_ids[index:], skip_special_tokens=True).strip("\n")
|
48 |
+
return content.strip()
|
49 |
+
|
50 |
+
# Configuraci贸n de Streamlit
|
51 |
+
st.set_page_config(page_title="Asistente SeguroCargo2", page_icon="馃摝")
|
52 |
+
st.title("馃摝 Bienvenido a SeguroCargo2")
|
53 |
+
st.markdown("Hola, yo soy **SEGU**, asistente virtual de SeguroCargo sin el modo de pensamiento!")
|
54 |
+
st.markdown("Consulta sobre tus **env铆os nacionales e internacionales** de manera r谩pida y profesional.")
|
55 |
+
|
56 |
+
if "messages" not in st.session_state:
|
57 |
+
st.session_state.messages = []
|
58 |
+
|
59 |
+
# Mostrar historial de mensajes
|
60 |
+
for msg in st.session_state.messages:
|
61 |
+
role, text = msg
|
62 |
+
with st.chat_message(role):
|
63 |
+
st.markdown(text)
|
64 |
+
|
65 |
+
# Entrada del usuario
|
66 |
+
user_input = st.chat_input("Escribe tu mensaje...")
|
67 |
+
|
68 |
+
if user_input:
|
69 |
+
st.session_state.messages.append(("user", user_input))
|
70 |
+
with st.chat_message("user"):
|
71 |
+
st.markdown(user_input)
|
72 |
+
|
73 |
+
# Generar y mostrar respuesta
|
74 |
+
respuesta = generar_respuesta(user_input)
|
75 |
+
st.session_state.messages.append(("assistant", respuesta))
|
76 |
+
with st.chat_message("assistant"):
|
77 |
+
st.markdown(respuesta)
|