MCPcopy
hub / github.com/InternLM/Tutorial / main

Function main

tools/streamlit_demo.py:235–282  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

233
234
235def main():
236 # torch.cuda.empty_cache()
237 print('load model begin.')
238 model, tokenizer = load_model()
239 print('load model end.')
240
241 st.title('InternLM2-Chat-1.8B')
242
243 generation_config = prepare_generation_config()
244
245 # Initialize chat history
246 if 'messages' not in st.session_state:
247 st.session_state.messages = []
248
249 # Display chat messages from history on app rerun
250 for message in st.session_state.messages:
251 with st.chat_message(message['role'], avatar=message.get('avatar')):
252 st.markdown(message['content'])
253
254 # Accept user input
255 if prompt := st.chat_input('What is up?'):
256 # Display user message in chat message container
257 with st.chat_message('user'):
258 st.markdown(prompt)
259 real_prompt = combine_history(prompt)
260 # Add user message to chat history
261 st.session_state.messages.append({
262 'role': 'user',
263 'content': prompt,
264 })
265 with st.chat_message('robot'):
266 message_placeholder = st.empty()
267 for cur_response in generate_interactive(
268 model=model,
269 tokenizer=tokenizer,
270 prompt=real_prompt,
271 additional_eos_token_id=92542,
272 **asdict(generation_config),
273 ):
274 # Display robot response in chat message container
275 message_placeholder.markdown(cur_response + '▌')
276 message_placeholder.markdown(cur_response)
277 # Add robot response to chat history
278 st.session_state.messages.append({
279 'role': 'robot',
280 'content': cur_response, # pylint: disable=undefined-loop-variable
281 })
282 torch.cuda.empty_cache()
283
284
285if __name__ == '__main__':

Callers 1

streamlit_demo.pyFile · 0.70

Calls 4

load_modelFunction · 0.70
combine_historyFunction · 0.70
generate_interactiveFunction · 0.70

Tested by

no test coverage detected