garyd1 commited on
Commit
e51d2b2
·
verified ·
1 Parent(s): 5d360a2

Update app.py

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