File size: 4,040 Bytes
e102cba
248713a
 
8f33c3e
 
e102cba
248713a
 
8f33c3e
e102cba
248713a
8f33c3e
 
 
e102cba
8f33c3e
 
 
 
 
 
e102cba
8f33c3e
 
 
 
e102cba
248713a
8f33c3e
 
 
 
e102cba
8f33c3e
 
 
 
 
e102cba
8f33c3e
734f006
 
 
 
 
 
 
 
 
 
e102cba
8f33c3e
 
 
 
 
 
 
 
 
 
 
 
e102cba
8f33c3e
734f006
 
 
 
 
 
 
 
8f33c3e
 
 
 
 
 
 
 
 
 
734f006
bcd4212
 
 
 
 
 
 
 
8f33c3e
bcd4212
 
8f33c3e
bcd4212
8f33c3e
e102cba
248713a
8f33c3e
248713a
8f33c3e
 
 
 
 
 
 
e102cba
248713a
 
1
2
3
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
29
30
31
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import gradio as gr
from transformers import pipeline
from speechbrain.pretrained import Tacotron2, HIFIGAN, EncoderDecoderASR
import matplotlib.pyplot as plt
import pandas as pd

# Initialize psychometric model
psych_model_name = "KevSun/Personality_LM"
psych_model = pipeline("text-classification", model=psych_model_name)

# Initialize ASR and TTS models
asr_model = EncoderDecoderASR.from_hparams(source="speechbrain/asr-crdnn-rnnlm-librispeech", savedir="tmp_asr")
tts_model = Tacotron2.from_hparams(source="speechbrain/tts-tacotron2-ljspeech", savedir="tmp_tts")
voc_model = HIFIGAN.from_hparams(source="speechbrain/tts-hifigan-ljspeech", savedir="tmp_voc")

# Psychometric Test Questions
text_questions = [
    "How do you handle criticism?",
    "Describe a time when you overcame a challenge.",
    "What motivates you to work hard?"
]

audio_questions = [
    "What does teamwork mean to you?",
    "How do you handle stressful situations?"
]

# Function to analyze text responses
def analyze_text_responses(responses):
    analysis = [psych_model(response)[0] for response in responses]
    traits = {response["label"]: response["score"] for response in analysis}
    return traits

# Function to handle TTS
def generate_audio_question(question):
    mel_output, alignment, _ = tts_model.encode_text(question)
    waveforms = voc_model.decode_batch(mel_output)
    return waveforms[0].numpy()

def process_audio_response(audio):
    # Check if the audio input is None
    if audio is None:
        return "No audio provided"
    
    # Process the audio if it's a valid input
    try:
        text_response = asr_model.transcribe_file(audio)
        return text_response
    except Exception as e:
        return f"Error processing audio: {str(e)}"

# Gradio interface functions
def text_part(candidate_name, responses):
    traits = analyze_text_responses(responses)
    df = pd.DataFrame(traits.items(), columns=["Trait", "Score"])
    plt.figure(figsize=(8, 6))
    plt.bar(df["Trait"], df["Score"], color="skyblue")
    plt.title(f"Psychometric Analysis for {candidate_name}")
    plt.xlabel("Traits")
    plt.ylabel("Score")
    plt.xticks(rotation=45)
    plt.tight_layout()
    return df, plt

def audio_part(candidate_name, audio_responses):
    # Check if any audio response is invalid (None)
    valid_audio_responses = [process_audio_response(audio) for audio in audio_responses if audio is not None]
    
    # If all responses are invalid, return an error message
    if not valid_audio_responses:
        return "No valid audio responses provided", None

    traits = analyze_text_responses(valid_audio_responses)
    df = pd.DataFrame(traits.items(), columns=["Trait", "Score"])
    plt.figure(figsize=(8, 6))
    plt.bar(df["Trait"], df["Score"], color="lightcoral")
    plt.title(f"Audio Psychometric Analysis for {candidate_name}")
    plt.xlabel("Traits")
    plt.ylabel("Score")
    plt.xticks(rotation=45)
    plt.tight_layout()
    return df, plt


# Gradio UI function
def chat_interface(candidate_name, *responses):
    # Separate text responses and audio responses
    num_text_questions = len(text_questions)
    text_responses = responses[:num_text_questions]
    audio_responses = responses[num_text_questions:]
    
    # Process text responses
    text_df, text_plot = text_part(candidate_name, text_responses)
    
    # Process audio responses
    audio_df, audio_plot = audio_part(candidate_name, audio_responses)
    
    return text_df, text_plot, audio_df, audio_plot

# Create text inputs and audio inputs
text_inputs = [gr.Textbox(label=f"Response to Q{i+1}: {q}") for i, q in enumerate(text_questions)]
audio_inputs = [gr.Audio(label=f"Response to Q{i+1}: {q}", type="filepath") for i, q in enumerate(audio_questions)]

interface = gr.Interface(
    fn=chat_interface,
    inputs=[gr.Textbox(label="Candidate Name")] + text_inputs + audio_inputs,
    outputs=["dataframe", "plot", "dataframe", "plot"],
    title="Psychometric Analysis Chatbot"
)

# Launch the interface
interface.launch()