kotoba_whisper / main.py
aka7774's picture
Create main.py
de95cc5 verified
raw
history blame
964 Bytes
import os
import sys
import time
import signal
import psutil
import io
from fastapi import FastAPI, Request, status, Form, UploadFile
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, Field
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
import fn
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=['*'],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
fn.load_model('large-v3')
@app.post("/transcribe")
async def transcribe_audio(file: UploadFile = Form(...)):
try:
file_content = await file.read()
file_stream = io.BytesIO(file_content)
text_only, text_with_timestamps = speech_to_text(file_stream)
return {"transcription": text_only, "text_with_timestamps": text_with_timestamps}
except Exception as e:
return {"error": str(e)}