Spaces:
Runtime error
Runtime error
| from fastapi import FastAPI, Request, Form | |
| from fastapi.responses import HTMLResponse, FileResponse | |
| from fastapi.staticfiles import StaticFiles | |
| from fastapi.templating import Jinja2Templates | |
| import os | |
| import yt_dlp | |
| import whisper | |
| # إنشاء المجلدات إذا لم تكن موجودة | |
| os.makedirs("static", exist_ok=True) | |
| os.makedirs("templates", exist_ok=True) | |
| app = FastAPI() | |
| # تحميل القوالب والملفات الثابتة | |
| templates = Jinja2Templates(directory="templates") | |
| app.mount("/static", StaticFiles(directory="static"), name="static") | |
| async def read_root(request: Request): | |
| return templates.TemplateResponse("index.html", {"request": request}) | |
| async def process_video(request: Request, youtube_url: str = Form(...)): | |
| try: | |
| # تنزيل الصوت من اليوتيوب | |
| ydl_opts = { | |
| 'format': 'bestaudio/best', | |
| 'postprocessors': [{ | |
| 'key': 'FFmpegExtractAudio', | |
| 'preferredcodec': 'mp3', | |
| 'preferredquality': '192', | |
| }], | |
| 'outtmpl': 'audio', | |
| } | |
| with yt_dlp.YoutubeDL(ydl_opts) as ydl: | |
| ydl.download([youtube_url]) | |
| # تحويل الصوت إلى نص | |
| model = whisper.load_model("base") | |
| result = model.transcribe("audio.mp3") | |
| return templates.TemplateResponse("result.html", { | |
| "request": request, | |
| "transcription": result["text"] | |
| }) | |
| except Exception as e: | |
| return {"error": str(e)} | |
| async def download_transcription(): | |
| return FileResponse("transcription.txt") |