imessien commited on
Commit
d9ed04a
1 Parent(s): b087228

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -77
app.py CHANGED
@@ -1,11 +1,6 @@
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
 
@@ -13,6 +8,9 @@ model = GPT2LMHeadModel.from_pretrained("gpt2")
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?",
@@ -29,10 +27,9 @@ questions = [
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
@@ -52,16 +49,6 @@ def understand_answers(audio_answers):
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)
@@ -69,69 +56,43 @@ def modified_summarize(answers):
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
- # Labeled input components
90
- #labeled_inputs = [(f"Question {i+1}: {question}", gr.components.Audio(source="microphone")) for i, question in enumerate(questions)]
91
-
92
-
93
- labeled_inputs = [
94
- {'name': 'input_1', 'label': 'Question 1: Are you basically satisfied with your life?', 'type': 'audio', 'source': 'microphone'},
95
- {'name': 'input_2', 'label': 'Question 2: Have you dropped many of your activities and interests?', 'type': 'audio', 'source': 'microphone'},
96
- {'name': 'input_3', 'label': 'Question 3:Do you feel that your life is empty?', 'type': 'audio', 'source': 'microphone'},
97
- {'name': 'input_4', 'label':'Question 4:Do you often get bored?','type': 'audio', 'source': 'microphone'},
98
- {'name': 'input_5', 'label':'Question 5:Are you in good spirits most of the time?','type': 'audio', 'source': 'microphone'},
99
- {'name': 'input_6', 'label':'Question 6:Are you afraid that something bad is going to happen to you?','type': 'audio', 'source': 'microphone'},
100
- {'name': 'input_7', 'label': 'Question 7:Do you feel happy most of the time?','type': 'audio', 'source': 'microphone'},
101
- {'name': 'input_8', 'label':'Question 8:Do you often feel helpless?','type': 'audio', 'source': 'microphone'},
102
- {'name': 'input_9', 'label':'Question 9: Do you prefer to stay at home, rather than going out and doing things?','type': 'audio', 'source': 'microphone'},
103
- {'name': 'input_10', 'label': 'Question 10: Do you feel that you have more problems with memory than most?','type': 'audio', 'source': 'microphone'},
104
- {'name': 'input_11', 'label':'Question 11: Do you think it is wonderful to be alive now?','type': 'audio', 'source': 'microphone'},
105
- {'name': 'input_12', 'label': 'Question 12:Do you feel worthless the way you are now?','type': 'audio', 'source': 'microphone'},
106
- {'name': 'input_13', 'label': 'Question 13:Do you feel full of energy?','type': 'audio', 'source': 'microphone'},
107
- {'name': 'input_14', 'label': 'Question 14:Do you feel that your situation is hopeless?','type': 'audio', 'source': 'microphone'},
108
- {'name': 'input_15', 'label': 'Question 15:Do you think that most people are better off than you are?','type': 'audio', 'source': 'microphone'}
109
- ]
110
-
111
- # Labeled output components
112
- labeled_outputs = [
113
- ("Score", "text"),
114
- ("Summary", "text"),
115
- ("Summary (Audio)", gr.components.Audio(type="numpy")),
116
- ("First Answer (Text)", "text")
117
- ]
118
-
119
- # Constructing the Gradio Interface with labeled components
120
- iface_score = gr.Interface(fn=assistant, inputs=labeled_inputs, outputs=labeled_outputs)
121
- iface_score.launch()
122
-
123
- # Labeled output components
124
- labeled_outputs = [
125
- ("Score", "text"),
126
- ("Summary", "text"),
127
- ("Summary (Audio)", gr.components.Audio(type="numpy")),
128
- ("First Answer (Text)", "text")
129
- ]
130
-
131
 
132
 
133
- # Constructing the Gradio Interface with labeled components
134
- iface_score = gr.Interface(fn=assistant,
135
- inputs=labeled_inputs,
136
- outputs=labeled_outputs)
137
- iface_score.launch()
 
1
  import gradio as gr
 
 
 
 
2
  from transformers import GPT2LMHeadModel, GPT2Tokenizer, pipeline
3
+ # Initialize the GPT2 model and tokenizer
 
4
  tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
5
  model = GPT2LMHeadModel.from_pretrained("gpt2")
6
 
 
8
  translation_pipeline = pipeline("automatic-speech-recognition", model="openai/whisper-large-v2")
9
 
10
 
11
+
12
+
13
+
14
  # Geriatric Depression Scale Quiz Questions
15
  questions = [
16
  "Are you basically satisfied with your life?",
 
27
  "Do you feel worthless the way you are now?",
28
  "Do you feel full of energy?",
29
  "Do you feel that your situation is hopeless?",
30
+ "Do you think that most people are better off than you are?"
31
  ]
32
 
 
33
  def ask_questions(answers):
34
  """Calculate score based on answers."""
35
  score = 0
 
49
  text_answers.append(transcript[0]['generated_text'])
50
  return text_answers
51
 
 
 
 
 
 
 
 
 
 
 
52
  def modified_summarize(answers):
53
  """Summarize answers using the GPT2 model."""
54
  answers_str = " ".join(answers)
 
56
  summary_ids = model.generate(inputs, max_length=150, num_beams=5, early_stopping=True)
57
  return tokenizer.decode(summary_ids[0], skip_special_tokens=True)
58
 
59
+
60
  def assistant(*audio_answers):
61
+ """Convert audio answers to text, evaluate and provide a summary."""
62
+ text_answers = understand_answers(audio_answers)
63
+ summarized_text = modified_summarize(text_answers)
64
+ score = ask_questions(text_answers)
65
+ return summarized_text, f"Your score is: {score}/{len(questions)}", text_answers # Return text_answers as well
66
+
67
+ # Create the Gradio Blocks interface with button click
68
+
69
+ def update():
70
+ audio_answers = [audio.value for audio in inp] # Using inp as it collects all the audio inputs
71
+ # Handling the three returned values from the assistant function
72
+ summarized_text, score_string, text_answers = assistant(*audio_answers)
73
+ out_last_transcription.value = summarized_text # Displaying the summarized text
74
+ out_score.value = score_string # Displaying the score
75
+
76
+ with gr.Blocks() as demo:
77
+ gr.Markdown("Start recording your responses below and then click **Run** to see the transcription and your score.")
78
 
79
+ # Clearly initializing Inputs and Outputs lists for the button click
80
+ inp = []
 
81
 
82
+ # Using Column to nest questions
83
+ with gr.Column(scale=1, min_width=600):
84
+ for i, question in enumerate(questions):
85
+ gr.Markdown(f"**Question {i+1}:** {question}")
86
+ audio_input = gr.Audio(source="microphone")
87
+ inp.append(audio_input)
88
 
89
+ # Two output textboxes: one for the last transcribed answer and another for the score
90
+ out_last_transcription = gr.Textbox(label="Last Transcribed Answer", placeholder="Last transcribed answer will appear here.")
91
+ out_score = gr.Textbox(label="Score", placeholder="Your score will appear here.")
92
 
93
+ # Button with click event
94
+ btn = gr.Button("Run")
95
+ btn.click(fn=update, inputs=inp, outputs=[out_last_transcription, out_score])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
 
97
 
98
+ demo.launch()