File size: 4,939 Bytes
1e97cbb
 
 
 
 
 
 
 
 
45fe6a4
1e97cbb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import streamlit as st
from langchain_groq import ChatGroq
from langchain.prompts import ChatPromptTemplate
import os
import tempfile
import json
from extraction import extract_cv_data, process_file, display_candidates_info  # importing from your extraction.py

# Initialize environment variables
# os.environ['GROQ_API_KEY'] = os.getenv("GROQ_API_KEY")
groq_api_key = os.getenv("GROQ_API_KEY")

class InterviewQuestionGenerator:
    def __init__(self):
        self.llm = ChatGroq(
            groq_api_key=groq_api_key,
            # model_name="mixtral-8x7b-32768",
            model_name = "llama3-8b-8192",
            temperature=0.7,
            max_tokens=4096
        )
        
        # The prompt template to generate questions based on extracted CV data
        self.question_template = """
        Based on the following CV excerpt, generate 5 specific basic technical interview questions 
        that are directly related to the candidate's experience and skills. Make sure the 
        questions test both their claimed knowledge and problem-solving abilities.

        CV Excerpt:
        {cv_text}

        Skills Mentioned:
        {skills}

       Return the questions in the following text format:

        (bold)
        Question 1:\n

        - Technical_question: "Your question here" \n

        - Follow_up_question: "Deep dive question here" \n

        - What_to_listen_for: "Key points to listen for here" \n

        \n\n
        Question 2:

        - Technical_question: "Your question here" \n
 
        - Follow_up_question: "Deep dive question here" \n

        - What_to_listen_for: "Key points to listen for here" \n


        
        Make sure to follow this format exactly, with the correct structure and labels for each question.
        

        (Repeat for all 5 questions)
        
        Be sure to make each question clear and actionable, and align it with the skills mentioned in the CV.
        """
        
        # Using ChatPromptTemplate for question generation
        self.question_prompt = ChatPromptTemplate.from_messages(
            [
                ("system", self.question_template),
                ("human", "{cv_text}\n{skills}")
            ]
        )
        
    def generate_questions(self, cv_text: str, skills: str) -> str:
        """Generate interview questions based on CV text and skills."""
        runnable = self.question_prompt | self.llm  # Using Runnable instead of LLMChain
        questions = runnable.invoke({
            "cv_text": cv_text,
            "skills": skills
        })
        return questions


def create_interview_questions_page():
    # Initializing session state variables since they dont exist at first
    if 'uploaded_file' not in st.session_state:
        st.session_state.uploaded_file = None
    if 'cv_text' not in st.session_state:
        st.session_state.cv_text = None
    if 'candidates_list' not in st.session_state:
        st.session_state.candidates_list = None
    if 'generated_questions' not in st.session_state:
        st.session_state.generated_questions = None

    st.title("Interview Question Generator")
    
    # File uploader
    uploaded_file = st.file_uploader("Upload a CV", type=['pdf', 'txt'])
    
    # Update session state when new file is uploaded
    if uploaded_file is not None and (st.session_state.uploaded_file is None or 
                                    uploaded_file.name != st.session_state.uploaded_file.name):
        st.session_state.uploaded_file = uploaded_file
        st.session_state.cv_text = None  # Reset CV text
        st.session_state.candidates_list = None  # Reset candidates
        st.session_state.generated_questions = None  # Reset questions
    
    # Process file if it exists in session state
    if st.session_state.uploaded_file is not None:
        # Only process the file if we haven't already
        if st.session_state.cv_text is None:
            st.session_state.cv_text = process_file(st.session_state.uploaded_file)
            st.session_state.candidates_list = extract_cv_data(st.session_state.cv_text)

        # Display candidates info if available
        if st.session_state.candidates_list:
            display_candidates_info(st.session_state.candidates_list)
            
            # Generate questions if not already generated
            if st.session_state.generated_questions is None:
                candidate = st.session_state.candidates_list[0]
                generator = InterviewQuestionGenerator()
                questions = generator.generate_questions(
                    cv_text=st.session_state.cv_text,
                    skills=", ".join(candidate.skills)
                )
                st.session_state.generated_questions = questions.content

            # Display the generated questions
            st.subheader("Recommended Interview Questions")
            st.markdown(st.session_state.generated_questions)