IanYeo commited on
Commit
08bbfac
·
1 Parent(s): 7161495

frontend chat

Browse files
Files changed (2) hide show
  1. app.py +104 -0
  2. requirements.txt +3 -0
app.py CHANGED
@@ -1,4 +1,105 @@
1
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  option = st.sidebar.selectbox(
4
  "Please select level of difficulty",
@@ -18,3 +119,6 @@ start = st.button("Start", type="primary")
18
  if start:
19
 
20
  st.write(":red[starting...]")
 
 
 
 
1
  import streamlit as st
2
+ import requests
3
+ import simplejson as json
4
+
5
+
6
+ URL = "http://localhost:7094/"
7
+
8
+ full_response = ""
9
+ message_placeholder = st.empty()
10
+
11
+ if "messages" not in st.session_state:
12
+ st.session_state.messages = []
13
+ st.session_state.conversation_id = None
14
+ st.session_state.history = []
15
+
16
+
17
+ def random_response(
18
+ message,
19
+ ):
20
+
21
+ params = {
22
+ 'question': message[-1]['content'],
23
+ # 'conversation_id': Optional[str] = None,
24
+ }
25
+
26
+
27
+ question = {
28
+ 'message': message[-1]['content'],
29
+ }
30
+
31
+ request_session = requests.Session()
32
+ stream = request_session.post(
33
+ f"{URL}stream",
34
+ params=params,
35
+ json=question,
36
+ )
37
+
38
+ for response in stream.iter_lines():
39
+ try:
40
+ response = json.loads(response.decode('utf-8'))
41
+ except json.decoder.JSONDecodeError:
42
+ continue
43
+ yield response
44
+
45
+ for message in st.session_state.messages:
46
+ with st.chat_message(message["role"]):
47
+ st.markdown(message["content"])
48
+
49
+ # Accept user input
50
+ if prompt := st.chat_input("How can I help?"):
51
+ # Add user message to chat history
52
+ st.session_state.messages.append({"role": "user", "content": prompt})
53
+ # Display user message in chat message container
54
+ with st.chat_message("user"):
55
+ st.markdown(prompt)
56
+ # Display assistant response in chat message container
57
+ with st.chat_message("assistant"):
58
+ message_placeholder = st.empty()
59
+ full_response = ""
60
+
61
+
62
+ if len(st.session_state.messages) > 0:
63
+
64
+ for response in random_response(
65
+ # model=st.session_state["openai_model"],
66
+ message=[{"role": m["role"], "content": m["content"]} for m in st.session_state.messages],
67
+ # conversation_id=st.session_state.conversation_id,
68
+ # user=user,
69
+ # chat_type=chat_options[chat_option],
70
+ # stream=True,
71
+ ):
72
+ if 'answer' in response:
73
+ full_response += response['answer']
74
+ message_placeholder.markdown(full_response + "▌")
75
+ # if 'id' in response:
76
+ # st.session_state.conversation_id=response["id"]
77
+ # # if 'type' in response and response['type'] == 'source':
78
+ # if 'next_questions' in response:
79
+ # # st.write(response)
80
+ # next_questions = response['next_questions']
81
+ # count = 0
82
+ # col1, col2= st.columns(2)
83
+ # for next_question in next_questions:
84
+ # count += 1
85
+ # if count % 2 != 0:
86
+ # col1.write(next_question)
87
+ # else:
88
+ # col2.write(next_question)
89
+
90
+ # message_placeholder.markdown(full_response)
91
+ st.session_state.messages.append(
92
+ {
93
+ "role": "assistant",
94
+ "content": full_response,
95
+ "prompt": prompt,
96
+ "history": st.session_state.history,
97
+ # "next_questions": next_questions,
98
+ }
99
+ )
100
+
101
+
102
+
103
 
104
  option = st.sidebar.selectbox(
105
  "Please select level of difficulty",
 
119
  if start:
120
 
121
  st.write(":red[starting...]")
122
+
123
+
124
+
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ streamlit
2
+ simplejson
3
+ requests