ukingyu commited on
Commit
8ef3482
·
verified ·
1 Parent(s): 227fde9

Initial add

Browse files
Files changed (1) hide show
  1. app.py +64 -0
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import streamlit as st
3
+ import requests
4
+ import uuid
5
+
6
+ API_URL = os.environ.get("AWS_INVOKE_URL")
7
+ st.title('Hi, This is AppliBot')
8
+
9
+ intro_message = """Need help navigating the maze of college applications?
10
+ Deadlines, recommendation letters, essays—I’ve got you covered.
11
+ Let’s tackle it together, one question at a time!"""
12
+
13
+ example_message = """For example:
14
+ When is the deadline for MIT undergraduate application?
15
+ How many recommendation letters are required?"""
16
+
17
+ # Init session_id
18
+ if 'session_id' not in st.session_state:
19
+ st.session_state.session_id = str(uuid.uuid4())
20
+
21
+ # Init chat_history and display
22
+ if 'chat_history' not in st.session_state:
23
+ st.session_state.chat_history = [
24
+ {'role': 'assistant', 'text': intro_message},
25
+ {'role': 'assistant', 'text': example_message}]
26
+ for message in st.session_state.chat_history:
27
+ with st.chat_message(message['role']):
28
+ st.markdown(message['text'])
29
+
30
+ def clean_message(message):
31
+ # Remove quote ""
32
+ message = message[1:-1]
33
+
34
+ # Take care of \n\n
35
+ message = message.replace("\\n", "\n") # \n(\\n) -> actual newline
36
+ message = message.replace("\n\n", "\n")
37
+
38
+ return message.strip()
39
+
40
+ # Input user message
41
+ input_text = st.chat_input('Chat here')
42
+ if input_text:
43
+ with st.chat_message('user'):
44
+ st.markdown(input_text)
45
+ st.session_state.chat_history.append({
46
+ 'role': 'user', 'text': input_text})
47
+ try:
48
+ response_raw = requests.post(API_URL,
49
+ params={'session_id': st.session_state.session_id,
50
+ 'user_input': input_text}
51
+ )
52
+ response_data = response_raw.json()
53
+ if response_data.get("statusCode") == 200:
54
+ chat_response = response_data.get('body')
55
+ chat_response = clean_message(chat_response)
56
+ else:
57
+ chat_response = "Sorry, some errors. Can you ask again?"
58
+
59
+ with st.chat_message('assistant'):
60
+ st.markdown(chat_response)
61
+ st.session_state.chat_history.append(
62
+ {'role': 'assistant', 'text': chat_response})
63
+ except requests.exceptions.RequestException as e:
64
+ st.error(f"Error communicating with the backend: {e}")