|
import streamlit as st |
|
from transformers import AutoModelForCausalLM, AutoTokenizer |
|
|
|
|
|
st.title("Chat con el modelo BSC-LT/ALIA-40b") |
|
st.markdown("Interactúa con el modelo BSC-LT/ALIA-40b en este chat. Escribe tu mensaje abajo:") |
|
|
|
|
|
@st.cache_resource |
|
def load_model(): |
|
model_name = "BSC-LT/ALIA-40b" |
|
tokenizer = AutoTokenizer.from_pretrained(model_name) |
|
model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto") |
|
return model, tokenizer |
|
|
|
model, tokenizer = load_model() |
|
|
|
|
|
if "messages" not in st.session_state: |
|
st.session_state.messages = [] |
|
|
|
|
|
for msg in st.session_state.messages: |
|
if msg["role"] == "user": |
|
st.markdown(f"**Tú:** {msg['content']}") |
|
else: |
|
st.markdown(f"**Modelo:** {msg['content']}") |
|
|
|
|
|
user_input = st.text_input("Escribe tu mensaje aquí:", key="input") |
|
|
|
|
|
if st.button("Enviar") and user_input.strip(): |
|
|
|
st.session_state.messages.append({"role": "user", "content": user_input}) |
|
|
|
|
|
inputs = tokenizer(user_input, return_tensors="pt").to("cuda") |
|
outputs = model.generate(inputs["input_ids"], max_length=100, num_return_sequences=1) |
|
response = tokenizer.decode(outputs[0], skip_special_tokens=True) |
|
|
|
|
|
st.session_state.messages.append({"role": "model", "content": response}) |
|
|
|
|
|
st.experimental_rerun() |
|
|