Update app.py
Browse files
app.py
CHANGED
|
@@ -1,63 +1,78 @@
|
|
| 1 |
-
|
| 2 |
-
from
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
def respond(
|
| 11 |
-
message,
|
| 12 |
-
history: list[tuple[str, str]],
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
-
|
| 20 |
-
for val in history:
|
| 21 |
-
if val[0]:
|
| 22 |
-
messages.append({"role": "user", "content": val[0]})
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
-
|
| 26 |
-
messages.append({"role": "user", "content": message})
|
| 27 |
-
|
| 28 |
-
response = ""
|
| 29 |
-
|
| 30 |
-
for message in client.chat_completion(
|
| 31 |
-
messages,
|
| 32 |
-
max_tokens=max_tokens,
|
| 33 |
-
stream=True,
|
| 34 |
-
temperature=temperature,
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
-
|
| 39 |
-
response += token
|
| 40 |
-
yield response
|
| 41 |
-
|
| 42 |
-
"""
|
| 43 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 44 |
-
"""
|
| 45 |
-
demo = gr.ChatInterface(
|
| 46 |
-
respond,
|
| 47 |
-
additional_inputs=[
|
| 48 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 49 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 50 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 51 |
-
gr.Slider(
|
| 52 |
-
minimum=0.1,
|
| 53 |
-
maximum=1.0,
|
| 54 |
-
value=0.95,
|
| 55 |
-
step=0.05,
|
| 56 |
-
label="Top-p (nucleus sampling)",
|
| 57 |
-
),
|
| 58 |
-
],
|
| 59 |
-
)
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
if __name__ == "__main__":
|
| 63 |
-
|
|
|
|
|
|
|
|
|
| 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 |
+
|