Rianofc commited on
Commit
6ef2d50
·
verified ·
1 Parent(s): 44acf33

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +59 -0
main.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+ from fastapi import FastAPI, HTTPException
3
+ from fastapi.responses import FileResponse
4
+ import os
5
+ import subprocess
6
+
7
+ app = FastAPI()
8
+ logging.basicConfig(level=logging.INFO)
9
+ TEMP_DIR = "/tmp"
10
+
11
+ @app.get("/")
12
+ def home():
13
+ return {"message": "Welcome to Brat API!"}
14
+
15
+ @app.get("/brat")
16
+ async def brat(text: str):
17
+ if not text:
18
+ raise HTTPException(status_code=400, detail="Text is required")
19
+
20
+ process = subprocess.run(["node", "brat.js", text], capture_output=True, text=True)
21
+
22
+ stdout = process.stdout.strip()
23
+ stderr = process.stderr.strip()
24
+
25
+ logging.info(f"stdout: {stdout}")
26
+ logging.error(f"stderr: {stderr}")
27
+
28
+ if process.returncode != 0:
29
+ raise HTTPException(status_code=500, detail=f"Failed to generate image: {stderr}")
30
+
31
+ if not os.path.exists(stdout):
32
+ raise HTTPException(status_code=500, detail=f"Generated file not found: {stdout}")
33
+
34
+ return FileResponse(stdout, media_type="image/png", filename="brat.png")
35
+
36
+ @app.get("/brat-vid")
37
+ async def brat_vid(text: str):
38
+ if not text:
39
+ raise HTTPException(status_code=400, detail="Text is required")
40
+
41
+ process = subprocess.run(["node", "brat.js", "vid", text], capture_output=True, text=True)
42
+
43
+ stdout = process.stdout.strip()
44
+ stderr = process.stderr.strip()
45
+
46
+ logging.info(f"stdout: {stdout}")
47
+ logging.error(f"stderr: {stderr}")
48
+
49
+ if process.returncode != 0:
50
+ raise HTTPException(status_code=500, detail=f"Failed to generate video: {stderr}")
51
+
52
+ if not os.path.exists(stdout):
53
+ raise HTTPException(status_code=500, detail=f"Generated file not found: {stdout}")
54
+
55
+ return FileResponse(stdout, media_type="video/mp4", filename="brat.mp4")
56
+
57
+ if __name__ == "__main__":
58
+ import uvicorn
59
+ uvicorn.run(app, host="0.0.0.0", port=7860)