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

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -34
app.py CHANGED
@@ -24,7 +24,7 @@ questions = [
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):
@@ -46,6 +46,12 @@ def understand_answers(audio_answers):
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)
@@ -53,43 +59,47 @@ def modified_summarize(answers):
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()
 
 
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):
 
46
  text_answers.append(transcript[0]['generated_text'])
47
  return text_answers
48
 
49
+ def whisper(text):
50
+ """Convert text to speech using the Whisper TTS model."""
51
+ tts_pipeline = pipeline("text-to-speech", model="facebook/wav2vec2-base-960h")
52
+ speech = tts_pipeline(text)
53
+ return speech[0]['generated_text']
54
+
55
  def modified_summarize(answers):
56
  """Summarize answers using the GPT2 model."""
57
  answers_str = " ".join(answers)
 
59
  summary_ids = model.generate(inputs, max_length=150, num_beams=5, early_stopping=True)
60
  return tokenizer.decode(summary_ids[0], skip_special_tokens=True)
61
 
 
62
  def assistant(*audio_answers):
63
+ """Calculate score, translate and summarize answers."""
64
+ # Convert audio answers to text
65
+ answers = understand_answers(audio_answers)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
+ # Calculate score and summarize
68
+ score = ask_questions(answers)
69
+ summary = modified_summarize(answers)
70
 
71
+ # Convert the summary to speech
72
+ speech = whisper(summary)
 
 
 
 
73
 
74
+ # Convert the first answer from audio to text (already done in answers[0])
75
+ text = answers[0]
 
76
 
77
+ return {"score": f"Score: {score}", "summary": f"Summary: {summary}", "speech": speech, "text": text}
78
+
79
+ labeled_inputs = [
80
+ {'name': 'input_1', 'label': 'Question 1: Are you basically satisfied with your life?', 'type': 'audio', 'source': 'microphone'},
81
+ {'name': 'input_2', 'label': 'Question 2: Have you dropped many of your activities and interests?', 'type': 'audio', 'source': 'microphone'},
82
+ {'name': 'input_3', 'label': 'Question 3:Do you feel that your life is empty?', 'type': 'audio', 'source': 'microphone'},
83
+ {'name': 'input_4', 'label':'Question 4:Do you often get bored?','type': 'audio', 'source': 'microphone'},
84
+ {'name': 'input_5', 'label':'Question 5:Are you in good spirits most of the time?','type': 'audio', 'source': 'microphone'},
85
+ {'name': 'input_6', 'label':'Question 6:Are you afraid that something bad is going to happen to you?','type': 'audio', 'source': 'microphone'},
86
+ {'name': 'input_7', 'label': 'Question 7:Do you feel happy most of the time?','type': 'audio', 'source': 'microphone'},
87
+ {'name': 'input_8', 'label':'Question 8:Do you often feel helpless?','type': 'audio', 'source': 'microphone'},
88
+ {'name': 'input_9', 'label':'Question 9: Do you prefer to stay at home, rather than going out and doing things?','type': 'audio', 'source': 'microphone'},
89
+ {'name': 'input_10', 'label': 'Question 10: Do you feel that you have more problems with memory than most?','type': 'audio', 'source': 'microphone'},
90
+ {'name': 'input_11', 'label':'Question 11: Do you think it is wonderful to be alive now?','type': 'audio', 'source': 'microphone'},
91
+ {'name': 'input_12', 'label': 'Question 12:Do you feel worthless the way you are now?','type': 'audio', 'source': 'microphone'},
92
+ {'name': 'input_13', 'label': 'Question 13:Do you feel full of energy?','type': 'audio', 'source': 'microphone'},
93
+ {'name': 'input_14', 'label': 'Question 14:Do you feel that your situation is hopeless?','type': 'audio', 'source': 'microphone'},
94
+ {'name': 'input_15', 'label': 'Question 15:Do you think that most people are better off than you are?','type': 'audio', 'source': 'microphone'}
95
+ ]
96
 
97
+ labeled_outputs = [
98
+ ("Score", "text"),
99
+ ("Summary", "text"),
100
+ ("Summary (Audio)", gr.components.Audio(type="numpy")),
101
+ ("First Answer (Text)", "text")
102
+ ]
103
 
104
+ iface_score = gr.Interface(fn=assistant, inputs=labeled_inputs, outputs=labeled_outputs)
105
+ iface_score.launch()