Spaces:
Sleeping
Sleeping
Rename streamlitApp.py to app.py
Browse files- streamlitApp.py → app.py +103 -73
streamlitApp.py → app.py
RENAMED
@@ -1,73 +1,103 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
|
3 |
-
import
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
#
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
#
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
return
|
29 |
-
|
30 |
-
# Function to
|
31 |
-
def
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
#
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import whisper
|
3 |
+
from moviepy.editor import VideoFileClip
|
4 |
+
from tempfile import NamedTemporaryFile
|
5 |
+
import numpy as np
|
6 |
+
from transformers import BertTokenizer, BertModel
|
7 |
+
import torch
|
8 |
+
import os
|
9 |
+
|
10 |
+
# Load the pre-trained BERT model and tokenizer
|
11 |
+
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
|
12 |
+
model = BertModel.from_pretrained('bert-base-uncased')
|
13 |
+
|
14 |
+
# Load Whisper model for transcription
|
15 |
+
whisper_model = whisper.load_model("base")
|
16 |
+
|
17 |
+
# Define criteria for scoring responses
|
18 |
+
criteria = {
|
19 |
+
"technical": ["machine learning", "data", "preprocess", "decision tree", "SVM", "neural network", "hyperparameter"],
|
20 |
+
"problem_solving": ["cross-validation", "grid search", "evaluate", "optimize", "performance"],
|
21 |
+
"communication": ["I would", "then", "and", "also"]
|
22 |
+
}
|
23 |
+
|
24 |
+
# Function to encode a response using BERT
|
25 |
+
def encode_response(response):
|
26 |
+
inputs = tokenizer(response, return_tensors='pt', padding=True, truncation=True)
|
27 |
+
outputs = model(**inputs)
|
28 |
+
return outputs.last_hidden_state.mean(dim=1).squeeze().detach().numpy()
|
29 |
+
|
30 |
+
# Function to score the response based on predefined criteria
|
31 |
+
def score_response(response, criteria):
|
32 |
+
scores = {}
|
33 |
+
for criterion, keywords in criteria.items():
|
34 |
+
scores[criterion] = sum([1 for word in keywords if word in response.lower()]) / len(keywords)
|
35 |
+
return scores
|
36 |
+
|
37 |
+
# Function to rank candidates by average score
|
38 |
+
def rank_candidates(candidates):
|
39 |
+
for candidate in candidates:
|
40 |
+
avg_score = np.mean(list(candidate['scores'].values()))
|
41 |
+
candidate['avg_score'] = avg_score
|
42 |
+
ranked_candidates = sorted(candidates, key=lambda x: x['avg_score'], reverse=True)
|
43 |
+
return ranked_candidates
|
44 |
+
|
45 |
+
# Function to extract audio from the video and perform transcription using Whisper
|
46 |
+
def transcribe_video(video_file):
|
47 |
+
# Save the uploaded file to a temporary location
|
48 |
+
with NamedTemporaryFile(delete=False, suffix=".mp4") as temp_video_file:
|
49 |
+
temp_video_file.write(video_file.read()) # Write video file to the temp file
|
50 |
+
temp_video_path = temp_video_file.name
|
51 |
+
|
52 |
+
# Load the video and extract audio
|
53 |
+
video = VideoFileClip(temp_video_path)
|
54 |
+
audio_file = "audio.wav"
|
55 |
+
video.audio.write_audiofile(audio_file)
|
56 |
+
|
57 |
+
# Perform transcription with Whisper
|
58 |
+
whisper_model = whisper.load_model("base") # Ensure model is loaded here
|
59 |
+
transcription = whisper_model.transcribe(audio_file)
|
60 |
+
|
61 |
+
# Clean up temporary files
|
62 |
+
os.remove(audio_file)
|
63 |
+
os.remove(temp_video_path)
|
64 |
+
|
65 |
+
return transcription['text']
|
66 |
+
|
67 |
+
# Streamlit app
|
68 |
+
st.title("AI Role Candidate Screening via Video Interview")
|
69 |
+
|
70 |
+
# Input for the number of candidates
|
71 |
+
num_candidates = st.number_input("Enter the number of candidates:", min_value=1, max_value=10, value=1)
|
72 |
+
|
73 |
+
mock_interviews = []
|
74 |
+
for i in range(num_candidates):
|
75 |
+
video_file = st.file_uploader(f"Upload interview video for Candidate {i+1}:", type=["mp4", "mov", "avi"], key=f"video_{i}")
|
76 |
+
if video_file:
|
77 |
+
st.write(f"Processing video for Candidate {i+1}...")
|
78 |
+
transcription = transcribe_video(video_file)
|
79 |
+
st.write(f"Transcript for Candidate {i+1}: {transcription}")
|
80 |
+
mock_interviews.append({"name": f"Candidate {i+1}", "response": transcription})
|
81 |
+
|
82 |
+
# Analyze the candidates when the user clicks the "Analyze" button
|
83 |
+
if st.button('Analyze Responses'):
|
84 |
+
if mock_interviews:
|
85 |
+
# Encode and score each candidate
|
86 |
+
scored_candidates = []
|
87 |
+
for candidate in mock_interviews:
|
88 |
+
scores = score_response(candidate['response'], criteria)
|
89 |
+
candidate['scores'] = scores
|
90 |
+
candidate['encoded'] = encode_response(candidate['response'])
|
91 |
+
scored_candidates.append(candidate)
|
92 |
+
|
93 |
+
# Rank the candidates based on scores
|
94 |
+
ranked_candidates = rank_candidates(scored_candidates)
|
95 |
+
|
96 |
+
# Display the results
|
97 |
+
st.write("### Candidate Rankings")
|
98 |
+
for rank, candidate in enumerate(ranked_candidates, 1):
|
99 |
+
st.write(f"**Rank {rank}: {candidate['name']}**")
|
100 |
+
st.write(f"Average Score: {candidate['avg_score']:.2f}")
|
101 |
+
st.write(f"Scores: {candidate['scores']}")
|
102 |
+
else:
|
103 |
+
st.write("Please upload videos for all candidates.")
|