adrabi-abderrahim commited on
Commit
d275656
1 Parent(s): 415cf1f
Files changed (2) hide show
  1. app.py +51 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from gtts import gTTS
2
+ from transformers import pipeline
3
+ import gradio as gr
4
+ import uuid
5
+
6
+ asr = pipeline('automatic-speech-recognition', "facebook/wav2vec2-conformer-rope-large-960h-ft")
7
+ corrector = pipeline("text2text-generation", model="pszemraj/grammar-synthesis-small")
8
+
9
+ transcribe = lambda audio: asr(audio)['text'].lower()
10
+
11
+ def to_audio(s):
12
+ audio_path = f'/tmp/{uuid.uuid4()}.mp3'
13
+ tts = gTTS(s, tld='us')
14
+ tts.save(audio_path)
15
+ return audio_path
16
+
17
+
18
+ #@title English Learning
19
+ def transcription(audio, history):
20
+ if audio:
21
+ message = transcribe(audio)
22
+ history.append(( (audio, ) , message))
23
+ results = corrector(message)
24
+ results = '\n'.join([t['generated_text'] for t in results])
25
+ history.append( (None, f'**[Grammar and examples]**\n {results}') )
26
+
27
+ return history
28
+
29
+ def chat(message, history):
30
+ audio_path = to_audio(message)
31
+ history.append((message, (audio_path,)))
32
+ results = corrector(message)
33
+ results = '\n'.join([t['generated_text'] for t in results])
34
+ history.append( (None, f'**[Grammar and examples]**\n {results}') )
35
+
36
+ return None, history
37
+
38
+ with gr.Blocks(theme=gr.themes.Soft()) as learning:
39
+ gr.Markdown('# The main aim of this app is to help English learners to speak fluently.')
40
+
41
+ chatbot = gr.Chatbot()
42
+
43
+ with gr.Row():
44
+ message = gr.Textbox(label='Send your message to TTS')
45
+ microphone = gr.Audio(label="Transcribe", source="microphone", type="filepath")
46
+
47
+ microphone.change(transcription, [microphone, chatbot], [chatbot])
48
+ microphone.change(lambda:None, None, microphone)
49
+ message.submit(chat, [message, chatbot], [message, chatbot])
50
+
51
+ learning.launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gtts
2
+ transformers
3
+ gradio