File size: 13,790 Bytes
103e34c
3c9b344
33a5665
bd3a877
5fab064
516beb5
 
 
 
f4faaba
 
dddffe1
f4faaba
 
 
 
aeab0b8
dddffe1
78d1f56
dddffe1
 
 
 
 
 
78d1f56
dddffe1
 
 
 
 
 
78d1f56
 
dddffe1
 
 
 
 
 
 
 
 
 
 
 
78d1f56
 
29b3e8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dddffe1
 
78d1f56
 
dddffe1
 
 
78d1f56
dddffe1
 
 
aeab0b8
78d1f56
dddffe1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29b3e8f
dddffe1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29b3e8f
 
 
 
 
fbcdfae
29b3e8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aeab0b8
f4faaba
 
 
 
 
 
 
 
 
 
516beb5
f4faaba
 
 
 
 
 
 
 
 
 
 
 
dddffe1
f4faaba
 
 
 
 
 
 
 
 
dddffe1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f4faaba
 
 
 
 
 
 
 
 
 
 
 
 
 
dddffe1
 
f4faaba
 
 
 
 
 
516beb5
f4faaba
aeab0b8
dddffe1
516beb5
29b3e8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
516beb5
f4faaba
 
 
 
 
 
 
 
dddffe1
f4faaba
 
 
 
 
aeab0b8
 
 
 
 
 
 
f4faaba
 
29b3e8f
 
 
 
 
 
dddffe1
29b3e8f
 
bd3a877
f4faaba
 
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import streamlit as st
from PyPDF2 import PdfReader
from transformers import pipeline
import re
import random
from concurrent.futures import ThreadPoolExecutor
import time
import torch
import spacy
from reportlab.lib import colors
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image, Table, TableStyle
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.pdfbase import pdfmetrics
from reportlab.lib.enums import TA_CENTER
from io import BytesIO

# Question Templates
QUESTION_TEMPLATES = {
    "Experience": [
        "Could you describe your experience with {skill} and how you've applied it in your work?",
        "What specific challenges have you overcome while working with {technology}?",
        "Tell me about a successful project where you utilized {skill}. What was your role and the outcome?",
        "How have you implemented {skill} in your previous roles?",
        "What's the most complex problem you've solved using {technology}?"
    ],
    "Technical": [
        "Could you explain the key concepts of {technical_concept} and how you've implemented them?",
        "What approach would you take to implement {technical_process} in a production environment?",
        "How have you leveraged {technical_tool} to solve complex problems in your previous work?",
        "Explain how you would design a system using {technical_concept}.",
        "What are the best practices you follow when working with {technical_tool}?"
    ],
    "Behavioral": [
        "Describe a situation where you had to handle {situation}. What was your approach and the outcome?",
        "Can you share a specific example that demonstrates your {soft_skill} abilities?",
        "Tell me about a time when you faced {work_scenario}. How did you handle it?",
        "How do you typically approach {situation} in your work?",
        "What strategies do you use to demonstrate {soft_skill} in challenging situations?"
    ],
    "Role-specific": [
        "What strategies would you employ to effectively manage {job_task} in this role?",
        "How would you optimize {job_responsibility} to improve team productivity?",
        "What's your perspective on {industry_trend} and its impact on our industry?",
        "How would you handle {job_task} if resources were limited?",
        "What innovations would you bring to {job_responsibility}?"
    ]
}

# Streamlit configuration
st.set_page_config(
    page_title="Interview Preparation Coach",
    page_icon="πŸ’Ό",
    layout="wide",
    initial_sidebar_state="expanded"
)

# Apply custom CSS
st.markdown("""
    <style>
        .main {
            padding: 2rem;
            background-color: #f8f9fa;
        }
        .stButton>button {
            width: 100%;
            background-color: #1E88E5;
            color: white;
        }
        .creator-header {
            color: #1E88E5;
            font-size: 2.5em;
            font-weight: bold;
            text-align: center;
            margin-bottom: 1em;
        }
        .creator-subheader {
            color: #424242;
            font-size: 1.2em;
            text-align: center;
            font-style: italic;
            margin-bottom: 2em;
        }
    </style>
""", unsafe_allow_html=True)

def extract_text_from_pdf(pdf_file):
    """Extract text from uploaded PDF file"""
    try:
        pdf = PdfReader(pdf_file)
        text = ""
        for page in pdf.pages:
            text += page.extract_text() + "\n"
        return re.sub(r'\s+', ' ', text).strip()
    except Exception as e:
        st.error(f"Error reading PDF: {str(e)}")
        return ""

@st.cache_resource
def load_nlp():
    try:
        return spacy.load('en_core_web_sm')
    except:
        spacy.cli.download('en_core_web_sm')
        return spacy.load('en_core_web_sm')

