Sagar23p commited on
Commit
200c107
1 Parent(s): a5a8f2b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -54
app.py CHANGED
@@ -1,37 +1,15 @@
1
- ### app.py code is taken from https://huggingface.co/spaces/ngebodh/SimpleChatbot-Backup/blob/main/app.py
2
- ### https://medium.com/@nigelgebodh/large-language-models-chatting-with-ai-chatbots-from-google-mistral-ai-and-hugging-face-b33efedea38d
3
-
4
- """ Simple Chatbot
5
- @author: Sagar Padhiyar
6
- @email: spadhiyar230595@gmail.com
7
- """
8
-
9
  import streamlit as st
10
- from openai import OpenAI
11
  import os
12
  import sys
13
- from dotenv import load_dotenv, dotenv_values
14
- from huggingface_hub import InferenceClient
15
- load_dotenv()
16
-
17
-
18
-
19
-
20
-
21
 
22
- # # initialize the client
23
- # client = OpenAI(
24
- # base_url="https://api-inference.huggingface.co/v1",
25
- # api_key=os.environ.get('HUGGINGFACE_API')#"hf_xxx" # Replace with your token
26
- # )
27
 
28
-
29
- base_url="https://api-inference.huggingface.co/v1"
30
 
31
  API_KEY = os.environ.get('HUGGINGFACE_API')
 
32
 
