ecom-qa-bert_f / src /app.py
rnyx's picture
Upload 3 files
227802e verified
"""
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)