Update app.py

#2
by Yusin - opened
Files changed (1) hide show
  1. app.py +71 -34
app.py CHANGED
@@ -1,46 +1,82 @@
1
  import tempfile
2
-
3
  import gradio as gr
4
-
5
  from neon_tts_plugin_coqui import CoquiTTS
6
-
7
-
8
  LANGUAGES = list(CoquiTTS.langs.keys())
9
  default_lang = "en"
10
 
 
 
 
 
 
11
 
12
-
13
- title = "πŸΈπŸ’¬ - NeonAI Coqui AI TTS Plugin"
14
- description = "πŸΈπŸ’¬ - a deep learning toolkit for Text-to-Speech, battle-tested in research and production"
15
- info = "more info at [Neon Coqui TTS Plugin](https://github.com/NeonGeckoCom/neon-tts-plugin-coqui), [Coqui TTS](https://github.com/coqui-ai/TTS)"
16
- badge = "https://visitor-badge-reloaded.herokuapp.com/badge?page_id=neongeckocom.neon-tts-plugin-coqui"
17
-
18
-
19
 
20
  coquiTTS = CoquiTTS()
21
 
22
 
23
- def tts(text: str, language: str):
24
- print(text, language)
25
- # return output
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as fp:
27
- coquiTTS.get_tts(text, fp, speaker = {"language" : language})
28
- return fp.name
29
-
30
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
  with gr.Blocks() as blocks:
33
  gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>"
34
  + title
35
  + "</h1>")
36
- gr.Markdown(description)
37
  with gr.Row():# equal_height=False
38
  with gr.Column():# variant="panel"
39
- textbox = gr.Textbox(
40
- label="Input",
41
- value=CoquiTTS.langs[default_lang]["sentence"],
42
- max_lines=3,
43
- )
44
  radio = gr.Radio(
45
  label="Language",
46
  choices=LANGUAGES,
@@ -48,20 +84,21 @@ with gr.Blocks() as blocks:
48
  )
49
  with gr.Row():# mobile_collapse=False
50
  submit = gr.Button("Submit", variant="primary")
 
 
51
  audio = gr.Audio(label="Output", interactive=False)
52
- gr.Markdown(info)
53
- gr.Markdown("<center>"
54
- +f'<img src={badge} alt="visitors badge"/>'
55
- +"</center>")
56
 
57
  # actions
58
  submit.click(
59
- tts,
60
- [textbox, radio],
61
- [audio],
62
  )
63
- radio.change(lambda lang: CoquiTTS.langs[lang]["sentence"], radio, textbox)
64
-
65
 
66
 
67
- blocks.launch()
 
1
  import tempfile
 
2
  import gradio as gr
 
3
  from neon_tts_plugin_coqui import CoquiTTS
 
 
4
  LANGUAGES = list(CoquiTTS.langs.keys())
5
  default_lang = "en"
6
 
7
+ # ChatGPT
8
+ from pyChatGPT import ChatGPT
9
+ import whisper
10
+ whisper_model = whisper.load_model("small")
11
+ session_token = os.environ.get('SessionToken')
12
 
13
+ title = "Voice to ChatGPT to Voice"
14
+ #info = "more info at [Neon Coqui TTS Plugin](https://github.com/NeonGeckoCom/neon-tts-plugin-coqui), [Coqui TTS](https://github.com/coqui-ai/TTS)"
15
+ #badge = "https://visitor-badge-reloaded.herokuapp.com/badge?page_id=neongeckocom.neon-tts-plugin-coqui"
 
 
 
 
16
 
17
  coquiTTS = CoquiTTS()
18
 
19
 
20
+ # ChatGPT
21
+ def chat_hf(audio, custom_token, language):
22
+ try:
23
+ whisper_text = translate(audio)
24
+ api = ChatGPT(session_token)
25
+ resp = api.send_message(whisper_text)
26
+
27
+ api.refresh_auth() # refresh the authorization token
28
+ api.reset_conversation() # reset the conversation
29
+ gpt_response = resp['message']
30
+
31
+ except:
32
+ whisper_text = translate(audio)
33
+ api = ChatGPT(custom_token)
34
+ resp = api.send_message(whisper_text)
35
+
36
+ api.refresh_auth() # refresh the authorization token
37
+ api.reset_conversation() # reset the conversation
38
+ gpt_response = resp['message']
39
+
40
+ # to voice
41
  with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as fp:
42
+ coquiTTS.get_tts(whisper_text, fp, speaker = {"language" : language})
43
+
44
+ return whisper_text, gpt_response, fp.name
45
+
46
+ # whisper
47
+ def translate(audio):
48
+ print("""
49
+ β€”
50
+ Sending audio to Whisper ...
51
+ β€”
52
+ """)
53
+
54
+ audio = whisper.load_audio(audio)
55
+ audio = whisper.pad_or_trim(audio)
56
+
57
+ mel = whisper.log_mel_spectrogram(audio).to(whisper_model.device)
58
+
59
+ _, probs = whisper_model.detect_language(mel)
60
+
61
+ transcript_options = whisper.DecodingOptions(task="transcribe", fp16 = False)
62
+
63
+ transcription = whisper.decode(whisper_model, mel, transcript_options)
64
+
65
+ print("language spoken: " + transcription.language)
66
+ print("transcript: " + transcription.text)
67
+ print("β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”")
68
+
69
+ return transcription.text
70
 
71
  with gr.Blocks() as blocks:
72
  gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>"
73
  + title
74
  + "</h1>")
75
+ #gr.Markdown(description)
76
  with gr.Row():# equal_height=False
77
  with gr.Column():# variant="panel"
78
+ audio_file = gr.inputs.Audio(source="microphone", type="filepath")
79
+ custom_token = gr.Textbox(label='If it fails, use your own session token', placeholder="your own session token")
 
 
 
80
  radio = gr.Radio(
81
  label="Language",
82
  choices=LANGUAGES,
 
84
  )
85
  with gr.Row():# mobile_collapse=False
86
  submit = gr.Button("Submit", variant="primary")
87
+ text1 = gr.Textbox(label="Speech to Text")
88
+ text2 = gr.Textbox(label="chatGPT response")
89
  audio = gr.Audio(label="Output", interactive=False)
90
+ #gr.Markdown(info)
91
+ #gr.Markdown("<center>"
92
+ # +f'<img src={badge} alt="visitors badge"/>'
93
+ # +"</center>")
94
 
95
  # actions
96
  submit.click(
97
+ chat_hf,
98
+ [audio_file, custom_token, radio]
99
+ [text1, text2, audio],
100
  )
101
+ radio.change(lambda lang: CoquiTTS.langs[lang]["sentence"], radio, text1)
 
102
 
103
 
104
+ blocks.launch(debug=True)