Create a Flask app. Args: config: GlmOcrConfig instance. Returns: Flask app instance.
(config: "GlmOcrConfig")
| 49 | |
| 50 | |
| 51 | def create_app(config: "GlmOcrConfig") -> Flask: |
| 52 | """Create a Flask app. |
| 53 | |
| 54 | Args: |
| 55 | config: GlmOcrConfig instance. |
| 56 | |
| 57 | Returns: |
| 58 | Flask app instance. |
| 59 | """ |
| 60 | if Flask is None: |
| 61 | raise ImportError( |
| 62 | "Flask server support requires the optional server extra. " |
| 63 | "Install with: pip install 'glmocr[server]'" |
| 64 | ) from _FLASK_IMPORT_ERROR |
| 65 | |
| 66 | app = Flask(__name__) |
| 67 | |
| 68 | # Create pipeline with typed config |
| 69 | pipeline = Pipeline(config=config.pipeline) |
| 70 | |
| 71 | # Store pipeline and config in app.config |
| 72 | app.config["pipeline"] = pipeline |
| 73 | app.config["doc_config"] = config |
| 74 | |
| 75 | @app.route("/glmocr/parse", methods=["POST"]) |
| 76 | def parse(): |
| 77 | """Document parsing endpoint. |
| 78 | |
| 79 | Request: |
| 80 | { |
| 81 | "images": ["url1", "url2", ...], # image URLs (http/https/file/data) |
| 82 | } |
| 83 | |
| 84 | Response: |
| 85 | { |
| 86 | "json_result": {...}, |
| 87 | "markdown_result": "..." |
| 88 | } |
| 89 | """ |
| 90 | # Validate Content-Type |
| 91 | if request.headers.get("Content-Type") != "application/json": |
| 92 | return ( |
| 93 | jsonify( |
| 94 | {"error": "Invalid Content-Type. Expected 'application/json'."} |
| 95 | ), |
| 96 | 400, |
| 97 | ) |
| 98 | |
| 99 | # Parse JSON payload |
| 100 | try: |
| 101 | data = request.json |
| 102 | except Exception: |
| 103 | return jsonify({"error": "Invalid JSON payload"}), 400 |
| 104 | |
| 105 | images = data.get("images", []) |
| 106 | if isinstance(images, str): |
| 107 | images = [images] |
| 108 |