File size: 5,310 Bytes
d79b46d
 
cf2d2f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
471bf08
cf2d2f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
471bf08
 
 
 
 
 
cf2d2f8
 
 
 
 
 
 
 
471bf08
 
 
ff57df2
471bf08
 
 
ff57df2
471bf08
 
ff57df2
471bf08
 
ff57df2
471bf08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf2d2f8
471bf08
 
 
 
 
 
cf2d2f8
471bf08
 
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
import gradio as gr
from transformers import GPT2LMHeadModel, GPT2Tokenizer, pipeline

# Initialize the GPT2 model and tokenizer
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
model = GPT2LMHeadModel.from_pretrained("gpt2")

# Initialize the Whisper GPT model
translation_pipeline =  pipeline("automatic-speech-recognition", model="openai/whisper-large-v2")

# Geriatric Depression Scale Quiz Questions
questions = [
"Are you basically satisfied with your life?",
"Have you dropped many of your activities and interests?",
"Do you feel that your life is empty?",
"Do you often get bored?",
"Are you in good spirits most of the time?",
"Are you afraid that something bad is going to happen to you?",
"Do you feel happy most of the time?",
"Do you often feel helpless?",
"Do you prefer to stay at home, rather than going out and doing things?",
"Do you feel that you have more problems with memory than most?",
"Do you think it is wonderful to be alive now?",
"Do you feel worthless the way you are now?",
"Do you feel full of energy?",
"Do you feel that your situation is hopeless?",
"Do you think that most people are better off than you are?"
]

def ask_questions(answers):
    """Calculate score based on answers."""
    score = 0
    for answer in answers:
        if answer.lower() == 'yes':
            score += 1
        elif answer.lower() != 'no':
            raise ValueError(f"Invalid answer: {answer}")
    return score

def understand_answers(audio_answers):
    """Convert audio answers to text using the Whisper ASR model."""
    asr_pipeline = pipeline("automatic-speech-recognition", model="openai/whisper-large-v2")
    text_answers = []
    for audio in audio_answers:
        transcript = asr_pipeline(audio)
        text_answers.append(transcript[0]['generated_text'])
    return text_answers

def whisper(text):
    """Convert text to speech using the Whisper TTS model."""
    tts_pipeline = pipeline("text-to-speech", model="facebook/wav2vec2-base-960h")
    speech = tts_pipeline(text)
    return speech[0]['generated_text']

def modified_summarize(answers):
    """Summarize answers using the GPT2 model."""
    answers_str = " ".join(answers)
    inputs = tokenizer.encode("summarize: " + answers_str, return_tensors='pt')
    summary_ids = model.generate(inputs, max_length=150, num_beams=5, early_stopping=True)
    return tokenizer.decode(summary_ids[0], skip_special_tokens=True)

def assistant(*audio_answers):
    """Calculate score, translate and summarize answers."""
    # Convert audio answers to text
    answers = understand_answers(audio_answers)
    
    # Calculate score and summarize
    score = ask_questions(answers)
    summary = modified_summarize(answers)
    
    # Convert the summary to speech
    speech = whisper(summary)
    
    # Convert the first answer from audio to text (already done in answers[0])
    text = answers[0]
    
    return {"score": f"Score: {score}", "summary": f"Summary: {summary}", "speech": speech, "text": text}

labeled_inputs = [
    {'name': 'input_1', 'label': 'Question 1: Are you basically satisfied with your life?', 'type': 'audio', 'source': 'microphone'},
    {'name': 'input_2', 'label': 'Question 2: Have you dropped many of your activities and interests?', 'type': 'audio', 'source': 'microphone'},
    {'name': 'input_3', 'label': 'Question 3:Do you feel that your life is empty?', 'type': 'audio', 'source': 'microphone'},
    {'name': 'input_4', 'label':'Question 4:Do you often get bored?','type': 'audio', 'source': 'microphone'},
    {'name': 'input_5', 'label':'Question 5:Are you in good spirits most of the time?','type': 'audio', 'source': 'microphone'},
    {'name': 'input_6', 'label':'Question 6:Are you afraid that something bad is going to happen to you?','type': 'audio', 'source': 'microphone'},
    {'name': 'input_7', 'label': 'Question 7:Do you feel happy most of the time?','type': 'audio', 'source': 'microphone'},
    {'name': 'input_8', 'label':'Question 8:Do you often feel helpless?','type': 'audio', 'source': 'microphone'},
    {'name': 'input_9', 'label':'Question 9: Do you prefer to stay at home, rather than going out and doing things?','type': 'audio', 'source': 'microphone'},
    {'name': 'input_10', 'label': 'Question 10: Do you feel that you have more problems with memory than most?','type': 'audio', 'source': 'microphone'},
    {'name': 'input_11', 'label':'Question 11: Do you think it is wonderful to be alive now?','type': 'audio', 'source': 'microphone'},
    {'name': 'input_12', 'label': 'Question 12:Do you feel worthless the way you are now?','type': 'audio', 'source': 'microphone'},
    {'name': 'input_13', 'label': 'Question 13:Do you feel full of energy?','type': 'audio', 'source': 'microphone'},
    {'name': 'input_14', 'label': 'Question 14:Do you feel that your situation is hopeless?','type': 'audio', 'source': 'microphone'},
    {'name': 'input_15', 'label': 'Question 15:Do you think that most people are better off than you are?','type': 'audio', 'source': 'microphone'}
]

labeled_outputs = [
    ("Score", "text"),
    ("Summary", "text"),
    ("Summary (Audio)", gr.components.Audio(type="numpy")),
    ("First Answer (Text)", "text")
]

iface_score = gr.Interface(fn=assistant, inputs=labeled_inputs, outputs=labeled_outputs)
iface_score.launch()