File size: 1,433 Bytes
2073668
 
 
9398202
9eec875
 
2073668
9398202
2073668
 
 
 
 
9eec875
 
 
507ccc3
9eec875
 
507ccc3
9eec875
 
 
507ccc3
2073668
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import streamlit as st
#from streamlit_chat import message as st_message
from streamlit_chat import message as st_message
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
import torch
from peft import PeftModel, PeftConfig

st.title("Chatbot Produit")

if "history" not in st.session_state:
    st.session_state.history = []
    
def get_models():

    peft_model_id = "tbboukhari/chatbot-produit-fr"
    config = PeftConfig.from_pretrained(peft_model_id)
    
    model = AutoModelForSeq2SeqLM.from_pretrained(config.base_model_name_or_path, torch_dtype="auto", device_map="auto")
    tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)
    
    # Load the Lora model
    model = PeftModel.from_pretrained(model, peft_model_id)
        
    return tokenizer, model
  
def generate_answer():
    tokenizer, model = get_models()
    user_message = st.session_state.input_text
    inputs = tokenizer(st.session_state.input_text, return_tensors="pt")
    result = model.generate(**inputs)
    message_bot = tokenizer.decode(result[0], skip_special_tokens=True)  # .replace("<s>", "").replace("</s>", "")
    st.session_state.history.append({"message": user_message, "is_user": True})
    st.session_state.history.append({"message": message_bot, "is_user": False})
    
st.text_input("Response", key="input_text", on_change=generate_answer)

for chat in st.session_state.history:
    st_message(**chat)