garyd1 commited on
Commit
2394291
·
verified ·
1 Parent(s): 9a9587c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -15
app.py CHANGED
@@ -4,15 +4,24 @@ from transformers import pipeline
4
  from sentence_transformers import SentenceTransformer
5
  from sklearn.metrics.pairwise import cosine_similarity
6
  import PyPDF2
 
 
7
 
8
  # Load local models for inference
9
  stt_model = pipeline("automatic-speech-recognition", model="openai/whisper-base")
10
  conversation_model = pipeline("text-generation", model="facebook/blenderbot-400M-distill")
11
- tts_model = pipeline("text-to-speech", model="facebook/fastspeech2-en-ljspeech")
12
 
13
  # Load a pre-trained model for vector embeddings
14
  embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
15
 
 
 
 
 
 
 
 
 
16
  # Parse PDF and create resume content
17
  def parse_resume(pdf):
18
  """Extract text from an uploaded PDF file."""
@@ -45,11 +54,6 @@ def generate_question(user_input, resume_embeddings):
45
  most_relevant_section = max(similarities, key=similarities.get)
46
  return f"Based on your experience in {most_relevant_section}, can you elaborate more?"
47
 
48
- # Generate TTS output
49
- def generate_audio(text):
50
- """Convert text to audio using TTS model."""
51
- return tts_model(text, return_tensors=True)["waveform"]
52
-
53
  # Gradio interface
54
  class MockInterview:
55
  def __init__(self):
@@ -61,31 +65,42 @@ class MockInterview:
61
  self.resume_embeddings = process_resume(resume)
62
  self.job_desc_embedding = process_job_description(job_desc)
63
  self.interview_active = True
64
- return "Resume and job description processed. Starting the interview.", generate_audio("Tell me about yourself.")
 
 
 
 
 
 
 
65
 
66
  def conduct_interview(self, audio_file):
67
  if not self.interview_active:
68
- return "Please upload your resume and job description first.", None
69
 
70
  # Transcribe audio
71
  transcription = stt_model(audio_file)["text"]
72
  if not transcription.strip():
73
- return "No audio detected. Please try again.", None
74
 
75
  # Generate next question
76
  question = generate_question(transcription, self.resume_embeddings)
77
- audio_output = generate_audio(question)
78
- return transcription, audio_output
79
 
80
  def end_interview(self):
81
  self.interview_active = False
82
- return "Interview ended. Thank you for participating.", generate_audio("Thank you for participating in the interview. Goodbye!")
 
83
 
84
  mock_interview = MockInterview()
85
 
86
  def upload_inputs(resume, job_desc):
87
  return mock_interview.upload_inputs(resume, job_desc)
88
 
 
 
 
89
  def conduct_interview(audio_file):
90
  return mock_interview.conduct_interview(audio_file)
91
 
@@ -105,13 +120,13 @@ Upload your resume and job description, then engage in a realistic audio-based i
105
  with gr.Row():
106
  audio_input = gr.Audio(type="filepath", label="Respond with Your Answer")
107
  transcription_output = gr.Textbox(label="Transcription")
108
- question_output = gr.Audio(label="Question Audio")
109
  submit_button = gr.Button("Submit Response")
110
  end_button = gr.Button("End Interview")
111
 
112
- upload_button.click(upload_inputs, inputs=[resume_input, job_desc_input], outputs=[transcription_output, question_output])
113
  submit_button.click(conduct_interview, inputs=[audio_input], outputs=[transcription_output, question_output])
114
- end_button.click(end_interview, outputs=[transcription_output, question_output])
115
 
116
  if __name__ == "__main__":
117
  interface.launch()
 
4
  from sentence_transformers import SentenceTransformer
5
  from sklearn.metrics.pairwise import cosine_similarity
6
  import PyPDF2
7
+ import pyttsx3
8
+ import time
9
 
10
  # Load local models for inference
11
  stt_model = pipeline("automatic-speech-recognition", model="openai/whisper-base")
12
  conversation_model = pipeline("text-generation", model="facebook/blenderbot-400M-distill")
 
13
 
14
  # Load a pre-trained model for vector embeddings
15
  embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
16
 
17
+ # Text-to-Speech engine setup
18
+ tts_engine = pyttsx3.init()
19
+
20
+ def speak_text(text):
21
+ """Speak the given text using TTS engine."""
22
+ tts_engine.say(text)
23
+ tts_engine.runAndWait()
24
+
25
  # Parse PDF and create resume content
26
  def parse_resume(pdf):
27
  """Extract text from an uploaded PDF file."""
 
54
  most_relevant_section = max(similarities, key=similarities.get)
55
  return f"Based on your experience in {most_relevant_section}, can you elaborate more?"
56
 
 
 
 
 
 
57
  # Gradio interface
58
  class MockInterview:
59
  def __init__(self):
 
65
  self.resume_embeddings = process_resume(resume)
66
  self.job_desc_embedding = process_job_description(job_desc)
67
  self.interview_active = True
68
+ return "Resume and job description processed. Starting the interview."
69
+
70
+ def start_interview(self):
71
+ if not self.interview_active:
72
+ return "Please upload your resume and job description first."
73
+ question = "Tell me about yourself."
74
+ speak_text(question)
75
+ return question
76
 
77
  def conduct_interview(self, audio_file):
78
  if not self.interview_active:
79
+ return "Please upload your resume and job description first.", ""
80
 
81
  # Transcribe audio
82
  transcription = stt_model(audio_file)["text"]
83
  if not transcription.strip():
84
+ return "No audio detected. Please try again.", ""
85
 
86
  # Generate next question
87
  question = generate_question(transcription, self.resume_embeddings)
88
+ speak_text(question)
89
+ return transcription, question
90
 
91
  def end_interview(self):
92
  self.interview_active = False
93
+ speak_text("Thank you for participating in the interview. Goodbye!")
94
+ return "Interview ended. Thank you for participating."
95
 
96
  mock_interview = MockInterview()
97
 
98
  def upload_inputs(resume, job_desc):
99
  return mock_interview.upload_inputs(resume, job_desc)
100
 
101
+ def start_interview():
102
+ return mock_interview.start_interview()
103
+
104
  def conduct_interview(audio_file):
105
  return mock_interview.conduct_interview(audio_file)
106
 
 
120
  with gr.Row():
121
  audio_input = gr.Audio(type="filepath", label="Respond with Your Answer")
122
  transcription_output = gr.Textbox(label="Transcription")
123
+ question_output = gr.Textbox(label="Question")
124
  submit_button = gr.Button("Submit Response")
125
  end_button = gr.Button("End Interview")
126
 
127
+ upload_button.click(upload_inputs, inputs=[resume_input, job_desc_input], outputs=[question_output])
128
  submit_button.click(conduct_interview, inputs=[audio_input], outputs=[transcription_output, question_output])
129
+ end_button.click(end_interview, outputs=[question_output])
130
 
131
  if __name__ == "__main__":
132
  interface.launch()