Update app.py
Browse files
app.py
CHANGED
@@ -4,8 +4,7 @@ import torch
|
|
4 |
from transformers import BertTokenizer, BertForSequenceClassification, pipeline
|
5 |
from app.questions import get_question
|
6 |
|
7 |
-
# Load
|
8 |
-
whisper_model = whisper.load_model("small")
|
9 |
confidence_model = BertForSequenceClassification.from_pretrained('RiteshAkhade/final_confidence')
|
10 |
confidence_tokenizer = BertTokenizer.from_pretrained('RiteshAkhade/final_confidence')
|
11 |
context_model = BertForSequenceClassification.from_pretrained('RiteshAkhade/context_model')
|
@@ -46,19 +45,19 @@ def predict_relevance(question, answer):
|
|
46 |
context_model.eval()
|
47 |
with torch.no_grad():
|
48 |
outputs = context_model(**inputs)
|
49 |
-
|
50 |
-
return "Relevant" if
|
51 |
|
52 |
# Confidence prediction
|
53 |
def predict_confidence(question, answer, threshold=0.4):
|
54 |
-
if not answer.strip():
|
55 |
return "Not Confident"
|
56 |
inputs = confidence_tokenizer(question, answer, return_tensors="pt", padding=True, truncation=True)
|
57 |
confidence_model.eval()
|
58 |
with torch.no_grad():
|
59 |
outputs = confidence_model(**inputs)
|
60 |
-
|
61 |
-
return "Confident" if
|
62 |
|
63 |
# Emotion detection
|
64 |
def detect_emotion(answer):
|
@@ -66,7 +65,8 @@ def detect_emotion(answer):
|
|
66 |
return "No Answer", ""
|
67 |
result = emotion_pipe(answer)
|
68 |
label = result[0][0]["label"].lower()
|
69 |
-
|
|
|
70 |
|
71 |
# Question navigation (non-tech)
|
72 |
def show_non_tech_question():
|
@@ -76,8 +76,7 @@ def show_non_tech_question():
|
|
76 |
def next_non_tech_question():
|
77 |
global current_non_tech_index
|
78 |
current_non_tech_index = (current_non_tech_index + 1) % len(non_technical_questions)
|
79 |
-
|
80 |
-
return non_technical_questions[current_non_tech_index], "", ""
|
81 |
|
82 |
# Question navigation (tech)
|
83 |
def show_tech_question():
|
@@ -87,33 +86,34 @@ def show_tech_question():
|
|
87 |
def next_tech_question():
|
88 |
global current_tech_index
|
89 |
current_tech_index = (current_tech_index + 1) % len(technical_questions)
|
90 |
-
|
91 |
-
return technical_questions[current_tech_index], "", "", ""
|
92 |
|
93 |
# Transcribe + analyze (non-technical)
|
94 |
def transcribe_and_analyze_non_tech(audio, question):
|
95 |
try:
|
96 |
-
|
97 |
-
|
98 |
-
mel = whisper.log_mel_spectrogram(
|
99 |
result = whisper.decode(whisper_model, mel, whisper.DecodingOptions(fp16=False))
|
100 |
-
|
101 |
-
emotion_text, emoji = detect_emotion(
|
102 |
-
return
|
103 |
except Exception as e:
|
104 |
-
return f"Error: {e}", "β"
|
105 |
|
106 |
# Transcribe + analyze (technical)
|
107 |
def transcribe_and_analyze_tech(audio, question):
|
108 |
try:
|
109 |
-
|
110 |
-
|
111 |
-
mel = whisper.log_mel_spectrogram(
|
112 |
result = whisper.decode(whisper_model, mel, whisper.DecodingOptions(fp16=False))
|
113 |
-
|
114 |
-
|
|
|
|
|
115 |
except Exception as e:
|
116 |
-
return f"Error: {e}", "", ""
|
117 |
|
118 |
# UI layout
|
119 |
with gr.Blocks(css="textarea, .gr-box { font-size: 18px !important; }") as demo:
|
@@ -124,44 +124,35 @@ with gr.Blocks(css="textarea, .gr-box { font-size: 18px !important; }") as demo:
|
|
124 |
# NON-TECHNICAL TAB
|
125 |
with gr.Tab("Non-Technical"):
|
126 |
gr.Markdown("### Emotional Context Analysis (π§ + π)")
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
)
|
137 |
-
|
138 |
-
|
139 |
-
btn1.click(
|
140 |
-
fn=next_non_tech_question,
|
141 |
-
inputs=[],
|
142 |
-
outputs=[q1, t1, e1]
|
143 |
-
)
|
144 |
|
145 |
# TECHNICAL TAB
|
146 |
with gr.Tab("Technical"):
|
147 |
gr.Markdown("### Technical Question Analysis (π + π€)")
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
)
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
fn=next_tech_question,
|
163 |
-
inputs=[],
|
164 |
-
outputs=[q2, t2, c2, f2]
|
165 |
-
)
|
166 |
|
167 |
demo.launch(share=True)
|
|
|
4 |
from transformers import BertTokenizer, BertForSequenceClassification, pipeline
|
5 |
from app.questions import get_question
|
6 |
|
7 |
+
# Load modelswhisper_model = whisper.load_model("small")
|
|
|
8 |
confidence_model = BertForSequenceClassification.from_pretrained('RiteshAkhade/final_confidence')
|
9 |
confidence_tokenizer = BertTokenizer.from_pretrained('RiteshAkhade/final_confidence')
|
10 |
context_model = BertForSequenceClassification.from_pretrained('RiteshAkhade/context_model')
|
|
|
45 |
context_model.eval()
|
46 |
with torch.no_grad():
|
47 |
outputs = context_model(**inputs)
|
48 |
+
probabilities = torch.softmax(outputs.logits, dim=-1)
|
49 |
+
return "Relevant" if probabilities[0, 1] > 0.5 else "Irrelevant"
|
50 |
|
51 |
# Confidence prediction
|
52 |
def predict_confidence(question, answer, threshold=0.4):
|
53 |
+
if not isinstance(answer, str) or not answer.strip():
|
54 |
return "Not Confident"
|
55 |
inputs = confidence_tokenizer(question, answer, return_tensors="pt", padding=True, truncation=True)
|
56 |
confidence_model.eval()
|
57 |
with torch.no_grad():
|
58 |
outputs = confidence_model(**inputs)
|
59 |
+
probabilities = torch.softmax(outputs.logits, dim=-1)
|
60 |
+
return "Confident" if probabilities[0, 1].item() > threshold else "Not Confident"
|
61 |
|
62 |
# Emotion detection
|
63 |
def detect_emotion(answer):
|
|
|
65 |
return "No Answer", ""
|
66 |
result = emotion_pipe(answer)
|
67 |
label = result[0][0]["label"].lower()
|
68 |
+
emotion_text, emoji = interview_emotion_map.get(label, ("Unknown", "β"))
|
69 |
+
return emotion_text, emoji
|
70 |
|
71 |
# Question navigation (non-tech)
|
72 |
def show_non_tech_question():
|
|
|
76 |
def next_non_tech_question():
|
77 |
global current_non_tech_index
|
78 |
current_non_tech_index = (current_non_tech_index + 1) % len(non_technical_questions)
|
79 |
+
return non_technical_questions[current_non_tech_index], None, "", ""
|
|
|
80 |
|
81 |
# Question navigation (tech)
|
82 |
def show_tech_question():
|
|
|
86 |
def next_tech_question():
|
87 |
global current_tech_index
|
88 |
current_tech_index = (current_tech_index + 1) % len(technical_questions)
|
89 |
+
return technical_questions[current_tech_index], None, "", "", ""
|
|
|
90 |
|
91 |
# Transcribe + analyze (non-technical)
|
92 |
def transcribe_and_analyze_non_tech(audio, question):
|
93 |
try:
|
94 |
+
audio = whisper.load_audio(audio)
|
95 |
+
audio = whisper.pad_or_trim(audio)
|
96 |
+
mel = whisper.log_mel_spectrogram(audio).to(whisper_model.device)
|
97 |
result = whisper.decode(whisper_model, mel, whisper.DecodingOptions(fp16=False))
|
98 |
+
transcribed_text = result.text
|
99 |
+
emotion_text, emoji = detect_emotion(transcribed_text)
|
100 |
+
return transcribed_text, f"{emotion_text} {emoji}"
|
101 |
except Exception as e:
|
102 |
+
return f"Error: {str(e)}", "β"
|
103 |
|
104 |
# Transcribe + analyze (technical)
|
105 |
def transcribe_and_analyze_tech(audio, question):
|
106 |
try:
|
107 |
+
audio = whisper.load_audio(audio)
|
108 |
+
audio = whisper.pad_or_trim(audio)
|
109 |
+
mel = whisper.log_mel_spectrogram(audio).to(whisper_model.device)
|
110 |
result = whisper.decode(whisper_model, mel, whisper.DecodingOptions(fp16=False))
|
111 |
+
transcribed_text = result.text
|
112 |
+
context_result = predict_relevance(question, transcribed_text)
|
113 |
+
confidence_result = predict_confidence(question, transcribed_text)
|
114 |
+
return transcribed_text, context_result, confidence_result
|
115 |
except Exception as e:
|
116 |
+
return f"Error: {str(e)}", "", ""
|
117 |
|
118 |
# UI layout
|
119 |
with gr.Blocks(css="textarea, .gr-box { font-size: 18px !important; }") as demo:
|
|
|
124 |
# NON-TECHNICAL TAB
|
125 |
with gr.Tab("Non-Technical"):
|
126 |
gr.Markdown("### Emotional Context Analysis (π§ + π)")
|
127 |
+
question_display_1 = gr.Textbox(label="Interview Question", value=show_non_tech_question(), interactive=False)
|
128 |
+
audio_input_1 = gr.Audio(type="filepath", label="Record Your Answer")
|
129 |
+
transcribed_text_1 = gr.Textbox(label="Transcribed Answer", interactive=False, lines=4)
|
130 |
+
emotion_output = gr.Textbox(label="Detected Emotion", interactive=False)
|
131 |
+
|
132 |
+
audio_input_1.change(fn=transcribe_and_analyze_non_tech,
|
133 |
+
inputs=[audio_input_1, question_display_1],
|
134 |
+
outputs=[transcribed_text_1, emotion_output])
|
135 |
+
|
136 |
+
next_button_1 = gr.Button("Next Question")
|
137 |
+
next_button_1.click(fn=next_non_tech_question,
|
138 |
+
outputs=[question_display_1, audio_input_1, transcribed_text_1, emotion_output])
|
|
|
|
|
|
|
|
|
|
|
139 |
|
140 |
# TECHNICAL TAB
|
141 |
with gr.Tab("Technical"):
|
142 |
gr.Markdown("### Technical Question Analysis (π + π€)")
|
143 |
+
question_display_2 = gr.Textbox(label="Interview Question", value=show_tech_question(), interactive=False)
|
144 |
+
audio_input_2 = gr.Audio(type="filepath", label="Record Your Answer")
|
145 |
+
transcribed_text_2 = gr.Textbox(label="Transcribed Answer", interactive=False, lines=4)
|
146 |
+
context_analysis_result = gr.Textbox(label="Context Analysis", interactive=False)
|
147 |
+
confidence_analysis_result = gr.Textbox(label="Confidence Analysis", interactive=False)
|
148 |
+
|
149 |
+
audio_input_2.change(fn=transcribe_and_analyze_tech,
|
150 |
+
inputs=[audio_input_2, question_display_2],
|
151 |
+
outputs=[transcribed_text_2, context_analysis_result, confidence_analysis_result])
|
152 |
+
|
153 |
+
next_button_2 = gr.Button("Next Question")
|
154 |
+
next_button_2.click(fn=next_tech_question,
|
155 |
+
outputs=[question_display_2, audio_input_2, transcribed_text_2,
|
156 |
+
context_analysis_result, confidence_analysis_result])
|
|
|
|
|
|
|
|
|
157 |
|
158 |
demo.launch(share=True)
|