Update app.py
Browse files
app.py
CHANGED
|
@@ -7,8 +7,8 @@ import os
|
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
-
# β
|
| 11 |
-
#
|
| 12 |
asr = pipeline(
|
| 13 |
"automatic-speech-recognition",
|
| 14 |
model="openai/whisper-base",
|
|
@@ -17,14 +17,14 @@ asr = pipeline(
|
|
| 17 |
|
| 18 |
@app.post("/predict")
|
| 19 |
async def predict(file: UploadFile = File(...)):
|
| 20 |
-
input_path = "/tmp/
|
| 21 |
-
wav_path = "/tmp/
|
| 22 |
|
| 23 |
# Save uploaded file
|
| 24 |
with open(input_path, "wb") as f:
|
| 25 |
f.write(await file.read())
|
| 26 |
|
| 27 |
-
# Convert
|
| 28 |
subprocess.run([
|
| 29 |
"ffmpeg", "-y", "-i", input_path,
|
| 30 |
"-ac", "1", "-ar", "16000", wav_path
|
|
@@ -34,19 +34,22 @@ async def predict(file: UploadFile = File(...)):
|
|
| 34 |
waveform, sr = torchaudio.load(wav_path)
|
| 35 |
waveform = waveform.to(torch.float32)
|
| 36 |
|
| 37 |
-
# β
|
|
|
|
| 38 |
result = asr(
|
| 39 |
{"array": waveform[0].numpy(), "sampling_rate": sr},
|
| 40 |
-
generate_kwargs={
|
|
|
|
|
|
|
|
|
|
| 41 |
)
|
| 42 |
|
| 43 |
-
# Cleanup
|
| 44 |
os.remove(input_path)
|
| 45 |
os.remove(wav_path)
|
| 46 |
|
| 47 |
-
# β
Return text + detected language
|
| 48 |
return {
|
| 49 |
-
"text": result["text"],
|
| 50 |
-
"language": result.get("language", "auto
|
| 51 |
-
"note": "Auto language detection enabled
|
| 52 |
}
|
|
|
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
+
# β
Multilingual model (better Hindi-English support than tiny)
|
| 11 |
+
# You can switch to "openai/whisper-small" for even better accuracy if your container allows.
|
| 12 |
asr = pipeline(
|
| 13 |
"automatic-speech-recognition",
|
| 14 |
model="openai/whisper-base",
|
|
|
|
| 17 |
|
| 18 |
@app.post("/predict")
|
| 19 |
async def predict(file: UploadFile = File(...)):
|
| 20 |
+
input_path = "/tmp/input_audio.webm"
|
| 21 |
+
wav_path = "/tmp/input_audio.wav"
|
| 22 |
|
| 23 |
# Save uploaded file
|
| 24 |
with open(input_path, "wb") as f:
|
| 25 |
f.write(await file.read())
|
| 26 |
|
| 27 |
+
# Convert to 16 kHz mono WAV β ensures consistency
|
| 28 |
subprocess.run([
|
| 29 |
"ffmpeg", "-y", "-i", input_path,
|
| 30 |
"-ac", "1", "-ar", "16000", wav_path
|
|
|
|
| 34 |
waveform, sr = torchaudio.load(wav_path)
|
| 35 |
waveform = waveform.to(torch.float32)
|
| 36 |
|
| 37 |
+
# β
Transcribe with automatic language detection
|
| 38 |
+
# The 'task': 'transcribe' ensures Whisper writes what it hears, no translation.
|
| 39 |
result = asr(
|
| 40 |
{"array": waveform[0].numpy(), "sampling_rate": sr},
|
| 41 |
+
generate_kwargs={
|
| 42 |
+
"task": "transcribe", # disables translation
|
| 43 |
+
"language": None # auto-detect language
|
| 44 |
+
}
|
| 45 |
)
|
| 46 |
|
| 47 |
+
# Cleanup temp files
|
| 48 |
os.remove(input_path)
|
| 49 |
os.remove(wav_path)
|
| 50 |
|
|
|
|
| 51 |
return {
|
| 52 |
+
"text": result["text"].strip(),
|
| 53 |
+
"language": result.get("language", "auto"),
|
| 54 |
+
"note": "Auto language detection enabled. Optimized for Hindi + English speech."
|
| 55 |
}
|