import streamlit as st import requests import simplejson as json from streamlit_option_menu import option_menu game_mode = { 'mode1': 'Sport ⛹🏽', 'mode2': 'Athlete🏋🏽', 'mode3': 'Capital City🌆', 'mode4': 'Country🏴󠁧󠁢󠁥󠁮󠁧󠁿', 'mode5': 'Smartphone📞', 'mode6': 'Graphics Card🖳', 'mode7': 'Animal🧸', 'mode8': 'UK TNC🧑🏼‍💼', 'mode9': 'British Flower🌹', 'mode10': 'Harry Pottter Characters', 'mode11': 'Flags of countries' } game_selection = st.sidebar.selectbox( "Please select your game mode", [x for x in game_mode.values()], index=0, placeholder="select here...", key = "game_selection", ) option = option_menu( menu_title=None, options = ["EASY", "NORMAL", "HARD"], default_index=1, orientation="horizontal", ) st.sidebar.write('You selected:', option) st.title(f"Welcome to the :red[{game_selection}] guessing game❓") st.markdown(f"This game will test your {game_selection} knowledge") st.markdown(f"Will you be able to guess the {game_selection} in the 10 yes or no questions?") clear_chat = st.sidebar.button("Reset", type = "primary") st.sidebar.write("[Instagram](https://www.instagram.com/freddieyeo1/)") URL = st.secrets["URL"] KEY = st.secrets["KEY"] full_response = "" message_placeholder = st.empty() answer = None if "messages" not in st.session_state: st.session_state.messages = [] st.session_state.conversation_id = None st.session_state.answer = None if clear_chat: st.session_state.messages = [] st.session_state.conversation_id = None st.session_state.answer = None def random_response( message, answer: str = None, difficulty: str = "Normal", ): all_history = [] for single_message in message[:-1]: if single_message['role'] == 'user': all_history.append( single_message["content"]) if single_message['role'] == 'assistant': all_history.append(single_message["content"]) count = 0 history_single = {} history_list = [] for item in all_history: count += 1 if count % 2 != 0: history_single["prompt"] = item else: history_single["response"] = item history_list.append(history_single) history_single = {} question = { 'message': message[-1]['content'], 'history': history_list, 'answer': answer, 'difficulty': option.lower(), 'gameSelection': game_selection, 'key': KEY, } request_session = requests.Session() stream = request_session.post( f"{URL}stream", json=question, ) for response in stream.iter_lines(): try: response = json.loads(response.decode('utf-8')) except json.decoder.JSONDecodeError: continue yield response for message in st.session_state.messages: with st.chat_message(message["role"]): st.markdown(message["content"]) # Accept user input if prompt := st.chat_input("How can I help?"): # Add user message to chat history st.session_state.messages.append({"role": "user", "content": prompt}) # Display user message in chat message container with st.chat_message("user"): st.markdown(prompt) # Display assistant response in chat message container with st.chat_message("assistant"): message_placeholder = st.empty() full_response = "" if len(st.session_state.messages) > 0: for response in random_response( message=[{"role": m["role"], "content": m["content"]} for m in st.session_state.messages], answer=st.session_state.answer, difficulty=option, ): if 'questionAnswer' in response: full_response += response['questionAnswer'] message_placeholder.markdown(full_response + "▌") if 'answer' in response: st.session_state.answer = response['answer'] st.session_state.messages.append( { "role": "assistant", "content": full_response, "prompt": prompt, } )