File size: 8,957 Bytes
5265538
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e390114
 
 
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
from __future__ import annotations

import json
import logging
import os
import uuid
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime

import structlog
from flask import Flask, Response, jsonify, render_template, request, stream_with_context
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address

from rag import answer, build_index, index_exists, load_index
from rag.memory import create_session, get_recent_history, save_message

logging.basicConfig(level=logging.INFO, format="%(message)s")
structlog.configure(
    processors=[
        structlog.stdlib.filter_by_level,
        structlog.stdlib.add_logger_name,
        structlog.stdlib.add_log_level,
        structlog.stdlib.PositionalArgumentsFormatter(),
        structlog.processors.TimeStamper(fmt="iso"),
        structlog.processors.JSONRenderer(),
    ],
    context_class=dict,
    logger_factory=structlog.stdlib.LoggerFactory(),
    cache_logger_on_first_use=True,
)
logger = structlog.get_logger(__name__)

os.environ.setdefault("HF_HUB_OFFLINE", "1")

app = Flask(__name__)
app.config["MAX_CONTENT_LENGTH"] = 50 * 1024 * 1024

limiter = Limiter(
    get_remote_address,
    app=app,
    default_limits=["20 per minute"],
    storage_uri="memory://",
)

_index_lock = ThreadPoolExecutor(max_workers=1)
_index_future = None
_vector_store = None

if index_exists():
    try:
        _vector_store = load_index()
        logger.info("vector_store_loaded")
    except Exception as e:
        logger.warning("vector_store_load_failed", error=str(e))


def _request_id() -> str:
    return request.headers.get("X-Request-Id") or str(uuid.uuid4())


@app.before_request
def _before_request():
    request.rid = _request_id()
    structlog.contextvars.clear_contextvars()
    structlog.contextvars.bind_contextvars(request_id=request.rid)


@app.after_request
def _after_request(response):
    response.headers["X-Request-Id"] = request.rid
    # Prevent browser caching for HTML pages so UI changes are always fresh
    if response.content_type and "text/html" in response.content_type:
        response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
        response.headers["Pragma"] = "no-cache"
        response.headers["Expires"] = "0"
    return response


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


@app.route("/api/health")
def health():
    return jsonify({
        "status": "ok",
        "vector_store_ready": index_exists(),
        "vector_store_loaded": _vector_store is not None,
        "timestamp": datetime.now().isoformat(),
    })


@app.route("/api/sessions", methods=["GET"])
def get_sessions():
    conn = _db()
    rows = conn.execute(
        "SELECT id, title, updated_at FROM sessions ORDER BY updated_at DESC"
    ).fetchall()
    conn.close()
    return jsonify([{"id": r[0], "title": r[1], "updated_at": r[2]} for r in rows])


@app.route("/api/sessions", methods=["POST"])
def create_session_api():
    title = request.json.get("title", "New chat") if request.json else "New chat"
    sid = create_session(title)
    return jsonify({"id": sid, "title": title})


@app.route("/api/sessions/<sid>", methods=["DELETE"])
def delete_session_api(sid):
    conn = _db()
    conn.execute("DELETE FROM messages WHERE session_id=?", (sid,))
    conn.execute("DELETE FROM sessions WHERE id=?", (sid,))
    conn.commit()
    conn.close()
    return jsonify({"ok": True})


@app.route("/api/sessions/<sid>/messages", methods=["GET"])
def get_messages_api(sid):
    msgs = get_recent_history(sid, max_turns=100)
    return jsonify(msgs)


