Spaces:
Paused
Paused
feat: lang detector from file by api
Browse files
main.py
CHANGED
@@ -59,12 +59,21 @@ async def health_check():
|
|
59 |
@app.post("/detect-language")
|
60 |
async def detect_language(file: UploadFile = File(...)):
|
61 |
try:
|
62 |
-
#
|
63 |
-
|
64 |
|
65 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
if language_detector:
|
67 |
-
detected_lang, confidence = language_detector.
|
|
|
|
|
|
|
68 |
|
69 |
return JSONResponse({
|
70 |
"language": detected_lang,
|
@@ -79,6 +88,9 @@ async def detect_language(file: UploadFile = File(...)):
|
|
79 |
except Exception as e:
|
80 |
logger.error(f"Error in language detection: {e}")
|
81 |
logger.error(f"Traceback: {traceback.format_exc()}")
|
|
|
|
|
|
|
82 |
return JSONResponse(
|
83 |
{"error": str(e)},
|
84 |
status_code=500
|
@@ -133,177 +145,33 @@ async def websocket_endpoint(websocket: WebSocket):
|
|
133 |
logger.debug(f"Received audio chunk of size: {len(message)}")
|
134 |
await audio_processor.process_audio(message)
|
135 |
except WebSocketDisconnect:
|
136 |
-
logger.
|
137 |
break
|
138 |
except Exception as e:
|
139 |
-
logger.error(f"Error processing
|
140 |
logger.error(f"Traceback: {traceback.format_exc()}")
|
141 |
break
|
142 |
|
143 |
-
except WebSocketDisconnect:
|
144 |
-
logger.warning("WebSocket disconnected during setup.")
|
145 |
except Exception as e:
|
146 |
logger.error(f"Error in WebSocket endpoint: {e}")
|
147 |
logger.error(f"Traceback: {traceback.format_exc()}")
|
148 |
finally:
|
|
|
|
|
149 |
if websocket_task:
|
150 |
websocket_task.cancel()
|
151 |
try:
|
152 |
await websocket_task
|
153 |
except asyncio.CancelledError:
|
154 |
pass
|
155 |
-
if audio_processor:
|
156 |
-
await audio_processor.cleanup()
|
157 |
-
logger.info("WebSocket endpoint cleaned up.")
|
158 |
-
|
159 |
-
def parse_args():
|
160 |
-
parser = argparse.ArgumentParser(description="Whisper FastAPI Online Server")
|
161 |
-
parser.add_argument(
|
162 |
-
"--host",
|
163 |
-
type=str,
|
164 |
-
default="localhost",
|
165 |
-
help="The host address to bind the server to.",
|
166 |
-
)
|
167 |
-
parser.add_argument(
|
168 |
-
"--port", type=int, default=8000, help="The port number to bind the server to."
|
169 |
-
)
|
170 |
-
parser.add_argument(
|
171 |
-
"--warmup-file",
|
172 |
-
type=str,
|
173 |
-
default=None,
|
174 |
-
dest="warmup_file",
|
175 |
-
help="""
|
176 |
-
The path to a speech audio wav file to warm up Whisper so that the very first chunk processing is fast.
|
177 |
-
If not set, uses https://github.com/ggerganov/whisper.cpp/raw/master/samples/jfk.wav.
|
178 |
-
If False, no warmup is performed.
|
179 |
-
""",
|
180 |
-
)
|
181 |
-
|
182 |
-
parser.add_argument(
|
183 |
-
"--confidence-validation",
|
184 |
-
action="store_true",
|
185 |
-
help="Accelerates validation of tokens using confidence scores. Transcription will be faster but punctuation might be less accurate.",
|
186 |
-
)
|
187 |
-
|
188 |
-
parser.add_argument(
|
189 |
-
"--diarization",
|
190 |
-
action="store_true",
|
191 |
-
default=False,
|
192 |
-
help="Enable speaker diarization.",
|
193 |
-
)
|
194 |
-
|
195 |
-
parser.add_argument(
|
196 |
-
"--no-transcription",
|
197 |
-
action="store_true",
|
198 |
-
help="Disable transcription to only see live diarization results.",
|
199 |
-
)
|
200 |
-
|
201 |
-
parser.add_argument(
|
202 |
-
"--min-chunk-size",
|
203 |
-
type=float,
|
204 |
-
default=0.5,
|
205 |
-
help="Minimum audio chunk size in seconds. It waits up to this time to do processing. If the processing takes shorter time, it waits, otherwise it processes the whole segment that was received by this time.",
|
206 |
-
)
|
207 |
-
|
208 |
-
parser.add_argument(
|
209 |
-
"--model",
|
210 |
-
type=str,
|
211 |
-
default="tiny",
|
212 |
-
help="Name size of the Whisper model to use (default: tiny). Suggested values: tiny.en,tiny,base.en,base,small.en,small,medium.en,medium,large-v1,large-v2,large-v3,large,large-v3-turbo. The model is automatically downloaded from the model hub if not present in model cache dir.",
|
213 |
-
)
|
214 |
-
|
215 |
-
parser.add_argument(
|
216 |
-
"--model_cache_dir",
|
217 |
-
type=str,
|
218 |
-
default=None,
|
219 |
-
help="Overriding the default model cache dir where models downloaded from the hub are saved",
|
220 |
-
)
|
221 |
-
parser.add_argument(
|
222 |
-
"--model_dir",
|
223 |
-
type=str,
|
224 |
-
default=None,
|
225 |
-
help="Dir where Whisper model.bin and other files are saved. This option overrides --model and --model_cache_dir parameter.",
|
226 |
-
)
|
227 |
-
parser.add_argument(
|
228 |
-
"--lan",
|
229 |
-
"--language",
|
230 |
-
type=str,
|
231 |
-
default="en",
|
232 |
-
help="Source language code, e.g. en,de,cs, or 'auto' for language detection.",
|
233 |
-
)
|
234 |
-
parser.add_argument(
|
235 |
-
"--task",
|
236 |
-
type=str,
|
237 |
-
default="transcribe",
|
238 |
-
choices=["transcribe", "translate"],
|
239 |
-
help="Transcribe or translate.",
|
240 |
-
)
|
241 |
-
parser.add_argument(
|
242 |
-
"--backend",
|
243 |
-
type=str,
|
244 |
-
default="faster-whisper",
|
245 |
-
choices=["faster-whisper", "whisper_timestamped", "mlx-whisper", "openai-api"],
|
246 |
-
help="Load only this backend for Whisper processing.",
|
247 |
-
)
|
248 |
-
parser.add_argument(
|
249 |
-
"--vac",
|
250 |
-
action="store_true",
|
251 |
-
default=False,
|
252 |
-
help="Use VAC = voice activity controller. Recommended. Requires torch.",
|
253 |
-
)
|
254 |
-
parser.add_argument(
|
255 |
-
"--vac-chunk-size", type=float, default=0.04, help="VAC sample size in seconds."
|
256 |
-
)
|
257 |
-
parser.add_argument(
|
258 |
-
"--no-vad",
|
259 |
-
action="store_true",
|
260 |
-
default=False,
|
261 |
-
help="Disable VAD = voice activity detection. Not recommended.",
|
262 |
-
)
|
263 |
-
parser.add_argument(
|
264 |
-
"--buffer_trimming",
|
265 |
-
type=str,
|
266 |
-
default="sentence",
|
267 |
-
choices=["sentence", "segment"],
|
268 |
-
help="Buffer trimming strategy.",
|
269 |
-
)
|
270 |
-
parser.add_argument(
|
271 |
-
"--buffer_trimming_sec",
|
272 |
-
type=float,
|
273 |
-
default=1.0,
|
274 |
-
help="Buffer trimming length in seconds.",
|
275 |
-
)
|
276 |
-
parser.add_argument(
|
277 |
-
"-l",
|
278 |
-
"--log-level",
|
279 |
-
type=str,
|
280 |
-
default="INFO",
|
281 |
-
choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
|
282 |
-
help="Set the logging level.",
|
283 |
-
)
|
284 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
285 |
args = parser.parse_args()
|
286 |
-
|
287 |
-
args.transcription = not args.no_transcription
|
288 |
-
args.vad = not args.no_vad
|
289 |
-
delattr(args, 'no_transcription')
|
290 |
-
delattr(args, 'no_vad')
|
291 |
-
|
292 |
-
return args
|
293 |
-
|
294 |
-
def main():
|
295 |
-
args = parse_args()
|
296 |
-
|
297 |
-
# Initialize WhisperLiveKit with parsed arguments
|
298 |
-
kit = WhisperLiveKit(args=args)
|
299 |
-
|
300 |
-
# Start the server
|
301 |
-
uvicorn.run(
|
302 |
-
"main:app",
|
303 |
-
host=args.host,
|
304 |
-
port=args.port,
|
305 |
-
log_level=args.log_level.lower()
|
306 |
-
)
|
307 |
|
308 |
-
|
309 |
-
main()
|
|
|
59 |
@app.post("/detect-language")
|
60 |
async def detect_language(file: UploadFile = File(...)):
|
61 |
try:
|
62 |
+
# Create uploads directory if it doesn't exist
|
63 |
+
os.makedirs("uploads", exist_ok=True)
|
64 |
|
65 |
+
# Save the uploaded file
|
66 |
+
file_path = os.path.join("uploads", file.filename)
|
67 |
+
with open(file_path, "wb") as buffer:
|
68 |
+
contents = await file.read()
|
69 |
+
buffer.write(contents)
|
70 |
+
|
71 |
+
# Use the language detector with the saved file
|
72 |
if language_detector:
|
73 |
+
detected_lang, confidence = language_detector.detect_language_from_file(file_path)
|
74 |
+
|
75 |
+
# Clean up - remove the temporary file
|
76 |
+
os.remove(file_path)
|
77 |
|
78 |
return JSONResponse({
|
79 |
"language": detected_lang,
|
|
|
88 |
except Exception as e:
|
89 |
logger.error(f"Error in language detection: {e}")
|
90 |
logger.error(f"Traceback: {traceback.format_exc()}")
|
91 |
+
# Clean up in case of error
|
92 |
+
if 'file_path' in locals() and os.path.exists(file_path):
|
93 |
+
os.remove(file_path)
|
94 |
return JSONResponse(
|
95 |
{"error": str(e)},
|
96 |
status_code=500
|
|
|
145 |
logger.debug(f"Received audio chunk of size: {len(message)}")
|
146 |
await audio_processor.process_audio(message)
|
147 |
except WebSocketDisconnect:
|
148 |
+
logger.info("WebSocket connection closed")
|
149 |
break
|
150 |
except Exception as e:
|
151 |
+
logger.error(f"Error processing WebSocket message: {e}")
|
152 |
logger.error(f"Traceback: {traceback.format_exc()}")
|
153 |
break
|
154 |
|
|
|
|
|
155 |
except Exception as e:
|
156 |
logger.error(f"Error in WebSocket endpoint: {e}")
|
157 |
logger.error(f"Traceback: {traceback.format_exc()}")
|
158 |
finally:
|
159 |
+
if audio_processor:
|
160 |
+
await audio_processor.cleanup()
|
161 |
if websocket_task:
|
162 |
websocket_task.cancel()
|
163 |
try:
|
164 |
await websocket_task
|
165 |
except asyncio.CancelledError:
|
166 |
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
167 |
|
168 |
+
if __name__ == "__main__":
|
169 |
+
parser = argparse.ArgumentParser()
|
170 |
+
parser.add_argument("--host", type=str, default="0.0.0.0", help="Host to run the server on")
|
171 |
+
parser.add_argument("--port", type=int, default=8000, help="Port to run the server on")
|
172 |
+
parser.add_argument("--model", type=str, default="base", help="Whisper model to use")
|
173 |
+
parser.add_argument("--backend", type=str, default="faster-whisper", help="Backend to use")
|
174 |
+
parser.add_argument("--task", type=str, default="transcribe", help="Task to perform")
|
175 |
args = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
176 |
|
177 |
+
uvicorn.run(app, host=args.host, port=args.port)
|
|