Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import openai | |
| import os | |
| from dotenv import load_dotenv | |
| # 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. | |
| **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. | |
| **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 | |
| #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() | |
| 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) | |
| # AI Chat with the survey analysis | |
| st.header("π¬ Chat with AI about the Survey") | |
| st.write("Ask questions about the pulse survey insights.") | |
| if "messages" not in st.session_state: | |
| st.session_state["messages"] = [ | |
| {"role": "system", "content": "You are an HR expert analyzing a pulse survey. Provide insights based on the summary below."}, | |
| {"role": "user", "content": st.session_state.get("survey_summary", "No survey uploaded yet.")} | |
| ] | |
| user_input = st.text_input("π Ask a question about the survey results:") | |
| if st.button("Ask AI") or len(prompt)>0: | |
| if user_input: | |
| st.session_state["messages"].append({"role": "user", "content": user_input}) | |
| with st.spinner("π Thinking..."): | |
| ai_response = chat_with_ai(st.session_state["messages"]) | |
| st.session_state["messages"].append({"role": "assistant", "content": ai_response}) | |
| st.markdown(f"**AI:** {ai_response}") | |
| else: | |
| st.warning("β οΈ Please enter a question.") | |
| return ai_response | |