imessien commited on
Commit
cf2d2f8
·
1 Parent(s): ff57df2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -89
app.py CHANGED
@@ -1,95 +1,95 @@
1
  import gradio as gr
2
  from transformers import GPT2LMHeadModel, GPT2Tokenizer, pipeline
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
- # Initialize the GPT2 model and tokenizer
5
- tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
6
- model = GPT2LMHeadModel.from_pretrained("gpt2")
7
 
8
- # Initialize the Whisper GPT model
9
- translation_pipeline = pipeline("automatic-speech-recognition", model="openai/whisper-large-v2")
 
 
 
 
10
 
11
- # Geriatric Depression Scale Quiz Questions
12
- questions = [
13
- "Are you basically satisfied with your life?",
14
- "Have you dropped many of your activities and interests?",
15
- "Do you feel that your life is empty?",
16
- "Do you often get bored?",
17
- "Are you in good spirits most of the time?",
18
- "Are you afraid that something bad is going to happen to you?",
19
- "Do you feel happy most of the time?",
20
- "Do you often feel helpless?",
21
- "Do you prefer to stay at home, rather than going out and doing things?",
22
- "Do you feel that you have more problems with memory than most?",
23
- "Do you think it is wonderful to be alive now?",
24
- "Do you feel worthless the way you are now?",
25
- "Do you feel full of energy?",
26
- "Do you feel that your situation is hopeless?",
27
- "Do you think that most people are better off than you are?"
28
- ]
29
 
30
- def ask_questions(answers):
31
- """Calculate score based on answers."""
32
- score = 0
33
- for answer in answers:
34
- if answer.lower() == 'yes':
35
- score += 1
36
- elif answer.lower() != 'no':
37
- raise ValueError(f"Invalid answer: {answer}")
38
- return score
39
-
40
- def understand_answers(audio_answers):
41
- """Convert audio answers to text using the Whisper ASR model."""
42
- asr_pipeline = pipeline("automatic-speech-recognition", model="openai/whisper-large-v2")
43
- text_answers = []
44
- for audio in audio_answers:
45
- transcript = asr_pipeline(audio)
46
- text_answers.append(transcript[0]['generated_text'])
47
- return text_answers
48
-
49
- def modified_summarize(answers):
50
- """Summarize answers using the GPT2 model."""
51
- answers_str = " ".join(answers)
52
- inputs = tokenizer.encode("summarize: " + answers_str, return_tensors='pt')
53
- summary_ids = model.generate(inputs, max_length=150, num_beams=5, early_stopping=True)
54
- return tokenizer.decode(summary_ids[0], skip_special_tokens=True)
55
-
56
-
57
- def assistant(*audio_answers):
58
- """Convert audio answers to text, evaluate and provide a summary."""
59
- text_answers = understand_answers(audio_answers)
60
- summarized_text = modified_summarize(text_answers)
61
- score = ask_questions(text_answers)
62
- return summarized_text, f"Your score is: {score}/{len(questions)}", text_answers # Return text_answers as well
63
-
64
- # Create the Gradio Blocks interface with button click
65
-
66
- def update():
67
- audio_answers = [audio.value for audio in inp] # Using inp as it collects all the audio inputs
68
- # Handling the three returned values from the assistant function
69
- summarized_text, score_string, text_answers = assistant(*audio_answers)
70
- out_last_transcription.value = summarized_text # Displaying the summarized text
71
- out_score.value = score_string # Displaying the score
72
-
73
- with gr.Blocks() as demo:
74
- gr.Markdown("Start recording your responses below and then click **Run** to see the transcription and your score.")
75
-
76
- # Clearly initializing Inputs and Outputs lists for the button click
77
- inp = []
78
-
79
- # Using Column to nest questions
80
- with gr.Column(scale=1, min_width=600):
81
- for i, question in enumerate(questions):
82
- gr.Markdown(f"**Question {i+1}:** {question}")
83
- audio_input = gr.Audio(source="microphone")
84
- inp.append(audio_input)
85
-
86
- # Two output textboxes: one for the last transcribed answer and another for the score
87
- out_last_transcription = gr.Textbox(label="Last Transcribed Answer", placeholder="Last transcribed answer will appear here.")
88
- out_score = gr.Textbox(label="Score", placeholder="Your score will appear here.")
89
-
90
- # Button with click event
91
- btn = gr.Button("Run")
92
- btn.click(fn=update, inputs=inp, outputs=[out_last_transcription, out_score])
93
-
94
-
95
- demo.launch()
 