@st.cache_resource
def load_model():
    device = "cuda" if torch.cuda.is_available() else "cpu"
    return pipeline(
        "text2text-generation",
        model="google/flan-t5-large",
        device=device,
        model_kwargs={"temperature": 0.7, "top_p": 0.9, "do_sample": True}
    )

def extract_keywords(text, nlp, max_keywords=30):
    """Enhanced keyword extraction using spaCy"""
    doc = nlp(text)
    keywords = []
    for token in doc:
        if token.pos_ in ['NOUN', 'PROPN'] and len(token.text) > 3:
            keywords.append(token.text.lower())
    for chunk in doc.noun_chunks:
        if len(chunk.text.split()) > 1:
            keywords.append(chunk.text.lower())
    return list(set(keywords))[:max_keywords]

def generate_single_qa(args):
    """Generate a single question and answer pair"""
    model, context, keywords, job_specific_terms, number = args
    try:
        category = random.choice(list(QUESTION_TEMPLATES.keys()))
        template = random.choice(QUESTION_TEMPLATES[category])
        
        replacements = {
            'skill': random.choice(job_specific_terms) if job_specific_terms else random.choice(keywords),
            'technology': random.choice(keywords),
            'technical_concept': random.choice(keywords),
            'technical_process': random.choice(job_specific_terms) if job_specific_terms else random.choice(keywords),
            'technical_tool': random.choice(keywords),
            'situation': random.choice([
                'a challenging deadline',
                'team conflicts',
                'unexpected project changes',
                'resource constraints',
                'stakeholder disagreements'
            ]),
            'soft_skill': random.choice([
                'leadership',
                'communication',
                'problem-solving',
                'adaptability',
                'project management',
                'team collaboration'
            ]),
            'work_scenario': random.choice([
                'a challenging project that required innovative solutions',
                'a situation where you had to lead a team through difficulty',
                'a critical decision that impacted project success'
            ]),
            'job_task': random.choice(job_specific_terms) if job_specific_terms else random.choice(keywords),
            'job_responsibility': random.choice(job_specific_terms) if job_specific_terms else random.choice(keywords),
            'industry_trend': random.choice(keywords)
        }
        
        question = template.format(**replacements)
        
        prompt = f"""
        Given the following context and question, provide a detailed, professional answer using the STAR method (Situation, Task, Action, Result) where appropriate. Make sure the response is specific and demonstrates expertise.

        Context: {context[:500]}
        Question: {question}

        Provide a comprehensive answer:
        """
        
        response = model(
            prompt,
            max_length=200,
            num_return_sequences=1,
            temperature=0.7,
            top_p=0.9
        )[0]['generated_text']
        
        response = response.strip()
        if not response.endswith(('.', '!', '?')):
            response += '.'
        
        return question, response
    except Exception as e:
        return f"Question {number}", f"Error generating response: {str(e)}"

def generate_interview_qa(cv_text, job_title, job_description, num_questions=50):
    model = load_model()
    nlp = load_nlp()
    start_time = time.time()
    
    context = f"Job Title: {job_title}\nJob Description: {job_description}\nCV: {cv_text}"
    job_specific_terms = extract_keywords(job_description, nlp, max_keywords=15)
    cv_keywords = extract_keywords(cv_text, nlp, max_keywords=20)
    keywords = list(set(cv_keywords + job_specific_terms))
    
    progress_bar = st.progress(0)
    status_text = st.empty()
    
    qa_pairs = []
    with ThreadPoolExecutor(max_workers=4) as executor:
        args_list = [(model, context, keywords, job_specific_terms, i) for i in range(num_questions)]
        for i, result in enumerate(executor.map(generate_single_qa, args_list)):
            qa_pairs.append(result)
            progress = (i + 1) / num_questions
            progress_bar.progress(progress)
            elapsed_time = time.time() - start_time
            estimated_total = elapsed_time / progress if progress > 0 else 0
            remaining_time = max(0, estimated_total - elapsed_time)
            status_text.text(f"Generated {i+1}/{num_questions} questions. Estimated time remaining: {remaining_time:.1f}s")
    
    return qa_pairs