@app.route("/api/chat", methods=["POST"])
@limiter.limit("10 per minute")
def chat():
    global _vector_store
    data = request.json or {}
    sid = data.get("session_id")
    query = data.get("message", "").strip()

    if not sid or not query:
        return jsonify({"error": "session_id and message required"}), 400

    save_message(sid, "user", query)

    msgs = get_recent_history(sid, max_turns=1)
    if len(msgs) <= 2:
        conn = _db()
        conn.execute("UPDATE sessions SET title=? WHERE id=?", (query[:50] + ("..." if len(query) > 50 else ""), sid))
        conn.commit()
        conn.close()

    def generate():
        try:
            if _vector_store is None:
                text = "No documents loaded. Upload PDFs and build the vector store first."
                confidence = 0.0
            else:
                result = answer(query, _vector_store, session_id=sid)
                text = result.get("answer", "No answer generated.")
                confidence = result.get("confidence", 0.0)

        except Exception as e:
            logger.error("answer_failed", error=str(e))
            text = f"Error: {str(e)[:200]}"
            confidence = 0.0

        words = text.split(" ")
        for i, word in enumerate(words):
            chunk = word + (" " if i < len(words) - 1 else "")
            yield f"data: {json.dumps({'token': chunk})}\n\n"

        yield f"data: {json.dumps({'done': True, 'full': text, 'confidence': confidence})}\n\n"

    return Response(
        stream_with_context(generate()),
        mimetype="text/event-stream",
        headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"},
    )


@app.route("/api/upload", methods=["POST"])
def upload():
    files = request.files.getlist("files")
    if not files:
        return jsonify({"error": "No files"}), 400
    os.makedirs("Artifacts", exist_ok=True)
    saved = []
    for f in files:
        if f.filename and f.filename.endswith(".pdf"):
            path = os.path.join("Artifacts", f.filename)
            f.save(path)
            saved.append(f.filename)
    return jsonify({"saved": saved})


@app.route("/api/vector-store/status", methods=["GET"])
def vs_status():
    global _index_future
    return jsonify({
        "ready": index_exists(),
        "building": _index_future is not None and not _index_future.done(),
    })


@app.route("/api/vector-store/build", methods=["POST"])
def build_vs():
    global _vector_store, _index_future

    if _index_future and not _index_future.done():
        return jsonify({"error": "Build already in progress"}), 409

    def _rebuild():
        global _vector_store
        logger.info("rebuild_started")
        _vector_store = build_index()
        logger.info("rebuild_complete", chunks=len(_vector_store.chunks))
        return _vector_store

    _index_future = _index_lock.submit(_rebuild)
    return jsonify({"ok": True, "message": "Build started in background"})


@app.route("/api/vector-store/delete", methods=["POST"])
def delete_vs():
    global _vector_store
    import shutil
    from rag.config import settings

    if os.path.exists(settings.VECTOR_STORE_PATH):
        try:
            shutil.rmtree(settings.VECTOR_STORE_PATH)
            logger.info("Deleted vector store index")
        except Exception as e:
            logger.error("Failed to delete vector store index: %s", e)

    if os.path.exists(settings.ARTIFACTS_DIR):
        try:
            shutil.rmtree(settings.ARTIFACTS_DIR)
            logger.info("Deleted artifacts directory")
        except Exception as e:
            logger.error("Failed to delete artifacts directory: %s", e)

    _vector_store = None
    return jsonify({"ok": True, "message": "Vector store and document storage deleted successfully."})



@app.route("/api/build-status", methods=["GET"])
def build_status():
    global _index_future
    if _index_future is None:
        return jsonify({"status": "idle"})
    if _index_future.done():
        exc = _index_future.exception()
        if exc:
            return jsonify({"status": "failed", "error": str(exc)})
        return jsonify({"status": "completed"})
    return jsonify({"status": "building"})


def _db():
    import sqlite3
    conn = sqlite3.connect("chat_history.db")
    conn.row_factory = sqlite3.Row
    conn.execute("PRAGMA journal_mode=WAL")
    conn.execute("""CREATE TABLE IF NOT EXISTS sessions (

        id TEXT PRIMARY KEY, title TEXT NOT NULL,

        created_at TEXT NOT NULL, updated_at TEXT NOT NULL)""")
    conn.execute("""CREATE TABLE IF NOT EXISTS messages (

        id INTEGER PRIMARY KEY AUTOINCREMENT,

        session_id TEXT NOT NULL,

        role TEXT NOT NULL,

        content TEXT NOT NULL,

        created_at TEXT NOT NULL,

        FOREIGN KEY (session_id) REFERENCES sessions(id))""")
    conn.commit()
    return conn


if __name__ == "__main__":
    import waitress
    port = int(os.environ.get("PORT", 5000))
    logger.info("starting_server", port=port)
    waitress.serve(app, host="0.0.0.0", port=port, threads=8)