Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -419,12 +419,13 @@ async def chat(
|
|
| 419 |
# ----------------------------
|
| 420 |
# Speech-to-Text (STT) Endpoint
|
| 421 |
# ----------------------------
|
| 422 |
-
from
|
|
|
|
|
|
|
| 423 |
import tempfile
|
| 424 |
import shutil
|
| 425 |
import os
|
| 426 |
-
|
| 427 |
-
openai_client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 428 |
|
| 429 |
@app.post("/api/stt")
|
| 430 |
async def speech_to_text(
|
|
@@ -432,42 +433,72 @@ async def speech_to_text(
|
|
| 432 |
session_id: str = Form(...)
|
| 433 |
):
|
| 434 |
"""
|
| 435 |
-
Speech-to-Text using
|
| 436 |
-
Uses OPENAI_API_KEY from environment
|
| 437 |
"""
|
| 438 |
try:
|
| 439 |
-
|
|
|
|
|
|
|
|
|
|
| 440 |
return JSONResponse(
|
| 441 |
-
{"success": False, "message": "
|
| 442 |
status_code=500
|
| 443 |
)
|
| 444 |
|
| 445 |
-
# Save audio temporarily
|
| 446 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".webm") as tmp:
|
| 447 |
audio_path = tmp.name
|
| 448 |
shutil.copyfileobj(audio.file, tmp)
|
| 449 |
|
| 450 |
try:
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 456 |
|
| 457 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 458 |
|
| 459 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 460 |
return JSONResponse(
|
| 461 |
-
{"success": False, "message": "No speech
|
| 462 |
status_code=400
|
| 463 |
)
|
| 464 |
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
|
|
|
|
|
|
| 471 |
|
| 472 |
finally:
|
| 473 |
if os.path.exists(audio_path):
|
|
|
|
| 419 |
# ----------------------------
|
| 420 |
# Speech-to-Text (STT) Endpoint
|
| 421 |
# ----------------------------
|
| 422 |
+
from fastapi import UploadFile, File, Form
|
| 423 |
+
from fastapi.responses import JSONResponse
|
| 424 |
+
import azure.cognitiveservices.speech as speechsdk
|
| 425 |
import tempfile
|
| 426 |
import shutil
|
| 427 |
import os
|
| 428 |
+
import traceback
|
|
|
|
| 429 |
|
| 430 |
@app.post("/api/stt")
|
| 431 |
async def speech_to_text(
|
|
|
|
| 433 |
session_id: str = Form(...)
|
| 434 |
):
|
| 435 |
"""
|
| 436 |
+
Speech-to-Text using Azure Cognitive Services
|
|
|
|
| 437 |
"""
|
| 438 |
try:
|
| 439 |
+
speech_key = os.getenv("AZURE_SPEECH_KEY")
|
| 440 |
+
speech_region = os.getenv("AZURE_SPEECH_REGION")
|
| 441 |
+
|
| 442 |
+
if not speech_key or not speech_region:
|
| 443 |
return JSONResponse(
|
| 444 |
+
{"success": False, "message": "Azure Speech credentials not set"},
|
| 445 |
status_code=500
|
| 446 |
)
|
| 447 |
|
| 448 |
+
# Save uploaded audio temporarily
|
| 449 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".webm") as tmp:
|
| 450 |
audio_path = tmp.name
|
| 451 |
shutil.copyfileobj(audio.file, tmp)
|
| 452 |
|
| 453 |
try:
|
| 454 |
+
# Azure Speech config
|
| 455 |
+
speech_config = speechsdk.SpeechConfig(
|
| 456 |
+
subscription=speech_key,
|
| 457 |
+
region=speech_region
|
| 458 |
+
)
|
| 459 |
+
|
| 460 |
+
speech_config.speech_recognition_language = "en-US"
|
| 461 |
+
|
| 462 |
+
# Azure prefers WAV, but WEBM works if codec is supported
|
| 463 |
+
audio_input = speechsdk.audio.AudioConfig(filename=audio_path)
|
| 464 |
|
| 465 |
+
recognizer = speechsdk.SpeechRecognizer(
|
| 466 |
+
speech_config=speech_config,
|
| 467 |
+
audio_config=audio_input
|
| 468 |
+
)
|
| 469 |
+
|
| 470 |
+
result = recognizer.recognize_once()
|
| 471 |
+
|
| 472 |
+
if result.reason == speechsdk.ResultReason.RecognizedSpeech:
|
| 473 |
+
text = result.text.strip()
|
| 474 |
+
|
| 475 |
+
if not text:
|
| 476 |
+
return JSONResponse(
|
| 477 |
+
{"success": False, "message": "No speech detected"},
|
| 478 |
+
status_code=400
|
| 479 |
+
)
|
| 480 |
|
| 481 |
+
return {
|
| 482 |
+
"success": True,
|
| 483 |
+
"text": text,
|
| 484 |
+
"result": text,
|
| 485 |
+
"transcription": text
|
| 486 |
+
}
|
| 487 |
+
|
| 488 |
+
elif result.reason == speechsdk.ResultReason.NoMatch:
|
| 489 |
return JSONResponse(
|
| 490 |
+
{"success": False, "message": "No speech could be recognized"},
|
| 491 |
status_code=400
|
| 492 |
)
|
| 493 |
|
| 494 |
+
else:
|
| 495 |
+
return JSONResponse(
|
| 496 |
+
{
|
| 497 |
+
"success": False,
|
| 498 |
+
"message": f"Azure STT failed: {result.reason}"
|
| 499 |
+
},
|
| 500 |
+
status_code=500
|
| 501 |
+
)
|
| 502 |
|
| 503 |
finally:
|
| 504 |
if os.path.exists(audio_path):
|