def generate_pdf(qa_pairs, job_title):
    buffer = BytesIO()
    doc = SimpleDocTemplate(
        buffer,
        pagesize=letter,
        rightMargin=72,
        leftMargin=72,
        topMargin=72,
        bottomMargin=72
    )
    
    styles = getSampleStyleSheet()
    title_style = ParagraphStyle(
        'CustomTitle',
        parent=styles['Heading1'],
        fontSize=24,
        spaceAfter=30,
        alignment=TA_CENTER
    )
    question_style = ParagraphStyle(
        'Question',
        parent=styles['Heading2'],
        fontSize=14,
        textColor=colors.HexColor('#1E88E5'),
        spaceAfter=12
    )
    answer_style = ParagraphStyle(
        'Answer',
        parent=styles['Normal'],
        fontSize=12,
        spaceAfter=20
    )
    
    story = []
    
    story.append(Paragraph(f"Interview Preparation Guide<br/>for {job_title}", title_style))
    story.append(Spacer(1, 30))
    
    watermark_style = ParagraphStyle(
        'Watermark',
        parent=styles['Normal'],
        fontSize=10,
        textColor=colors.gray,
        alignment=TA_CENTER
    )
    story.append(Paragraph("Created by Muhammad Shaheer<br/>Professional Interview Coach", watermark_style))
    story.append(Spacer(1, 30))
    
    quote_style = ParagraphStyle(
        'Quote',
        parent=styles['Italic'],
        fontSize=12,
        textColor=colors.HexColor('#666666'),
        alignment=TA_CENTER,
        spaceAfter=30
    )
    story.append(Paragraph(
        '"Success is not final, failure is not fatal: '
        'it is the courage to continue that counts."<br/>- Winston Churchill',
        quote_style
    ))
    
    for i, (question, answer) in enumerate(qa_pairs, 1):
        story.append(Paragraph(f"Q{i}: {question}", question_style))
        story.append(Paragraph(f"A: {answer}", answer_style))
        story.append(Spacer(1, 10))
    
    footer_style = ParagraphStyle(
        'Footer',
        parent=styles['Normal'],
        fontSize=8,
        textColor=colors.gray,
        alignment=TA_CENTER
    )
    story.append(Spacer(1, 30))
    story.append(Paragraph(
        "This document is generated by Interview Preparation Coach<br/>"
        "Copyright Β© 2024 Muhammad Shaheer. All rights reserved.",
        footer_style
    ))
    
    doc.build(story)
    buffer.seek(0)
    return buffer

def main():
    st.markdown('<p class="creator-header">Interview Preparation Coach</p>', unsafe_allow_html=True)
    st.markdown('<p class="creator-subheader">Created by Muhammad Shaheer</p>', unsafe_allow_html=True)
    
    col1, col2 = st.columns([1, 1])
    
    with col1:
        st.subheader("πŸ“„ Upload Your CV")
        cv_file = st.file_uploader("Upload your CV (PDF format only):", type=["pdf"])
        if cv_file:
            cv_text = extract_text_from_pdf(cv_file)
            if cv_text:
                st.success("βœ… CV uploaded successfully!")
            else:
                st.error("❌ Error reading PDF. Please try again.")
                return
    
    with col2:
        st.subheader("🎯 Job Details")
        job_title = st.text_input("Enter the job title:", placeholder="e.g., Senior Software Engineer")
        job_description = st.text_area("Paste the job description:", height=150)
    
    with st.expander("βš™οΈ Advanced Options"):
        num_questions = st.slider("Number of questions to generate", 5, 50, 20)
        categories = st.multiselect(
            "Question categories to include",
            list(QUESTION_TEMPLATES.keys()),
            default=list(QUESTION_TEMPLATES.keys())
        )
    
    if st.button("πŸš€ Generate Interview Questions & Answers"):
        if cv_file and job_title and job_description:
            with st.spinner("πŸ”„ Generating your personalized interview guide..."):
                qa_pairs = generate_interview_qa(cv_text, job_title, job_description, num_questions)
                
                st.session_state.qa_pairs = qa_pairs
                st.session_state.job_title = job_title
                
                st.subheader("πŸ“š Generated Interview Questions and Answers:")
                for i, (question, answer) in enumerate(qa_pairs, 1):
                    with st.expander(f"Q{i}: {question}"):
                        st.markdown("**Answer:**")
                        st.write(answer)
                
                pdf_buffer = generate_pdf(qa_pairs, job_title)
                st.download_button(
                    label="πŸ“„ Download PDF Guide",
                    data=pdf_buffer,
                    file_name=f"Interview_Guide_{job_title.replace(' ', '_')}.pdf",
                    mime="application/pdf"
                )
        else:
            st.error("⚠️ Please provide all required information (CV, job title, and job description).")
    
    st.markdown("---")
    st.markdown("""
        <div style='text-align: center'>
            <h4>πŸ’Ό Interview Preparation Coach</h4>
            <p>Created by Muhammad Shaheer</p>
            <p><small>Powered by Advanced AI Technology β€’ Professional Interview Preparation Tool</small></p>
        </div>
    """, unsafe_allow_html=True)

if __name__ == "__main__":
    main()