| | |
| |
|
| | import os |
| | import shutil |
| | import tempfile |
| | import torch |
| | import whisper |
| | from fastapi import FastAPI, File, UploadFile, Query |
| | from typing import Dict, Optional |
| |
|
| | |
| |
|
| | |
| | DEVICE = "cuda" if torch.cuda.is_available() else "cpu" |
| | print(f"Using device: {DEVICE}") |
| |
|
| | |
| | print("Loading 'base' model...") |
| | model = whisper.load_model("base", device=DEVICE) |
| | print("Model 'base' loaded successfully.") |
| |
|
| | |
| | app = FastAPI() |
| |
|
| | |
| |
|
| | @app.get("/") |
| | def read_root(): |
| | """ نقطة نهاية للتحقق من أن الـ API يعمل. """ |
| | return {"message": "Whisper API is running. Use the /transcribe endpoint to submit audio files."} |
| |
|
| | @app.post("/transcribe") |
| | async def transcribe_audio( |
| | file: UploadFile = File(...), |
| | language: Optional[str] = Query(None, description="Language of the audio (e.g., 'ar' for Arabic)") |
| | ): |
| | """ |
| | نقطة نهاية لتحويل ملف صوتي إلى نص. |
| | تقبل ملفًا صوتيًا وتُرجع النص المحوّل. |
| | """ |
| | |
| | with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(file.filename)[1]) as tmp_file: |
| | shutil.copyfileobj(file.file, tmp_file) |
| | tmp_file_path = tmp_file.name |
| |
|
| | try: |
| | |
| | transcribe_options = {"language": language} if language else {} |
| | |
| | |
| | result = model.transcribe(tmp_file_path, **transcribe_options) |
| | |
| | transcript = result.get("text", "لم يتمكن من التعرف على نص.") |
| | detected_language = result.get("language", "لم يتم تحديد اللغة.") |
| | |
| | return {"transcript": transcript, "language": detected_language} |
| |
|
| | finally: |
| | |
| | os.remove(tmp_file_path) |
| | await file.close() |