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})