Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI, UploadFile, File, HTTPException | |
| from fastapi.responses import PlainTextResponse | |
| from fastapi.middleware.gzip import GZipMiddleware | |
| from recognizer_tunisian_vosk import RecognizerTunisianVosk | |
| from pydub import AudioSegment | |
| import shutil | |
| import os | |
| app = FastAPI() | |
| app.add_middleware(GZipMiddleware, minimum_size=1000) | |
| vosk_recognizer = RecognizerTunisianVosk() | |
| TEMP_RAW = "temp_input" | |
| TEMP_WAV = "temp.wav" | |
| def read_root(): | |
| return {"message": "Audio Transcription API is running."} | |
| async def transcribe_tunisian(file: UploadFile = File(...)): | |
| try: | |
| # Save uploaded file | |
| with open(TEMP_RAW, "wb") as buffer: | |
| shutil.copyfileobj(file.file, buffer) | |
| # Convert to correct format using pydub | |
| audio = AudioSegment.from_file(TEMP_RAW) | |
| audio = audio.set_channels(1).set_frame_rate(16000) | |
| audio.export(TEMP_WAV, format="wav") | |
| # Transcribe | |
| text = vosk_recognizer.transcribe(TEMP_WAV) | |
| return text | |
| except Exception as e: | |
| raise HTTPException(status_code=400, detail=f"Audio processing error: {str(e)}") | |
| finally: | |
| for path in [TEMP_RAW, TEMP_WAV]: | |
| if os.path.exists(path): | |
| os.remove(path) |