Spaces:
Running
Running
hertogateis
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -24,12 +24,22 @@ def get_response(user_input):
|
|
24 |
"temperature": 0.1,
|
25 |
"top_p": 0.9
|
26 |
}
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
# Streamlit chat UI
|
32 |
-
st.title("Kursus AI
|
33 |
|
34 |
# Display the chat history
|
35 |
for message in st.session_state.messages:
|
@@ -38,15 +48,45 @@ for message in st.session_state.messages:
|
|
38 |
else:
|
39 |
st.chat_message("assistant").markdown(message["content"])
|
40 |
|
41 |
-
#
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
# Handle user input and update the chat
|
45 |
-
if user_input:
|
46 |
st.session_state.messages.append({"role": "user", "content": user_input})
|
47 |
response = get_response(user_input)
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
|
|
|
24 |
"temperature": 0.1,
|
25 |
"top_p": 0.9
|
26 |
}
|
27 |
+
|
28 |
+
try:
|
29 |
+
response = requests.post(url, headers=headers, json=data)
|
30 |
+
# Check if the response status code is OK
|
31 |
+
if response.status_code == 200:
|
32 |
+
return response.json() # Only parse JSON if status is OK
|
33 |
+
else:
|
34 |
+
# Log the error if the status code is not 200
|
35 |
+
st.error(f"Error: {response.status_code} - {response.text}")
|
36 |
+
return {}
|
37 |
+
except requests.exceptions.RequestException as e:
|
38 |
+
st.error(f"Request failed: {e}")
|
39 |
+
return {}
|
40 |
|
41 |
# Streamlit chat UI
|
42 |
+
st.title("Contoh ChatBot Kursus AI : silahkan ketik pertanyaan")
|
43 |
|
44 |
# Display the chat history
|
45 |
for message in st.session_state.messages:
|
|
|
48 |
else:
|
49 |
st.chat_message("assistant").markdown(message["content"])
|
50 |
|
51 |
+
# Sticky input box at the bottom with Send button
|
52 |
+
st.markdown("""
|
53 |
+
<style>
|
54 |
+
.chat-container {
|
55 |
+
display: flex;
|
56 |
+
flex-direction: column;
|
57 |
+
height: 80vh;
|
58 |
+
justify-content: flex-end;
|
59 |
+
}
|
60 |
+
.input-container {
|
61 |
+
display: flex;
|
62 |
+
justify-content: space-between;
|
63 |
+
position: sticky;
|
64 |
+
bottom: 0;
|
65 |
+
background-color: white;
|
66 |
+
padding: 10px;
|
67 |
+
border-top: 1px solid #ccc;
|
68 |
+
box-shadow: 0px -1px 5px rgba(0, 0, 0, 0.1);
|
69 |
+
}
|
70 |
+
.send-button {
|
71 |
+
margin-left: 10px;
|
72 |
+
}
|
73 |
+
</style>
|
74 |
+
""", unsafe_allow_html=True)
|
75 |
+
|
76 |
+
# Input container with a text area and button
|
77 |
+
with st.container():
|
78 |
+
input_container = st.empty()
|
79 |
+
user_input = input_container.text_area("You:", "", height=50, key="input_text")
|
80 |
+
send_button = st.button("Send", key="send_button", help="Click to send the message")
|
81 |
|
82 |
# Handle user input and update the chat
|
83 |
+
if send_button and user_input:
|
84 |
st.session_state.messages.append({"role": "user", "content": user_input})
|
85 |
response = get_response(user_input)
|
86 |
|
87 |
+
if response:
|
88 |
+
# Assuming the response is in the 'choices' field of the API response
|
89 |
+
bot_response = response.get('choices', [{}])[0].get('message', {}).get('content', 'Sorry, I did not understand that.')
|
90 |
+
st.session_state.messages.append({"role": "assistant", "content": bot_response})
|
91 |
+
|
92 |
+
st.rerun() # Rerun the app to update the chat history
|