Spaces:
Runtime error
Runtime error
| import os | |
| import uuid | |
| import torch | |
| from fastapi import FastAPI, HTTPException | |
| from fastapi.responses import FileResponse | |
| from pydantic import BaseModel | |
| # سنستخدم الـ Pipeline العام للـ GGUF ليتناسب مع الـ CPU | |
| from diffusers import DiffusionPipeline | |
| app = FastAPI(title="Free CPU Video API") | |
| OUTPUT_DIR = "/tmp/generated_videos" | |
| os.makedirs(OUTPUT_DIR, exist_ok=True) | |
| print("Loading Quantized Model for CPU...") | |
| # تحميل نسخة خفيفة جداً ومضغوطة تعمل على المعالج وبدون ذاكرة كارت شاشة | |
| pipeline = DiffusionPipeline.from_pretrained( | |
| "Lightricks/LTX-Video", | |
| torch_dtype=torch.float32, # الـ CPU يعمل بـ float32 | |
| use_safetensors=True | |
| ) | |
| # تحديد تشغيل النموذج على الـ CPU مجاناً | |
| pipeline.to("cpu") | |
| print("Model loaded successfully on CPU!") | |
| class VideoPrompt(BaseModel): | |
| prompt: str | |
| def home(): | |
| return {"status": "Server is running on FREE CPU!"} | |
| async def generate_video(video_req: VideoPrompt): | |
| try: | |
| # تقليل الأبعاد وعدد الفريمات والخطوات لأقصى حد لكي لا يموت الـ CPU | |
| video_frames = pipeline( | |
| prompt=video_req.prompt, | |
| num_frames=17, # تقليل الفريمات (حوالي ثانيتين) لتسريع المعالجة | |
| height=256, # تقليل الأبعاد لـ 256 لكي يتحملها الـ CPU | |
| width=256, | |
| num_inference_steps=7, # تقليل الخطوات جداً لتوليد سريع مجاني | |
| guidance_scale=3.0 | |
| ).frames[0] | |
| file_name = f"{uuid.uuid4()}.mp4" | |
| file_path = os.path.join(OUTPUT_DIR, file_name) | |
| from diffusers.utils import export_to_video | |
| export_to_video(video_frames, file_path, fps=8) | |
| return {"status": "success", "video_name": file_name} | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| async def download_video(file_name: str): | |
| file_path = os.path.join(OUTPUT_DIR, file_name) | |
| if os.path.exists(file_path): | |
| return FileResponse(file_path, media_type="video/mp4") | |
| raise HTTPException(status_code=404, detail="Video not found") |