File size: 5,798 Bytes
f52234e
17ef86f
 
f52234e
 
 
0051294
17ef86f
f52234e
 
17ef86f
 
f52234e
0051294
 
f52234e
 
17ef86f
f52234e
17ef86f
f52234e
17ef86f
0051294
17ef86f
 
 
f52234e
17ef86f
 
 
 
f52234e
 
 
17ef86f
 
 
 
 
 
 
 
 
 
f52234e
 
17ef86f
 
f52234e
 
0051294
 
 
 
 
17ef86f
 
 
 
 
 
0051294
 
 
 
 
 
 
 
 
17ef86f
 
0051294
 
 
 
 
17ef86f
 
 
 
 
0051294
17ef86f
 
0051294
 
17ef86f
 
0051294
 
 
 
 
 
 
 
 
 
 
f52234e
 
17ef86f
 
 
 
f52234e
17ef86f
 
 
f52234e
17ef86f
 
f52234e
17ef86f
 
 
 
f52234e
17ef86f
 
f52234e
17ef86f
 
 
 
f52234e
17ef86f
f52234e
17ef86f
f52234e
 
 
 
17ef86f
 
 
 
f52234e
 
17ef86f
 
 
 
 
 
f52234e
 
 
 
17ef86f
 
f52234e
 
 
 
 
17ef86f
f52234e
 
17ef86f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f52234e
 
 
 
 
 
 
17ef86f
 
 
 
f52234e
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
"""
AIFinder Flask API
Serves the trained sklearn ensemble via the AIFinder inference class.
"""

import os
import re

import joblib
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from flask import Flask, jsonify, request, send_from_directory, render_template
from flask_cors import CORS
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address

from config import MODEL_DIR
from inference import AIFinder

app = Flask(__name__)
CORS(app)
limiter = Limiter(get_remote_address, app=app)

finder: AIFinder | None = None
community_finder: AIFinder | None = None
using_community = False

DEFAULT_TOP_N = 4
COMMUNITY_DIR = os.path.join(MODEL_DIR, "community")
CORRECTIONS_FILE = os.path.join(COMMUNITY_DIR, "corrections.joblib")
corrections: list[dict] = []


def load_models():
    global finder, community_finder, corrections
    finder = AIFinder(model_dir=MODEL_DIR)
    os.makedirs(COMMUNITY_DIR, exist_ok=True)
    if os.path.exists(CORRECTIONS_FILE):
        corrections = joblib.load(CORRECTIONS_FILE)
    if os.path.exists(os.path.join(COMMUNITY_DIR, "rf_4provider.joblib")):
        try:
            community_finder = AIFinder(model_dir=COMMUNITY_DIR)
        except Exception:
            community_finder = None


def _active_finder():
    return community_finder if using_community and community_finder else finder


def _strip_think_tags(text):
    text = re.sub(r"<think(?:ing)?>.*?</think(?:ing)?>", "", text, flags=re.DOTALL)
    return text.strip()


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


@app.route("/api/classify", methods=["POST"])
@app.route("/v1/classify", methods=["POST"])
@limiter.limit("60/minute")
def v1_classify():
    data = request.get_json(silent=True)
    if not data or "text" not in data:
        return jsonify({"error": "Request body must be JSON with a 'text' field."}), 400

    raw_text = data["text"]
    text = _strip_think_tags(raw_text)
    af = _active_finder()
    top_n = min(data.get("top_n", DEFAULT_TOP_N), len(af.le.classes_))

    if not isinstance(top_n, int) or top_n < 1:
        top_n = DEFAULT_TOP_N

    if len(text) < 20:
        return jsonify(
            {
                "error": "Text too short (minimum 20 characters after stripping think tags)."
            }
        ), 400

    proba = af.predict_proba(text)
    sorted_providers = sorted(proba.items(), key=lambda x: x[1], reverse=True)[:top_n]

    top_providers = [
        {"name": name, "confidence": round(float(conf * 100), 2)}
        for name, conf in sorted_providers
    ]

    return jsonify(
        {
            "provider": top_providers[0]["name"],
            "confidence": top_providers[0]["confidence"],
            "top_providers": top_providers,
        }
    )


@app.route("/api/correct", methods=["POST"])
def correct():
    global community_finder
    data = request.get_json(silent=True)
    if not data or "text" not in data or "correct_provider" not in data:
        return jsonify({"error": "Need 'text' and 'correct_provider'."}), 400

    provider = data["correct_provider"]
    if provider not in list(finder.le.classes_):
        return jsonify({"error": f"Unknown provider: {provider}"}), 400

    text = _strip_think_tags(data["text"])
    corrections.append({"text": text, "provider": provider})

    texts = [c["text"] for c in corrections]
    providers = [c["provider"] for c in corrections]
    X = finder.pipeline.transform(texts)
    y = finder.le.transform(providers)

    rf = RandomForestClassifier(n_estimators=100, random_state=42, n_jobs=-1)
    rf.fit(X, y)

    joblib.dump([rf], os.path.join(COMMUNITY_DIR, "rf_4provider.joblib"))
    joblib.dump(finder.pipeline, os.path.join(COMMUNITY_DIR, "pipeline_4provider.joblib"))
    joblib.dump(finder.le, os.path.join(COMMUNITY_DIR, "enc_4provider.joblib"))
    joblib.dump(corrections, CORRECTIONS_FILE)

    community_finder = AIFinder(model_dir=COMMUNITY_DIR)

    return jsonify({"status": "ok", "loss": 0.0, "corrections": len(corrections)})


@app.route("/api/save", methods=["POST"])
def save_model():
    if community_finder is None:
        return jsonify({"error": "No community model trained yet."}), 400
    filename = "community_rf_4provider.joblib"
    return jsonify({"status": "ok", "filename": filename})


@app.route("/api/toggle_community", methods=["POST"])
def toggle_community():
    global using_community
    data = request.get_json(silent=True) or {}
    using_community = bool(data.get("enabled", not using_community))
    return jsonify({"using_community": using_community, "available": community_finder is not None})


@app.route("/models/<filename>")
def download_model(filename):
    if filename.startswith("community_"):
        return send_from_directory(COMMUNITY_DIR, filename.replace("community_", "", 1))
    return send_from_directory(MODEL_DIR, filename)


@app.route("/api/status", methods=["GET"])
def status():
    af = _active_finder()
    return jsonify(
        {
            "loaded": af is not None,
            "device": "cpu",
            "providers": list(af.le.classes_) if af else [],
            "num_providers": len(af.le.classes_) if af else 0,
            "using_community": using_community,
            "community_available": community_finder is not None,
            "corrections_count": len(corrections),
        }
    )


@app.route("/api/providers", methods=["GET"])
def providers():
    return jsonify(
        {
            "providers": list(finder.le.classes_) if finder else [],
        }
    )


if __name__ == "__main__":
    print("Loading models...")
    load_models()
    print(
        f"Ready on cpu — {len(finder.le.classes_)} providers: "
        f"{', '.join(finder.le.classes_)}"
    )
    app.run(host="0.0.0.0", port=7860)