Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,91 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
""
|
| 7 |
-
client = InferenceClient(model="meta-llama/Llama-3.2-3B")
|
| 8 |
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 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 |
-
|
|
|
|
| 1 |
+
# Import required libraries
|
| 2 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
import gradio as gr
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import pandas as pd
|
| 6 |
+
from speechbrain.pretrained import Tacotron2, HIFIGAN, EncoderDecoderASR
|
| 7 |
|
| 8 |
+
# Load Hugging Face psychometric model
|
| 9 |
+
psych_model_name = "KevSun/Personality_LM" # Big Five personality traits
|
| 10 |
+
psych_model = pipeline("text-classification", model=psych_model_name)
|
|
|
|
| 11 |
|
| 12 |
+
# Load ASR model
|
| 13 |
+
asr_model = EncoderDecoderASR.from_hparams(source="speechbrain/asr-crdnn-rnnlm-librispeech", savedir="tmp_asr")
|
| 14 |
|
| 15 |
+
# Load TTS model
|
| 16 |
+
tts_model = Tacotron2.from_hparams(source="speechbrain/tts-tacotron2-ljspeech", savedir="tmp_tts")
|
| 17 |
+
voc_model = HIFIGAN.from_hparams(source="speechbrain/tts-hifigan-ljspeech", savedir="tmp_voc")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
# Psychometric Test Questions
|
| 20 |
+
text_questions = [
|
| 21 |
+
"How do you handle criticism?",
|
| 22 |
+
"Describe a time when you overcame a challenge.",
|
| 23 |
+
"What motivates you to work hard?"
|
| 24 |
+
]
|
| 25 |
|
| 26 |
+
audio_questions = [
|
| 27 |
+
"What does teamwork mean to you?",
|
| 28 |
+
"How do you handle stressful situations?"
|
| 29 |
+
]
|
| 30 |
|
| 31 |
+
# Function to analyze text response
|
| 32 |
+
def analyze_text_responses(responses):
|
| 33 |
+
analysis = [psych_model(response)[0] for response in responses]
|
| 34 |
+
traits = {response["label"]: response["score"] for response in analysis}
|
| 35 |
+
return traits
|
| 36 |
|
| 37 |
+
# Function to handle TTS
|
| 38 |
+
def generate_audio_question(question):
|
| 39 |
+
mel_output, alignment, _ = tts_model.encode_text(question)
|
| 40 |
+
waveforms = voc_model.decode_batch(mel_output)
|
| 41 |
+
return waveforms[0].numpy()
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
# Function to process audio response
|
| 44 |
+
def process_audio_response(audio):
|
| 45 |
+
text_response = asr_model.transcribe_file(audio)
|
| 46 |
+
return text_response
|
| 47 |
|
| 48 |
+
# Gradio interface functions
|
| 49 |
+
def text_part(candidate_name, responses):
|
| 50 |
+
traits = analyze_text_responses(responses)
|
| 51 |
+
df = pd.DataFrame(traits.items(), columns=["Trait", "Score"])
|
| 52 |
+
plt.figure(figsize=(8, 6))
|
| 53 |
+
plt.bar(df["Trait"], df["Score"], color="skyblue")
|
| 54 |
+
plt.title(f"Psychometric Analysis for {candidate_name}")
|
| 55 |
+
plt.xlabel("Traits")
|
| 56 |
+
plt.ylabel("Score")
|
| 57 |
+
plt.xticks(rotation=45)
|
| 58 |
+
plt.tight_layout()
|
| 59 |
+
return df, plt
|
| 60 |
|
| 61 |
+
def audio_part(candidate_name, audio_responses):
|
| 62 |
+
text_responses = [process_audio_response(audio) for audio in audio_responses]
|
| 63 |
+
traits = analyze_text_responses(text_responses)
|
| 64 |
+
df = pd.DataFrame(traits.items(), columns=["Trait", "Score"])
|
| 65 |
+
plt.figure(figsize=(8, 6))
|
| 66 |
+
plt.bar(df["Trait"], df["Score"], color="lightcoral")
|
| 67 |
+
plt.title(f"Audio Psychometric Analysis for {candidate_name}")
|
| 68 |
+
plt.xlabel("Traits")
|
| 69 |
+
plt.ylabel("Score")
|
| 70 |
+
plt.xticks(rotation=45)
|
| 71 |
+
plt.tight_layout()
|
| 72 |
+
return df, plt
|
| 73 |
+
|
| 74 |
+
# Gradio UI
|
| 75 |
+
def chat_interface(candidate_name, text_responses, audio_responses):
|
| 76 |
+
text_df, text_plot = text_part(candidate_name, text_responses)
|
| 77 |
+
audio_df, audio_plot = audio_part(candidate_name, audio_responses)
|
| 78 |
+
return text_df, text_plot, audio_df, audio_plot
|
| 79 |
|
| 80 |
+
text_inputs = [gr.Textbox(label=f"Response to Q{i+1}: {q}") for i, q in enumerate(text_questions)]
|
| 81 |
+
audio_inputs = [gr.Audio(label=f"Response to Q{i+1}: {q}", type="file") for i, q in enumerate(audio_questions)]
|
| 82 |
+
|
| 83 |
+
interface = gr.Interface(
|
| 84 |
+
fn=chat_interface,
|
| 85 |
+
inputs=[gr.Textbox(label="Candidate Name")] + text_inputs + audio_inputs,
|
| 86 |
+
outputs=["dataframe", "plot", "dataframe", "plot"],
|
| 87 |
+
title="Psychometric Analysis Chatbot"
|
| 88 |
+
)
|
| 89 |
|
| 90 |
+
# Launch chatbot
|
| 91 |
+
interface.launch()
|