-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwyoming-forward.py
227 lines (208 loc) · 5.5 KB
/
wyoming-forward.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import argparse
import asyncio
import io
import os
import wave
import aiohttp
from wyoming.server import AsyncEventHandler, AsyncServer, partial
from wyoming.event import Event
from wyoming.audio import AudioChunk, AudioStop
from wyoming.asr import Transcribe, Transcript
from wyoming.info import Describe, Info
from wyoming.info import AsrModel, AsrProgram, Attribution, Info
parser = argparse.ArgumentParser(description="Wyoming Forward to OpenAI API")
parser.add_argument("--wyoming-uri", type=str, default="tcp://0.0.0.0:3001")
args = parser.parse_args()
OPENAI_BASE_URL = os.environ.get("OPENAI_BASE_URL", "https://api.openai.com/v1")
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY", "")
if not OPENAI_API_KEY:
print(
"[Warning] OPENAI_API_KEY is not set. You may need to set it if you are not self-hosting Whisper"
)
# code from https://github.com/rhasspy/wyoming-faster-whisper
class Handler(AsyncEventHandler):
file_obj: io.BytesIO | None = None
wav_file: wave.Wave_write | None = None
lang: str | None = None
async def handle_event(self, event: Event) -> bool:
if AudioChunk.is_type(event.type):
chunk = AudioChunk.from_event(event)
if self.wav_file is None:
print("AudioChunk begin")
self.file_obj = io.BytesIO()
self.wav_file = wave.open(self.file_obj, "wb")
self.wav_file.setframerate(chunk.rate)
self.wav_file.setsampwidth(chunk.width)
self.wav_file.setnchannels(chunk.channels)
self.wav_file.writeframes(chunk.audio)
return True
if AudioStop.is_type(event.type):
print("AudioStop")
assert self.wav_file is not None
assert self.file_obj is not None
self.wav_file.close()
self.wav_file = None
self.file_obj.seek(0)
form = aiohttp.FormData()
form.add_field("file", self.file_obj, filename="audio.wav")
form.add_field("model", "whisper-1")
form.add_field("response_format", "text")
async with aiohttp.ClientSession() as session:
async with session.post(
f"{OPENAI_BASE_URL}/audio/transcriptions",
headers={"Authorization": f"Bearer {OPENAI_API_KEY}"},
data=form,
) as resp:
assert resp.status == 200
text = await resp.text()
print(text)
await self.write_event(Transcript(text=text).event())
self.lang = None
return False
if Transcribe.is_type(event.type):
print("Transcribe")
transcribe = Transcribe.from_event(event)
if transcribe.language:
self.lang = transcribe.language
return True
if Describe.is_type(event.type):
print("Describe")
await self.write_event(
Info(
asr=[
AsrProgram(
name="whisper-forward",
description="Whisper forward to OpenAI API endpoint",
attribution=Attribution(
name="heimoshuiyu",
url="https://github.com/heimoshuiyu/whisper-fastapi",
),
installed=True,
version="0.1",
models=[
AsrModel(
name="whisper-1",
description="whisper-1",
attribution=Attribution(
name="Systran",
url="https://huggingface.co/Systran",
),
installed=True,
languages=_LANGUAGE_CODES, # pylint: disable=protected-access
version="0.1",
)
],
)
],
).event()
)
return True
return True
_LANGUAGE_CODES = [
"af",
"am",
"ar",
"as",
"az",
"ba",
"be",
"bg",
"bn",
"bo",
"br",
"bs",
"ca",
"cs",
"cy",
"da",
"de",
"el",
"en",
"es",
"et",
"eu",
"fa",
"fi",
"fo",
"fr",
"gl",
"gu",
"ha",
"haw",
"he",
"hi",
"hr",
"ht",
"hu",
"hy",
"id",
"is",
"it",
"ja",
"jw",
"ka",
"kk",
"km",
"kn",
"ko",
"la",
"lb",
"ln",
"lo",
"lt",
"lv",
"mg",
"mi",
"mk",
"ml",
"mn",
"mr",
"ms",
"mt",
"my",
"ne",
"nl",
"nn",
"no",
"oc",
"pa",
"pl",
"ps",
"pt",
"ro",
"ru",
"sa",
"sd",
"si",
"sk",
"sl",
"sn",
"so",
"sq",
"sr",
"su",
"sv",
"sw",
"ta",
"te",
"tg",
"th",
"tk",
"tl",
"tr",
"tt",
"uk",
"ur",
"uz",
"vi",
"yi",
"yo",
"zh",
"yue",
]
async def wyoming_server(uri: str):
server = AsyncServer.from_uri(uri)
print(f"Running wyoming server on {uri}")
await server.run(partial(Handler))
if __name__ == "__main__":
asyncio.run(wyoming_server(args.wyoming_uri))