Spaces:
Runtime error
Runtime error
| import os | |
| # Set cache directory to a writable folder inside the app directory | |
| CACHE_DIR = os.path.join(os.getcwd(), ".cache") | |
| os.environ["XDG_CACHE_HOME"] = CACHE_DIR | |
| os.environ["TRANSFORMERS_CACHE"] = CACHE_DIR | |
| os.environ["HF_HOME"] = CACHE_DIR | |
| # Create the cache directory if it doesn't exist | |
| os.makedirs(CACHE_DIR, exist_ok=True) | |
| from fastapi import FastAPI | |
| from fastapi.responses import JSONResponse | |
| import whisper | |
| from transformers import pipeline | |
| import subprocess | |
| # Check ffmpeg | |
| def check_ffmpeg(): | |
| try: | |
| subprocess.run(["ffmpeg", "-version"], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) | |
| except Exception: | |
| raise RuntimeError("ffmpeg is not installed or not found in PATH. It is required for Whisper to process audio/video.") | |
| check_ffmpeg() | |
| app = FastAPI(title="Auto Audio/Video Listener API") | |
| # Load models AFTER setting cache environment variables | |
| asr_model = whisper.load_model("base") | |
| summarizer = pipeline("summarization", model="facebook/bart-large-cnn") | |
| AUDIO_FILE_PATH = "./uploads/input_media.mp4" | |
| async def auto_summarize(): | |
| try: | |
| if not os.path.exists(AUDIO_FILE_PATH): | |
| return JSONResponse(status_code=404, content={"error": "Media file not found."}) | |
| result = asr_model.transcribe(AUDIO_FILE_PATH) | |
| transcription = result.get("text", "") | |
| summary = summarizer( | |
| transcription, max_length=150, min_length=30, do_sample=False | |
| )[0]['summary_text'] | |
| return JSONResponse(content={"transcription": transcription, "summary": summary}) | |
| except Exception as e: | |
| return JSONResponse(status_code=500, content={"error": str(e)}) | |