Spaces:
Build error
Build error
File size: 6,113 Bytes
d4092d0 6ef1358 d4092d0 a0eb315 d4092d0 fc8ecb0 d4092d0 6159d3f fc8ecb0 6159d3f fc8ecb0 6f2f782 6159d3f fc8ecb0 6f2f782 fc8ecb0 6159d3f fc8ecb0 6159d3f fc8ecb0 6159d3f fc8ecb0 d7e1598 9edfc36 fc8ecb0 d7e1598 fc8ecb0 d7e1598 fc8ecb0 e062738 fc8ecb0 3d743f0 fc8ecb0 9edfc36 fc8ecb0 3d743f0 5254733 9edfc36 d4092d0 5254733 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | 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(
"""
<style>
.st-ew2d {
box-shadow: 0px 2px 5px rgba(0, 0, 0, 0.1);
border: 2px solid goldenrod;
border-radius: 5px;
}
</style>
""",
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(
"""
<style>
.stTextInput input {
box-shadow: 0px 2px 5px rgba(0, 0, 0, 0.1);
border: 2px solid goldenrod;
border-radius: 5px;
}
.section-container {
padding: 10px;
margin-bottom: 10px;
background-color: rgba(0, 0, 0, 0.05);
border-radius: 5px;
box-shadow: 0px 2px 5px rgba(0, 0, 0, 0.1);
}
</style>
""",
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(
"""
<style>
.chat-container {
background-color: #f5f5f5;
border-radius: 5px;
box-shadow: 0px 2px 5px rgba(0, 0, 0, 0.1);
padding: 10px;
margin-bottom: 20px;
}
.user-message {
background-color: #e1ffc7;
padding: 8px;
margin-bottom: 8px;
border-radius: 5px;
max-width: 80%;
}
.assistant-message {
background-color: #f2f2f2;
padding: 8px;
margin-bottom: 8px;
border-radius: 5px;
max-width: 80%;
}
</style>
""",
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"""
<div class="chat-container">
<div class="user-message">{content}</div>
</div>
""",
unsafe_allow_html=True
)
elif role == "assistant":
chat_history.markdown(
f"""
<div class="chat-container">
<div class="assistant-message">{content}</div>
</div>
""",
unsafe_allow_html=True
)
if __name__ == '__main__':
app() |