yashxx07's picture
Update app.py
dc92314 verified
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from manim import *
import uuid
import os
from supabase import create_client, Client
import tempfile
url: str = os.environ.get("SUPABASE_URL")
key: str = os.environ.get("SUPABASE_KEY")
supabase: Client = create_client(url, key)
app = FastAPI()
class CodeData(BaseModel):
code: str # This will expect the "code" field to be a string
@app.get("/")
def greet_json():
return {"Hello": "World!"}
@app.post("/test")
def fun(data: CodeData):
try:
# Use tempfile to create a temporary file
with tempfile.NamedTemporaryFile(delete=False) as f1:
temp_file_path = f1.name
f1.write(data.code.encode()) # Make sure to encode the string
with open(temp_file_path, 'r') as f1:
code = f1.read()
exec(code)
print(os.listdir("/app"))
# Proceed with your video upload logic
with open('/app/media/videos/1080p60/DemoScene.mp4', 'rb') as f:
response = supabase.storage.from_("anim videos").upload(
file=f,
path=f"public/DemoScene_1_{str(uuid.uuid4())}.mp4",
file_options={"cache-control": "3600", "upsert": "false", "content-type": "video/mp4"},
)
except Exception as err:
# Raise an HTTPException if there's an error
raise HTTPException(status_code=500, detail=str(err))
return {"message": "Code executed successfully", "url": response}