Spaces:
Runtime error
Runtime error
| #uvicorn app:app --host 0.0.0.0 --port 8000 --reload | |
| from fastapi import FastAPI, UploadFile | |
| import librosa | |
| from fastapi.responses import HTMLResponse | |
| import io | |
| app = FastAPI() | |
| from faster_whisper import WhisperModel | |
| model_size = "medium" | |
| ts_model = WhisperModel(model_size, device = "cpu", compute_type = "int8") | |
| def read_root(): | |
| html_form = """ | |
| <html> | |
| <body> | |
| <h2>Audio Transcription</h2> | |
| <form action="/transcribe" method="post" enctype="multipart/form-data"> | |
| <label for="audio_file">Upload an audio file (MP3 or WAV):</label> | |
| <input type="file" id="audio_file" name="audio_file" accept=".mp3, .wav" required><br><br> | |
| <input type="submit" value="Transcribe"> | |
| </form> | |
| </body> | |
| </html> | |
| """ | |
| return HTMLResponse(content=html_form, status_code=200) | |
| async def transcribe_audio(audio_file: UploadFile): | |
| audio_data = await audio_file.read() | |
| audio_data, _ = librosa.load(io.BytesIO(audio_data), sr=16000) | |
| segments, _ = ts_model.transcribe(audio_data) | |
| lst = '' | |
| for segment in segments: | |
| lst = lst + segment.text | |
| return lst |