Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,30 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from fastapi import FastAPI, UploadFile, File, Form, HTTPException
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
-
import whisper
|
| 7 |
from transformers import pipeline
|
| 8 |
|
| 9 |
-
# ----------------
|
| 10 |
-
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
except PermissionError:
|
| 16 |
-
UPLOAD_DIR = "."
|
| 17 |
|
| 18 |
-
#
|
| 19 |
app = FastAPI()
|
| 20 |
|
|
|
|
| 21 |
app.add_middleware(
|
| 22 |
CORSMiddleware,
|
| 23 |
allow_origins=["*"],
|
|
@@ -26,45 +33,23 @@ app.add_middleware(
|
|
| 26 |
allow_headers=["*"],
|
| 27 |
)
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
|
| 31 |
-
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 32 |
-
|
| 33 |
-
# ---------------- Utils ----------------
|
| 34 |
-
def save_to_csv(audio_file: str, transcription: str, summary: str):
|
| 35 |
-
csv_file = os.path.join(UPLOAD_DIR, "output.csv")
|
| 36 |
-
file_exists = os.path.isfile(csv_file)
|
| 37 |
|
| 38 |
-
with open(csv_file, mode="a", newline="", encoding="utf-8") as f:
|
| 39 |
-
writer = csv.writer(f)
|
| 40 |
-
if not file_exists:
|
| 41 |
-
writer.writerow(["Timestamp", "Audio_File", "Transcription", "Summary"])
|
| 42 |
-
writer.writerow([datetime.now().isoformat(), audio_file, transcription, summary])
|
| 43 |
|
| 44 |
-
# ---------------- Routes ----------------
|
| 45 |
@app.post("/transcribe/")
|
| 46 |
-
async def transcribe_audio(file: UploadFile = File(...)
|
| 47 |
try:
|
| 48 |
-
# Save uploaded file
|
| 49 |
file_path = os.path.join(UPLOAD_DIR, file.filename)
|
| 50 |
-
with open(file_path, "wb") as
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
# Transcribe
|
| 54 |
-
result = asr_model.transcribe(file_path)
|
| 55 |
-
transcription = result["text"]
|
| 56 |
|
| 57 |
-
#
|
| 58 |
-
|
| 59 |
|
| 60 |
-
#
|
| 61 |
-
|
| 62 |
|
| 63 |
-
return {
|
| 64 |
-
"filename": file.filename,
|
| 65 |
-
"transcription": transcription,
|
| 66 |
-
"summary": summary,
|
| 67 |
-
"csv_saved": True
|
| 68 |
-
}
|
| 69 |
except Exception as e:
|
| 70 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
| 1 |
import os
|
| 2 |
+
import time
|
| 3 |
+
import json
|
| 4 |
+
import tempfile
|
| 5 |
+
import threading
|
| 6 |
+
from typing import Optional
|
| 7 |
+
from datetime import datetime, timezone
|
| 8 |
+
|
| 9 |
+
import httpx
|
| 10 |
+
import whisper
|
| 11 |
+
import nltk
|
| 12 |
+
from nltk.tokenize import sent_tokenize
|
| 13 |
from fastapi import FastAPI, UploadFile, File, Form, HTTPException
|
| 14 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 15 |
from transformers import pipeline
|
| 16 |
|
| 17 |
+
# ---------------- Fix Whisper cache issue ----------------
|
| 18 |
+
os.environ["XDG_CACHE_HOME"] = "/tmp/.cache"
|
| 19 |
|
| 20 |
+
# ---------------- Config ----------------
|
| 21 |
+
UPLOAD_DIR = "uploads"
|
| 22 |
+
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
# Initialize FastAPI
|
| 25 |
app = FastAPI()
|
| 26 |
|
| 27 |
+
# Allow CORS for all origins
|
| 28 |
app.add_middleware(
|
| 29 |
CORSMiddleware,
|
| 30 |
allow_origins=["*"],
|
|
|
|
| 33 |
allow_headers=["*"],
|
| 34 |
)
|
| 35 |
|
| 36 |
+
# Load Whisper model (small for speed)
|
| 37 |
+
model = whisper.load_model("small")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
|
|
|
| 40 |
@app.post("/transcribe/")
|
| 41 |
+
async def transcribe_audio(file: UploadFile = File(...)):
|
| 42 |
try:
|
| 43 |
+
# Save uploaded file temporarily
|
| 44 |
file_path = os.path.join(UPLOAD_DIR, file.filename)
|
| 45 |
+
with open(file_path, "wb") as f:
|
| 46 |
+
f.write(await file.read())
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
# Transcribe with Whisper
|
| 49 |
+
result = model.transcribe(file_path)
|
| 50 |
|
| 51 |
+
# Return text output
|
| 52 |
+
return {"filename": file.filename, "transcription": result["text"]}
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
except Exception as e:
|
| 55 |
raise HTTPException(status_code=500, detail=str(e))
|