Spaces:
Sleeping
Sleeping
import streamlit as st | |
import openai | |
import os | |
from dotenv import load_dotenv | |
import pandas as pd | |
# Load API key from .env file | |
load_dotenv() | |
openai.api_key = os.getenv("OPENAI_API_KEY") | |
def analyze_survey_document(survey_text): | |
"""Send entire survey document to OpenAI for RAG-based analysis.""" | |
prompt = f""" | |
You are an HR analyst evaluating employee pulse survey responses. | |
Below is the entire survey with questions and corresponding employee answers. | |
Also, you have access to LinkedIn posts for your employees Katerina Gawthorpe, Steven Gawthorpe, Deepali Khalkar, and Tyler Robinson. Look at her posts and include them in your analysis about what your employees is up to. Thier role is AI expert, notice if they are personally interested in AI and mention it. | |
**Your tasks:** | |
1. **Summarize responses for each question** - Capture overall themes and sentiments. | |
2. **Identify key areas of dissatisfaction** - Highlight the most frequent complaints. | |
3. **Provide recommendations** - Suggest how the company can address these concerns. | |
4. **Include LinkedIn posts** - Look at their posts and include them in your analysis about what your employees are up to. Their roles are data scinetists and AI experts, notice if they are personally interested in AI and mention it. | |
**Survey Responses:** | |
{survey_text} | |
**Your analysis should include:** | |
- **Summary for each question** | |
- **Overall themes across all questions** | |
- **Most common complaints** | |
- **Actionable recommendations** | |
""" | |
try: | |
response = openai.chat.completions.create( | |
model="gpt-4o", | |
messages=[{"role": "system", "content": "You are an HR analyst summarizing an employee pulse survey."}, | |
{"role": "user", "content": prompt}] | |
) | |
return response.choices[0].message.content | |
except Exception as e: | |
return f"Error: {str(e)}" | |
def chat_with_ai(chat_history): | |
"""Chat interface with OpenAI model.""" | |
try: | |
response = openai.chat.completions.create( | |
model="gpt-4o", | |
messages=chat_history | |
) | |
return response.choices[0].message.content | |
except Exception as e: | |
return f"Error: {str(e)}" | |
def survey_agent(prompt,uploaded_file): | |
st.subheader("π Employee Pulse Survey Analysis & AI Chat") | |
with open('./data/employee_pulse_survey.txt', 'r') as file: | |
survey_text_base = file.read() | |
#uploaded_file = st.file_uploader("π Upload Pulse Survey (.txt)", type="txt") | |
if not uploaded_file: | |
uploaded_file1 = survey_text_base | |
# Get LinkedIn posts | |
df_linkedin=pd.read_csv('./data/linkedin_post_result.csv', usecols=['postTimestamp','postUrl','postContent','author']) | |
df_linkedin=df_linkedin.dropna() | |
df_linkedin_selected=pd.DataFrame() | |
df_linkedin_selected['text']=df_linkedin['postContent'].str.replace('\n', ' ') | |
df_linkedin_selected['author']=df_linkedin['author'].str.replace('\n', ' ') | |
df_linkedin_selected['date']=pd.to_datetime(df_linkedin['postTimestamp']).dt.strftime('%Y-%m-%d') | |
df_linkedin_selected['url']=df_linkedin['postUrl'] | |
df_linkedin_selected=df_linkedin_selected.values.flatten() | |
docs_text_linkedin = "\n".join([f"- {value}" for value in df_linkedin_selected if not pd.isna(value)]) | |
uploaded_file1=uploaded_file1+'\n'+'LinkedIn posts:'+'\n'+docs_text_linkedin | |
#check1 = st.button(f"Generate summary") | |
#if check1: | |
if uploaded_file or uploaded_file1: | |
st.write("β File uploaded successfully! Analyzing responses...") | |
if uploaded_file: | |
survey_text = uploaded_file.read().decode("utf-8").strip() | |
survey_text=survey_text+'\n'+'LinkedIn posts:'+'\n'+docs_text_linkedin | |
else: | |
survey_text = uploaded_file1 | |
with st.spinner("π Analyzing entire survey..."): | |
analysis = analyze_survey_document(survey_text) | |
#st.session_state["survey_summary"] = analysis | |
#st.markdown(analysis) | |
return analysis |