-
Notifications
You must be signed in to change notification settings - Fork 346
Expand file tree
/
Copy pathserver_pa.py
More file actions
89 lines (69 loc) · 2.48 KB
/
server_pa.py
File metadata and controls
89 lines (69 loc) · 2.48 KB
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
#!/usr/bin/env python3
"""
FastAPI Backend Server — web service entry for Edit Banana.
Provides upload and conversion API. Run with: python server_pa.py
Server runs at http://localhost:8000
"""
import os
import sys
from pathlib import Path
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, PROJECT_ROOT)
from fastapi import FastAPI, File, UploadFile, HTTPException
import uvicorn
app = FastAPI(
title="Edit Banana API",
description="Image to editable DrawIO (XML) — upload a diagram image, get DrawIO XML.",
version="1.0.0",
)
@app.get("/health")
def health():
return {"status": "ok"}
@app.get("/")
def root():
return {"service": "Edit Banana", "docs": "/docs"}
@app.post("/convert")
async def convert(file: UploadFile = File(...)):
"""Upload an image and get editable DrawIO XML. Supported: PNG, JPG, BMP, TIFF, WebP."""
name = file.filename or ""
ext = Path(name).suffix.lower()
allowed = {".png", ".jpg", ".jpeg", ".bmp", ".tiff", ".webp"}
if ext not in allowed:
raise HTTPException(400, f"Unsupported format. Use one of: {', '.join(sorted(allowed))}.")
config_path = os.path.join(PROJECT_ROOT, "config", "config.yaml")
if not os.path.exists(config_path):
raise HTTPException(503, "Server not configured (missing config/config.yaml)")
try:
from main import load_config, Pipeline
import tempfile
import shutil
config = load_config()
output_dir = config.get("paths", {}).get("output_dir", "./output")
os.makedirs(output_dir, exist_ok=True)
with tempfile.NamedTemporaryFile(delete=False, suffix=ext) as tmp:
shutil.copyfileobj(file.file, tmp)
tmp_path = tmp.name
try:
pipeline = Pipeline(config)
result_path = pipeline.process_image(
tmp_path,
output_dir=output_dir,
with_refinement=False,
with_text=True,
)
if not result_path or not os.path.exists(result_path):
raise HTTPException(500, "Conversion failed")
return {"success": True, "output_path": result_path}
finally:
try:
os.unlink(tmp_path)
except Exception:
pass
except HTTPException:
raise
except Exception as e:
raise HTTPException(500, str(e))
def main():
uvicorn.run(app, host="0.0.0.0", port=8000)
if __name__ == "__main__":
main()