import streamlit as st from services import analyse_service, generation_service from mock_data import MOCK_JD, MOCK_RESUME, Analysis_Summary, Resume, Cover_Letter def initialize_session_state(): """Initializes session state variables.""" if 'summary' not in st.session_state: st.session_state.summary = None if 'resume_md' not in st.session_state: st.session_state.resume_md = None if 'cover_letter_txt' not in st.session_state: st.session_state.cover_letter_txt = None if 'error' not in st.session_state: st.session_state.error = None def handle_analyse(jd, user_info): """Handles the 'Analyse' button click.""" if not jd or not user_info: st.session_state.error = "Job Description and User Info cannot be empty." return st.session_state.error = None with st.spinner("Analyzing job description..."): try: summary = analyse_service.analyse(jd, user_info) st.session_state.summary = summary except Exception as e: st.session_state.error = f"An error occurred during analysis: {e}" def handle_generate_resume(): """Handles the 'Generate Resume' button click.""" if not st.session_state.summary: st.session_state.error = "You must perform an initial analysis first." return st.session_state.error = None with st.spinner("Generating resume..."): try: user_info = st.session_state.user_info_input resume_md = generation_service.generate_resume(st.session_state.summary, user_info) st.session_state.resume_md = resume_md except Exception as e: st.session_state.error = f"An error occurred during resume generation: {e}" def handle_generate_cover_letter(): """Handles the 'Generate Cover Letter' button click.""" if not st.session_state.summary: st.session_state.error = "You must perform an initial analysis first." return st.session_state.error = None with st.spinner("Generating cover letter..."): try: user_info = st.session_state.user_info_input cover_letter_txt = generation_service.generate_cover_letter(st.session_state.summary, user_info) st.session_state.cover_letter_txt = cover_letter_txt except Exception as e: st.session_state.error = f"An error occurred during cover letter generation: {e}" def handle_refine(feedback): """Handles the 'Refine' button click.""" if not st.session_state.summary: st.session_state.error = "You must perform an initial analysis first." return if not feedback: st.session_state.error = "Feedback cannot be empty." return st.session_state.error = None with st.spinner("Refining analysis..."): try: user_info = st.session_state.user_info_input updated_summary = analyse_service.refine(st.session_state.summary, feedback) st.session_state.summary = updated_summary except Exception as e: st.session_state.error = f"An error occurred during refinement: {e}" def handle_mock_analyse(): """Handles the 'Mock Analyse' button click.""" st.session_state.error = None st.session_state.summary = Analysis_Summary st.session_state.resume_md = Resume st.session_state.cover_letter_txt = Cover_Letter def main(): """Main function to run the Streamlit app.""" st.set_page_config(layout="wide") st.title("AI-powered Resume & Cover Letter Generator") initialize_session_state() # --- Sidebar for Inputs and Controls --- with st.sidebar: st.header("Inputs") # Mock data buttons col1, col2, col3 = st.columns(3) with col1: if st.button("Load Mock JD", help="Load sample job description"): st.session_state.jd_mock = MOCK_JD with col2: if st.button("Load Mock Resume", help="Load sample resume"): st.session_state.resume_mock = MOCK_RESUME with col3: if st.button("Mock Analyse", help="Load sample analysis results"): handle_mock_analyse() jd_input = st.text_area("1. Paste Job Description", value=st.session_state.get('jd_mock', ''), height=150) user_info_input = st.text_area("2. Paste Your Resume/Info", value=st.session_state.get('resume_mock', ''), height=200) st.session_state.user_info_input = user_info_input # Store for refine if st.button("Analyse", use_container_width=True): handle_analyse(jd_input, user_info_input) st.header("Generate Documents") col1, col2 = st.columns(2) with col1: if st.button("Generate Resume", use_container_width=True): handle_generate_resume() with col2: if st.button("Generate Cover Letter", use_container_width=True): handle_generate_cover_letter() st.header("Refine") feedback_input = st.text_area("3. Provide Feedback to Refine", height=100) if st.button("Refine", use_container_width=True): handle_refine(feedback_input) # --- Main Area for Outputs --- if st.session_state.error: st.error(st.session_state.error) if st.session_state.summary: st.header("Analysis Summary") summary = st.session_state.summary # Key Skills if 'key_skills' in summary: st.subheader("🎯 Key Skills") st.write(", ".join(summary['key_skills'])) # Match Points if 'match_points' in summary: st.subheader("✅ Match Points") for point in summary['match_points']: st.write(f"• {point}") # Gap Points if 'gap_points' in summary: st.subheader("⚠️ Gap Points") for gap in summary['gap_points']: st.write(f"• {gap}") # Suggestions if 'suggestions' in summary: st.subheader("💡 Suggestions") for suggestion in summary['suggestions']: st.write(f"• {suggestion}") # Pitch if 'pitch' in summary: st.subheader("🎤 Executive Pitch") st.write(summary['pitch']) col1, col2 = st.columns(2) with col1: st.header("Resume Preview") if st.session_state.resume_md: st.markdown(st.session_state.resume_md) else: st.info("Resume will be generated here.") with col2: st.header("Cover Letter Preview") if st.session_state.cover_letter_txt: st.text(st.session_state.cover_letter_txt) else: st.info("Cover Letter will be generated here.") if __name__ == "__main__": main()