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