File size: 2,545 Bytes
50776af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
90
from flask import Flask, render_template, request, jsonify, send_file
from inference import predict
from pdf_generator import generate_pdf
from ocr import extract_text_from_image, allowed_file
import os
import tempfile

app = Flask(__name__)

# Limit incoming request body to 15 MB (guards /ocr_analyze against huge uploads)
app.config["MAX_CONTENT_LENGTH"] = 15 * 1024 * 1024


@app.route("/")
def home():
    return render_template("index.html")


@app.route("/analyze", methods=["POST"])
def analyze():
    data = request.get_json(silent=True)

    if not data or not data.get("message"):
        return jsonify({"error": "No message provided."}), 400

    text = data["message"].strip()
    if not text:
        return jsonify({"error": "Message cannot be empty."}), 400

    result = predict(text)
    return jsonify(result)


@app.route("/ocr_analyze", methods=["POST"])
def ocr_analyze():
    if "image" not in request.files:
        return jsonify({"error": "No image uploaded."}), 400

    file = request.files["image"]

    if file.filename == "":
        return jsonify({"error": "No file selected."}), 400

    if not allowed_file(file.filename):
        return jsonify({"error": "Unsupported file type. Use PNG, JPG, JPEG, WEBP, or BMP."}), 400

    file_bytes = file.read()
    ocr_result = extract_text_from_image(file_bytes)

    if not ocr_result["success"]:
        return jsonify({"error": ocr_result["error"]}), 422

    extracted_text = ocr_result["text"]

    # Pipe directly into existing inference — zero changes to inference.py
    prediction = predict(extracted_text)
    prediction["extracted_text"] = extracted_text
    prediction["input_method"] = "ocr"

    return jsonify(prediction)


@app.route("/download_report", methods=["POST"])
def download_report():
    data = request.get_json(silent=True)
    if not data:
        return jsonify({"error": "No data provided."}), 400

    # Use a temp file so concurrent requests don't overwrite each other
    with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as tmp:
        filepath = tmp.name

    try:
        generate_pdf(data, filepath)
        return send_file(
            filepath,
            as_attachment=True,
            mimetype="application/pdf",
            download_name="SentinelAI_Report.pdf",
        )
    finally:
        # Clean up the temp file after Flask sends it
        try:
            os.unlink(filepath)
        except OSError:
            pass


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=7860, debug=False)