|
|
import os |
|
|
import time |
|
|
import json |
|
|
import tempfile |
|
|
import threading |
|
|
from typing import Optional |
|
|
from datetime import datetime, timezone |
|
|
|
|
|
import httpx |
|
|
import whisper |
|
|
import nltk |
|
|
from nltk.tokenize import sent_tokenize |
|
|
from fastapi import FastAPI, UploadFile, File, Form, HTTPException |
|
|
from fastapi.middleware.cors import CORSMiddleware |
|
|
from transformers import pipeline |
|
|
|
|
|
|
|
|
os.environ["XDG_CACHE_HOME"] = "/tmp/.cache" |
|
|
|
|
|
|
|
|
UPLOAD_DIR = "uploads" |
|
|
os.makedirs(UPLOAD_DIR, exist_ok=True) |
|
|
|
|
|
|
|
|
app = FastAPI() |
|
|
|
|
|
|
|
|
app.add_middleware( |
|
|
CORSMiddleware, |
|
|
allow_origins=["*"], |
|
|
allow_credentials=True, |
|
|
allow_methods=["*"], |
|
|
allow_headers=["*"], |
|
|
) |
|
|
|
|
|
|
|
|
model = whisper.load_model("small") |
|
|
|
|
|
|
|
|
@app.post("/transcribe/") |
|
|
async def transcribe_audio(file: UploadFile = File(...)): |
|
|
try: |
|
|
|
|
|
file_path = os.path.join(UPLOAD_DIR, file.filename) |
|
|
with open(file_path, "wb") as f: |
|
|
f.write(await file.read()) |
|
|
|
|
|
|
|
|
result = model.transcribe(file_path) |
|
|
|
|
|
|
|
|
return {"filename": file.filename, "transcription": result["text"]} |
|
|
|
|
|
except Exception as e: |
|
|
raise HTTPException(status_code=500, detail=str(e)) |
|
|
|