Update main.py
Browse files
main.py
CHANGED
|
@@ -1,11 +1,13 @@
|
|
| 1 |
-
# main.py
|
| 2 |
-
|
| 3 |
from fastapi import FastAPI, UploadFile, File
|
| 4 |
from fastapi.responses import JSONResponse
|
| 5 |
from transformers import pipeline
|
| 6 |
import traceback
|
| 7 |
import re
|
| 8 |
import uvicorn
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
app = FastAPI(title="Tacab ASR Somali API")
|
| 11 |
|
|
@@ -23,7 +25,7 @@ asr = pipeline(
|
|
| 23 |
# Auto punctuation
|
| 24 |
def auto_punctuate(text):
|
| 25 |
text = text.strip()
|
| 26 |
-
|
| 27 |
def capitalize_sentences(text):
|
| 28 |
sentences = re.split(r'(?<=[.?!])\s+', text)
|
| 29 |
return '. '.join(s.strip().capitalize() for s in sentences if s)
|
|
@@ -39,23 +41,19 @@ def auto_punctuate(text):
|
|
| 39 |
|
| 40 |
return capitalize_sentences(new_text.strip())
|
| 41 |
|
| 42 |
-
@app.post("/transcribe")
|
| 43 |
async def transcribe(file: UploadFile = File(...)):
|
| 44 |
try:
|
| 45 |
-
# Save the uploaded file temporarily
|
| 46 |
temp_path = f"/tmp/{file.filename}"
|
| 47 |
with open(temp_path, "wb") as f:
|
| 48 |
f.write(await file.read())
|
| 49 |
|
| 50 |
-
# Transcribe
|
| 51 |
result = asr(temp_path)
|
| 52 |
raw_text = result.get("text", "").strip()
|
| 53 |
if not raw_text:
|
| 54 |
return JSONResponse({"error": "No transcription result."}, status_code=400)
|
| 55 |
|
| 56 |
-
# Punctuate
|
| 57 |
cleaned_text = auto_punctuate(raw_text)
|
| 58 |
-
|
| 59 |
return {"transcription": cleaned_text}
|
| 60 |
|
| 61 |
except Exception as e:
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI, UploadFile, File
|
| 2 |
from fastapi.responses import JSONResponse
|
| 3 |
from transformers import pipeline
|
| 4 |
import traceback
|
| 5 |
import re
|
| 6 |
import uvicorn
|
| 7 |
+
import torchaudio
|
| 8 |
+
|
| 9 |
+
# ✅ Force torchaudio to use the soundfile backend
|
| 10 |
+
torchaudio.set_audio_backend("soundfile")
|
| 11 |
|
| 12 |
app = FastAPI(title="Tacab ASR Somali API")
|
| 13 |
|
|
|
|
| 25 |
# Auto punctuation
|
| 26 |
def auto_punctuate(text):
|
| 27 |
text = text.strip()
|
| 28 |
+
|
| 29 |
def capitalize_sentences(text):
|
| 30 |
sentences = re.split(r'(?<=[.?!])\s+', text)
|
| 31 |
return '. '.join(s.strip().capitalize() for s in sentences if s)
|
|
|
|
| 41 |
|
| 42 |
return capitalize_sentences(new_text.strip())
|
| 43 |
|
| 44 |
+
@app.post("/transcribe", tags=["ASR"], operation_id="generate")
|
| 45 |
async def transcribe(file: UploadFile = File(...)):
|
| 46 |
try:
|
|
|
|
| 47 |
temp_path = f"/tmp/{file.filename}"
|
| 48 |
with open(temp_path, "wb") as f:
|
| 49 |
f.write(await file.read())
|
| 50 |
|
|
|
|
| 51 |
result = asr(temp_path)
|
| 52 |
raw_text = result.get("text", "").strip()
|
| 53 |
if not raw_text:
|
| 54 |
return JSONResponse({"error": "No transcription result."}, status_code=400)
|
| 55 |
|
|
|
|
| 56 |
cleaned_text = auto_punctuate(raw_text)
|
|
|
|
| 57 |
return {"transcription": cleaned_text}
|
| 58 |
|
| 59 |
except Exception as e:
|