Spaces:
Running
Running
File size: 2,085 Bytes
f7ef14e 6d6caf2 f7ef14e 6d6caf2 f7ef14e a3b77b9 f7ef14e 6d6caf2 f7ef14e 6d6caf2 f7ef14e 6d6caf2 a3b77b9 6d6caf2 a3b77b9 6d6caf2 a3b77b9 6d6caf2 a3b77b9 6d6caf2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
from fastapi import FastAPI, File, UploadFile, Form
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
import tempfile, subprocess, whisper, os
# Set writable cache dir
os.environ["XDG_CACHE_HOME"] = "/tmp"
# Instantiate app first
app = FastAPI()
# CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"]
)
# Serve static frontend files
app.mount("/", StaticFiles(directory="static", html=True), name="static")
# Load whisper model
model = whisper.load_model("base")
@app.post("/api/analyze")
async def analyze(file: UploadFile = File(None), url: str = Form(None)):
if not file and not url:
return JSONResponse({"error": "No input provided"}, status_code=400)
# Download or save uploaded file
if url:
tmp = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4')
subprocess.run(["yt-dlp", "-o", tmp.name, url], check=True)
path = tmp.name
else:
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=file.filename)
tmp.write(await file.read())
tmp.close()
path = tmp.name
# Extract audio using ffmpeg
wav_path = path + ".wav"
subprocess.run(["ffmpeg", "-y", "-i", path, "-ar", "16000", wav_path],
stderr=subprocess.DEVNULL, stdout=subprocess.DEVNULL)
# Transcribe and detect cues
result = model.transcribe(wav_path)
transcript = [seg["text"].strip() for seg in result["segments"]]
flags = []
for seg in result["segments"]:
if "buy" in seg["text"].lower() and seg["avg_logprob"] < -1:
flags.append({
"type": "keyword_lowprob",
"timestamp": f"{seg['start']:.02f}s",
"content": seg['text']
})
summary = "No obvious subliminals" if not flags else "⚠ Potential subliminal cues found"
return {
"summary": summary,
"transcript": transcript,
"subliminal_flags": flags
}
|