Spaces:
Paused
Paused
update to mistral chatbot
Browse files
app.py
CHANGED
|
@@ -1,4 +1,145 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
x = st.slider('Select a value')
|
| 4 |
-
st.write(x, 'squared is', x * x)
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
+
import os
|
| 4 |
+
import sys
|
| 5 |
+
|
| 6 |
+
st.title("ChatGPT-like Chatbot")
|
| 7 |
+
|
| 8 |
+
base_url="https://api-inference.huggingface.co/models/"
|
| 9 |
+
|
| 10 |
+
API_KEY = os.environ.get('HUGGINGFACE_API_KEY')
|
| 11 |
+
headers = {"Authorization":"Bearer "+API_KEY}
|
| 12 |
+
|
| 13 |
+
model_links ={
|
| 14 |
+
"Mistral-7B":base_url+"mistralai/Mistral-7B-Instruct-v0.2",
|
| 15 |
+
"Mistral-22B":base_url+"mistral-community/Mixtral-8x22B-v0.1",
|
| 16 |
+
# "Gemma-2B":base_url+"google/gemma-2b-it",
|
| 17 |
+
# "Zephyr-7B-β":base_url+"HuggingFaceH4/zephyr-7b-beta",
|
| 18 |
+
# "Llama-2":"meta-llama/Llama-2-7b-chat-hf"
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
#Pull info about the model to display
|
| 22 |
+
model_info ={
|
| 23 |
+
"Mistral-7B":
|
| 24 |
+
{'description':"""The Mistral model is a **Large Language Model (LLM)** that's able to have question and answer interactions.\n \
|
| 25 |
+
\nIt was created by the [**Mistral AI**](https://mistral.ai/news/announcing-mistral-7b/) team as has over **7 billion parameters.** \n""",
|
| 26 |
+
'logo':'https://mistral.ai/images/logo_hubc88c4ece131b91c7cb753f40e9e1cc5_2589_256x0_resize_q97_h2_lanczos_3.webp'},
|
| 27 |
+
"Mistral-22B":
|
| 28 |
+
{'description':"""The Mistral model is a **Large Language Model (LLM)** that's able to have question and answer interactions.\n \
|
| 29 |
+
\nIt was created by the [**Mistral AI**](https://mistral.ai/news/announcing-mistral-22b/) team as has over **22 billion parameters.** \n""",
|
| 30 |
+
'logo':'https://mistral.ai/images/logo_hubc88c4ece131b91c7cb753f40e9e1cc5_2589_256x0_resize_q97_h2_lanczos_3.webp'}
|
| 31 |
+
|
| 32 |
+
# "Gemma-7B":
|
| 33 |
+
# {'description':"""The Gemma model is a **Large Language Model (LLM)** that's able to have question and answer interactions.\n \
|
| 34 |
+
# \nIt was created by the [**Google's AI Team**](https://blog.google/technology/developers/gemma-open-models/) team as has over **7 billion parameters.** \n""",
|
| 35 |
+
# 'logo':'https://pbs.twimg.com/media/GG3sJg7X0AEaNIq.jpg'},
|
| 36 |
+
# "Gemma-2B":
|
| 37 |
+
# {'description':"""The Gemma model is a **Large Language Model (LLM)** that's able to have question and answer interactions.\n \
|
| 38 |
+
# \nIt was created by the [**Google's AI Team**](https://blog.google/technology/developers/gemma-open-models/) team as has over **2 billion parameters.** \n""",
|
| 39 |
+
# 'logo':'https://pbs.twimg.com/media/GG3sJg7X0AEaNIq.jpg'},
|
| 40 |
+
# "Zephyr-7B":
|
| 41 |
+
# {'description':"""The Zephyr model is a **Large Language Model (LLM)** that's able to have question and answer interactions.\n \
|
| 42 |
+
# \nFrom Huggingface: \n\
|
| 43 |
+
# Zephyr is a series of language models that are trained to act as helpful assistants. \
|
| 44 |
+
# [Zephyr 7B Gemma](https://huggingface.co/HuggingFaceH4/zephyr-7b-gemma-v0.1)\
|
| 45 |
+
# is the third model in the series, and is a fine-tuned version of google/gemma-7b \
|
| 46 |
+
# that was trained on on a mix of publicly available, synthetic datasets using Direct Preference Optimization (DPO)\n""",
|
| 47 |
+
# 'logo':'https://huggingface.co/HuggingFaceH4/zephyr-7b-gemma-v0.1/resolve/main/thumbnail.png'},
|
| 48 |
+
# "Zephyr-7B-β":
|
| 49 |
+
# {'description':"""The Zephyr model is a **Large Language Model (LLM)** that's able to have question and answer interactions.\n \
|
| 50 |
+
# \nFrom Huggingface: \n\
|
| 51 |
+
# Zephyr is a series of language models that are trained to act as helpful assistants. \
|
| 52 |
+
# [Zephyr-7B-β](https://huggingface.co/HuggingFaceH4/zephyr-7b-beta)\
|
| 53 |
+
# is the second model in the series, and is a fine-tuned version of mistralai/Mistral-7B-v0.1 \
|
| 54 |
+
# that was trained on on a mix of publicly available, synthetic datasets using Direct Preference Optimization (DPO)\n""",
|
| 55 |
+
# 'logo':'https://huggingface.co/HuggingFaceH4/zephyr-7b-alpha/resolve/main/thumbnail.png'},
|
| 56 |
+
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
def format_promt(message, custom_instructions=None):
|
| 60 |
+
prompt = ""
|
| 61 |
+
if custom_instructions:
|
| 62 |
+
prompt += f"[INST] {custom_instructions} [/INST]"
|
| 63 |
+
prompt += f"[INST] {message} [/INST]"
|
| 64 |
+
return prompt
|
| 65 |
+
|
| 66 |
+
def reset_conversation():
|
| 67 |
+
'''
|
| 68 |
+
Resets Conversation
|
| 69 |
+
'''
|
| 70 |
+
st.session_state.conversation = []
|
| 71 |
+
st.session_state.messages = []
|
| 72 |
+
return None
|
| 73 |
+
|
| 74 |
+
models =[key for key in model_links.keys()]
|
| 75 |
+
|
| 76 |
+
# Create the sidebar with the dropdown for model selection
|
| 77 |
+
selected_model = st.sidebar.selectbox("Select Model", models)
|
| 78 |
+
|
| 79 |
+
#Create a temperature slider
|
| 80 |
+
temp_values = st.sidebar.slider('Select a temperature value', 0.0, 1.0, (0.5))
|
| 81 |
+
|
| 82 |
+
#Add reset button to clear conversation
|
| 83 |
+
st.sidebar.button('Reset Chat', on_click=reset_conversation) #Reset button
|
| 84 |
+
|
| 85 |
+
# Create model description
|
| 86 |
+
st.sidebar.write(f"You're now chatting with **{selected_model}**")
|
| 87 |
+
st.sidebar.markdown(model_info[selected_model]['description'])
|
| 88 |
+
st.sidebar.image(model_info[selected_model]['logo'])
|
| 89 |
+
st.sidebar.markdown("*Generated content may be inaccurate or false.*")
|
| 90 |
+
#st.sidebar.markdown("\nLearn how to build this chatbot [here](https://ngebodh.github.io/projects/2024-03-05/).")
|
| 91 |
+
|
| 92 |
+
if "prev_option" not in st.session_state:
|
| 93 |
+
st.session_state.prev_option = selected_model
|
| 94 |
+
|
| 95 |
+
if st.session_state.prev_option != selected_model:
|
| 96 |
+
st.session_state.messages = []
|
| 97 |
+
# st.write(f"Changed to {selected_model}")
|
| 98 |
+
st.session_state.prev_option = selected_model
|
| 99 |
+
reset_conversation()
|
| 100 |
+
|
| 101 |
+
#Pull in the model we want to use
|
| 102 |
+
repo_id = model_links[selected_model]
|
| 103 |
+
|
| 104 |
+
st.subheader(f'AI - {selected_model}')
|
| 105 |
+
# st.title(f'ChatBot Using {selected_model}')
|
| 106 |
+
|
| 107 |
+
# Initialize chat history
|
| 108 |
+
if "messages" not in st.session_state:
|
| 109 |
+
st.session_state.messages = []
|
| 110 |
+
|
| 111 |
+
# Display chat messages from history on app rerun
|
| 112 |
+
for message in st.session_state.messages:
|
| 113 |
+
with st.chat_message(message["role"]):
|
| 114 |
+
st.markdown(message["content"])
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
# Accept user input
|
| 118 |
+
if prompt := st.chat_input(f"Hi I'm IELTS Writing Examiner, ask me a essay topic"):
|
| 119 |
+
|
| 120 |
+
custom_instruction = "Act like a IELTS Writing Examiner in conversation"
|
| 121 |
+
|
| 122 |
+
# Display user message in chat message container
|
| 123 |
+
with st.chat_message("user"):
|
| 124 |
+
st.markdown(prompt)
|
| 125 |
+
# Add user message to chat history
|
| 126 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 127 |
+
|
| 128 |
+
formated_text = format_promt(prompt, custom_instruction)
|
| 129 |
+
|
| 130 |
+
# Display assistant response in chat message container
|
| 131 |
+
with st.chat_message("assistant"):
|
| 132 |
+
client = InferenceClient(
|
| 133 |
+
model=model_links[selected_model],
|
| 134 |
+
headers=headers)
|
| 135 |
+
|
| 136 |
+
output = client.text_generation(
|
| 137 |
+
formated_text,
|
| 138 |
+
temperature=temp_values,#0.5
|
| 139 |
+
max_new_tokens=3000,
|
| 140 |
+
stream=True
|
| 141 |
+
)
|
| 142 |
+
|
| 143 |
+
response = st.write_stream(output)
|
| 144 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 145 |
|
|
|
|
|
|