Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import streamlit as st
|
|
| 3 |
from langchain_openai import ChatOpenAI
|
| 4 |
from langchain_openai.embeddings import OpenAIEmbeddings
|
| 5 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
|
|
|
| 6 |
import markdown
|
| 7 |
from operator import itemgetter
|
| 8 |
from langchain.schema.runnable import RunnablePassthrough
|
|
@@ -16,7 +17,7 @@ import os
|
|
| 16 |
import pandas as pd
|
| 17 |
import numpy as np
|
| 18 |
import datetime
|
| 19 |
-
|
| 20 |
|
| 21 |
# App config
|
| 22 |
load_dotenv()
|
|
@@ -33,7 +34,7 @@ st.set_page_config(
|
|
| 33 |
page_icon="π",
|
| 34 |
)
|
| 35 |
|
| 36 |
-
|
| 37 |
# Load environment variables
|
| 38 |
uploaded_file = None
|
| 39 |
topic='employment'
|
|
@@ -114,10 +115,26 @@ with sidebar:
|
|
| 114 |
date=str(date)
|
| 115 |
prompt = st.button("Summarize News", key="chat_button", use_container_width=True)
|
| 116 |
|
| 117 |
-
st.subheader("π
|
| 118 |
-
uploaded_file = st.file_uploader("π Upload
|
| 119 |
st.session_state['uploaded_file'] = uploaded_file
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
# Handle search submission
|
| 123 |
|
|
@@ -298,7 +315,7 @@ if "openai_model" not in st.session_state:
|
|
| 298 |
prompt1 = st.chat_input("Type your additional questions here...")
|
| 299 |
|
| 300 |
# Suggested keywords with enhanced styling
|
| 301 |
-
suggested_keywords = ["Latest News", "News on remote work", f"Survey sentiment", f"Employee satisfaction", f"How many employees
|
| 302 |
st.markdown("**Suggested Keywords:**")
|
| 303 |
cols = st.columns(len(suggested_keywords))
|
| 304 |
for idx, keyword in enumerate(suggested_keywords):
|
|
@@ -307,12 +324,26 @@ for idx, keyword in enumerate(suggested_keywords):
|
|
| 307 |
|
| 308 |
if prompt1:
|
| 309 |
st.session_state.messages.append({"role": "user", "content": prompt1})
|
| 310 |
-
|
| 311 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 312 |
df_linkedin=pd.read_csv('./data/linkedin_post_result.csv', usecols=['postTimestamp','postUrl','postContent','author'])
|
| 313 |
df_linkedin_selected=df_linkedin.values.flatten()
|
| 314 |
docs_text_linkedin = "\n".join([f"- {value}" for value in df_linkedin_selected if not pd.isna(value)])
|
| 315 |
-
survey_txt_all=survey_txt+'\n'+'LinkedIn posts:'+'\n'+docs_text_linkedin
|
| 316 |
with open('./data/events.txt', 'r') as file:
|
| 317 |
events_txt = file.read()
|
| 318 |
|
|
@@ -327,7 +358,7 @@ if prompt1:
|
|
| 327 |
response = base_llm.invoke(f"""You are the Supervisor of the EVE company. In your team you have, general conversation analyst, data analyst, survey analyst and news article analyst.
|
| 328 |
If the question {prompt1} can be answered from the history of the conversation:{st.session_state.messages[-10:]}, from events file: {events_txt} or you can use your knowledge respond 'history' with the exception of the reddit news those leave to 'news' team member.
|
| 329 |
If not: decide if the question: '{prompt1}' is about employees data (such as about Betsy or Katerina or Steven). You have employees data in a huge database with the following columns: {database_columns}, it has information about all employees. If yes, respond 'data'.
|
| 330 |
-
If not: decide if the question is asking about the survey: {survey_txt}. If yes, respond 'survey'.
|
| 331 |
If not: decide if the question is asking about linkedin posts: {docs_text_linkedin}. If yes, respond 'history'.
|
| 332 |
If not: decide if the question is asking about news articles on employment trends or remote work or reddit news articles. If yes, respond 'news'.
|
| 333 |
Your response will be either 'history' or 'data' or 'survey' or 'news'.
|
|
@@ -374,7 +405,8 @@ if prompt1:
|
|
| 374 |
print('survey')
|
| 375 |
# SURVEY AGENT
|
| 376 |
import survey_agent1
|
| 377 |
-
|
|
|
|
| 378 |
st.session_state.messages.append({"role": "survey_agent", "content": response})
|
| 379 |
# st.chat_message("assistant").write(str(response))
|
| 380 |
|
|
|
|
| 3 |
from langchain_openai import ChatOpenAI
|
| 4 |
from langchain_openai.embeddings import OpenAIEmbeddings
|
| 5 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 6 |
+
from langchain_community.document_loaders.pdf import PyMuPDFLoader
|
| 7 |
import markdown
|
| 8 |
from operator import itemgetter
|
| 9 |
from langchain.schema.runnable import RunnablePassthrough
|
|
|
|
| 17 |
import pandas as pd
|
| 18 |
import numpy as np
|
| 19 |
import datetime
|
| 20 |
+
import tempfile
|
| 21 |
|
| 22 |
# App config
|
| 23 |
load_dotenv()
|
|
|
|
| 34 |
page_icon="π",
|
| 35 |
)
|
| 36 |
|
| 37 |
+
st.session_state.uploaded_files_data = {}
|
| 38 |
# Load environment variables
|
| 39 |
uploaded_file = None
|
| 40 |
topic='employment'
|
|
|
|
| 115 |
date=str(date)
|
| 116 |
prompt = st.button("Summarize News", key="chat_button", use_container_width=True)
|
| 117 |
|
| 118 |
+
st.subheader("π Document upload")
|
| 119 |
+
uploaded_file = st.file_uploader("π Upload document (.txt, .pdf)", type=["txt", "pdf"])
|
| 120 |
st.session_state['uploaded_file'] = uploaded_file
|
| 121 |
+
if uploaded_file:
|
| 122 |
+
if uploaded_file.name not in st.session_state.uploaded_files_data:
|
| 123 |
+
file_suffix = ".pdf" if uploaded_file.name.lower().endswith(".pdf") else ".txt"
|
| 124 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=file_suffix) as temp:
|
| 125 |
+
temp.write(uploaded_file.read())
|
| 126 |
+
temp_path = temp.name
|
| 127 |
+
# Store the temp path in session state
|
| 128 |
+
st.session_state.uploaded_files_data[uploaded_file.name] = {
|
| 129 |
+
'path': temp_path,
|
| 130 |
+
'type': file_suffix
|
| 131 |
+
}
|
| 132 |
+
else:
|
| 133 |
+
# Use the stored path
|
| 134 |
+
temp_path = st.session_state.uploaded_files_data[uploaded_file.name]['path']
|
| 135 |
+
file_suffix = st.session_state.uploaded_files_data[uploaded_file.name]['type']
|
| 136 |
+
|
| 137 |
+
prompt_survey = st.button("Results", key="chat_button1", use_container_width=True)
|
| 138 |
|
| 139 |
# Handle search submission
|
| 140 |
|
|
|
|
| 315 |
prompt1 = st.chat_input("Type your additional questions here...")
|
| 316 |
|
| 317 |
# Suggested keywords with enhanced styling
|
| 318 |
+
suggested_keywords = ["Latest News", "News on remote work", f"Survey sentiment", f"Employee satisfaction", f"How many employees?"]
|
| 319 |
st.markdown("**Suggested Keywords:**")
|
| 320 |
cols = st.columns(len(suggested_keywords))
|
| 321 |
for idx, keyword in enumerate(suggested_keywords):
|
|
|
|
| 324 |
|
| 325 |
if prompt1:
|
| 326 |
st.session_state.messages.append({"role": "user", "content": prompt1})
|
| 327 |
+
try:
|
| 328 |
+
temp_path = st.session_state.uploaded_files_data[uploaded_file.name]['path']
|
| 329 |
+
file_suffix = st.session_state.uploaded_files_data[uploaded_file.name]['type']
|
| 330 |
+
survey_txt=''
|
| 331 |
+
handbook_txt=''
|
| 332 |
+
|
| 333 |
+
if st.session_state.uploaded_files_data[uploaded_file.name]['path']:
|
| 334 |
+
if file_suffix == ".pdf":
|
| 335 |
+
docs = PyMuPDFLoader(temp_path).load()
|
| 336 |
+
handbook_txt = "\n".join(d.page_content for d in docs)
|
| 337 |
+
else:
|
| 338 |
+
survey_txt = uploaded_file.read().decode('utf-8')
|
| 339 |
+
except Exception as e:
|
| 340 |
+
st.write(f"Upload your survey or handbook document to analyze it.")
|
| 341 |
+
survey_txt = ''
|
| 342 |
+
handbook_txt = ''
|
| 343 |
df_linkedin=pd.read_csv('./data/linkedin_post_result.csv', usecols=['postTimestamp','postUrl','postContent','author'])
|
| 344 |
df_linkedin_selected=df_linkedin.values.flatten()
|
| 345 |
docs_text_linkedin = "\n".join([f"- {value}" for value in df_linkedin_selected if not pd.isna(value)])
|
| 346 |
+
survey_txt_all=survey_txt+'\n'+'LinkedIn posts:'+'\n'+docs_text_linkedin +'\n'+'Employee handbook:'+'\n'+handbook_txt
|
| 347 |
with open('./data/events.txt', 'r') as file:
|
| 348 |
events_txt = file.read()
|
| 349 |
|
|
|
|
| 358 |
response = base_llm.invoke(f"""You are the Supervisor of the EVE company. In your team you have, general conversation analyst, data analyst, survey analyst and news article analyst.
|
| 359 |
If the question {prompt1} can be answered from the history of the conversation:{st.session_state.messages[-10:]}, from events file: {events_txt} or you can use your knowledge respond 'history' with the exception of the reddit news those leave to 'news' team member.
|
| 360 |
If not: decide if the question: '{prompt1}' is about employees data (such as about Betsy or Katerina or Steven). You have employees data in a huge database with the following columns: {database_columns}, it has information about all employees. If yes, respond 'data'.
|
| 361 |
+
If not: decide if the question is asking about the survey or employee handbook: {survey_txt}. If yes, respond 'survey'.
|
| 362 |
If not: decide if the question is asking about linkedin posts: {docs_text_linkedin}. If yes, respond 'history'.
|
| 363 |
If not: decide if the question is asking about news articles on employment trends or remote work or reddit news articles. If yes, respond 'news'.
|
| 364 |
Your response will be either 'history' or 'data' or 'survey' or 'news'.
|
|
|
|
| 405 |
print('survey')
|
| 406 |
# SURVEY AGENT
|
| 407 |
import survey_agent1
|
| 408 |
+
print(survey_txt_all)
|
| 409 |
+
response = survey_agent1.analyze_survey_document(survey_txt_all, f'the question is: {prompt1} and the history is: {st.session_state.messages[-10:]}')
|
| 410 |
st.session_state.messages.append({"role": "survey_agent", "content": response})
|
| 411 |
# st.chat_message("assistant").write(str(response))
|
| 412 |
|