File size: 1,775 Bytes
8c7c78b bd33e48 8c7c78b 60d5812 8c7c78b 60d5812 8c7c78b e798c33 8c7c78b |
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
import streamlit as st
torch.random.manual_seed(0)
model = AutoModelForCausalLM.from_pretrained(
"microsoft/Phi-3-mini-4k-instruct",
#"microsoft/Phi-3-mini-128k-instruct",
device_map="cpu",
torch_dtype="auto",
trust_remote_code=True,
)
tokenizer = AutoTokenizer.from_pretrained("microsoft/Phi-3-mini-4k-instruct") #("microsoft/Phi-3-mini-128k-instruct")
pipe = pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
)
generation_args = {
"max_new_tokens": 500,
"return_full_text": False,
"temperature": 0.0,
"do_sample": False,
}
st.title("π¬ Chatbot")
st.caption("π A streamlit chatbot powered by Microsoft Phi-3-mini")
# Initialize chat history
if 'messages' not in st.session_state:
st.session_state['messages'] = [] #[{"role": "assistant", "content": "How can I help you?"}]
# Display chat messages from history on app rerun
for messasge in st.session_state.messages:
st.chat_message(messasge["role"]).write(messasge["content"])
# React to user input
if prompt := st.chat_input():
# Display user message in chat message container
st.chat_message("user").write(prompt)
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
messages=st.session_state.messages
##Get response to the message using client
output = pipe(messages, **generation_args)
msg = output[0]['generated_text']
# Display assistant response in chat message container
st.chat_message("assistant").write(msg)
# Add assistant response to chat history
st.session_state.messages.append({"role": "assistant", "content": msg}) |