imessien commited on
Commit
d79b46d
1 Parent(s): 437b669

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +93 -0
app.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+
4
+
5
+
6
+ from transformers import GPT2LMHeadModel, GPT2Tokenizer, pipeline
7
+
8
+ # Initialize the GPT2 model and tokenizer
9
+ tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
10
+ model = GPT2LMHeadModel.from_pretrained("gpt2")
11
+
12
+ # Initialize the Whisper GPT model
13
+ translation_pipeline = pipeline("automatic-speech-recognition", model="openai/whisper-large-v2")
14
+
15
+
16
+ # Geriatric Depression Scale Quiz Questions
17
+ questions = [
18
+ "Are you basically satisfied with your life?",
19
+ "Have you dropped many of your activities and interests?",
20
+ "Do you feel that your life is empty?",
21
+ "Do you often get bored?",
22
+ "Are you in good spirits most of the time?",
23
+ "Are you afraid that something bad is going to happen to you?",
24
+ "Do you feel happy most of the time?",
25
+ "Do you often feel helpless?",
26
+ "Do you prefer to stay at home, rather than going out and doing things?",
27
+ "Do you feel that you have more problems with memory than most?",
28
+ "Do you think it is wonderful to be alive now?",
29
+ "Do you feel worthless the way you are now?",
30
+ "Do you feel full of energy?",
31
+ "Do you feel that your situation is hopeless?",
32
+ "Do you think that most people are better off than you are?"
33
+ ]
34
+
35
+
36
+ def ask_questions(answers):
37
+ """Calculate score based on answers."""
38
+ score = 0
39
+ for answer in answers:
40
+ if answer.lower() == 'yes':
41
+ score += 1
42
+ elif answer.lower() != 'no':
43
+ raise ValueError(f"Invalid answer: {answer}")
44
+ return score
45
+
46
+ def understand_answers(audio_answers):
47
+ """Convert audio answers to text using the Whisper ASR model."""
48
+ asr_pipeline = pipeline("automatic-speech-recognition", model="openai/whisper-large-v2")
49
+ text_answers = []
50
+ for audio in audio_answers:
51
+ transcript = asr_pipeline(audio)
52
+ text_answers.append(transcript[0]['generated_text'])
53
+ return text_answers
54
+
55
+ # Removing the understand function as it's functionality is covered by understand_answers
56
+
57
+ # Keeping the whisper function for text-to-speech conversion
58
+ def whisper(text):
59
+ """Convert text to speech using the Whisper TTS model."""
60
+ tts_pipeline = pipeline("text-to-speech", model="facebook/wav2vec2-base-960h")
61
+ speech = tts_pipeline(text)
62
+ return speech[0]['generated_text']
63
+
64
+
65
+ def modified_summarize(answers):
66
+ """Summarize answers using the GPT2 model."""
67
+ answers_str = " ".join(answers)
68
+ inputs = tokenizer.encode("summarize: " + answers_str, return_tensors='pt')
69
+ summary_ids = model.generate(inputs, max_length=150, num_beams=5, early_stopping=True)
70
+ return tokenizer.decode(summary_ids[0], skip_special_tokens=True)
71
+
72
+ def assistant(*audio_answers):
73
+ """Calculate score, translate and summarize answers."""
74
+ # Convert audio answers to text
75
+ answers = understand_answers(audio_answers)
76
+
77
+ # Calculate score and summarize
78
+ score = ask_questions(answers)
79
+ summary = modified_summarize(answers)
80
+
81
+ # Convert the summary to speech
82
+ speech = whisper(summary)
83
+
84
+ # Convert the first answer from audio to text (already done in answers[0])
85
+ text = answers[0]
86
+
87
+ return {"score": f"Score: {score}", "summary": f"Summary: {summary}", "speech": speech, "text": text}
88
+
89
+ iface_score = gr.Interface(fn=assistant,
90
+ inputs=[gr.inputs.Audio(source="microphone")] * len(questions),
91
+ outputs=["text", "text", gr.outputs.Audio(type="auto"), "text"])
92
+ iface_score.launch()
93
+