Update app.py
Browse files
app.py
CHANGED
|
@@ -1,73 +1,78 @@
|
|
| 1 |
-
|
| 2 |
-
from
|
| 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 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
| 72 |
if __name__ == "__main__":
|
| 73 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
from peft import AutoPeftModelForCausalLM
|
| 2 |
+
from transformers import GenerationConfig
|
| 3 |
+
from transformers import AutoTokenizer
|
| 4 |
+
import torch
|
| 5 |
+
import streamlit as st
|
| 6 |
+
from streamlit_chat import message
|
| 7 |
+
|
| 8 |
+
st.session_state.clicked=True
|
| 9 |
+
|
| 10 |
+
def process_data_sample(example):
|
| 11 |
+
|
| 12 |
+
processed_example = "<|system|>\n You are a support chatbot who helps with user queries chatbot who always responds in the style of a professional.</s>\n<|user|>\n" + example + "</s>\n<|assistant|>\n"
|
| 13 |
+
|
| 14 |
+
return processed_example
|
| 15 |
+
|
| 16 |
+
@st.cache_resource(show_spinner=True)
|
| 17 |
+
def create_bot():
|
| 18 |
+
|
| 19 |
+
tokenizer = AutoTokenizer.from_pretrained("Vasanth/zephyr-support-chatbot")
|
| 20 |
+
|
| 21 |
+
model = AutoPeftModelForCausalLM.from_pretrained(
|
| 22 |
+
"Vasanth/zephyr-support-chatbot",
|
| 23 |
+
low_cpu_mem_usage=True,
|
| 24 |
+
return_dict=True,
|
| 25 |
+
torch_dtype=torch.float16,
|
| 26 |
+
device_map="cuda"
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
generation_config = GenerationConfig(
|
| 30 |
+
do_sample=True,
|
| 31 |
+
temperature=0.5,
|
| 32 |
+
max_new_tokens=256,
|
| 33 |
+
pad_token_id=tokenizer.eos_token_id
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
return model, tokenizer, generation_config
|
| 37 |
+
|
| 38 |
+
model, tokenizer, generation_config = create_bot()
|
| 39 |
+
|
| 40 |
+
bot = create_bot()
|
| 41 |
+
|
| 42 |
+
def infer_bot(prompt):
|
| 43 |
+
inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
|
| 44 |
+
outputs = model.generate(**inputs, generation_config=generation_config)
|
| 45 |
+
out_str = tokenizer.decode(outputs[0], skip_special_tokens=True).replace(prompt, '')
|
| 46 |
+
return out_str
|
| 47 |
+
|
| 48 |
+
def display_conversation(history):
|
| 49 |
+
for i in range(len(history["assistant"])):
|
| 50 |
+
message(history["user"][i], is_user=True, key=str(i) + "_user")
|
| 51 |
+
message(history["assistant"][i],key=str(i))
|
| 52 |
+
|
| 53 |
+
def main():
|
| 54 |
+
|
| 55 |
+
st.title("Support Member 📚🤖")
|
| 56 |
+
st.subheader("A bot created using Zephyr which was finetuned to possess the capabilities to be a support member")
|
| 57 |
+
|
| 58 |
+
user_input = st.text_input("Enter your query")
|
| 59 |
+
|
| 60 |
+
if "assistant" not in st.session_state:
|
| 61 |
+
st.session_state["assistant"] = ["I am ready to help you"]
|
| 62 |
+
if "user" not in st.session_state:
|
| 63 |
+
st.session_state["user"] = ["Hey there!"]
|
| 64 |
+
|
| 65 |
+
if st.session_state.clicked:
|
| 66 |
+
if st.button("Answer"):
|
| 67 |
+
|
| 68 |
+
answer = infer_bot(user_input)
|
| 69 |
+
st.session_state["user"].append(user_input)
|
| 70 |
+
st.session_state["assistant"].append(answer)
|
| 71 |
+
|
| 72 |
+
if st.session_state["assistant"]:
|
| 73 |
+
display_conversation(st.session_state)
|
| 74 |
+
|
| 75 |
if __name__ == "__main__":
|
| 76 |
+
main()
|
| 77 |
+
|
| 78 |
+
|