File size: 1,305 Bytes
81726c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
529b177
 
 
 
81726c9
 
 
 
 
529b177
81726c9
 
 
 
 
 
 
 
 
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
# app.py

import os
from flask import Flask, request, jsonify, render_template
from dotenv import load_dotenv
from chain.qa_chain import run_chain

load_dotenv()

app = Flask(__name__)

# ── Routes ────────────────────────────────────────────────────────────────────

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


@app.route("/chat", methods=["POST"])
def chat():
    data    = request.get_json()
    query   = data.get("query", "").strip()
    topic   = data.get("topic", None) or None
    history = data.get("history", [])        # list of {role, content}

    if not query:
        return jsonify({"error": "Query is empty."}), 400

    try:
        response = run_chain(query, topic=topic, history=history)
        return jsonify({"response": response})
    except Exception as e:
        return jsonify({"error": str(e)}), 500


# ── Run ───────────────────────────────────────────────────────────────────────

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