adeelshuaib commited on
Commit
8f33c3e
·
verified ·
1 Parent(s): 6dce488

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -51
app.py CHANGED
@@ -1,64 +1,91 @@
 
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
 
 
3
 
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient(model="meta-llama/Llama-3.2-3B")
8
 
 
 
9
 
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
 
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
 
25
 
26
- messages.append({"role": "user", "content": message})
 
 
 
27
 
28
- response = ""
 
 
 
 
29
 
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
 
39
- response += token
40
- yield response
 
 
41
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
61
 
 
 
 
 
 
 
 
 
 
62
 
63
- if __name__ == "__main__":
64
- demo.launch()
 
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()