Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,52 +1,35 @@
|
|
| 1 |
-
import
|
| 2 |
-
import os
|
| 3 |
-
import queue
|
| 4 |
-
import sounddevice as sd
|
| 5 |
import numpy as np
|
| 6 |
-
import
|
| 7 |
from sentence_transformers import SentenceTransformer
|
| 8 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 9 |
-
import json
|
| 10 |
import PyPDF2
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
HF_API_KEY = os.getenv('HF_API_KEY') # Replace with your Hugging Face API key
|
| 16 |
-
|
| 17 |
-
# Parameters
|
| 18 |
-
silence_threshold = 0.01 # Silence threshold for audio detection
|
| 19 |
-
silence_duration = 2.0 # Duration of silence to detect end of speech
|
| 20 |
-
sample_rate = 16000 # Audio sample rate
|
| 21 |
-
|
| 22 |
-
# Audio buffer
|
| 23 |
-
audio_queue = queue.Queue()
|
| 24 |
|
| 25 |
# Load a pre-trained model for vector embeddings
|
| 26 |
embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 27 |
|
| 28 |
# Parse PDF and create resume content
|
| 29 |
-
def parse_resume(
|
| 30 |
-
"""Extract text from
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
return {}
|
| 40 |
-
|
| 41 |
-
# Load vector database (resume content)
|
| 42 |
-
def load_resume(pdf_path):
|
| 43 |
-
resume_content = parse_resume(pdf_path)
|
| 44 |
resume_embeddings = {
|
| 45 |
section: embedding_model.encode(content) for section, content in resume_content.items()
|
| 46 |
}
|
| 47 |
return resume_embeddings
|
| 48 |
|
| 49 |
-
|
|
|
|
| 50 |
"""Find the most relevant section in the resume and generate a question."""
|
| 51 |
user_embedding = embedding_model.encode(user_input)
|
| 52 |
similarities = {
|
|
@@ -56,105 +39,20 @@ def get_relevant_question(user_input, resume_embeddings):
|
|
| 56 |
most_relevant_section = max(similarities, key=similarities.get)
|
| 57 |
return f"Based on your experience in {most_relevant_section}, can you elaborate more?"
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
try:
|
| 74 |
-
# Fetch audio data
|
| 75 |
-
data = audio_queue.get()
|
| 76 |
-
buffer.append(data)
|
| 77 |
-
|
| 78 |
-
# Check for silence
|
| 79 |
-
rms = np.sqrt(np.mean(data**2))
|
| 80 |
-
if rms < silence_threshold:
|
| 81 |
-
if silence_start is None:
|
| 82 |
-
silence_start = time.time()
|
| 83 |
-
elif time.time() - silence_start > silence_duration:
|
| 84 |
-
print("Silence detected. Stopping recording.")
|
| 85 |
-
break
|
| 86 |
-
else:
|
| 87 |
-
silence_start = None
|
| 88 |
-
|
| 89 |
-
except KeyboardInterrupt:
|
| 90 |
-
print("Recording stopped by user.")
|
| 91 |
-
break
|
| 92 |
-
|
| 93 |
-
audio_data = np.concatenate(buffer, axis=0)
|
| 94 |
-
return audio_data
|
| 95 |
-
|
| 96 |
-
def transcribe_audio(audio_data):
|
| 97 |
-
"""Transcribe audio to text using Hugging Face Whisper API."""
|
| 98 |
-
print("Transcribing audio...")
|
| 99 |
-
headers = {"Authorization": f"Bearer {HF_API_KEY}"}
|
| 100 |
-
response = requests.post(
|
| 101 |
-
HF_API_URL_STT,
|
| 102 |
-
headers=headers,
|
| 103 |
-
data=audio_data.tobytes(),
|
| 104 |
-
)
|
| 105 |
-
if response.status_code == 200:
|
| 106 |
-
return response.json().get("text", "")
|
| 107 |
-
else:
|
| 108 |
-
print(f"Error: {response.status_code} {response.text}")
|
| 109 |
-
return ""
|
| 110 |
-
|
| 111 |
-
def generate_question(response, resume_embeddings):
|
| 112 |
-
"""Generate a question based on the user's response using Hugging Face API."""
|
| 113 |
-
if resume_embeddings:
|
| 114 |
-
return get_relevant_question(response, resume_embeddings)
|
| 115 |
-
|
| 116 |
-
print("Generating a question...")
|
| 117 |
-
headers = {"Authorization": f"Bearer {HF_API_KEY}"}
|
| 118 |
-
payload = {"inputs": {"past_user_inputs": [""], "generated_responses": [""], "text": response}}
|
| 119 |
-
response = requests.post(
|
| 120 |
-
HF_API_URL_CONVERSATION,
|
| 121 |
-
headers=headers,
|
| 122 |
-
json=payload
|
| 123 |
-
)
|
| 124 |
-
if response.status_code == 200:
|
| 125 |
-
return response.json().get("generated_text", "Could you elaborate on that?")
|
| 126 |
-
else:
|
| 127 |
-
print(f"Error: {response.status_code} {response.text}")
|
| 128 |
-
return "Sorry, I couldn't generate a question."
|
| 129 |
-
|
| 130 |
-
def main():
|
| 131 |
-
print("Mock Interview System Initialized")
|
| 132 |
-
|
| 133 |
-
# Load the resume embeddings
|
| 134 |
-
pdf_path = "resume.pdf" # Replace with the path to your PDF resume file
|
| 135 |
-
if os.path.exists(pdf_path):
|
| 136 |
-
print("Loading resume...")
|
| 137 |
-
resume_embeddings = load_resume(pdf_path)
|
| 138 |
-
else:
|
| 139 |
-
print("Resume file not found. Proceeding without it.")
|
| 140 |
-
resume_embeddings = None
|
| 141 |
-
|
| 142 |
-
while True:
|
| 143 |
-
try:
|
| 144 |
-
# Record audio
|
| 145 |
-
audio_data = record_audio()
|
| 146 |
-
|
| 147 |
-
# Transcribe to text
|
| 148 |
-
response = transcribe_audio(audio_data)
|
| 149 |
-
print(f"You said: {response}")
|
| 150 |
-
|
| 151 |
-
# Generate and ask the next question
|
| 152 |
-
question = generate_question(response, resume_embeddings)
|
| 153 |
-
print(f"Interview AI: {question}")
|
| 154 |
-
|
| 155 |
-
except Exception as e:
|
| 156 |
-
print(f"Error: {e}")
|
| 157 |
-
break
|
| 158 |
|
| 159 |
if __name__ == "__main__":
|
| 160 |
-
|
|
|
|
| 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 |
|
| 8 |
+
# Load local models for inference
|
| 9 |
+
stt_model = pipeline("automatic-speech-recognition", model="openai/whisper-base")
|
| 10 |
+
conversation_model = pipeline("conversational", model="facebook/blenderbot-400M-distill")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# Load a pre-trained model for vector embeddings
|
| 13 |
embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 14 |
|
| 15 |
# Parse PDF and create resume content
|
| 16 |
+
def parse_resume(pdf):
|
| 17 |
+
"""Extract text from an uploaded PDF file."""
|
| 18 |
+
reader = PyPDF2.PdfReader(pdf)
|
| 19 |
+
text = "\n".join(page.extract_text() for page in reader.pages if page.extract_text())
|
| 20 |
+
sections = {"Resume Content": text}
|
| 21 |
+
return sections
|
| 22 |
+
|
| 23 |
+
# Process resume and generate embeddings
|
| 24 |
+
def process_resume(pdf):
|
| 25 |
+
resume_content = parse_resume(pdf)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
resume_embeddings = {
|
| 27 |
section: embedding_model.encode(content) for section, content in resume_content.items()
|
| 28 |
}
|
| 29 |
return resume_embeddings
|
| 30 |
|
| 31 |
+
# Generate question from user response
|
| 32 |
+
def generate_question(user_input, resume_embeddings):
|
| 33 |
"""Find the most relevant section in the resume and generate a question."""
|
| 34 |
user_embedding = embedding_model.encode(user_input)
|
| 35 |
similarities = {
|
|
|
|
| 39 |
most_relevant_section = max(similarities, key=similarities.get)
|
| 40 |
return f"Based on your experience in {most_relevant_section}, can you elaborate more?"
|
| 41 |
|
| 42 |
+
# Gradio interface
|
| 43 |
+
def mock_interview(audio, pdf):
|
| 44 |
+
resume_embeddings = process_resume(pdf)
|
| 45 |
+
transcription = stt_model(audio)["text"]
|
| 46 |
+
question = generate_question(transcription, resume_embeddings)
|
| 47 |
+
return transcription, question
|
| 48 |
+
|
| 49 |
+
interface = gr.Interface(
|
| 50 |
+
fn=mock_interview,
|
| 51 |
+
inputs=[gr.Audio(source="microphone", type="filepath"), gr.File(label="Upload Resume (PDF)")],
|
| 52 |
+
outputs=["text", "text"],
|
| 53 |
+
title="Mock Interview AI",
|
| 54 |
+
description="Upload your resume and answer questions in a mock interview."
|
| 55 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
if __name__ == "__main__":
|
| 58 |
+
interface.launch()
|