garyd1 commited on
Commit
d72f5a1
·
verified ·
1 Parent(s): e03fb29

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -38
app.py CHANGED
@@ -4,8 +4,6 @@ from transformers import pipeline
4
  from sentence_transformers import SentenceTransformer
5
  from sklearn.metrics.pairwise import cosine_similarity
6
  import PyPDF2
7
- import sounddevice as sd
8
- import queue
9
 
10
  # Load local models for inference
11
  stt_model = pipeline("automatic-speech-recognition", model="openai/whisper-base")
@@ -35,38 +33,17 @@ def process_resume(pdf):
35
  }
36
  return resume_embeddings
37
 
38
- # Generate a conversation response
39
- def generate_conversation_response(user_input, job_desc_embedding):
40
- prompt = f"The user said: {user_input}. Respond appropriately as a professional hiring manager. Focus on how the response relates to the job description."
41
- response = conversation_model(prompt, max_length=100, num_return_sequences=1)
42
- return response[0]["generated_text"]
43
-
44
  # Generate question from user response
45
- def generate_question(user_input, resume_embeddings, job_desc_embedding):
46
  """Find the most relevant section in the resume and generate a question."""
47
  user_embedding = embedding_model.encode(user_input)
48
  similarities = {
49
- section: cosine_similarity([user_embedding], [embedding])[0][0]
50
  for section, embedding in resume_embeddings.items()
51
  }
52
  most_relevant_section = max(similarities, key=similarities.get)
53
  return f"Based on your experience in {most_relevant_section}, can you elaborate more?"
54
 
55
- # Real-time audio recording and processing
56
- def record_audio(callback):
57
- """Record audio and process it in real-time."""
58
- q = queue.Queue()
59
-
60
- def audio_callback(indata, frames, time, status):
61
- if status:
62
- print(status)
63
- q.put(indata.copy())
64
-
65
- with sd.InputStream(samplerate=16000, channels=1, callback=audio_callback):
66
- while True:
67
- audio_data = q.get()
68
- callback(audio_data)
69
-
70
  # Gradio interface
71
  class MockInterview:
72
  def __init__(self):
@@ -80,12 +57,12 @@ class MockInterview:
80
  self.interview_active = True
81
  return "Resume and job description processed. Interview is starting."
82
 
83
- def conduct_interview(self, audio_data):
84
  if not self.interview_active:
85
  return "Please upload your resume and job description first.", ""
86
 
87
- transcription = stt_model(audio_data)["text"] # Transcribe audio
88
- question = generate_question(transcription, self.resume_embeddings, self.job_desc_embedding)
89
  return transcription, question
90
 
91
  def end_interview(self):
@@ -97,16 +74,11 @@ mock_interview = MockInterview()
97
  def upload_inputs(resume, job_desc):
98
  return mock_interview.upload_inputs(resume, job_desc)
99
 
100
- def start_interview(audio_data_callback):
101
- """Start the interview automatically, processing audio in real-time."""
102
- if not mock_interview.interview_active:
103
- return "Please upload your resume and job description first."
104
-
105
- def process_audio(audio_data):
106
- transcription, question = mock_interview.conduct_interview(audio_data)
107
- audio_data_callback(transcription, question)
108
 
109
- record_audio(process_audio)
 
110
 
111
  interface = gr.Blocks()
112
  with interface:
@@ -119,12 +91,15 @@ Upload your resume and job description, then engage in a realistic interview sim
119
  upload_button = gr.Button("Upload and Start Interview")
120
 
121
  with gr.Row():
 
122
  transcription_output = gr.Textbox(label="Transcription")
123
  question_output = gr.Textbox(label="Question")
 
124
  end_button = gr.Button("End Interview")
125
 
126
  upload_button.click(upload_inputs, inputs=[resume_input, job_desc_input], outputs=[transcription_output])
127
- end_button.click(mock_interview.end_interview, outputs=[transcription_output])
 
128
 
129
  if __name__ == "__main__":
130
  interface.launch()
 
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")
 
33
  }
34
  return resume_embeddings
35
 
 
 
 
 
 
 
36
  # Generate question from user response
37
+ def generate_question(user_input, resume_embeddings):
38
  """Find the most relevant section in the resume and generate a question."""
39
  user_embedding = embedding_model.encode(user_input)
40
  similarities = {
41
+ section: cosine_similarity([user_embedding], [embedding])[0][0]
42
  for section, embedding in resume_embeddings.items()
43
  }
44
  most_relevant_section = max(similarities, key=similarities.get)
45
  return f"Based on your experience in {most_relevant_section}, can you elaborate more?"
46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  # Gradio interface
48
  class MockInterview:
49
  def __init__(self):
 
57
  self.interview_active = True
58
  return "Resume and job description processed. Interview is starting."
59
 
60
+ def conduct_interview(self, audio_file):
61
  if not self.interview_active:
62
  return "Please upload your resume and job description first.", ""
63
 
64
+ transcription = stt_model(audio_file)["text"] # Transcribe audio
65
+ question = generate_question(transcription, self.resume_embeddings)
66
  return transcription, question
67
 
68
  def end_interview(self):
 
74
  def upload_inputs(resume, job_desc):
75
  return mock_interview.upload_inputs(resume, job_desc)
76
 
77
+ def conduct_interview(audio_file):
78
+ return mock_interview.conduct_interview(audio_file)
 
 
 
 
 
 
79
 
80
+ def end_interview():
81
+ return mock_interview.end_interview()
82
 
83
  interface = gr.Blocks()
84
  with interface:
 
91
  upload_button = gr.Button("Upload and Start Interview")
92
 
93
  with gr.Row():
94
+ audio_input = gr.Audio(type="filepath", label="Respond with Your Answer")
95
  transcription_output = gr.Textbox(label="Transcription")
96
  question_output = gr.Textbox(label="Question")
97
+ submit_button = gr.Button("Submit Response")
98
  end_button = gr.Button("End Interview")
99
 
100
  upload_button.click(upload_inputs, inputs=[resume_input, job_desc_input], outputs=[transcription_output])
101
+ submit_button.click(conduct_interview, inputs=[audio_input], outputs=[transcription_output, question_output])
102
+ end_button.click(end_interview, outputs=[transcription_output])
103
 
104
  if __name__ == "__main__":
105
  interface.launch()