garyd1 commited on
Commit
5144ac6
·
verified ·
1 Parent(s): beff4dd

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +123 -0
app.py ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ from transformers import pipeline
4
+ from sentence_transformers import SentenceTransformer
5
+ from sklearn.metrics.pairwise import cosine_similarity
6
+ import PyPDF2
7
+ import torch
8
+ import gc
9
+
10
+ # Load local models for inference
11
+ stt_model = pipeline("automatic-speech-recognition", model="openai/whisper-small", torch_dtype=torch.float16)
12
+ conversation_model = pipeline("text-generation", model="facebook/blenderbot-400M-distill", torch_dtype=torch.float16)
13
+ tts_model = pipeline("text-to-speech", model="facebook/fastspeech2-en-ljspeech", torch_dtype=torch.float16)
14
+
15
+ # Load a pre-trained model for vector embeddings
16
+ embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
17
+
18
+ # Parse PDF and create resume content
19
+ def parse_resume(pdf):
20
+ """Extract text from an uploaded PDF file."""
21
+ reader = PyPDF2.PdfReader(pdf)
22
+ text = "\n".join(page.extract_text() for page in reader.pages if page.extract_text())
23
+ sections = {"Resume Content": text}
24
+ return sections
25
+
26
+ # Process job description text
27
+ def process_job_description(job_desc):
28
+ """Encode the job description for analysis."""
29
+ return embedding_model.encode(job_desc)
30
+
31
+ # Process resume and generate embeddings
32
+ def process_resume(pdf):
33
+ resume_content = parse_resume(pdf)
34
+ resume_embeddings = {
35
+ section: embedding_model.encode(content) for section, content in resume_content.items()
36
+ }
37
+ return resume_embeddings
38
+
39
+ # Generate question from user response
40
+ def generate_question(user_input, resume_embeddings):
41
+ """Find the most relevant section in the resume and generate a question."""
42
+ user_embedding = embedding_model.encode(user_input)
43
+ similarities = {
44
+ section: cosine_similarity([user_embedding], [embedding])[0][0]
45
+ for section, embedding in resume_embeddings.items()
46
+ }
47
+ most_relevant_section = max(similarities, key=similarities.get)
48
+ return f"Based on your experience in {most_relevant_section}, can you elaborate more?"
49
+
50
+ # Generate TTS output
51
+ def generate_audio(text):
52
+ """Convert text to audio using Hugging Face TTS model."""
53
+ audio_data = tts_model(text, return_tensors=True)["waveform"]
54
+ return audio_data
55
+
56
+ # Gradio interface
57
+ class MockInterview:
58
+ def __init__(self):
59
+ self.resume_embeddings = None
60
+ self.job_desc_embedding = None
61
+ self.interview_active = False
62
+
63
+ def upload_inputs(self, resume, job_desc):
64
+ self.resume_embeddings = process_resume(resume)
65
+ self.job_desc_embedding = process_job_description(job_desc)
66
+ self.interview_active = True
67
+ question = "Tell me about yourself."
68
+ audio_output = generate_audio(question)
69
+ return "Resume and job description processed. Starting the interview.", audio_output
70
+
71
+ def conduct_interview(self, audio_file):
72
+ if not self.interview_active:
73
+ return "Please upload your resume and job description first.", None
74
+
75
+ # Transcribe audio
76
+ transcription = stt_model(audio_file)["text"]
77
+ if not transcription.strip():
78
+ return "No audio detected. Please try again.", None
79
+
80
+ # Generate next question
81
+ question = generate_question(transcription, self.resume_embeddings)
82
+ audio_output = generate_audio(question)
83
+ return transcription, audio_output
84
+
85
+ def end_interview(self):
86
+ self.interview_active = False
87
+ audio_output = generate_audio("Thank you for participating in the interview. Goodbye!")
88
+ return "Interview ended. Thank you for participating.", audio_output
89
+
90
+ mock_interview = MockInterview()
91
+
92
+ def upload_inputs(resume, job_desc):
93
+ return mock_interview.upload_inputs(resume, job_desc)
94
+
95
+ def conduct_interview(audio_file):
96
+ return mock_interview.conduct_interview(audio_file)
97
+
98
+ def end_interview():
99
+ return mock_interview.end_interview()
100
+
101
+ interface = gr.Blocks()
102
+ with interface:
103
+ gr.Markdown("""# Mock Interview AI
104
+ Upload your resume and job description, then engage in a realistic audio-based interview simulation.""")
105
+
106
+ with gr.Row():
107
+ resume_input = gr.File(label="Upload Resume (PDF)")
108
+ job_desc_input = gr.Textbox(label="Paste Job Description")
109
+ upload_button = gr.Button("Upload and Start Interview")
110
+
111
+ with gr.Row():
112
+ audio_input = gr.Audio(type="filepath", label="Respond with Your Answer")
113
+ transcription_output = gr.Textbox(label="Transcription")
114
+ question_output = gr.Audio(label="Question Audio")
115
+ submit_button = gr.Button("Submit Response")
116
+ end_button = gr.Button("End Interview")
117
+
118
+ upload_button.click(upload_inputs, inputs=[resume_input, job_desc_input], outputs=[transcription_output, question_output])
119
+ submit_button.click(conduct_interview, inputs=[audio_input], outputs=[transcription_output, question_output])
120
+ end_button.click(end_interview, outputs=[transcription_output, question_output])
121
+
122
+ if __name__ == "__main__":
123
+ interface.launch()