1
  import gradio as gr
2
  from transformers import GPT2LMHeadModel, GPT2Tokenizer, pipeline
3
+
4
+ # Initialize the GPT2 model and tokenizer
5
+ tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
6
+ model = GPT2LMHeadModel.from_pretrained("gpt2")
7
+
8
+ # Initialize the Whisper GPT model
9
+ translation_pipeline = pipeline("automatic-speech-recognition", model="openai/whisper-large-v2")
10
+
11
+ # Geriatric Depression Scale Quiz Questions
12
+ questions = [
13
+ "Are you basically satisfied with your life?",
14
+ "Have you dropped many of your activities and interests?",
15
+ "Do you feel that your life is empty?",
16
+ "Do you often get bored?",
17
+ "Are you in good spirits most of the time?",
18
+ "Are you afraid that something bad is going to happen to you?",
19
+ "Do you feel happy most of the time?",
20
+ "Do you often feel helpless?",
21
+ "Do you prefer to stay at home, rather than going out and doing things?",
22
+ "Do you feel that you have more problems with memory than most?",
23
+ "Do you think it is wonderful to be alive now?",
24
+ "Do you feel worthless the way you are now?",
25
+ "Do you feel full of energy?",
26
+ "Do you feel that your situation is hopeless?",
27
+ "Do you think that most people are better off than you are?"
28
+ ]
29
+
30
+ def ask_questions(answers):
31
+ """Calculate score based on answers."""
32
+ score = 0
33
+ for answer in answers:
34
+ if answer.lower() == 'yes':
35
+ score += 1
36
+ elif answer.lower() != 'no':
37
+ raise ValueError(f"Invalid answer: {answer}")
38
+ return score
39
+
40
+ def understand_answers(audio_answers):
41
+ """Convert audio answers to text using the Whisper ASR model."""
42
+ asr_pipeline = pipeline("automatic-speech-recognition", model="openai/whisper-large-v2")
43
+ text_answers = []
44
+ for audio in audio_answers:
45
+ transcript = asr_pipeline(audio)
46
+ text_answers.append(transcript[0]['generated_text'])
47
+ return text_answers
48
+
49
+ def modified_summarize(answers):
50
+ """Summarize answers using the GPT2 model."""
51
+ answers_str = " ".join(answers)
52
+ inputs = tokenizer.encode("summarize: " + answers_str, return_tensors='pt')
53
+ summary_ids = model.generate(inputs, max_length=150, num_beams=5, early_stopping=True)
54
+ return tokenizer.decode(summary_ids[0], skip_special_tokens=True)
55
+
56
+
57
+ def assistant(*audio_answers):
58
+ """Convert audio answers to text, evaluate and provide a summary."""
59
+ text_answers = understand_answers(audio_answers)
60
+ summarized_text = modified_summarize(text_answers)
61
+ score = ask_questions(text_answers)
62
+ return summarized_text, f"Your score is: {score}/{len(questions)}", text_answers # Return text_answers as well
63
+
64
+ # Create the Gradio Blocks interface with button click
65
+
66
+ def update():
67
+ audio_answers = [audio.value for audio in inp] # Using inp as it collects all the audio inputs
68
+ # Handling the three returned values from the assistant function
69
+ summarized_text, score_string, text_answers = assistant(*audio_answers)
70
+ out_last_transcription.value = summarized_text # Displaying the summarized text
71
+ out_score.value = score_string # Displaying the score
72
+
73
+ with gr.Blocks() as demo:
74
+ gr.Markdown("Start recording your responses below and then click **Run** to see the transcription and your score.")
75
 
76
+ # Clearly initializing Inputs and Outputs lists for the button click
77
+ inp = []
 
78
 
79
+ # Using Column to nest questions
80
+ with gr.Column(scale=1, min_width=600):
81
+ for i, question in enumerate(questions):
82
+ gr.Markdown(f"**Question {i+1}:** {question}")
83
+ audio_input = gr.Audio(source="microphone")
84
+ inp.append(audio_input)
85
 
86
+ # Two output textboxes: one for the last transcribed answer and another for the score
87
+ out_last_transcription = gr.Textbox(label="Last Transcribed Answer", placeholder="Last transcribed answer will appear here.")
88
+ out_score = gr.Textbox(label="Score", placeholder="Your score will appear here.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
 
90
+ # Button with click event
91
+ btn = gr.Button("Run")
92
+ btn.click(fn=update, inputs=inp, outputs=[out_last_transcription, out_score])
93
+
94
+
95
+ demo.launch()