import os import tempfile from fastapi import FastAPI, File, UploadFile from starlette.responses import JSONResponse from supabase import create_client from src.components.vidgen import VideoCreator supabase_url = os.getenv('SUPABASE_URL') supabase_key = os.getenv('SUPABASE_KEY') supabase = create_client(supabase_url, supabase_key) app = FastAPI() RESOURCES_DIR = "resources" os.makedirs(RESOURCES_DIR, exist_ok=True) def upload_to_supabase(video_path, bucket_name="JewelmirrorVideoGeneration"): try: with open(video_path, 'rb') as f: file_name = os.path.basename(video_path) supabase.storage.from_(bucket_name).upload(file_name, f) public_url = supabase.storage.from_(bucket_name).get_public_url(file_name) return public_url except Exception as e: print(f"Error uploading to Supabase: {str(e)}") return None @app.post("/create-video/") async def create_video( necklace_image: UploadFile = File(...), necklace_tryon_image: UploadFile = File(...), makeup_image: UploadFile = File(...), clothing_image_1: UploadFile = File(...), clothing_image_2: UploadFile = File(...) ): try: def save_temp_file(file: UploadFile) -> str: temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png") temp_file.write(file.file.read()) return temp_file.name temp_files = { 'necklace': save_temp_file(necklace_image), 'necklace_tryon': save_temp_file(necklace_tryon_image), 'makeup': save_temp_file(makeup_image), 'clothing1': save_temp_file(clothing_image_1), 'clothing2': save_temp_file(clothing_image_2) } intro_path = f"{RESOURCES_DIR}/intro/ChamundiJewelsMandir_intro.mp4" output_path = f"{RESOURCES_DIR}/temp_video/video_{os.urandom(8).hex()}.mp4" font_path = f"{RESOURCES_DIR}/fonts/PlayfairDisplay-VariableFont_wght.ttf" audio_path = f"{RESOURCES_DIR}/audio/background.mp3" video_creator = VideoCreator( intro_video_path=intro_path, necklace_image=temp_files['necklace'], nto_image1=temp_files['necklace_tryon'], nto_cto_1=temp_files['clothing1'], nto_cto_2=temp_files['clothing2'], makeup_1=temp_files['makeup'], font_path=font_path, output_path=output_path, audio_path=audio_path ) # Generate video video_creator.create_final_video() # Clean up temp files for temp_file in temp_files.values(): if os.path.exists(temp_file): os.unlink(temp_file) url = upload_to_supabase(video_path=output_path) response = {"status": "success", "message": "Video created successfully", "public_url": url } os.remove(output_path) return JSONResponse(content=response, status_code=200) except Exception as e: return JSONResponse(content={"status": "error", "message": "Please try again", "error": str(e)}, status_code=500) if __name__ == "__main__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=8000)