Spaces:
Sleeping
Sleeping
Update app.py
Browse files23 unmodified lines2425262728293031323334352 unmodified lines38394041424344454647484950515223 unmodified lines"temperature": 0.1,"top_p": 0.9}response = requests.post(url, headers=headers, json=data)return response.json()# Streamlit chat UIst.title("Kursus AI chatbot")# Display the chat historyfor message in st.session_state.messages:2 unmodified lineselse:st.chat_message("assistant").markdown(message["content"])# Accept user inputuser_input = st.text_input("You: ", "")# Handle user input and update the chatif user_input:st.session_state.messages.append({"role": "user", "content": user_input})response = get_response(user_input)# Assuming the response is in the 'choices' field of the API responsebot_response = response.get('choices', [{}])[0].get('message', {}).get('content', 'Sorry, I did not understand that.')st.session_state.messages.append({"role": "assistant", "content": bot_response})st.rerun() # Corrected line to rerun the app after the response is added23 unmodified lines242526272829303132333435363738394041424344452 unmodified lines48495051525354555657585960616263646566676869707172737475767778798081828384858687888990919223 unmodified lines"temperature": 0.1,"top_p": 0.9}try:response = requests.post(url, headers=headers, json=data)# Check if the response status code is OKif response.status_code == 200:return response.json() # Only parse JSON if status is OKelse:# Log the error if the status code is not 200st.error(f"Error: {response.status_code} - {response.text}")return {}except requests.exceptions.RequestException as e:st.error(f"Request failed: {e}")return {}# Streamlit chat UIst.title("Contoh ChatBot Kursus AI : silahkan ketik pertanyaan")# Display the chat historyfor message in st.session_state.messages:2 unmodified lineselse:st.chat_message("assistant").markdown(message["content"])# Sticky input box at the bottom with Send buttonst.markdown("""<style>.chat-container {display: flex;flex-direction: column;height: 80vh;justify-content: flex-end;}.input-container {display: flex;justify-content: space-between;position: sticky;bottom: 0;background-color: white;padding: 10px;border-top: 1px solid #ccc;box-shadow: 0px -1px 5px rgba(0, 0, 0, 0.1);}.send-button {margin-left: 10px;}</style>""", unsafe_allow_html=True)# Input container with a text area and buttonwith st.container():input_container = st.empty()user_input = input_container.text_area("You:", "", height=50, key="input_text")send_button = st.button("Send", key="send_button", help="Click to send the message")# Handle user input and update the chatif send_button and user_input:st.session_state.messages.append({"role": "user", "content": user_input})response = get_response(user_input)if response:# Assuming the response is in the 'choices' field of the API responsebot_response = response.get('choices', [{}])[0].get('message', {}).get('content', 'Sorry, I did not understand that.')st.session_state.messages.append({"role": "assistant", "content": bot_response})st.rerun() # Rerun the app to update the chat history
app.py+51-11