File size: 4,202 Bytes
28ba182
08bbfac
 
9698547
08bbfac
 
9698547
8071692
 
 
 
 
 
 
 
 
b07fba6
 
 
8071692
9698547
 
 
 
17c0b2e
8071692
9698547
 
 
 
 
 
64b81fb
9698547
 
805c6e4
 
 
 
8071692
805c6e4
64b81fb
8071692
805c6e4
 
 
9698547
a60d865
805c6e4
3c774ca
 
08bbfac
 
 
f6bff1c
08bbfac
 
 
 
9ce7596
805c6e4
 
bdb858e
 
 
66641f9
08bbfac
 
 
9ce7596
f6bff1c
08bbfac
 
9ce7596
 
 
 
 
 
 
 
 
f6bff1c
 
 
 
 
 
 
 
 
 
 
9ce7596
08bbfac
 
9ce7596
60bef38
 
9698547
e364d2b
08bbfac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9ce7596
f6bff1c
08bbfac
9ce7596
f6bff1c
08bbfac
9ce7596
f6bff1c
 
08bbfac
 
 
 
 
 
 
 
 
 
28ba182
08bbfac
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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,
        }
    )