import os import openai import streamlit as st import pandas as pd openai.api_key = os.getenv('OPENAI_API_KEY') def app(): st.title('NURYAP | Front Desk AI-assistant') # Change placement location using columns and containers col1, col2 = st.columns([1, 3]) with col1: st.write("Organization Name") organization_name = st.text_input("", key="org_input") st.sidebar.title("Add Sections") section = st.sidebar.text_input("Section", key="section_input") section_phone_number = st.sidebar.text_input("Phone Number", key="phone_input") section_email = st.sidebar.text_input("Email", key="email_input") add_section = st.sidebar.button('Add Section') # Apply styling to sidebar input areas st.sidebar.markdown( """ """, unsafe_allow_html=True ) with col2: # Initialize an empty dataframe if 'data' not in st.session_state: st.session_state['data'] = pd.DataFrame(columns=['Section', 'Phone Number', 'Email']) # Add the new section to the dataframe if add_section: new_data = {'Section': section, 'Phone Number': section_phone_number, 'Email': section_email} st.session_state['data'] = st.session_state['data'].append(new_data, ignore_index=True) # Display the table st.table(st.session_state['data']) # Apply styling to main input area st.markdown( """ """, unsafe_allow_html=True ) user_input = st.text_input("Enter some text") if st.button('Submit'): sections_string = "\n".join(st.session_state['data'].apply(lambda row: f"{row['Section']},{row['Phone Number']},{row['Email']}", axis=1)) messages = [] if organization_name: messages.append({'role': 'system', 'content': f"Welcome to {organization_name} Front Desk!"}) messages.append({'role': 'system', 'content': f"""you are the front desk officer that works for an NGO called {organization_name}. your job is to redirect people to relevant sections in the organization after you analyze their psychological status through a short friendly conversation. Below are the sections inside the organization you will redirect them according to the severity of their case or depending on the need\n\n {sections_string} """}) if user_input: messages.append({"role": "user", "content": user_input}) completion = openai.ChatCompletion.create( model='gpt-4', messages=messages, max_tokens=300, temperature=0.4, presence_penalty=0.2, frequency_penalty=0.7, ) ai_response = completion.choices[-1].message['content'] # Append AI's response to messages messages.append({"role": "assistant", "content": ai_response}) st.markdown( """ """, unsafe_allow_html=True ) # Update chat history chat_history = st.empty() if 'chat_history' not in st.session_state: st.session_state['chat_history'] = [] st.session_state['chat_history'].append({ "role": "user", "content": user_input }) st.session_state['chat_history'].append({ "role": "assistant", "content": ai_response }) for message in st.session_state['chat_history']: role = message["role"] content = message["content"] if role == "user": chat_history.markdown( f"""
{content}
""", unsafe_allow_html=True ) elif role == "assistant": chat_history.markdown( f"""
{content}
""", unsafe_allow_html=True ) if __name__ == '__main__': app()