Spaces:
Paused
Paused
File size: 2,416 Bytes
c72ac42 94561ca c72ac42 94561ca c72ac42 94561ca c72ac42 94561ca c72ac42 94561ca c72ac42 94561ca c72ac42 94561ca c72ac42 94561ca c72ac42 94561ca c72ac42 94561ca c72ac42 94561ca c72ac42 94561ca c72ac42 94561ca | 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 69 70 | 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")
@app.get("/")
def home():
return {"status": "API Active", "usage": "/generate?url=YOUR_AUDIO_URL"}
@app.get("/generate")
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) |