Spaces:
Build error
Build error
Create Home.py
Browse files
Home.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 4 |
+
|
| 5 |
+
# Initialize model and tokenizer
|
| 6 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 7 |
+
"microsoft/Phi-3.5-mini-instruct",
|
| 8 |
+
device_map="cpu",
|
| 9 |
+
torch_dtype=torch.float16,
|
| 10 |
+
low_cpu_mem_usage=True,
|
| 11 |
+
trust_remote_code=True,
|
| 12 |
+
)
|
| 13 |
+
tokenizer = AutoTokenizer.from_pretrained("microsoft/Phi-3.5-mini-instruct")
|
| 14 |
+
|
| 15 |
+
# Create pipeline
|
| 16 |
+
pipe = pipeline(
|
| 17 |
+
"text-generation",
|
| 18 |
+
model=model,
|
| 19 |
+
tokenizer=tokenizer
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# Generation arguments
|
| 23 |
+
generation_args = {
|
| 24 |
+
"max_new_tokens": 500,
|
| 25 |
+
"return_full_text": False,
|
| 26 |
+
"temperature": 0.0,
|
| 27 |
+
"do_sample": False,
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
# Chat function
|
| 31 |
+
def chat(message, history, system_prompt):
|
| 32 |
+
# Prepare messages
|
| 33 |
+
messages = [
|
| 34 |
+
{"role": "system", "content": system_prompt},
|
| 35 |
+
]
|
| 36 |
+
|
| 37 |
+
# Add history to messages
|
| 38 |
+
for human, assistant in history:
|
| 39 |
+
messages.append({"role": "user", "content": human})
|
| 40 |
+
messages.append({"role": "assistant", "content": assistant})
|
| 41 |
+
|
| 42 |
+
# Add current message
|
| 43 |
+
messages.append({"role": "user", "content": message})
|
| 44 |
+
|
| 45 |
+
# Generate response
|
| 46 |
+
output = pipe(messages, **generation_args)
|
| 47 |
+
response = output[0]['generated_text']
|
| 48 |
+
|
| 49 |
+
return response
|
| 50 |
+
|
| 51 |
+
# Streamlit App Layout
|
| 52 |
+
st.title("Testing new Phi 3.5 model")
|
| 53 |
+
st.sidebar.title("Settings")
|
| 54 |
+
system_prompt = st.sidebar.text_input("System Prompt", value="You are a helpful AI assistant.")
|
| 55 |
+
|
| 56 |
+
# Initialize chat history
|
| 57 |
+
if "chat_history" not in st.session_state:
|
| 58 |
+
st.session_state["chat_history"] = []
|
| 59 |
+
|
| 60 |
+
# Input text box
|
| 61 |
+
user_input = st.text_input("You:", key="input")
|
| 62 |
+
|
| 63 |
+
# Chat history display
|
| 64 |
+
if st.session_state["chat_history"]:
|
| 65 |
+
for user_msg, bot_msg in st.session_state["chat_history"]:
|
| 66 |
+
st.write(f"**You:** {user_msg}")
|
| 67 |
+
st.write(f"**Bot:** {bot_msg}")
|
| 68 |
+
|
| 69 |
+
# On submit
|
| 70 |
+
if user_input:
|
| 71 |
+
bot_response = chat(user_input, st.session_state["chat_history"], system_prompt)
|
| 72 |
+
st.session_state["chat_history"].append((user_input, bot_response))
|
| 73 |
+
st.experimental_rerun()
|
| 74 |
+
|
| 75 |
+
# Clear chat history button
|
| 76 |
+
if st.button("Clear Chat"):
|
| 77 |
+
st.session_state["chat_history"] = []
|
| 78 |
+
st.experimental_rerun()
|