Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
|
4 |
+
# T铆tulo de la aplicaci贸n
|
5 |
+
st.title("Chat con el modelo BSC-LT/ALIA-40b")
|
6 |
+
st.markdown("Interact煤a con el modelo BSC-LT/ALIA-40b en este chat. Escribe tu mensaje abajo:")
|
7 |
+
|
8 |
+
# Cargar modelo y tokenizador
|
9 |
+
@st.cache_resource
|
10 |
+
def load_model():
|
11 |
+
model_name = "BSC-LT/ALIA-40b"
|
12 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
13 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto")
|
14 |
+
return model, tokenizer
|
15 |
+
|
16 |
+
model, tokenizer = load_model()
|
17 |
+
|
18 |
+
# Historial de chat
|
19 |
+
if "messages" not in st.session_state:
|
20 |
+
st.session_state.messages = []
|
21 |
+
|
22 |
+
# Mostrar historial
|
23 |
+
for msg in st.session_state.messages:
|
24 |
+
if msg["role"] == "user":
|
25 |
+
st.markdown(f"**T煤:** {msg['content']}")
|
26 |
+
else:
|
27 |
+
st.markdown(f"**Modelo:** {msg['content']}")
|
28 |
+
|
29 |
+
# Input del usuario
|
30 |
+
user_input = st.text_input("Escribe tu mensaje aqu铆:", key="input")
|
31 |
+
|
32 |
+
# Bot贸n de enviar
|
33 |
+
if st.button("Enviar") and user_input.strip():
|
34 |
+
# A帽adir el mensaje del usuario al historial
|
35 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
36 |
+
|
37 |
+
# Preparar entrada para el modelo
|
38 |
+
inputs = tokenizer(user_input, return_tensors="pt").to("cuda")
|
39 |
+
outputs = model.generate(inputs["input_ids"], max_length=100, num_return_sequences=1)
|
40 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
41 |
+
|
42 |
+
# A帽adir la respuesta del modelo al historial
|
43 |
+
st.session_state.messages.append({"role": "model", "content": response})
|
44 |
+
|
45 |
+
# Recargar la p谩gina para mostrar el mensaje
|
46 |
+
st.experimental_rerun()
|