File size: 3,282 Bytes
227802e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""

FILE 1: src/app.py β€” Flask Server (Entry Point)

=================================================

IMPORTS FROM: model.py (init_model, predict_qa), scraper.py (scrape_url)

SERVES:       templates/index.html via route "/"

CALLED BY:    static/js/main.js via fetch() to /api/scrape and /api/predict



Routes:

  GET  /             β†’ serves index.html (the UI)

  POST /api/scrape   β†’ receives {url}, calls scraper.py, returns scraped text

  POST /api/predict  β†’ receives {question, context}, calls model.py, returns answer

"""

import os
import logging
from flask import Flask, render_template, request, jsonify

from model import init_model, predict_qa
from scraper import scrape_url

FORMAT = "%(asctime)-15s [%(levelname)s] %(message)s"
logging.basicConfig(format=FORMAT, level=logging.INFO)
logger = logging.getLogger(__name__)

app = Flask(
    __name__,
    template_folder=os.path.join(os.path.dirname(__file__), "..", "templates"),
    static_folder=os.path.join(os.path.dirname(__file__), "..", "static"),
)


@app.route("/")
def index():
    """Serve the main UI page."""
    return render_template("index.html")


@app.route("/api/scrape", methods=["POST"])
def api_scrape():
    """

    Called by: main.js β†’ scrapeURL()

    Input:    { "url": "https://amazon.in/..." }

    Output:   { "title", "context", "source", "char_count" }

    Calls:    scraper.py β†’ scrape_url()

    """
    data = request.get_json()
    url = data.get("url", "").strip()
    if not url:
        return jsonify({"error": "URL is required."}), 400
    try:
        result = scrape_url(url)
        if result.get("error"):
            return jsonify(result), 400
        return jsonify(result)
    except Exception as e:
        logger.exception("Scraping failed")
        return jsonify({"error": str(e)}), 500


@app.route("/api/predict", methods=["POST"])
def api_predict():
    """

    Called by: main.js β†’ doAsk()

    Input:    { "question": "What is the battery?", "context": "Samsung Galaxy..." }

    Output:   { "answer", "confidence", "confidence_pct", "confidence_level",

                "tokens", "answer_start_char", "answer_end_char", ... }

    Calls:    model.py β†’ predict_qa()

    """
    data = request.get_json()
    question = data.get("question", "").strip()
    context = data.get("context", "").strip()
    if not question or not context:
        return jsonify({"error": "Both question and context are required."}), 400
    if len(context) < 20:
        return jsonify({"error": "Context too short. Need at least a few sentences."}), 400
    try:
        result = predict_qa(question, context)
        return jsonify(result)
    except Exception as e:
        logger.exception("Prediction failed")
        return jsonify({"error": str(e)}), 500


# Initialize the BERT model at import time so gunicorn workers have it ready.
# This runs both when gunicorn imports the app AND when you run `python app.py` directly.
logger.info("Initializing BERT QA model...")
init_model()
logger.info("Model ready.")

if __name__ == "__main__":
    logger.info("Server starting on http://localhost:5000")
    app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 5000)), debug=False)