File size: 1,332 Bytes
1f14da1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from flask import Flask, request, jsonify
from flask_cors import CORS
from sethu_agent import agent_inference, translate_to_english

app = Flask(__name__)
CORS(app)


@app.get("/")
def health_check():
    return jsonify({
        "status": "ok",
        "message": "Legal / Immigration Context Retrieval API is running"
    })


@app.post("/chat")
def chat():
    data = request.get_json(force=True)

    question = data.get("question")
    language = data.get("language", "en")  # "en" or "si"

    if not question:
        return jsonify({"error": "Question is required"}), 400

    try:
        # If Sinhala, translate question to English for retrieval
        backend_question = question
        if language == "si":
            backend_question = translate_to_english(question)

        # Pass language so LLM responds in correct language directly
        answer = agent_inference(backend_question, language)

        print("ANSWER:", answer)

        return jsonify({
            "question": question,
            "answer": str(answer)
        })

    except Exception as e:
        import traceback
        traceback.print_exc()
        return jsonify({
            "error": "Internal server error",
            "details": str(e)
        }), 500


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