purrbits commited on
Commit
51cdd3e
·
verified ·
1 Parent(s): 35dc49e

Upload 5 files

Browse files
Files changed (5) hide show
  1. Dockerfile +25 -0
  2. index.html +18 -0
  3. main.py +270 -0
  4. requirements.txt +4 -0
  5. run.py +13 -0
Dockerfile ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10
2
+
3
+ WORKDIR /app
4
+
5
+ RUN apt-get update && apt-get install -y \
6
+ ffmpeg \
7
+ && rm -rf /var/lib/apt/lists/*
8
+
9
+ COPY requirements.txt .
10
+ RUN pip install --no-cache-dir -r requirements.txt
11
+
12
+ RUN playwright install --with-deps chromium
13
+
14
+ RUN useradd -m -u 1000 user && \
15
+ mkdir -p /app/output /app/tmp_brat && \
16
+ chown -R user:user /app && \
17
+ chmod -R 777 /app/output /app/tmp_brat
18
+
19
+ USER user
20
+
21
+ COPY --chown=user . .
22
+
23
+ EXPOSE 7860
24
+
25
+ CMD ["python", "run.py"]
index.html ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!doctype html>
2
+
3
+ <html lang="id">
4
+ <head>
5
+ <meta charset="utf-8">
6
+ <meta name="viewport" content="width=device-width, initial-scale=1">
7
+ <title>InuSoft Brat - FastAPI</title>
8
+ <meta name="description" content="Brat Generator Api's">
9
+ </head>
10
+ <body>
11
+ <main>
12
+ <h1>InuSoft Brat - FastAPI</h1>
13
+ <p>This REST API is built using FastAPI - a modern, high-performance web framework for Python specifically designed for API creation. This API especially for making text to images with brat style, you can check on the brat generator website www.bratgenerator.com</p>
14
+ <button type="button" onclick="location.href='/docs'">View Documentation</button>
15
+ </main>
16
+
17
+ </body>
18
+ </html>
main.py ADDED
@@ -0,0 +1,270 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Query, Request
2
+ from fastapi.responses import JSONResponse, FileResponse, Response, StreamingResponse
3
+ from playwright.async_api import async_playwright
4
+ import os, uuid, shutil, asyncio, time, stat
5
+ from collections import defaultdict
6
+
7
+ app = FastAPI(
8
+ title="BRAT GEN - API",
9
+ description="API for generating BRAT-style text images & videos.",
10
+ version="1.0.0",
11
+ )
12
+
13
+ def _ensure_dir(path: str) -> str:
14
+ """
15
+ Ensures the directory can be created and written to. If it fails, falls back to /tmp.
16
+ Sets 0777 permissions for safety in non-root containers.
17
+ """
18
+ try:
19
+ os.makedirs(path, exist_ok=True)
20
+ os.chmod(path, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO) # 0o777
21
+ return path
22
+ except Exception:
23
+ fallback = os.path.join("/tmp", os.path.basename(path))
24
+ os.makedirs(fallback, exist_ok=True)
25
+ os.chmod(fallback, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
26
+ return fallback
27
+
28
+ OUTPUT_DIR = _ensure_dir(os.environ.get("OUTPUT_DIR", os.path.join(os.getcwd(), "output")))
29
+ TMP_DIR = _ensure_dir(os.environ.get("TMP_DIR", os.path.join(os.getcwd(), "tmp_brat")))
30
+
31
+ REQUEST_LIMIT = 10 # Max requests allowed
32
+ TIME_WINDOW = 60 # Time window in seconds (1 minute)
33
+ BAN_DURATION = 300 # Ban duration in seconds (5 minutes)
34
+
35
+ request_logs: dict[str, list[float]] = defaultdict(list)
36
+ banned_ips: dict[str, float] = {}
37
+
38
+ def get_client_ip(request: Request) -> str:
39
+ cf = request.headers.get("CF-Connecting-IP")
40
+ if cf:
41
+ return cf.strip()
42
+ xff = request.headers.get("X-Forwarded-For")
43
+ if xff:
44
+ return xff.split(",")[0].strip()
45
+ return (request.client.host or "").strip()
46
+
47
+ @app.middleware("http")
48
+ async def anti_ddos_middleware(request: Request, call_next):
49
+ ip = get_client_ip(request)
50
+ now = time.time()
51
+
52
+ if ip in banned_ips:
53
+ if now < banned_ips[ip]:
54
+ return JSONResponse(
55
+ status_code=429,
56
+ content={"error": "IP is blocked for 5 minutes due to too many requests."},
57
+ )
58
+ else:
59
+ del banned_ips[ip]
60
+
61
+ window = [ts for ts in request_logs[ip] if now - ts < TIME_WINDOW]
62
+ window.append(now)
63
+ request_logs[ip] = window
64
+
65
+ if len(request_logs[ip]) > REQUEST_LIMIT:
66
+ banned_ips[ip] = now + BAN_DURATION
67
+ return JSONResponse(
68
+ status_code=429,
69
+ content={"error": "Too many requests. IP is blocked for 5 minutes."},
70
+ )
71
+
72
+ return await call_next(request)
73
+
74
+
75
+ async def delete_file_after_delay(filepath: str, delay: int = 600):
76
+ await asyncio.sleep(delay)
77
+ try:
78
+ if os.path.exists(filepath):
79
+ os.remove(filepath)
80
+ except Exception:
81
+ pass
82
+
83
+ @app.get("/api/brat", tags=["MAIN"], summary="Generate BRAT text image")
84
+ async def generate_brat(
85
+ request: Request,
86
+ text: str = Query(..., description="Text to be inserted into the BRAT image"),
87
+ background: str | None = Query(None, description="Background color (e.g., #000000)"),
88
+ color: str | None = Query(None, description="Text color (e.g., #FFFFFF)"),
89
+ ):
90
+ text = (text or "").strip()
91
+ if not text:
92
+ return JSONResponse(status_code=400, content={"error": "Text cannot be empty."})
93
+
94
+ try:
95
+ async with async_playwright() as p:
96
+ browser = await p.chromium.launch(args=["--no-sandbox"])
97
+ context = await browser.new_context(viewport={"width": 1536, "height": 695})
98
+ page = await context.new_page()
99
+ await page.goto("https://www.bratgenerator.com/", wait_until="domcontentloaded")
100
+
101
+ try:
102
+ await page.click("text=Accept", timeout=3000)
103
+ except Exception:
104
+ pass
105
+
106
+ await page.click("#toggleButtonWhite")
107
+ await page.click("#textOverlay")
108
+ await page.click("#textInput")
109
+ await page.fill("#textInput", text)
110
+
111
+ await page.evaluate(
112
+ """(data) => {
113
+ if (data.background) $('.node__content.clearfix').css('background-color', data.background);
114
+ if (data.color) $('.textFitted').css('color', data.color);
115
+ }""",
116
+ {"background": background, "color": color},
117
+ )
118
+
119
+ await asyncio.sleep(0.5)
120
+
121
+ element = await page.query_selector("#textOverlay")
122
+ if not element:
123
+ await browser.close()
124
+ return JSONResponse(status_code=500, content={"error": "Target element not found."})
125
+ box = await element.bounding_box()
126
+ if not box:
127
+ await browser.close()
128
+ return JSONResponse(status_code=500, content={"error": "Failed to read element bounding box."})
129
+
130
+ filename = f"purrbits-{uuid.uuid4().hex[:8]}.png"
131
+ filepath = os.path.join(OUTPUT_DIR, filename)
132
+
133
+ screenshot = await page.screenshot(
134
+ clip={"x": box["x"], "y": box["y"], "width": 500, "height": 440}
135
+ )
136
+ with open(filepath, "wb") as f:
137
+ f.write(screenshot)
138
+
139
+ await context.close()
140
+ await browser.close()
141
+
142
+ asyncio.create_task(delete_file_after_delay(filepath))
143
+ base_url = str(request.base_url).rstrip("/")
144
+ return {"status": 200, "URL": f"{base_url}/download/file/{filename}"}
145
+
146
+ except Exception as e:
147
+ return JSONResponse(status_code=500, content={"error": f"Failed to generate image: {str(e)}"})
148
+
149
+ @app.get("/api/bratvid", tags=["MAIN"], summary="Create animated video from BRAT text")
150
+ async def generate_brat_video(
151
+ request: Request,
152
+ text: str = Query(..., description="Sentence to be animated (space-separated)"),
153
+ background: str | None = Query(None, description="Background color (e.g., #000000)"),
154
+ color: str | None = Query(None, description="Text color (e.g., #FFFFFF)"),
155
+ ):
156
+ text = (text or "").strip()
157
+ if not text:
158
+ return JSONResponse(status_code=400, content={"error": "Text cannot be empty."})
159
+
160
+ words = text.split()
161
+ if not words:
162
+ return JSONResponse(status_code=400, content={"error": "Text must contain at least one word."})
163
+
164
+ temp_dir = _ensure_dir(os.path.join(TMP_DIR, str(uuid.uuid4())))
165
+
166
+ try:
167
+ async with async_playwright() as p:
168
+ browser = await p.chromium.launch(args=["--no-sandbox"])
169
+ context = await browser.new_context(viewport={"width": 1536, "height": 695})
170
+ page = await context.new_page()
171
+ await page.goto("https://www.bratgenerator.com/", wait_until="domcontentloaded")
172
+
173
+ try:
174
+ await page.click("text=Accept", timeout=3000)
175
+ except Exception:
176
+ pass
177
+
178
+ await page.click("#toggleButtonWhite")
179
+ await page.click("#textOverlay")
180
+ await page.click("#textInput")
181
+
182
+ for i in range(len(words)):
183
+ partial_text = " ".join(words[: i + 1])
184
+ await page.fill("#textInput", partial_text)
185
+
186
+ await page.evaluate(
187
+ """(data) => {
188
+ if (data.background) $('.node__content.clearfix').css('background-color', data.background);
189
+ if (data.color) $('.textFitted').css('color', data.color);
190
+ }""",
191
+ {"background": background, "color": color},
192
+ )
193
+
194
+ await asyncio.sleep(0.2)
195
+
196
+ element = await page.query_selector("#textOverlay")
197
+ if not element:
198
+ await context.close()
199
+ await browser.close()
200
+ shutil.rmtree(temp_dir, ignore_errors=True)
201
+ return JSONResponse(status_code=500, content={"error": "Target element not found."})
202
+ box = await element.bounding_box()
203
+ if not box:
204
+ await context.close()
205
+ await browser.close()
206
+ shutil.rmtree(temp_dir, ignore_errors=True)
207
+ return JSONResponse(status_code=500, content={"error": "Failed to read element bounding box."})
208
+
209
+ screenshot = await page.screenshot(
210
+ clip={"x": box["x"], "y": box["y"], "width": 500, "height": 440}
211
+ )
212
+ frame_path = os.path.join(temp_dir, f"frame{i:03d}.png")
213
+ with open(frame_path, "wb") as f:
214
+ f.write(screenshot)
215
+
216
+ await context.close()
217
+ await browser.close()
218
+
219
+ # Render video with ffmpeg
220
+ output_filename = f"purrbits-{uuid.uuid4().hex[:8]}.mp4"
221
+ output_path = os.path.join(OUTPUT_DIR, output_filename)
222
+
223
+ ffmpeg_cmd = [
224
+ "ffmpeg",
225
+ "-y",
226
+ "-framerate", "1.428",
227
+ "-i", os.path.join(temp_dir, "frame%03d.png"),
228
+ "-vf", "scale=trunc(iw/2)*2:trunc(ih/2)*2,fps=30",
229
+ "-c:v", "libx264",
230
+ "-preset", "ultrafast",
231
+ "-pix_fmt", "yuv420p",
232
+ output_path,
233
+ ]
234
+
235
+ process = await asyncio.create_subprocess_exec(
236
+ *ffmpeg_cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
237
+ )
238
+ _, stderr = await process.communicate()
239
+
240
+ if process.returncode != 0:
241
+ shutil.rmtree(temp_dir, ignore_errors=True)
242
+ return JSONResponse(status_code=500, content={"error": stderr.decode()})
243
+
244
+ asyncio.create_task(delete_file_after_delay(output_path))
245
+ base_url = str(request.base_url).rstrip("/")
246
+ return {"status": 200, "URL": f"{base_url}/download/file/{output_filename}"}
247
+
248
+ except Exception as e:
249
+ return JSONResponse(status_code=500, content={"error": str(e)})
250
+ finally:
251
+ shutil.rmtree(temp_dir, ignore_errors=True)
252
+
253
+ @app.get("/", summary="Root Endpoint", tags=["MISC"], description="Displaying the main page.")
254
+ async def root():
255
+ # take absolute path based on main.py file location
256
+ html_path = os.path.join(os.path.dirname(__file__), "index.html")
257
+
258
+ if os.path.exists(html_path):
259
+ return FileResponse(html_path)
260
+ return JSONResponse(status_code=404, content={"error": "index.html file not found"})
261
+
262
+ @app.get("/download/file/{filename}", tags=["MISC"])
263
+ async def download_file(filename: str):
264
+ filepath = os.path.join(OUTPUT_DIR, filename)
265
+ if not os.path.exists(filepath):
266
+ return JSONResponse(status_code=404, content={"error": "File not found"})
267
+
268
+ with open(filepath, "rb") as f:
269
+ data = f.read()
270
+ return Response(data, media_type="application/octet-stream")
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ playwright
4
+ asyncio
run.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import subprocess
2
+ import uvicorn
3
+ from main import app
4
+
5
+ if __name__ == "__main__":
6
+ subprocess.run(["playwright", "install", "chromium"], check=True)
7
+ uvicorn.run(
8
+ app,
9
+ host="0.0.0.0",
10
+ port=7860,
11
+ proxy_headers=True,
12
+ forwarded_allow_ips="*"
13
+ )