Spaces:
Paused
Paused
| import os | |
| import requests | |
| import tempfile | |
| import uvicorn | |
| from fastapi import FastAPI, HTTPException, Query | |
| from faster_whisper import WhisperModel | |
| # 1. Inisialisasi FastAPI | |
| app = FastAPI(title="Open Whisper API (No Limits)") | |
| # 2. Inisialisasi Model (Base sudah cukup akurat dan cepat) | |
| # Gunakan device="cuda" jika server punya GPU NVIDIA | |
| model_size = "base" | |
| print(f"Loading Whisper model '{model_size}'...") | |
| model = WhisperModel(model_size, device="cpu", compute_type="int8") | |
| def home(): | |
| return {"status": "API Active", "usage": "/generate?url=YOUR_AUDIO_URL"} | |
| async def generate_transcription(url: str = Query(..., description="URL file audio (mp3, wav, m4a, dll)")): | |
| """ | |
| Menerima URL audio, mendownloadnya, dan mengembalikan teks hasil transkripsi. | |
| """ | |
| tmp_path = None | |
| try: | |
| # 3. Download file dari URL ke folder temp | |
| suffix = ".wav" # Default suffix | |
| if "." in url: | |
| potential_ext = "." + url.split(".")[-1].split("?")[0] | |
| if len(potential_ext) <= 5: | |
| suffix = potential_ext | |
| with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as tmp: | |
| response = requests.get(url, stream=True, timeout=30) | |
| response.raise_for_status() # Cek jika download gagal | |
| for chunk in response.iter_content(chunk_size=8192): | |
| tmp.write(chunk) | |
| tmp_path = tmp.name | |
| # 4. Proses Transkripsi | |
| print(f"Processing: {url}") | |
| segments, info = model.transcribe(tmp_path, beam_size=5) | |
| full_text = " ".join([segment.text for segment in segments]).strip() | |
| # 5. Kembalikan Response JSON | |
| return { | |
| "success": True, | |
| "language": info.language, | |
| "language_probability": info.language_probability, | |
| "text": full_text, | |
| "url_processed": url | |
| } | |
| except requests.exceptions.RequestException as e: | |
| raise HTTPException(status_code=400, detail=f"Gagal mendownload file: {str(e)}") | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=f"Internal Error: {str(e)}") | |
| finally: | |
| # 6. Bersihkan file sementara | |
| if tmp_path and os.path.exists(tmp_path): | |
| os.remove(tmp_path) | |
| if __name__ == "__main__": | |
| # Jalankan server | |
| uvicorn.run(app, host="0.0.0.0", port=7860) |