kontol / main.py
Rianofc's picture
Create main.py
6ef2d50 verified
import logging
from fastapi import FastAPI, HTTPException
from fastapi.responses import FileResponse
import os
import subprocess
app = FastAPI()
logging.basicConfig(level=logging.INFO)
TEMP_DIR = "/tmp"
@app.get("/")
def home():
return {"message": "Welcome to Brat API!"}
@app.get("/brat")
async def brat(text: str):
if not text:
raise HTTPException(status_code=400, detail="Text is required")
process = subprocess.run(["node", "brat.js", text], capture_output=True, text=True)
stdout = process.stdout.strip()
stderr = process.stderr.strip()
logging.info(f"stdout: {stdout}")
logging.error(f"stderr: {stderr}")
if process.returncode != 0:
raise HTTPException(status_code=500, detail=f"Failed to generate image: {stderr}")
if not os.path.exists(stdout):
raise HTTPException(status_code=500, detail=f"Generated file not found: {stdout}")
return FileResponse(stdout, media_type="image/png", filename="brat.png")
@app.get("/brat-vid")
async def brat_vid(text: str):
if not text:
raise HTTPException(status_code=400, detail="Text is required")
process = subprocess.run(["node", "brat.js", "vid", text], capture_output=True, text=True)
stdout = process.stdout.strip()
stderr = process.stderr.strip()
logging.info(f"stdout: {stdout}")
logging.error(f"stderr: {stderr}")
if process.returncode != 0:
raise HTTPException(status_code=500, detail=f"Failed to generate video: {stderr}")
if not os.path.exists(stdout):
raise HTTPException(status_code=500, detail=f"Generated file not found: {stdout}")
return FileResponse(stdout, media_type="video/mp4", filename="brat.mp4")
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=7860)