33
-
34
- #Create supported models
35
  model_links ={
36
  "Mistral":base_url+"mistralai/Mistral-7B-Instruct-v0.2",
37
  "Gemma-7B":base_url+"google/gemma-7b-it",
@@ -40,8 +18,6 @@ model_links ={
40
  # "Llama-2":"meta-llama/Llama-2-7b-chat-hf"
41
  }
42
 
43
- headers = {"Authorization":"Bearer "+API_KEY}
44
-
45
  #Pull info about the model to display
46
  model_info ={
47
  "Mistral":
@@ -75,6 +51,13 @@ model_info ={
75
 
76
  }
77
 
 
 
 
 
 
 
 
78
  def reset_conversation():
79
  '''
80
  Resets Conversation
@@ -82,16 +65,7 @@ def reset_conversation():
82
  st.session_state.conversation = []
83
  st.session_state.messages = []
84
  return None
85
-
86
- def format_promt(message, custom_instructions=None):
87
- if custom_instructions:
88
- promt += f"[INST] {custom_instructions} [/INST]"
89
- promt += f"[INST] {message} [/INST]"
90
- return promt
91
-
92
-
93
 
94
- # Define the available models
95
  models =[key for key in model_links.keys()]
96
 
97
  # Create the sidebar with the dropdown for model selection
@@ -100,11 +74,9 @@ selected_model = st.sidebar.selectbox("Select Model", models)
100
  #Create a temperature slider
101
  temp_values = st.sidebar.slider('Select a temperature value', 0.0, 1.0, (0.5))
102
 
103
-
104
  #Add reset button to clear conversation
105
  st.sidebar.button('Reset Chat', on_click=reset_conversation) #Reset button
106
 
107
-
108
  # Create model description
109
  st.sidebar.write(f"You're now chatting with **{selected_model}**")
110
  st.sidebar.markdown(model_info[selected_model]['description'])
@@ -112,8 +84,6 @@ st.sidebar.image(model_info[selected_model]['logo'])
112
  st.sidebar.markdown("*Generated content may be inaccurate or false.*")
113
  st.sidebar.markdown("\nLearn how to build this chatbot [here](https://ngebodh.github.io/projects/2024-03-05/).")
114
 
115
-
116
-
117
  if "prev_option" not in st.session_state:
118
  st.session_state.prev_option = selected_model
119
 
@@ -123,44 +93,34 @@ if st.session_state.prev_option != selected_model:
123
  st.session_state.prev_option = selected_model
124
  reset_conversation()
125
 
126
-
127
-
128
  #Pull in the model we want to use
129
  repo_id = model_links[selected_model]
130
 
131
-
132
  st.subheader(f'AI - {selected_model}')
133
  # st.title(f'ChatBot Using {selected_model}')
134
 
135
- # Set a default model
136
- if selected_model not in st.session_state:
137
- st.session_state[selected_model] = model_links[selected_model]
138
-
139
  # Initialize chat history
140
  if "messages" not in st.session_state:
141
  st.session_state.messages = []
142
 
143
-
144
  # Display chat messages from history on app rerun
145
  for message in st.session_state.messages:
146
  with st.chat_message(message["role"]):
147
  st.markdown(message["content"])
148
 
149
 
150
-
151
  # Accept user input
152
  if prompt := st.chat_input(f"Hi I'm {selected_model}, ask me a question"):
153
 
154
  custom_instruction = "Act like a Human in conversation"
155
 
156
- formated_text = format_promt(prompt, custom_instruction)
157
-
158
  # Display user message in chat message container
159
  with st.chat_message("user"):
160
  st.markdown(prompt)
161
  # Add user message to chat history
162
  st.session_state.messages.append({"role": "user", "content": prompt})
163
 
 
164
 
165
  # Display assistant response in chat message container
166
  with st.chat_message("assistant"):
@@ -171,8 +131,10 @@ if prompt := st.chat_input(f"Hi I'm {selected_model}, ask me a question"):
171
  output = client.text_generation(
172
  formated_text,
173
  temperature=temp_values,#0.5
174
- max_tokens=3000,
 
175
  )
176
 
177
  response = st.write_stream(output)
178
- st.session_state.messages.append({"role": "assistant", "content": response})
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ from huggingface_hub import InferenceClient
3
  import os
4
  import sys
 
 
 
 
 
 
 
 
5
 
6
+ st.title("ChatGPT-like Chat boat")
 
 
 
 
7
 
8
+ base_url="https://api-inference.huggingface.co/models/"
 
9
 
10
  API_KEY = os.environ.get('HUGGINGFACE_API')
11
+ headers = {"Authorization":"Bearer "+API_KEY}
12
 
 
 
13
  model_links ={
14
  "Mistral":base_url+"mistralai/Mistral-7B-Instruct-v0.2",
15
  "Gemma-7B":base_url+"google/gemma-7b-it",
 
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":
 
51
 
52
  }
53
 
54
+ def format_promt(message, custom_instructions=None):
55
+ prompt = ""
56
+ if custom_instructions:
57
+ prompt += f"[INST] {custom_instructions} [/INST]"
58
+ prompt += f"[INST] {message} [/INST]"
59
+ return prompt
60
+
61
  def reset_conversation():
62
  '''
63
  Resets Conversation
 
65
  st.session_state.conversation = []
66
  st.session_state.messages = []
67
  return None
 
 
 
 
 
 
 
 
68
 
 
69
  models =[key for key in model_links.keys()]
70
 
71
  # Create the sidebar with the dropdown for model selection
 
74
  #Create a temperature slider
75
  temp_values = st.sidebar.slider('Select a temperature value', 0.0, 1.0, (0.5))
76
 
 
77
  #Add reset button to clear conversation
78
  st.sidebar.button('Reset Chat', on_click=reset_conversation) #Reset button
79
 
 
80
  # Create model description
81
  st.sidebar.write(f"You're now chatting with **{selected_model}**")
82
  st.sidebar.markdown(model_info[selected_model]['description'])
 
84
  st.sidebar.markdown("*Generated content may be inaccurate or false.*")
85
  st.sidebar.markdown("\nLearn how to build this chatbot [here](https://ngebodh.github.io/projects/2024-03-05/).")
86
 
 
 
87
  if "prev_option" not in st.session_state:
88
  st.session_state.prev_option = selected_model
89
 
 
93
  st.session_state.prev_option = selected_model
94
  reset_conversation()
95
 
 
 
96
  #Pull in the model we want to use
97
  repo_id = model_links[selected_model]
98
 
 
99
  st.subheader(f'AI - {selected_model}')
100
  # st.title(f'ChatBot Using {selected_model}')
101
 
 
 
 
 
102
  # Initialize chat history
103
  if "messages" not in st.session_state:
104
  st.session_state.messages = []
105
 
 
106
  # Display chat messages from history on app rerun
107
  for message in st.session_state.messages:
108
  with st.chat_message(message["role"]):
109
  st.markdown(message["content"])
110
 
111
 
 
112
  # Accept user input
113
  if prompt := st.chat_input(f"Hi I'm {selected_model}, ask me a question"):
114
 
115
  custom_instruction = "Act like a Human in conversation"
116
 
 
 
117
  # Display user message in chat message container
118
  with st.chat_message("user"):
119
  st.markdown(prompt)
120
  # Add user message to chat history
121
  st.session_state.messages.append({"role": "user", "content": prompt})
122
 
123
+ formated_text = format_promt(prompt, custom_instruction)
124
 
125
  # Display assistant response in chat message container
126
  with st.chat_message("assistant"):
 
131
  output = client.text_generation(
132
  formated_text,
133
  temperature=temp_values,#0.5
134
+ max_new_tokens=3000,
135
+ stream=True
136
  )
137
 
138
  response = st.write_stream(output)
139
+ st.session_state.messages.append({"role": "assistant", "content": response})
140
+