MCPcopy Create free account
hub / github.com/InternLM/Tutorial / main

Function main

tools/xtuner_streamlit_demo.py:216–265  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

214
215
216def main():
217 # torch.cuda.empty_cache()
218 print('load model begin.')
219 model, tokenizer = load_model()
220 print('load model end.')
221
222
223 st.title('InternLM2-Chat-1.8B')
224
225 generation_config = prepare_generation_config()
226
227 # Initialize chat history
228 if 'messages' not in st.session_state:
229 st.session_state.messages = []
230
231 # Display chat messages from history on app rerun
232 for message in st.session_state.messages:
233 with st.chat_message(message['role'], avatar=message.get('avatar')):
234 st.markdown(message['content'])
235
236 # Accept user input
237 if prompt := st.chat_input('What is up?'):
238 # Display user message in chat message container
239 with st.chat_message('user'):
240 st.markdown(prompt)
241 real_prompt = combine_history(prompt)
242 # Add user message to chat history
243 st.session_state.messages.append({
244 'role': 'user',
245 'content': prompt,
246 })
247
248 with st.chat_message('robot'):
249 message_placeholder = st.empty()
250 for cur_response in generate_interactive(
251 model=model,
252 tokenizer=tokenizer,
253 prompt=real_prompt,
254 additional_eos_token_id=92542,
255 **asdict(generation_config),
256 ):
257 # Display robot response in chat message container
258 message_placeholder.markdown(cur_response + '▌')
259 message_placeholder.markdown(cur_response)
260 # Add robot response to chat history
261 st.session_state.messages.append({
262 'role': 'robot',
263 'content': cur_response, # pylint: disable=undefined-loop-variable
264 })
265 torch.cuda.empty_cache()
266
267
268if __name__ == '__main__':

Callers 1

Calls 4

load_modelFunction · 0.70
combine_historyFunction · 0.70
generate_interactiveFunction · 0.70

Tested by

no test coverage detected