File size: 3,308 Bytes
0c78d95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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)