hyuan5040 Yusin commited on
Commit
da8a0d7
0 Parent(s):

Duplicate from Yusin/Speech-ChatGPT-Speech

Browse files

Co-authored-by: Chen, Yusin <Yusin@users.noreply.huggingface.co>

Files changed (7) hide show
  1. .gitattributes +27 -0
  2. .gitignore +1 -0
  3. README.md +41 -0
  4. app.py +154 -0
  5. packages.txt +2 -0
  6. pygpt.py +111 -0
  7. requirements.txt +5 -0
.gitattributes ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ftz filter=lfs diff=lfs merge=lfs -text
6
+ *.gz filter=lfs diff=lfs merge=lfs -text
7
+ *.h5 filter=lfs diff=lfs merge=lfs -text
8
+ *.joblib filter=lfs diff=lfs merge=lfs -text
9
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
10
+ *.model filter=lfs diff=lfs merge=lfs -text
11
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
12
+ *.onnx filter=lfs diff=lfs merge=lfs -text
13
+ *.ot filter=lfs diff=lfs merge=lfs -text
14
+ *.parquet filter=lfs diff=lfs merge=lfs -text
15
+ *.pb filter=lfs diff=lfs merge=lfs -text
16
+ *.pt filter=lfs diff=lfs merge=lfs -text
17
+ *.pth filter=lfs diff=lfs merge=lfs -text
18
+ *.rar filter=lfs diff=lfs merge=lfs -text
19
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
20
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
21
+ *.tflite filter=lfs diff=lfs merge=lfs -text
22
+ *.tgz filter=lfs diff=lfs merge=lfs -text
23
+ *.wasm filter=lfs diff=lfs merge=lfs -text
24
+ *.xz filter=lfs diff=lfs merge=lfs -text
25
+ *.zip filter=lfs diff=lfs merge=lfs -text
26
+ *.zstandard filter=lfs diff=lfs merge=lfs -text
27
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ .env
README.md ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Speech2ChatGPT2Speech
3
+ emoji: 🗣️🙉
4
+ colorFrom: indigo
5
+ colorTo: yellow
6
+ sdk: gradio
7
+ python_version: 3.9
8
+ sdk_version: 3.12.0
9
+ app_file: app.py
10
+ models:
11
+ - neongeckocom/tts-vits-ljspeech-en
12
+ - neongeckocom/tts-vits-css10-es
13
+ - neongeckocom/tts-vits-css10-fr
14
+ - neongeckocom/tts-vits-css10-de
15
+ - neongeckocom/tts-vits-cv-it
16
+ - neongeckocom/tts-vits-mai-pl
17
+ - neongeckocom/tts-vits-mai-uk
18
+ - neongeckocom/tts-vits-cv-ro
19
+ - neongeckocom/tts-vits-css10-hu
20
+ - neongeckocom/tts-vits-cv-el
21
+ - neongeckocom/tts-vits-cv-cs
22
+ - neongeckocom/tts-vits-cv-sv
23
+ - neongeckocom/tts-vits-cv-pt
24
+ - neongeckocom/tts-vits-cv-bg
25
+ - neongeckocom/tts-vits-cv-hr
26
+ - neongeckocom/tts-vits-cv-da
27
+ - neongeckocom/tts-vits-cv-sk
28
+ - neongeckocom/tts-vits-css10-nl
29
+ - neongeckocom/tts-vits-css10-fi
30
+ - neongeckocom/tts-vits-cv-lt
31
+ - neongeckocom/tts-vits-cv-sl
32
+ - neongeckocom/tts-vits-cv-lv
33
+ - neongeckocom/tts-vits-cv-et
34
+ - neongeckocom/tts-vits-cv-ga
35
+ - neongeckocom/tts-vits-cv-mt
36
+ pinned: false
37
+ license: apache-2.0
38
+ duplicated_from: Yusin/Speech-ChatGPT-Speech
39
+ ---
40
+
41
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces#reference
app.py ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ import telnetlib
7
+ #import whisper
8
+ #whisper_model = whisper.load_model("small")
9
+ whisper = gr.Interface.load(name="spaces/sanchit-gandhi/whisper-large-v2")
10
+ #chatgpt = gr.Blocks.load(name="spaces/fffiloni/whisper-to-chatGPT")
11
+ import os
12
+ import json
13
+ session_token = os.environ.get('SessionToken')
14
+ #api_endpoint = os.environ.get('API_EndPoint')
15
+ # ChatGPT
16
+ #from revChatGPT.ChatGPT import Chatbot
17
+ #chatbot = Chatbot({"session_token": session_token}) # You can start a custom conversation
18
+ import asyncio
19
+ from pygpt import PyGPT
20
+
21
+ title = "Speech to ChatGPT to Speech"
22
+ #info = "more info at [Neon Coqui TTS Plugin](https://github.com/NeonGeckoCom/neon-tts-plugin-coqui), [Coqui TTS](https://github.com/coqui-ai/TTS)"
23
+ #badge = "https://visitor-badge-reloaded.herokuapp.com/badge?page_id=neongeckocom.neon-tts-plugin-coqui"
24
+ coquiTTS = CoquiTTS()
25
+ chat_id = {'conversation_id': None, 'parent_id': None}
26
+ headers = {'Authorization': 'yusin'}
27
+
28
+ async def chat_gpt_ask(prompt):
29
+ chat_gpt = PyGPT(session_token)
30
+ await chat_gpt.connect()
31
+ await chat_gpt.wait_for_ready()
32
+ answer = await chat_gpt.ask(prompt)
33
+ print(answer)
34
+ await chat_gpt.disconnect()
35
+
36
+ # ChatGPT
37
+ def chat_hf(audio, custom_token, language):
38
+ #output = chatgpt(audio, "transcribe", fn_index=0)
39
+ #whisper_text, gpt_response = output[0], output[1]
40
+ try:
41
+ whisper_text = translate(audio)
42
+ if whisper_text == "ERROR: You have to either use the microphone or upload an audio file":
43
+ gpt_response = "MISSING AUDIO: Record your voice by clicking the microphone button, do not forget to stop recording before sending your message ;)"
44
+ else:
45
+ #gpt_response = chatbot.ask(whisper_text, conversation_id=conversation_id, parent_id=None)
46
+ gpt_response = asyncio.run(chat_gpt_ask(whisper_text, id='yusin'))
47
+ #if chat_id['conversation_id'] != None:
48
+ # data = {"content": whisper_text, "conversation_id": chat_id['conversation_id'], "parent_id": chat_id['parent_id']}
49
+ #else:
50
+ # data = {"content": whisper_text}
51
+ #print(data)
52
+ #res = requests.get('http://myip.ipip.net', timeout=5).text
53
+ #print(res)
54
+ #response = requests.post('api_endpoint', headers=headers, json=data, verify=False, timeout=5)
55
+ #print('this is my answear', response.text)
56
+ #chat_id['parent_id'] = response.json()["response_id"]
57
+ #chat_id['conversation_id'] = response.json()["conversation_id"]
58
+ #gpt_response = response.json()["content"]
59
+ #response = requests.get('https://api.pawan.krd/chat/gpt?text=' + whisper_text + '&cache=false', verify=False, timeout=5)
60
+ #print(response.text)
61
+
62
+ #whisper_text = translate(audio)
63
+ #api = ChatGPT(session_token)
64
+ #resp = api.send_message(whisper_text)
65
+
66
+ #api.refresh_auth() # refresh the authorization token
67
+ #api.reset_conversation() # reset the conversation
68
+ #gpt_response = resp['message']
69
+
70
+ except:
71
+ whisper_text = translate(audio)
72
+ gpt_response = """Sorry, I'm quite busy right now, but please try again later :)"""
73
+ #whisper_text = translate(audio)
74
+ #api = ChatGPT(custom_token)
75
+ #resp = api.send_message(whisper_text)
76
+
77
+ #api.refresh_auth() # refresh the authorization token
78
+ #api.reset_conversation() # reset the conversation
79
+ #gpt_response = resp['message']
80
+
81
+ # to voice
82
+ with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as fp:
83
+ coquiTTS.get_tts(gpt_response, fp, speaker = {"language" : language})
84
+
85
+ return whisper_text, gpt_response, fp.name
86
+
87
+ # whisper
88
+ #def translate(audio):
89
+ # print("""
90
+ # —
91
+ # Sending audio to Whisper ...
92
+ # —
93
+ # """)
94
+ #
95
+ # audio = whisper.load_audio(audio)
96
+ # audio = whisper.pad_or_trim(audio)
97
+ #
98
+ # mel = whisper.log_mel_spectrogram(audio).to(whisper_model.device)
99
+ #
100
+ # _, probs = whisper_model.detect_language(mel)
101
+ #
102
+ # transcript_options = whisper.DecodingOptions(task="transcribe", fp16 = False)
103
+ #
104
+ # transcription = whisper.decode(whisper_model, mel, transcript_options)
105
+ #
106
+ # print("language spoken: " + transcription.language)
107
+ # print("transcript: " + transcription.text)
108
+ # print("———————————————————————————————————————————")
109
+ #
110
+ # return transcription.text
111
+
112
+ def translate(audio):
113
+ print("""
114
+
115
+ Sending audio to Whisper ...
116
+
117
+ """)
118
+
119
+ text_result = whisper(audio, None, "transcribe", fn_index=0)
120
+ #print(text_result)
121
+ return text_result
122
+
123
+
124
+ with gr.Blocks() as blocks:
125
+ gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>"
126
+ + title
127
+ + "</h1>")
128
+ #gr.Markdown(description)
129
+ radio = gr.Radio(label="Language",choices=LANGUAGES,value=default_lang)
130
+ with gr.Row(equal_height=True):# equal_height=False
131
+ with gr.Column():# variant="panel"
132
+ audio_file = gr.Audio(source="microphone",type="filepath")
133
+ custom_token = gr.Textbox(label='If it fails, use your own session token', placeholder="your own session token")
134
+ with gr.Row():# mobile_collapse=False
135
+ submit = gr.Button("Submit", variant="primary")
136
+ with gr.Column():
137
+ text1 = gr.Textbox(label="Speech to Text")
138
+ text2 = gr.Textbox(label="ChatGPT Response")
139
+ audio = gr.Audio(label="Output", interactive=False)
140
+ #gr.Markdown(info)
141
+ #gr.Markdown("<center>"
142
+ # +f'<img src={badge} alt="visitors badge"/>'
143
+ # +"</center>")
144
+
145
+ # actions
146
+ submit.click(
147
+ chat_hf,
148
+ [audio_file, custom_token, radio],
149
+ [text1, text2, audio],
150
+ )
151
+ radio.change(lambda lang: CoquiTTS.langs[lang]["sentence"], radio, text2)
152
+
153
+
154
+ blocks.launch(debug=True)
packages.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ libsndfile1
2
+ espeak-ng
pygpt.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import uuid
2
+ import asyncio
3
+ import socketio
4
+ import datetime
5
+ import json
6
+ import base64
7
+
8
+ class PyGPT:
9
+ def __init__(self, session_token, bypass_node='https://gpt.pawan.krd'):
10
+ self.ready = False
11
+ self.socket = socketio.AsyncClient()
12
+ self.socket.on('connect', self.on_connect)
13
+ self.socket.on('disconnect', self.on_disconnect)
14
+ self.session_token = session_token
15
+ self.conversations = []
16
+ self.auth = None
17
+ self.expires = datetime.datetime.now()
18
+ self.pause_token_checks = False
19
+ self.bypass_node = bypass_node
20
+ asyncio.create_task(self.cleanup_conversations())
21
+
22
+ async def connect(self):
23
+ await self.socket.connect(self.bypass_node)
24
+
25
+ async def disconnect(self):
26
+ await self.socket.disconnect()
27
+
28
+ def on_connect(self):
29
+ print('Connected to server')
30
+ asyncio.create_task(self.check_tokens())
31
+
32
+ def on_disconnect(self):
33
+ print('Disconnected from server')
34
+ self.ready = False
35
+
36
+ async def check_tokens(self):
37
+ while True:
38
+ if self.pause_token_checks:
39
+ await asyncio.sleep(0.5)
40
+ continue
41
+ self.pause_token_checks = True
42
+ now = datetime.datetime.now()
43
+ offset = datetime.timedelta(minutes=2)
44
+ if self.expires < (now - offset) or not self.auth:
45
+ await self.get_tokens()
46
+ self.pause_token_checks = False
47
+ await asyncio.sleep(0.5)
48
+
49
+ async def cleanup_conversations(self):
50
+ while True:
51
+ await asyncio.sleep(60)
52
+ now = datetime.datetime.now()
53
+ self.conversations = [c for c in self.conversations if now - c['last_active'] < datetime.timedelta(minutes=2)]
54
+
55
+ def add_conversation(self, id):
56
+ conversation = {
57
+ 'id': id,
58
+ 'conversation_id': None,
59
+ 'parent_id': uuid.uuid4(),
60
+ 'last_active': datetime.datetime.now()
61
+ }
62
+ self.conversations.append(conversation)
63
+ return conversation
64
+
65
+ def get_conversation_by_id(self, id):
66
+ conversation = next((c for c in self.conversations if c['id'] == id), None)
67
+ if conversation is None:
68
+ conversation = self.add_conversation(id)
69
+ else:
70
+ conversation['last_active'] = datetime.datetime.now()
71
+ return conversation
72
+
73
+ async def wait_for_ready(self):
74
+ while not self.ready:
75
+ await asyncio.sleep(0.025)
76
+ print('Ready!!')
77
+
78
+ async def ask(self, prompt, id='default'):
79
+ if not self.auth or not self.validate_token(self.auth):
80
+ await self.get_tokens()
81
+ conversation = self.get_conversation_by_id(id)
82
+ data = await self.socket.call('askQuestion', {
83
+ 'prompt': prompt,
84
+ 'parentId': str(conversation['parent_id']),
85
+ 'conversationId': str(conversation['conversation_id']),
86
+ 'auth': self.auth
87
+ })
88
+
89
+ if 'error' in data:
90
+ print(f'Error: {data["error"]}')
91
+ conversation['parent_id'] = data['messageId']
92
+ conversation['conversation_id'] = data['conversationId']
93
+ return data['answer']
94
+
95
+ def validate_token(self, token):
96
+ if not token:
97
+ return False
98
+ parsed = json.loads(base64.b64decode(f'{token.split(".")[1]}==').decode())
99
+ return datetime.datetime.now() <= datetime.datetime.fromtimestamp(parsed['exp'])
100
+
101
+ async def get_tokens(self):
102
+ await asyncio.sleep(1)
103
+ data = await self.socket.call('getSession', self.session_token)
104
+
105
+ if 'error' in data:
106
+ print(f'Error getting session: {data["error"]}')
107
+ else:
108
+ self.auth = data['auth']
109
+ self.expires = datetime.datetime.strptime(data['expires'], '%Y-%m-%dT%H:%M:%S.%fZ')
110
+ self.session_token = data['sessionToken']
111
+ self.ready = True
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ neon-tts-plugin-coqui==0.7.0
2
+ python-socketio[asyncio_client]
3
+ #undetected-chromedriver
4
+ #revChatGPT
5
+ #git+https://github.com/openai/whisper.git