Spaces:
Build error
Build error
Download app.py from drgriffen/SmartRelief: direct link, hf CLI and curl.
- Browser
- Download file 6.11 kB
-
https://huggingface.co/spaces/drgriffen/SmartRelief/resolve/main/app.py
- Command line
-
hf download hf://spaces/drgriffen/SmartRelief/app.py
-
curl -L -o app.py https://huggingface.co/spaces/drgriffen/SmartRelief/resolve/main/app.py
6.11 kB
| 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() |