Harisri commited on
Commit
80b6680
Β·
0 Parent(s):

Initial deployment: PromiseTrack AI with 15 companies

Browse files
This view is limited to 50 files because it contains too many changes. Β  See raw diff
Files changed (50) hide show
  1. .dockerignore +32 -0
  2. .gitattributes +7 -0
  3. .gitignore +31 -0
  4. Dockerfile +56 -0
  5. README.md +13 -0
  6. app/__init__.py +42 -0
  7. app/routes/__init__.py +16 -0
  8. app/routes/frontend.py +15 -0
  9. app/routes/pipeline.py +153 -0
  10. app/services/cache_service.py +179 -0
  11. app/services/pipeline_service.py +163 -0
  12. app/templates/index.html +855 -0
  13. chroma_db/589bdc22-9a2d-4c25-b432-0b35caad7759/data_level0.bin +3 -0
  14. chroma_db/589bdc22-9a2d-4c25-b432-0b35caad7759/header.bin +3 -0
  15. chroma_db/589bdc22-9a2d-4c25-b432-0b35caad7759/index_metadata.pickle +3 -0
  16. chroma_db/589bdc22-9a2d-4c25-b432-0b35caad7759/length.bin +3 -0
  17. chroma_db/589bdc22-9a2d-4c25-b432-0b35caad7759/link_lists.bin +3 -0
  18. chroma_db/chroma.sqlite3 +3 -0
  19. claim_classification_model_distilbert_trained/claim_classifier_model/checkpoint-902/config.json +28 -0
  20. claim_classification_model_distilbert_trained/claim_classifier_model/checkpoint-902/model.safetensors +3 -0
  21. claim_classification_model_distilbert_trained/claim_classifier_model/checkpoint-902/tokenizer.json +0 -0
  22. claim_classification_model_distilbert_trained/claim_classifier_model/checkpoint-902/tokenizer_config.json +14 -0
  23. claim_classification_model_distilbert_trained/claim_classifier_model/checkpoint-902/trainer_state.json +184 -0
  24. claim_classification_model_distilbert_trained/claim_classifier_model/checkpoint-902/training_args.bin +3 -0
  25. config.py +52 -0
  26. data/logos/AXIS_Bank.png +0 -0
  27. data/logos/BAJAJ_FINANCE.png +0 -0
  28. data/logos/Bharthi_Airtel.png +0 -0
  29. data/logos/HCL.png +0 -0
  30. data/logos/HDFC_Bank.png +0 -0
  31. data/logos/ICICI_Bank.png +0 -0
  32. data/logos/INFOSYS.png +0 -0
  33. data/logos/ITC.png +0 -0
  34. data/logos/Kotak_Mahindra_Bank.png +0 -0
  35. data/logos/L_T.png +0 -0
  36. data/logos/Mahindra___Mahindra.png +0 -0
  37. data/logos/Reliance__Industries.png +0 -0
  38. data/logos/SBI.png +0 -0
  39. data/logos/SUN_PHARMA.png +0 -0
  40. data/logos/TCS.png +0 -0
  41. db.py +101 -0
  42. pipelines/__init__.py +0 -0
  43. pipelines/finance/__init__.py +0 -0
  44. pipelines/finance/extract_xbrl_data.py +239 -0
  45. pipelines/finance/prepare_timeseries_data.py +109 -0
  46. pipelines/finance/verify_claims.py +197 -0
  47. pipelines/ml/__init__.py +0 -0
  48. pipelines/ml/extract_attributes.py +148 -0
  49. pipelines/ml/merge_claim_dataset.py +40 -0
  50. pipelines/ml/non_claim_extractor.py +193 -0
.dockerignore ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ── Raw source data β€” never deployed ─────────────────────────────────────────
2
+ data/raw/
3
+ data/interim/
4
+ data/processed/
5
+ data/vector/
6
+
7
+ # ── Training-only model artifacts (not needed for inference) ─────────────────
8
+ **/optimizer.pt
9
+ **/scheduler.pt
10
+ **/rng_state.pth
11
+
12
+ # ── Pipeline scripts β€” not needed at runtime ──────────────────────────────────
13
+ process_company.py
14
+ XBRL/
15
+
16
+ # ── Zipped archives ───────────────────────────────────────────────────────────
17
+ *.zip
18
+
19
+ # ── Python cache ──────────────────────────────────────────────────────────────
20
+ __pycache__/
21
+ *.py[cod]
22
+ *.pyo
23
+ .pytest_cache/
24
+
25
+ # ── Dev/OS junk ───────────────────────────────────────────────────────────────
26
+ .DS_Store
27
+ .env
28
+ *.log
29
+
30
+ # ── Git ───────────────────────────────────────────────────────────────────────
31
+ .git/
32
+ .gitignore
.gitattributes ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
2
+ *.bin filter=lfs diff=lfs merge=lfs -text
3
+ *.pt filter=lfs diff=lfs merge=lfs -text
4
+ *.pth filter=lfs diff=lfs merge=lfs -text
5
+ *.db filter=lfs diff=lfs merge=lfs -text
6
+ *.sqlite3 filter=lfs diff=lfs merge=lfs -text
7
+ *.pickle filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Raw data β€” never commit
2
+ data/raw/
3
+ data/interim/
4
+ data/processed/
5
+ data/vector/
6
+
7
+ # Training artifacts not needed for inference
8
+ **/optimizer.pt
9
+ **/scheduler.pt
10
+ **/rng_state.pth
11
+
12
+ # Python cache
13
+ __pycache__/
14
+ *.py[cod]
15
+ *.pyo
16
+ .pytest_cache/
17
+
18
+ # Environment & secrets
19
+ .env
20
+
21
+ # OS
22
+ .DS_Store
23
+ *.log
24
+
25
+ # Pipeline scratch
26
+ pipeline_run.log
27
+ rebuild_rag.log
28
+ app.zip
29
+ pipelines.zip
30
+
31
+ XBRL/
Dockerfile ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ── Stage 1: Build image ──────────────────────────────────────────────────────
2
+ FROM python:3.10-slim
3
+
4
+ # System deps needed by some python packages (pdfplumber, lxml, etc.)
5
+ RUN apt-get update && apt-get install -y --no-install-recommends \
6
+ build-essential \
7
+ libglib2.0-0 \
8
+ libgomp1 \
9
+ && rm -rf /var/lib/apt/lists/*
10
+
11
+ WORKDIR /app
12
+
13
+ # ── Install Python deps ───────────────────────────────────────────────────────
14
+ COPY requirements.txt requirements.txt
15
+ RUN pip install --no-cache-dir -r requirements.txt
16
+
17
+ # Download the sentence-transformer model at build time so it's baked in
18
+ # (avoids downloading at runtime on every cold start)
19
+ RUN python -c "from sentence_transformers import SentenceTransformer; SentenceTransformer('all-MiniLM-L6-v2')"
20
+
21
+ # ── Copy application code ─────────────────────────────────────────────────────
22
+ COPY app/ app/
23
+ COPY pipelines/ pipelines/
24
+ COPY config.py config.py
25
+ COPY db.py db.py
26
+ COPY run.py run.py
27
+
28
+ # ── Copy pre-built databases (generated locally before deployment) ─────────────
29
+ # These are read-only at runtime β€” no pipeline runs on the server.
30
+ COPY promisetrack.db promisetrack.db
31
+ COPY chroma_db/ chroma_db/
32
+
33
+ # ── Copy the trained DistilBERT model checkpoint ──────────────────────────────
34
+ COPY claim_classification_model_distilbert_trained/ \
35
+ claim_classification_model_distilbert_trained/
36
+
37
+ # ── Logo cache (optional β€” pre-warm if desired, otherwise fetched on demand) ──
38
+ COPY data/logos/ data/logos/
39
+
40
+ # ── Expose port 7860 (HuggingFace Spaces standard) ───────────────────────────
41
+ EXPOSE 7860
42
+
43
+ # ── Set env defaults (real secrets go in HF Space Settings > Secrets) ─────────
44
+ ENV FLASK_HOST=0.0.0.0 \
45
+ FLASK_PORT=7860 \
46
+ FLASK_DEBUG=false
47
+
48
+ # ── Run with Gunicorn (production-grade, matches your existing Space) ──────────
49
+ # run:app = the `app` object created in run.py
50
+ CMD ["gunicorn", \
51
+ "--bind", "0.0.0.0:7860", \
52
+ "--workers", "1", \
53
+ "--worker-class", "sync", \
54
+ "--worker-tmp-dir", "/dev/shm", \
55
+ "--timeout", "180", \
56
+ "run:app"]
README.md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: PromiseTrack AI
3
+ emoji: πŸ“Š
4
+ colorFrom: blue
5
+ colorTo: indigo
6
+ sdk: docker
7
+ pinned: false
8
+ license: mit
9
+ ---
10
+
11
+ # PromiseTrack AI
12
+
13
+ An AI system that tracks forward-looking management commitments and measures whether they materialise in real financial outcomes.
app/__init__.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ app/__init__.py
3
+ Flask application factory.
4
+ """
5
+
6
+ from flask import Flask
7
+
8
+ import config
9
+ from db import init_db
10
+ from pipelines.ml.run_claim_model import load_claim_model
11
+ from pipelines.rag.build_vector_db import load_vector_db
12
+ from pipelines.rag.rag_explainer import load_groq_client
13
+
14
+
15
+ def create_app() -> Flask:
16
+ app = Flask(
17
+ __name__,
18
+ template_folder="templates",
19
+ static_folder=str(config.FRONTEND_DIR),
20
+ static_url_path="",
21
+ )
22
+
23
+ app.secret_key = config.SECRET_KEY
24
+ app.config["MAX_CONTENT_LENGTH"] = 32 * 1024 * 1024
25
+ app.config["UPLOAD_FOLDER"] = str(config.DATA_DIR)
26
+
27
+ # Initialise DB schema (creates tables if they don't exist)
28
+ init_db()
29
+
30
+ # Load heavyweight models once at startup
31
+ load_claim_model(config.MODEL_PATH)
32
+ load_vector_db(config.CHROMA_DB_PATH)
33
+ load_groq_client()
34
+
35
+ # Register blueprints
36
+ from app.routes.frontend import frontend_bp
37
+ from app.routes.pipeline import pipeline_bp
38
+
39
+ app.register_blueprint(pipeline_bp, url_prefix="/api")
40
+ app.register_blueprint(frontend_bp)
41
+
42
+ return app
app/routes/__init__.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ app/routes/__init__.py
3
+ Registers all blueprints with the Flask app.
4
+ Import and call register_blueprints(app) from create_app().
5
+ """
6
+
7
+ from flask import Flask
8
+
9
+
10
+ def register_blueprints(app: Flask) -> None:
11
+ from app.routes.frontend import frontend_bp
12
+ from app.routes.pipeline import pipeline_bp
13
+
14
+ app.register_blueprint(frontend_bp)
15
+ app.register_blueprint(pipeline_bp, url_prefix="/api")
16
+
app/routes/frontend.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ app/routes/frontend.py
3
+ Serves the frontend index.html for all non-API routes.
4
+ """
5
+
6
+ from flask import Blueprint, render_template
7
+
8
+ frontend_bp = Blueprint("frontend", __name__)
9
+
10
+
11
+ @frontend_bp.route("/", defaults={"path": ""})
12
+ @frontend_bp.route("/<path:path>")
13
+ def serve_frontend(path):
14
+ return render_template("index.html")
15
+
app/routes/pipeline.py ADDED
@@ -0,0 +1,153 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ app/routes/pipeline.py
3
+ All API endpoints. Every route delegates to pipeline_service β€” no pipeline
4
+ logic lives here.
5
+ """
6
+
7
+ from flask import Blueprint, jsonify, request
8
+
9
+ from app.services.pipeline_service import analyse_company, get_known_companies
10
+ from pipelines.ml.run_claim_model import is_model_loaded
11
+ from pipelines.rag.build_vector_db import is_vector_db_loaded, build_vector_db
12
+
13
+ pipeline_bp = Blueprint("pipeline", __name__)
14
+
15
+
16
+ # ── Health check ──────────────────────────────────────────────────────────────
17
+
18
+ @pipeline_bp.get("/health")
19
+ def health():
20
+ return jsonify({
21
+ "status": "ok",
22
+ "model_loaded": is_model_loaded(),
23
+ "vector_db_loaded": is_vector_db_loaded(),
24
+ })
25
+
26
+
27
+ # ── Companies list (for autocomplete) ────────────────────────────────────────
28
+
29
+ @pipeline_bp.get("/companies")
30
+ def companies():
31
+ return jsonify(get_known_companies())
32
+
33
+
34
+ # ── Main analysis endpoint ────────────────────────────────────────────────────
35
+
36
+ @pipeline_bp.post("/analyse")
37
+ def analyse():
38
+ body = request.get_json(silent=True) or {}
39
+ company = (body.get("company") or "").strip()
40
+ mode = (body.get("mode") or "full").strip().lower()
41
+
42
+ if not company:
43
+ return jsonify({"error": "company is required"}), 400
44
+ if mode not in ("full", "earnings", "financial"):
45
+ return jsonify({"error": "mode must be full | earnings | financial"}), 400
46
+
47
+ try:
48
+ result = analyse_company(company, mode)
49
+ # Cache miss β€” return 404 with helpful message
50
+ if "error" in result and result["error"] in ("not_found", "not_cached"):
51
+ return jsonify(result), 404
52
+ return jsonify(result)
53
+ except Exception as exc:
54
+ return jsonify({"error": str(exc)}), 500
55
+
56
+
57
+ # ── Vector DB query endpoint ─────────────────────────────────────────────────
58
+
59
+ @pipeline_bp.post("/chat")
60
+ def chat():
61
+ body = request.get_json(silent=True) or {}
62
+ company = (body.get("company") or "").strip()
63
+ query = (body.get("query") or "").strip()
64
+
65
+ if not company:
66
+ return jsonify({"error": "company is required"}), 400
67
+ if not query:
68
+ return jsonify({"error": "query is required"}), 400
69
+
70
+ try:
71
+ from app.services.pipeline_service import chat_with_company
72
+ result = chat_with_company(company, query)
73
+ if "error" in result and result["error"] == "not_found":
74
+ return jsonify(result), 404
75
+ return jsonify(result)
76
+ except Exception as exc:
77
+ return jsonify({"error": str(exc)}), 500
78
+
79
+ # ── Vector DB build endpoint (admin / one-time setup) ─────────────────────────
80
+
81
+ @pipeline_bp.post("/admin/build-vector-db")
82
+ def admin_build_vector_db():
83
+ body = request.get_json(silent=True) or {}
84
+ records = body.get("verified_records", [])
85
+ if not records:
86
+ return jsonify({"error": "verified_records is required"}), 400
87
+ try:
88
+ status = build_vector_db(records)
89
+ return jsonify(status)
90
+ except Exception as exc:
91
+ return jsonify({"error": str(exc)}), 500
92
+
93
+
94
+ # ── Logo cache endpoint ──────────────────────────────────────────────────────
95
+
96
+ @pipeline_bp.get("/logo/<company_name>")
97
+ def get_company_logo(company_name):
98
+ import os
99
+ import requests
100
+ from flask import send_file
101
+
102
+ LOGO_DEV_PUBLIC_KEY = 'pk_MmKxh9tYQ1Gvapx5RFmcTA'
103
+ LOGO_CACHE_DIR = os.path.join("data", "logos")
104
+ os.makedirs(LOGO_CACHE_DIR, exist_ok=True)
105
+
106
+ # We use a safe filename
107
+ import re
108
+ safe_name = re.sub(r'[^a-zA-Z0-9_\-]', '_', company_name)
109
+ cache_path = os.path.join(LOGO_CACHE_DIR, f"{safe_name}.png")
110
+
111
+ if os.path.exists(cache_path):
112
+ return send_file(os.path.abspath(cache_path), mimetype='image/png')
113
+
114
+ DOMAIN_MAP = {
115
+ # Current folder names (updated after data reorganization)
116
+ "INFOSYS": "infosys.com",
117
+ "BAJAJ FINANCE": "bajajfinserv.in",
118
+ "Kotak Mahindra Bank": "kotakbank.com",
119
+ "SUN PHARMA": "sunpharma.com",
120
+ "Reliance Industries": "ril.com",
121
+ "Reliance Industries": "ril.com",
122
+ "TCS": "tcs.com",
123
+ "HDFC Bank": "hdfcbank.com",
124
+ "Mahindra & Mahindra": "mahindra.com",
125
+ "SBI": "sbi.co.in",
126
+ "ITC": "itcportal.com",
127
+ "L&T": "larsentoubro.com",
128
+ "ICICI Bank": "icicibank.com",
129
+ "HCL": "hcltech.com",
130
+ "Bharthi Airtel": "airtel.in",
131
+ "AXIS Bank": "axisbank.com",
132
+ }
133
+
134
+ try:
135
+ if company_name in DOMAIN_MAP:
136
+ domain = DOMAIN_MAP[company_name]
137
+ url = f"https://img.logo.dev/{domain}?size=120&token={LOGO_DEV_PUBLIC_KEY}"
138
+ else:
139
+ import urllib.parse
140
+ encoded_name = urllib.parse.quote(company_name)
141
+ url = f"https://img.logo.dev/name/{encoded_name}?size=120&token={LOGO_DEV_PUBLIC_KEY}"
142
+
143
+ response = requests.get(url, timeout=10)
144
+
145
+ if response.status_code == 200:
146
+ with open(cache_path, 'wb') as f:
147
+ f.write(response.content)
148
+ return send_file(os.path.abspath(cache_path), mimetype='image/png')
149
+ else:
150
+ return jsonify({"error": "Logo not found"}), response.status_code
151
+
152
+ except Exception as exc:
153
+ return jsonify({"error": str(exc)}), 500
app/services/cache_service.py ADDED
@@ -0,0 +1,179 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ app/services/cache_service.py
3
+ All SQLite read/write operations for the cache layer.
4
+ No pipeline logic lives here β€” only DB operations.
5
+ """
6
+
7
+ import json
8
+ import re
9
+ from datetime import datetime
10
+ from typing import Optional
11
+
12
+ from db import get_db
13
+
14
+
15
+ # ── Helpers ───────────────────────────────────────────────────────────────────
16
+
17
+ def _clean_display_name(folder_name: str) -> str:
18
+ """Strip year/quarter suffixes from folder names."""
19
+ name = re.sub(r'\s*\d{4}\s*Quarterly\s*Data\s*', '', folder_name, flags=re.IGNORECASE)
20
+ name = re.sub(r'\s*Quarterly\s*Data\s*', '', name, flags=re.IGNORECASE)
21
+ return name.strip()
22
+
23
+
24
+ # ── Company registry ──────────────────────────────────────────────────────────
25
+
26
+ def get_all_companies() -> list:
27
+ """Return all companies with their processing status."""
28
+ with get_db() as conn:
29
+ rows = conn.execute(
30
+ "SELECT id, folder_name, display_name, status, processed_at FROM companies ORDER BY display_name"
31
+ ).fetchall()
32
+ return [dict(r) for r in rows]
33
+
34
+
35
+ def get_ready_companies() -> list:
36
+ """Return only companies that have been successfully processed."""
37
+ with get_db() as conn:
38
+ rows = conn.execute(
39
+ "SELECT id, folder_name, display_name, processed_at FROM companies WHERE status = 'ready' ORDER BY display_name"
40
+ ).fetchall()
41
+ return [dict(r) for r in rows]
42
+
43
+
44
+ def get_company_by_folder(folder_name: str) -> Optional[dict]:
45
+ with get_db() as conn:
46
+ row = conn.execute(
47
+ "SELECT * FROM companies WHERE folder_name = ?", (folder_name,)
48
+ ).fetchone()
49
+ return dict(row) if row else None
50
+
51
+
52
+ def upsert_company(folder_name: str, status: str = "pending", error_msg: str = None) -> int:
53
+ """Insert or update a company record. Returns company id."""
54
+ display = _clean_display_name(folder_name)
55
+ with get_db() as conn:
56
+ conn.execute("""
57
+ INSERT INTO companies (folder_name, display_name, status, processed_at, error_msg)
58
+ VALUES (?, ?, ?, ?, ?)
59
+ ON CONFLICT(folder_name) DO UPDATE SET
60
+ status = excluded.status,
61
+ processed_at = excluded.processed_at,
62
+ error_msg = excluded.error_msg
63
+ """, (folder_name, display, status,
64
+ datetime.utcnow().isoformat() if status == "ready" else None,
65
+ error_msg))
66
+ row = conn.execute(
67
+ "SELECT id FROM companies WHERE folder_name = ?", (folder_name,)
68
+ ).fetchone()
69
+ return row["id"]
70
+
71
+
72
+ # ── Analysis cache ────────────────────────────────────────────────────────────
73
+
74
+ def get_cached_analysis(company_id: int, mode: str) -> Optional[dict]:
75
+ """Return cached analysis result or None if not found."""
76
+ with get_db() as conn:
77
+ row = conn.execute(
78
+ "SELECT result_json FROM analysis_cache WHERE company_id = ? AND mode = ?",
79
+ (company_id, mode)
80
+ ).fetchone()
81
+ return json.loads(row["result_json"]) if row else None
82
+
83
+
84
+ def save_analysis(company_id: int, mode: str, result: dict) -> None:
85
+ """Save (overwrite) analysis result for a company+mode."""
86
+ with get_db() as conn:
87
+ conn.execute("""
88
+ INSERT INTO analysis_cache (company_id, mode, result_json, created_at)
89
+ VALUES (?, ?, ?, ?)
90
+ ON CONFLICT(company_id, mode) DO UPDATE SET
91
+ result_json = excluded.result_json,
92
+ created_at = excluded.created_at
93
+ """, (company_id, mode, json.dumps(result), datetime.utcnow().isoformat()))
94
+
95
+
96
+ # ── Claims ────────────────────────────────────────────────────────────────────
97
+
98
+ def save_claims(company_id: int, claims: list) -> None:
99
+ """Overwrite all claims for a company."""
100
+ with get_db() as conn:
101
+ conn.execute("DELETE FROM claims WHERE company_id = ?", (company_id,))
102
+ conn.executemany("""
103
+ INSERT INTO claims
104
+ (company_id, quarter, sentence, metric, direction, magnitude, result, actual_change, confidence)
105
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
106
+ """, [(
107
+ company_id,
108
+ c.get("quarter"),
109
+ c.get("sentence"),
110
+ c.get("metric"),
111
+ c.get("direction"),
112
+ c.get("magnitude"),
113
+ c.get("result"),
114
+ c.get("actual_change"),
115
+ c.get("confidence"),
116
+ ) for c in claims])
117
+
118
+
119
+ def get_claims(company_id: int) -> list:
120
+ with get_db() as conn:
121
+ rows = conn.execute(
122
+ "SELECT * FROM claims WHERE company_id = ? ORDER BY quarter", (company_id,)
123
+ ).fetchall()
124
+ return [dict(r) for r in rows]
125
+
126
+
127
+ # ── Timeseries ────────────────────────────────────────────────────────────────
128
+
129
+ def save_timeseries(company_id: int, ts_records: list) -> None:
130
+ """Overwrite all timeseries rows for a company."""
131
+ with get_db() as conn:
132
+ conn.execute("DELETE FROM timeseries WHERE company_id = ?", (company_id,))
133
+ conn.executemany("""
134
+ INSERT OR REPLACE INTO timeseries
135
+ (company_id, quarter, revenue, net_profit, operating_profit, profit_margin,
136
+ revenue_qoq_change, net_profit_qoq_change, operating_profit_qoq_change, profit_margin_qoq_change,
137
+ revenue_yoy_change, net_profit_yoy_change, operating_profit_yoy_change, profit_margin_yoy_change)
138
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
139
+ """, [(
140
+ company_id,
141
+ r.get("quarter"),
142
+ r.get("revenue"),
143
+ r.get("net_profit"),
144
+ r.get("operating_profit"),
145
+ r.get("profit_margin"),
146
+ r.get("revenue_qoq_change"),
147
+ r.get("net_profit_qoq_change"),
148
+ r.get("operating_profit_qoq_change"),
149
+ r.get("profit_margin_qoq_change"),
150
+ r.get("revenue_yoy_change"),
151
+ r.get("net_profit_yoy_change"),
152
+ r.get("operating_profit_yoy_change"),
153
+ r.get("profit_margin_yoy_change"),
154
+ ) for r in ts_records])
155
+
156
+
157
+ # ── Risk ──────────────────────────────────────────────────────────────────────
158
+
159
+ def save_risk(company_id: int, risk_records: list) -> None:
160
+ """Overwrite all risk rows for a company."""
161
+ with get_db() as conn:
162
+ conn.execute("DELETE FROM risk WHERE company_id = ?", (company_id,))
163
+ conn.executemany("""
164
+ INSERT OR REPLACE INTO risk
165
+ (company_id, quarter, total_claims, verification_rate, failure_rate,
166
+ partial_rate, direction_mismatch_rate, consistency_score, risk_drift, warning_flag)
167
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
168
+ """, [(
169
+ company_id,
170
+ r.get("quarter"),
171
+ r.get("total_claims"),
172
+ r.get("verification_rate"),
173
+ r.get("failure_rate"),
174
+ r.get("partial_rate"),
175
+ r.get("direction_mismatch_rate"),
176
+ r.get("consistency_score"),
177
+ r.get("risk_drift"),
178
+ r.get("warning_flag"),
179
+ ) for r in risk_records])
app/services/pipeline_service.py ADDED
@@ -0,0 +1,163 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ app/services/pipeline_service.py
3
+ Serves analysis results from SQLite cache.
4
+ No pipeline logic runs here β€” all processing happens in process_company.py.
5
+ """
6
+
7
+ import difflib
8
+ from app.services.cache_service import (
9
+ get_ready_companies,
10
+ get_cached_analysis,
11
+ )
12
+
13
+
14
+ def get_known_companies() -> list:
15
+ """
16
+ Returns list of processed companies for the autocomplete dropdown.
17
+ Only companies with status='ready' are returned.
18
+ Automatically reflects new companies added via process_company.py.
19
+ """
20
+ return [
21
+ {"display": c["display_name"], "folder": c["folder_name"]}
22
+ for c in get_ready_companies()
23
+ ]
24
+
25
+
26
+ def _fuzzy_match_company(query: str):
27
+ """
28
+ Finds the best matching company from the DB for a user query.
29
+ Tries exact β†’ substring β†’ difflib fuzzy in order.
30
+ Returns the full company DB row or None.
31
+ """
32
+ companies = get_ready_companies()
33
+ if not companies:
34
+ return None
35
+
36
+ query_lower = query.strip().lower()
37
+
38
+ lookup = {}
39
+ for c in companies:
40
+ lookup[c["display_name"].lower()] = c
41
+ lookup[c["folder_name"].lower()] = c
42
+
43
+ if query_lower in lookup:
44
+ return lookup[query_lower]
45
+
46
+ for key, company in lookup.items():
47
+ if query_lower in key or key.startswith(query_lower):
48
+ return company
49
+
50
+ matches = difflib.get_close_matches(query_lower, lookup.keys(), n=1, cutoff=0.5)
51
+ if matches:
52
+ return lookup[matches[0]]
53
+
54
+ return None
55
+
56
+
57
+ def analyse_company(company_name: str, mode: str) -> dict:
58
+ """
59
+ Looks up cached analysis for the best matching company.
60
+ Returns the cached result dict, or an informative error dict if not found.
61
+ """
62
+ company = _fuzzy_match_company(company_name)
63
+
64
+ if not company:
65
+ return {
66
+ "error": "not_found",
67
+ "message": (
68
+ f"No processed data found for '{company_name}'. "
69
+ f"Run: python3 process_company.py \"{company_name}\""
70
+ ),
71
+ "company": company_name,
72
+ "mode": mode,
73
+ }
74
+
75
+ result = get_cached_analysis(company["id"], mode)
76
+
77
+ if not result:
78
+ return {
79
+ "error": "not_cached",
80
+ "message": (
81
+ f"'{company['display_name']}' is registered but mode '{mode}' "
82
+ f"has not been cached yet. "
83
+ f"Run: python3 process_company.py \"{company['display_name']}\""
84
+ ),
85
+ "company": company["display_name"],
86
+ "mode": mode,
87
+ }
88
+
89
+ return result
90
+
91
+ def chat_with_company(company_name: str, query: str) -> dict:
92
+ """
93
+ Handles interactive RAG chat by querying ChromaDB directly and passing context to Groq LLM.
94
+ """
95
+ company = _fuzzy_match_company(company_name)
96
+
97
+ if not company:
98
+ return {
99
+ "error": "not_found",
100
+ "message": f"No processed data found for '{company_name}'.",
101
+ "company": company_name,
102
+ }
103
+
104
+ display_name = company["display_name"]
105
+
106
+ try:
107
+ import config
108
+ import chromadb
109
+ from groq import Groq
110
+
111
+ # 1. Query ChromaDB for relevant claims
112
+ chroma_client = chromadb.PersistentClient(path=config.CHROMA_DB_PATH)
113
+ collection = chroma_client.get_collection(name=config.CHROMA_COLLECTION)
114
+
115
+ results = collection.query(
116
+ query_texts=[query],
117
+ n_results=config.RAG_TOP_K,
118
+ where={"company": display_name}
119
+ )
120
+
121
+ # If no context is found
122
+ if not results or not results.get("documents") or not results["documents"][0]:
123
+ return {"answer": f"I couldn't find any specific claims or financial data regarding your query for {display_name} in my database."}
124
+
125
+ documents = results["documents"][0]
126
+ metadatas = results["metadatas"][0]
127
+
128
+ # 2. Format context for the LLM
129
+ context_blocks = []
130
+ for doc, meta in zip(documents, metadatas):
131
+ q = meta.get("quarter", "Unknown Qtr")
132
+ res = meta.get("result", "UNVERIFIED")
133
+ context_blocks.append(f"[{q}] Claim ({res}): {doc}")
134
+
135
+ context_str = "\n".join(context_blocks)
136
+
137
+ # 3. Ask Groq LLM
138
+ client = Groq(api_key=config.GROQ_API_KEY)
139
+ prompt = f"""You are a financial AI assistant answering questions about {display_name}.
140
+ Use ONLY the following extracted claims and verified outcomes to answer the question.
141
+ If the context doesn't contain the answer, explicitly state that you don't have enough information.
142
+ Keep your answer concise, analytical, and professional.
143
+
144
+ CONTEXT:
145
+ {context_str}
146
+
147
+ QUESTION:
148
+ {query}
149
+ """
150
+
151
+ completion = client.chat.completions.create(
152
+ model=config.GROQ_MODEL,
153
+ messages=[{"role": "user", "content": prompt}],
154
+ temperature=0.2,
155
+ max_tokens=600
156
+ )
157
+
158
+ return {"answer": completion.choices[0].message.content}
159
+
160
+ except Exception as e:
161
+ import traceback
162
+ traceback.print_exc()
163
+ return {"error": "rag_error", "answer": f"Backend connected, but RAG encountered an error: {str(e)}"}
app/templates/index.html ADDED
@@ -0,0 +1,855 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en" class="dark">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>PromiseTrack AI</title>
7
+ <script src="https://cdn.tailwindcss.com"></script>
8
+ <link rel="preconnect" href="https://fonts.googleapis.com">
9
+ <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
10
+ <link href="https://fonts.googleapis.com/css2?family=DM+Sans:wght@400;500;600;700&family=Playfair+Display:wght@600;700;800&family=JetBrains+Mono:wght@400;500;600&display=swap" rel="stylesheet">
11
+
12
+ <script>
13
+ tailwind.config = {
14
+ darkMode: 'class',
15
+ theme: {
16
+ extend: {
17
+ fontFamily: {
18
+ sans: ['DM Sans', 'sans-serif'],
19
+ serif: ['Playfair Display', 'serif'],
20
+ mono: ['JetBrains Mono', 'monospace'],
21
+ },
22
+ colors: {
23
+ pt: {
24
+ bg: '#0D1117',
25
+ surface: '#161B22',
26
+ accent: '#4493F8',
27
+ success: '#3FB950',
28
+ danger: '#F85149',
29
+ warning: '#D29922'
30
+ }
31
+ }
32
+ }
33
+ }
34
+ }
35
+ </script>
36
+ <style>
37
+ body {
38
+ background-color: #F8FAFC; /* Light mode bg */
39
+ color: #0F172A; /* Light mode text */
40
+ position: relative;
41
+ overflow-x: hidden;
42
+ transition: background-color 0.3s ease, color 0.3s ease;
43
+ }
44
+
45
+ /* Dark mode body styles */
46
+ html.dark body {
47
+ background-color: theme('colors.pt.bg');
48
+ background-image: radial-gradient(circle at 45% 35%, #0D1F3C 0%, #0D1117 100%);
49
+ background-attachment: fixed;
50
+ color: #E6EDF3;
51
+ }
52
+
53
+ /* Nav & Panel Glass effects */
54
+ .glass-panel {
55
+ background: rgba(255, 255, 255, 0.9);
56
+ backdrop-filter: blur(24px);
57
+ -webkit-backdrop-filter: blur(24px);
58
+ border: 1px solid rgba(0, 0, 0, 0.08);
59
+ box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.1);
60
+ }
61
+
62
+ html.dark .glass-panel {
63
+ background: rgba(22, 27, 34, 0.75);
64
+ border: 1px solid rgba(255, 255, 255, 0.08);
65
+ box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
66
+ }
67
+
68
+ .glass-card {
69
+ background: rgba(255, 255, 255, 0.6);
70
+ backdrop-filter: blur(12px);
71
+ border: 1px solid rgba(0, 0, 0, 0.05);
72
+ transition: all 0.3s ease;
73
+ box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.05);
74
+ }
75
+
76
+ html.dark .glass-card {
77
+ background: rgba(30, 41, 59, 0.3);
78
+ border: 1px solid rgba(255, 255, 255, 0.05);
79
+ box-shadow: none;
80
+ }
81
+
82
+ .glass-card:hover {
83
+ border-color: rgba(68, 147, 248, 0.4);
84
+ transform: translateY(-4px);
85
+ box-shadow: 0 10px 30px -10px rgba(68, 147, 248, 0.15);
86
+ }
87
+
88
+ html.dark .glass-card:hover {
89
+ background: rgba(30, 41, 59, 0.6);
90
+ border-color: rgba(68, 147, 248, 0.3);
91
+ box-shadow: 0 10px 30px -10px rgba(68, 147, 248, 0.2);
92
+ }
93
+
94
+ /* Scrollbar styles */
95
+ .custom-scrollbar::-webkit-scrollbar { width: 6px; height: 6px; }
96
+ .custom-scrollbar::-webkit-scrollbar-track { background: rgba(0,0,0,0.05); border-radius: 4px; }
97
+ html.dark .custom-scrollbar::-webkit-scrollbar-track { background: rgba(0,0,0,0.2); }
98
+
99
+ .custom-scrollbar::-webkit-scrollbar-thumb { background: rgba(0,0,0,0.2); border-radius: 4px; }
100
+ html.dark .custom-scrollbar::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.15); }
101
+
102
+ .custom-scrollbar::-webkit-scrollbar-thumb:hover { background: rgba(0,0,0,0.3); }
103
+ html.dark .custom-scrollbar::-webkit-scrollbar-thumb:hover { background: rgba(255,255,255,0.25); }
104
+
105
+ .glow-text { text-shadow: 0 0 20px rgba(68, 147, 248, 0.2); }
106
+ html.dark .glow-text { text-shadow: 0 0 20px rgba(68, 147, 248, 0.4); }
107
+
108
+ /* SVG Graph Animation */
109
+ .draw-path {
110
+ stroke-dasharray: 500;
111
+ stroke-dashoffset: 500;
112
+ animation: drawPath 1.5s ease-in-out forwards;
113
+ animation-delay: 200ms;
114
+ }
115
+ @keyframes drawPath { to { stroke-dashoffset: 0; } }
116
+
117
+ /* General Animations */
118
+ .fade-in { animation: fadeIn 0.4s ease-out forwards; }
119
+ @keyframes fadeIn {
120
+ from { opacity: 0; transform: translateY(10px); }
121
+ to { opacity: 1; transform: translateY(0); }
122
+ }
123
+
124
+ .hidden-panel {
125
+ opacity: 0;
126
+ pointer-events: none;
127
+ transform: scale(0.98);
128
+ transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
129
+ }
130
+ .show-panel {
131
+ opacity: 1;
132
+ pointer-events: auto;
133
+ transform: scale(1);
134
+ transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
135
+ }
136
+
137
+ /* Chat bubble animations */
138
+ @keyframes bubbleIn {
139
+ from { opacity: 0; transform: translateY(8px); }
140
+ to { opacity: 1; transform: translateY(0); }
141
+ }
142
+ .chat-bubble { animation: bubbleIn 0.3s ease-out forwards; }
143
+
144
+ .typing-dot {
145
+ animation: typing 1.4s infinite ease-in-out both;
146
+ }
147
+ .typing-dot:nth-child(1) { animation-delay: -0.32s; }
148
+ .typing-dot:nth-child(2) { animation-delay: -0.16s; }
149
+ @keyframes typing {
150
+ 0%, 80%, 100% { transform: scale(0); }
151
+ 40% { transform: scale(1); }
152
+ }
153
+ </style>
154
+ </head>
155
+ <body class="antialiased min-h-screen flex flex-col">
156
+
157
+ <!-- Navbar -->
158
+ <nav class="sticky top-0 z-40 border-b border-slate-200 dark:border-white/5 bg-white/80 dark:bg-pt-bg/60 backdrop-blur-md transition-colors">
159
+ <div class="max-w-7xl mx-auto px-6 h-16 flex items-center justify-between">
160
+ <div class="flex items-center gap-2">
161
+ <span class="font-serif text-2xl font-bold text-slate-900 dark:text-white tracking-wide">PromiseTrack<sup class="text-pt-accent text-xs font-sans ml-0.5">AI</sup></span>
162
+ </div>
163
+
164
+ <div class="flex items-center">
165
+ <!-- Theme Toggle Button -->
166
+ <button id="themeToggle" class="p-2 rounded-lg border border-slate-300 dark:border-slate-700 text-slate-600 dark:text-amber-400 hover:bg-slate-100 dark:hover:bg-slate-800 transition-colors" title="Toggle theme">
167
+ <!-- Sun Icon (shows in dark mode) -->
168
+ <svg id="themeIconSun" class="w-5 h-5 hidden" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 3v1m0 16v1m9-9h-1M4 12H3m15.364 6.364l-.707-.707M6.343 6.343l-.707-.707m12.728 0l-.707.707M6.343 17.657l-.707.707M16 12a4 4 0 11-8 0 4 4 0 018 0z"></path></svg>
169
+ <!-- Moon Icon (shows in light mode) -->
170
+ <svg id="themeIconMoon" class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20.354 15.354A9 9 0 018.646 3.646 9.003 9.003 0 0012 21a9.003 9.003 0 008.354-5.646z"></path></svg>
171
+ </button>
172
+ </div>
173
+ </div>
174
+ </nav>
175
+
176
+ <!-- Hero Section -->
177
+ <section class="max-w-7xl mx-auto px-6 pt-20 pb-16 lg:pt-32 lg:pb-24 grid lg:grid-cols-2 gap-12 lg:gap-8 items-center">
178
+ <div class="fade-in" style="animation-delay: 50ms;">
179
+ <h1 class="text-5xl lg:text-7xl font-serif font-bold mb-6 text-slate-900 dark:text-white leading-[1.15]">
180
+ Bridging What's<br/>Promised β€” With<br/>What's Delivered.
181
+ </h1>
182
+ <p class="text-lg text-slate-600 dark:text-slate-400 mb-10 max-w-lg leading-relaxed">
183
+ An AI system that tracks forward-looking management commitments and measures whether they materialise in real financial outcomes.
184
+ </p>
185
+ <div class="flex flex-wrap items-center gap-4">
186
+ <button onclick="document.getElementById('dashboard-view').scrollIntoView({behavior: 'smooth'})" class="bg-pt-accent hover:bg-blue-500 text-white px-6 py-3.5 rounded-xl font-semibold transition-all shadow-lg shadow-blue-500/20 flex items-center gap-2">
187
+ Analyse a Company <span aria-hidden="true">&rarr;</span>
188
+ </button>
189
+ <button onclick="document.getElementById('dashboard-view').scrollIntoView({behavior: 'smooth'})" class="border border-slate-300 dark:border-slate-600 hover:border-slate-400 dark:hover:border-slate-400 hover:bg-slate-50 dark:hover:bg-white/5 text-slate-700 dark:text-slate-300 px-6 py-3.5 rounded-xl font-semibold transition-all">
190
+ View Methodology
191
+ </button>
192
+ </div>
193
+ </div>
194
+
195
+ <!-- Divergence SVG Illustration -->
196
+ <div class="fade-in hidden sm:block justify-self-center lg:justify-self-end w-full max-w-[500px]" style="animation-delay: 200ms;">
197
+ <svg viewBox="0 0 480 340" fill="none" xmlns="http://www.w3.org/2000/svg" class="w-full h-auto drop-shadow-2xl">
198
+ <!-- Promise Path -->
199
+ <path d="M100 220 C 180 160, 250 120, 360 90" class="draw-path" stroke="#4493F8" stroke-width="3" stroke-linecap="round"/>
200
+ <!-- Outcome Path -->
201
+ <path d="M100 220 C 180 245, 250 275, 360 295" class="draw-path" stroke="#F85149" stroke-width="3" stroke-linecap="round"/>
202
+ <!-- Divergence Area Fill -->
203
+ <path d="M100 220 C 180 160, 250 120, 360 90 L 360 295 C 250 275, 180 245, 100 220 Z" fill="rgba(68, 147, 248, 0.05)"/>
204
+
205
+ <!-- Nodes -->
206
+ <circle cx="100" cy="220" r="6" class="fill-slate-800 dark:fill-[#E6EDF3]"/>
207
+ <circle cx="360" cy="90" r="5" fill="#4493F8"/>
208
+ <circle cx="360" cy="295" r="5" fill="#F85149"/>
209
+
210
+ <!-- Labels -->
211
+ <text x="372" y="95" fill="#4493F8" font-size="14" font-family="DM Sans" font-weight="600">Promise</text>
212
+ <text x="372" y="300" fill="#F85149" font-size="14" font-family="DM Sans" font-weight="600">Outcome</text>
213
+
214
+ <!-- Annotations -->
215
+ <text x="210" y="198" class="fill-slate-500 dark:fill-[#8B949E]" font-size="11" font-family="JetBrains Mono">Divergence</text>
216
+ <line x1="205" y1="195" x2="205" y2="170" class="stroke-slate-500 dark:stroke-[#8B949E]" stroke-width="1" stroke-dasharray="2 2" opacity="0.5"/>
217
+ <text x="110" y="238" class="fill-slate-500 dark:fill-[#8B949E]" font-size="10" font-family="JetBrains Mono">Q1 Earnings Call</text>
218
+ </svg>
219
+ </div>
220
+ </section>
221
+
222
+ <!-- Main Dashboard View -->
223
+ <main id="dashboard-view" class="flex-1 max-w-7xl mx-auto w-full px-6 py-12 transition-opacity duration-300 border-t border-slate-200 dark:border-white/5 mt-10">
224
+ <header class="mb-8">
225
+ <h2 class="text-3xl font-serif font-bold text-slate-900 dark:text-white mb-3">Company Intelligence</h2>
226
+ <p class="text-slate-600 dark:text-slate-400 text-base max-w-2xl mb-8">Select a company from the repository to extract verified performance metrics against historical management claims.</p>
227
+
228
+ <!-- Search Bar -->
229
+ <div class="relative max-w-md">
230
+ <input type="text" id="companySearch" placeholder="Search companies by name or ticker..."
231
+ class="w-full bg-white dark:bg-[#161B22] border border-slate-300 dark:border-slate-700 rounded-xl px-4 py-3 pl-11 text-slate-900 dark:text-white focus:outline-none focus:border-pt-accent focus:ring-1 focus:ring-pt-accent transition-colors shadow-sm">
232
+ <svg class="w-5 h-5 absolute left-4 top-3.5 text-slate-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
233
+ <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"></path>
234
+ </svg>
235
+ </div>
236
+ </header>
237
+
238
+ <!-- Flashcards Grid -->
239
+ <div id="flashcards-container" class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-6">
240
+ <!-- Loading Skeletons -->
241
+ <div class="glass-card rounded-2xl p-6 h-40 animate-pulse flex flex-col justify-center">
242
+ <div class="h-6 w-3/4 bg-slate-200 dark:bg-slate-700/50 rounded mb-4"></div>
243
+ <div class="h-4 w-1/2 bg-slate-200 dark:bg-slate-700/30 rounded"></div>
244
+ </div>
245
+ <div class="glass-card rounded-2xl p-6 h-40 animate-pulse flex flex-col justify-center">
246
+ <div class="h-6 w-2/3 bg-slate-200 dark:bg-slate-700/50 rounded mb-4"></div>
247
+ <div class="h-4 w-1/2 bg-slate-200 dark:bg-slate-700/30 rounded"></div>
248
+ </div>
249
+ </div>
250
+
251
+ <!-- Empty state (hidden by default) -->
252
+ <div id="no-results" class="hidden py-12 text-center border border-dashed border-slate-300 dark:border-white/20 rounded-2xl mt-6">
253
+ <p class="text-slate-500 dark:text-slate-400">No companies found matching your search.</p>
254
+ </div>
255
+ </main>
256
+
257
+ <!-- Analysis Overlay (Glass Panel) -->
258
+ <div id="analysis-overlay" class="fixed inset-0 z-50 flex items-center justify-center p-4 sm:p-6 hidden-panel bg-slate-900/40 dark:bg-black/60 backdrop-blur-sm">
259
+ <div class="glass-panel w-full max-w-7xl h-full max-h-[92vh] rounded-3xl flex flex-col overflow-hidden relative">
260
+
261
+ <!-- Header -->
262
+ <div class="px-8 py-6 border-b border-slate-200 dark:border-white/10 bg-slate-50/50 dark:bg-slate-800/20 flex flex-col sm:flex-row justify-between items-start sm:items-center gap-4 flex-shrink-0">
263
+ <div>
264
+ <div class="flex items-center gap-3 mb-1">
265
+ <h2 id="panel-company-name" class="text-3xl font-bold text-slate-900 dark:text-white font-serif">Company Name</h2>
266
+ <span id="panel-verdict" class="px-3 py-1 rounded-full text-xs font-bold uppercase tracking-wider border">Verdict</span>
267
+ </div>
268
+ <p id="panel-source" class="text-slate-500 dark:text-slate-400 text-sm font-mono">Source Details</p>
269
+ </div>
270
+ <div class="flex items-center gap-4 w-full sm:w-auto justify-end">
271
+ <!-- Close button -->
272
+ <button id="close-panel-btn" class="p-2 rounded-full hover:bg-slate-200 dark:hover:bg-white/10 text-slate-500 dark:text-slate-300 transition-colors bg-white dark:bg-white/5 border border-slate-200 dark:border-white/10 shadow-sm" title="Close Analysis">
273
+ <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path></svg>
274
+ </button>
275
+ </div>
276
+ </div>
277
+
278
+ <!-- Scrollable Body -->
279
+ <div class="flex-1 overflow-y-auto custom-scrollbar p-8 space-y-10" id="panel-content">
280
+
281
+ <div class="grid grid-cols-1 xl:grid-cols-3 gap-8">
282
+ <!-- Left: RAG Engine & Ask RAG -->
283
+ <div class="xl:col-span-2 space-y-6">
284
+ <div class="flex items-center gap-2 mb-4">
285
+ <div class="w-2 h-6 bg-pt-accent rounded-sm"></div>
286
+ <h3 class="text-xl font-bold text-slate-900 dark:text-white">RAG Intelligence Engine</h3>
287
+ </div>
288
+
289
+ <!-- AI Explanation -->
290
+ <div class="bg-white dark:bg-[#0D1117]/80 border border-slate-200 dark:border-pt-accent/20 rounded-2xl p-6 shadow-sm dark:shadow-inner">
291
+ <h4 class="text-xs font-mono text-pt-accent mb-3 uppercase tracking-widest">Synthesized Analysis</h4>
292
+ <div id="panel-explanation" class="text-slate-700 dark:text-slate-300 leading-relaxed space-y-3 text-sm sm:text-base">
293
+ <!-- Explanation content -->
294
+ </div>
295
+ </div>
296
+
297
+ <!-- Ask RAG Chat UI (Staged) -->
298
+ <div class="bg-white dark:bg-[#0D1117]/80 border border-slate-200 dark:border-pt-accent/20 rounded-2xl p-6 shadow-sm dark:shadow-inner mt-6 flex flex-col h-[400px]">
299
+ <h4 class="text-xs font-mono text-pt-accent mb-4 uppercase tracking-widest flex items-center gap-2">
300
+ <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 10h.01M12 10h.01M16 10h.01M9 16H5a2 2 0 01-2-2V6a2 2 0 012-2h14a2 2 0 012 2v8a2 2 0 01-2 2h-5l-5 5v-5z"></path></svg>
301
+ Ask RAG (Vector Search)
302
+ </h4>
303
+
304
+ <!-- Chat Area -->
305
+ <div id="rag-chat-history" class="flex-1 overflow-y-auto custom-scrollbar space-y-4 mb-4 pr-2">
306
+ <!-- Initial Greeting -->
307
+ <div class="flex flex-col gap-1 items-start">
308
+ <div class="bg-slate-100 dark:bg-slate-800/60 text-slate-800 dark:text-slate-200 px-4 py-3 rounded-2xl rounded-tl-sm text-sm max-w-[85%] border border-slate-200 dark:border-white/5">
309
+ Hello! I have loaded all the contextual transcripts and financial reports for this company. Ask me anything about their forward-looking guidance, margins, or risk factors.
310
+ </div>
311
+ <span class="text-[10px] text-slate-400 font-mono ml-1">AI Agent</span>
312
+ </div>
313
+ </div>
314
+
315
+ <!-- Chat Input -->
316
+ <form id="rag-chat-form" class="relative mt-auto">
317
+ <input type="text" id="rag-chat-input" placeholder="e.g. What did management say about expected revenue growth?"
318
+ class="w-full bg-slate-50 dark:bg-[#161B22] border border-slate-300 dark:border-slate-700 rounded-xl px-4 py-3 pr-12 text-slate-900 dark:text-white focus:outline-none focus:border-pt-accent transition-colors text-sm shadow-inner">
319
+ <button type="submit" class="absolute right-2 top-2 p-1.5 bg-pt-accent text-white rounded-lg hover:bg-blue-500 transition-colors shadow-md">
320
+ <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M14 5l7 7m0 0l-7 7m7-7H3"></path></svg>
321
+ </button>
322
+ </form>
323
+ </div>
324
+
325
+ <!-- Signals -->
326
+ <div class="grid grid-cols-1 md:grid-cols-2 gap-4 mt-6">
327
+ <!-- Positive -->
328
+ <div class="bg-emerald-50 dark:bg-emerald-950/20 border border-emerald-200 dark:border-emerald-500/20 rounded-2xl p-5">
329
+ <h4 class="text-xs font-mono text-emerald-600 dark:text-emerald-400 mb-4 uppercase tracking-widest flex items-center gap-2">
330
+ <span class="w-2 h-2 rounded-full bg-emerald-500 dark:bg-emerald-400"></span> Positive Signals
331
+ </h4>
332
+ <div id="panel-positive" class="space-y-4">
333
+ <!-- Positive signals list -->
334
+ </div>
335
+ </div>
336
+ <!-- Negative -->
337
+ <div class="bg-rose-50 dark:bg-rose-950/20 border border-rose-200 dark:border-rose-500/20 rounded-2xl p-5">
338
+ <h4 class="text-xs font-mono text-rose-600 dark:text-rose-400 mb-4 uppercase tracking-widest flex items-center gap-2">
339
+ <span class="w-2 h-2 rounded-full bg-rose-500 dark:bg-rose-400"></span> Negative Signals
340
+ </h4>
341
+ <div id="panel-negative" class="space-y-4">
342
+ <!-- Negative signals list -->
343
+ </div>
344
+ </div>
345
+ </div>
346
+ </div>
347
+
348
+ <!-- Right: Metrics Snapshot -->
349
+ <div class="space-y-6">
350
+ <div class="flex items-center gap-2 mb-4">
351
+ <div class="w-2 h-6 bg-slate-400 dark:bg-slate-600 rounded-sm"></div>
352
+ <h3 class="text-xl font-bold text-slate-900 dark:text-white">Financial Snapshot</h3>
353
+ </div>
354
+
355
+ <!-- Metrics Grid -->
356
+ <div class="grid grid-cols-2 xl:grid-cols-1 gap-4" id="panel-metrics">
357
+ <!-- Metric cards -->
358
+ </div>
359
+
360
+ <!-- QoQ Bars -->
361
+ <div class="bg-white dark:bg-[#0D1117]/60 border border-slate-200 dark:border-white/5 rounded-2xl p-5 mt-6 shadow-sm">
362
+ <h4 class="text-xs font-mono text-slate-500 dark:text-slate-400 mb-4 uppercase tracking-widest">Quarter-over-Quarter</h4>
363
+ <div id="panel-bar-metrics" class="space-y-4">
364
+ <!-- Bar metrics -->
365
+ </div>
366
+ </div>
367
+ </div>
368
+ </div>
369
+
370
+ <!-- Claim Tracker Table -->
371
+ <div class="pt-6 border-t border-slate-200 dark:border-white/10">
372
+ <div class="flex items-center gap-2 mb-4">
373
+ <div class="w-2 h-6 bg-purple-500 rounded-sm"></div>
374
+ <h3 class="text-xl font-bold text-slate-900 dark:text-white">Commitment Tracker</h3>
375
+ </div>
376
+
377
+ <div class="bg-white dark:bg-[#0D1117]/60 border border-slate-200 dark:border-white/10 rounded-2xl overflow-hidden shadow-sm">
378
+ <div class="overflow-x-auto custom-scrollbar">
379
+ <table class="w-full text-left text-sm whitespace-nowrap">
380
+ <thead class="bg-slate-50 dark:bg-slate-800/30 text-slate-500 dark:text-slate-400 font-mono text-xs uppercase tracking-wider">
381
+ <tr>
382
+ <th class="px-6 py-4 font-medium border-b border-slate-200 dark:border-transparent">Quarter</th>
383
+ <th class="px-6 py-4 font-medium border-b border-slate-200 dark:border-transparent w-full max-w-md">Extracted Claim</th>
384
+ <th class="px-6 py-4 font-medium border-b border-slate-200 dark:border-transparent">Metric</th>
385
+ <th class="px-6 py-4 font-medium border-b border-slate-200 dark:border-transparent">Direction</th>
386
+ <th class="px-6 py-4 font-medium border-b border-slate-200 dark:border-transparent">Verification</th>
387
+ </tr>
388
+ </thead>
389
+ <tbody id="panel-claims" class="divide-y divide-slate-100 dark:divide-white/5 text-slate-700 dark:text-slate-300">
390
+ <!-- Claims rows -->
391
+ </tbody>
392
+ </table>
393
+ </div>
394
+ </div>
395
+ </div>
396
+
397
+ </div>
398
+ </div>
399
+ </div>
400
+
401
+ <!-- Global Loader -->
402
+ <div id="global-loader" class="fixed inset-0 z-[100] bg-white/90 dark:bg-pt-bg/90 backdrop-blur-md hidden flex-col items-center justify-center transition-opacity">
403
+ <div class="relative w-24 h-24 mb-6">
404
+ <svg class="animate-spin w-full h-full text-slate-200 dark:text-white/10" viewBox="0 0 100 100">
405
+ <circle cx="50" cy="50" r="45" fill="none" stroke="currentColor" stroke-width="8"></circle>
406
+ </svg>
407
+ <svg class="animate-spin w-full h-full text-pt-accent absolute top-0 left-0" viewBox="0 0 100 100" style="animation-direction: reverse; animation-duration: 1.5s;">
408
+ <circle cx="50" cy="50" r="45" fill="none" stroke="currentColor" stroke-width="8" stroke-dasharray="80 200" stroke-linecap="round"></circle>
409
+ </svg>
410
+ </div>
411
+ <h2 class="text-2xl font-serif font-bold text-slate-900 dark:text-white tracking-wide">Processing Pipeline</h2>
412
+ <p class="text-pt-accent mt-2 font-mono text-sm" id="loader-status">Running models...</p>
413
+ </div>
414
+
415
+ <script>
416
+ // --- Theme Logic ---
417
+ const themeToggleBtn = document.getElementById('themeToggle');
418
+ const themeIconSun = document.getElementById('themeIconSun');
419
+ const themeIconMoon = document.getElementById('themeIconMoon');
420
+ const htmlEl = document.documentElement;
421
+
422
+ // Ensure default to dark mode unless explicitly set to light
423
+ if (localStorage.getItem('theme') === 'light') {
424
+ htmlEl.classList.remove('dark');
425
+ if (themeIconSun) themeIconSun.classList.add('hidden');
426
+ if (themeIconMoon) themeIconMoon.classList.remove('hidden');
427
+ } else {
428
+ htmlEl.classList.add('dark');
429
+ if (themeIconSun) themeIconSun.classList.remove('hidden');
430
+ if (themeIconMoon) themeIconMoon.classList.add('hidden');
431
+ }
432
+
433
+ if (themeToggleBtn) {
434
+ themeToggleBtn.addEventListener('click', () => {
435
+ htmlEl.classList.toggle('dark');
436
+ if (htmlEl.classList.contains('dark')) {
437
+ localStorage.setItem('theme', 'dark');
438
+ themeIconSun.classList.remove('hidden');
439
+ themeIconMoon.classList.add('hidden');
440
+ } else {
441
+ localStorage.setItem('theme', 'light');
442
+ themeIconSun.classList.add('hidden');
443
+ themeIconMoon.classList.remove('hidden');
444
+ }
445
+ });
446
+ }
447
+
448
+ // DOM Elements
449
+ const API_BASE = '/api';
450
+ const flashcardsContainer = document.getElementById('flashcards-container');
451
+ const searchInput = document.getElementById('companySearch');
452
+ const noResults = document.getElementById('no-results');
453
+ const overlay = document.getElementById('analysis-overlay');
454
+ const closeBtn = document.getElementById('close-panel-btn');
455
+ const loader = document.getElementById('global-loader');
456
+ const loaderStatus = document.getElementById('loader-status');
457
+
458
+ // Current state
459
+ let currentCompany = null;
460
+ let allCompanies = [];
461
+
462
+ // --- Initialization ---
463
+ document.addEventListener('DOMContentLoaded', () => {
464
+ fetchCompanies();
465
+ });
466
+
467
+ async function fetchCompanies() {
468
+ try {
469
+ const res = await fetch(`${API_BASE}/companies`);
470
+ if (!res.ok) throw new Error('Failed to fetch companies');
471
+ allCompanies = await res.json();
472
+ renderFlashcards(allCompanies);
473
+ } catch (error) {
474
+ console.warn('API fetch failed, loading dummy data for UI testing...', error);
475
+ // Fallback dummy data if backend is offline
476
+ allCompanies = [
477
+ { display: "Axis Bank Ltd", folder: "AXISBANK" },
478
+ { display: "Infosys Limited", folder: "INFY" },
479
+ { display: "Reliance Industries", folder: "RELIANCE" },
480
+ { display: "Tata Motors", folder: "TATAMOTORS" },
481
+ { display: "HDFC Bank", folder: "HDFCBANK" },
482
+ { display: "Larsen & Toubro", folder: "LT" }
483
+ ];
484
+ renderFlashcards(allCompanies);
485
+ }
486
+ }
487
+
488
+ // --- Search/Filter Logic ---
489
+ searchInput.addEventListener('input', (e) => {
490
+ const query = e.target.value.toLowerCase();
491
+ const filtered = allCompanies.filter(comp =>
492
+ comp.display.toLowerCase().includes(query) ||
493
+ comp.folder.toLowerCase().includes(query)
494
+ );
495
+
496
+ if (filtered.length === 0) {
497
+ flashcardsContainer.innerHTML = '';
498
+ noResults.classList.remove('hidden');
499
+ } else {
500
+ noResults.classList.add('hidden');
501
+ renderFlashcards(filtered);
502
+ }
503
+ });
504
+
505
+ function renderFlashcards(companies) {
506
+ flashcardsContainer.innerHTML = '';
507
+
508
+ if (!companies || companies.length === 0) return;
509
+
510
+ companies.forEach((comp, index) => {
511
+ const delay = (index % 10) * 30; // Stagger animation
512
+ const card = document.createElement('div');
513
+ card.className = `glass-card rounded-2xl cursor-pointer min-h-[160px] fade-in relative overflow-hidden group`;
514
+ card.style.animationDelay = `${delay}ms`;
515
+
516
+ card.innerHTML = `
517
+ <div class="absolute -right-4 -bottom-4 w-32 h-32 opacity-[0.08] dark:opacity-[0.15] mix-blend-luminosity group-hover:opacity-20 transition-opacity pointer-events-none"
518
+ style="background-image: url('${API_BASE}/logo/${comp.folder}'); background-size: contain; background-repeat: no-repeat; background-position: center;">
519
+ </div>
520
+ <div class="p-6 h-full flex flex-col justify-between relative z-10">
521
+ <div>
522
+ <div class="w-10 h-10 rounded-full bg-white dark:bg-[#0D1117] border border-slate-200 dark:border-white/10 flex items-center justify-center font-serif text-lg text-slate-800 dark:text-white shadow-sm dark:shadow-inner overflow-hidden">
523
+ <img src="${API_BASE}/logo/${comp.folder}" alt="logo" class="w-full h-full object-contain p-1" onerror="this.onerror=null; this.parentElement.innerHTML='${comp.display.charAt(0)}';" />
524
+ </div>
525
+ </div>
526
+ <div class="mt-4">
527
+ <h3 class="text-xl font-bold text-slate-900 dark:text-white truncate">${comp.display}</h3>
528
+ <p class="text-sm text-slate-500 dark:text-slate-400 font-mono mt-1">${comp.folder}</p>
529
+ </div>
530
+ </div>
531
+ `;
532
+
533
+ card.addEventListener('click', () => openAnalysis(comp.folder));
534
+ flashcardsContainer.appendChild(card);
535
+ });
536
+ }
537
+
538
+ // --- Analysis Overlay Logic ---
539
+ async function openAnalysis(companyId) {
540
+ currentCompany = companyId;
541
+ showLoader();
542
+ resetRagChat(); // Clear old chat history
543
+
544
+ try {
545
+ // Hardcoded to 'full' mode
546
+ const res = await fetch(`${API_BASE}/analyse`, {
547
+ method: 'POST',
548
+ headers: { 'Content-Type': 'application/json' },
549
+ body: JSON.stringify({ company: companyId, mode: 'full' })
550
+ });
551
+
552
+ if (!res.ok) {
553
+ const errorData = await res.json();
554
+ throw new Error(errorData.message || errorData.error || 'Failed to analyze');
555
+ }
556
+
557
+ const data = await res.json();
558
+ populatePanel(data);
559
+
560
+ // Show panel
561
+ overlay.classList.remove('hidden');
562
+ setTimeout(() => {
563
+ overlay.classList.remove('hidden-panel');
564
+ overlay.classList.add('show-panel');
565
+ document.body.style.overflow = 'hidden'; // prevent bg scrolling
566
+ }, 10);
567
+
568
+ } catch (error) {
569
+ console.error("Analysis Error:", error);
570
+ alert(`Analysis Error: ${error.message}`);
571
+ } finally {
572
+ hideLoader();
573
+ }
574
+ }
575
+
576
+ function closePanel() {
577
+ overlay.classList.remove('show-panel');
578
+ overlay.classList.add('hidden-panel');
579
+ setTimeout(() => {
580
+ overlay.classList.add('hidden');
581
+ document.body.style.overflow = '';
582
+ }, 300);
583
+ }
584
+
585
+ closeBtn.addEventListener('click', closePanel);
586
+ overlay.addEventListener('click', (e) => {
587
+ if (e.target === overlay) closePanel();
588
+ });
589
+ document.addEventListener('keydown', (e) => {
590
+ if (e.key === 'Escape' && overlay.classList.contains('show-panel')) closePanel();
591
+ });
592
+
593
+ // --- Ask RAG Chat Logic (Staged/Mocked) ---
594
+ const chatForm = document.getElementById('rag-chat-form');
595
+ const chatInput = document.getElementById('rag-chat-input');
596
+ const chatHistory = document.getElementById('rag-chat-history');
597
+
598
+ function resetRagChat() {
599
+ chatHistory.innerHTML = `
600
+ <div class="flex flex-col gap-1 items-start">
601
+ <div class="bg-slate-100 dark:bg-slate-800/60 text-slate-800 dark:text-slate-200 px-4 py-3 rounded-2xl rounded-tl-sm text-sm max-w-[85%] border border-slate-200 dark:border-white/5">
602
+ Hello! I have loaded all the contextual transcripts and financial reports for this company. Ask me anything about their forward-looking guidance, margins, or risk factors.
603
+ </div>
604
+ <span class="text-[10px] text-slate-400 font-mono ml-1">AI Agent</span>
605
+ </div>
606
+ `;
607
+ chatInput.value = '';
608
+ }
609
+
610
+ chatForm.addEventListener('submit', async (e) => {
611
+ e.preventDefault();
612
+ const message = chatInput.value.trim();
613
+ if (!message) return;
614
+
615
+ // 1. Add User Message
616
+ const userHtml = `
617
+ <div class="flex flex-col gap-1 items-end chat-bubble">
618
+ <div class="bg-pt-accent text-white px-4 py-3 rounded-2xl rounded-tr-sm text-sm max-w-[85%] shadow-md">
619
+ ${message}
620
+ </div>
621
+ <span class="text-[10px] text-slate-400 font-mono mr-1">You</span>
622
+ </div>
623
+ `;
624
+ chatHistory.insertAdjacentHTML('beforeend', userHtml);
625
+ chatInput.value = '';
626
+ scrollToBottom();
627
+
628
+ // 2. Add Loading Indicator
629
+ const loaderId = 'loader-' + Date.now();
630
+ const loadingHtml = `
631
+ <div id="${loaderId}" class="flex flex-col gap-1 items-start chat-bubble">
632
+ <div class="bg-slate-100 dark:bg-slate-800/60 px-4 py-4 rounded-2xl rounded-tl-sm max-w-[85%] border border-slate-200 dark:border-white/5 flex gap-1 items-center">
633
+ <div class="w-2 h-2 bg-slate-400 rounded-full typing-dot"></div>
634
+ <div class="w-2 h-2 bg-slate-400 rounded-full typing-dot"></div>
635
+ <div class="w-2 h-2 bg-slate-400 rounded-full typing-dot"></div>
636
+ </div>
637
+ </div>
638
+ `;
639
+ chatHistory.insertAdjacentHTML('beforeend', loadingHtml);
640
+ scrollToBottom();
641
+
642
+ // 3. Backend Integration for RAG Chat
643
+ try {
644
+ // Call the actual backend endpoint (e.g., /api/chat)
645
+ const res = await fetch(`${API_BASE}/chat`, {
646
+ method: 'POST',
647
+ headers: { 'Content-Type': 'application/json' },
648
+ body: JSON.stringify({ company: currentCompany, query: message })
649
+ });
650
+
651
+ if (document.getElementById(loaderId)) {
652
+ document.getElementById(loaderId).remove();
653
+ }
654
+
655
+ if (!res.ok) {
656
+ throw new Error('Failed to fetch response from RAG engine');
657
+ }
658
+
659
+ const data = await res.json();
660
+
661
+ const botHtml = `
662
+ <div class="flex flex-col gap-1 items-start chat-bubble">
663
+ <div class="bg-slate-100 dark:bg-slate-800/60 text-slate-800 dark:text-slate-200 px-4 py-3 rounded-2xl rounded-tl-sm text-sm max-w-[85%] border border-slate-200 dark:border-white/5">
664
+ ${data.answer || data.reply || data.message || "I found the relevant documents, but couldn't generate a summary."}
665
+ </div>
666
+ <span class="text-[10px] text-slate-400 font-mono ml-1 flex items-center gap-1">
667
+ <svg class="w-3 h-3 text-pt-accent" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 10V3L4 14h7v7l9-11h-7z"></path></svg> RAG Engine
668
+ </span>
669
+ </div>
670
+ `;
671
+ chatHistory.insertAdjacentHTML('beforeend', botHtml);
672
+ scrollToBottom();
673
+
674
+ } catch (error) {
675
+ console.error("Chat Error:", error);
676
+
677
+ if (document.getElementById(loaderId)) {
678
+ document.getElementById(loaderId).remove();
679
+ }
680
+
681
+ // Fallback error UI if endpoint is not connected yet
682
+ const errorHtml = `
683
+ <div class="flex flex-col gap-1 items-start chat-bubble">
684
+ <div class="bg-rose-50 dark:bg-rose-900/20 text-rose-600 dark:text-rose-400 px-4 py-3 rounded-2xl rounded-tl-sm text-sm max-w-[85%] border border-rose-200 dark:border-rose-500/20">
685
+ <strong>Connection Error:</strong> Backend /api/chat endpoint not found or unreachable. Please ensure the Flask route is implemented.
686
+ </div>
687
+ <span class="text-[10px] text-slate-400 font-mono ml-1">System Error</span>
688
+ </div>
689
+ `;
690
+ chatHistory.insertAdjacentHTML('beforeend', errorHtml);
691
+ scrollToBottom();
692
+ }
693
+ });
694
+
695
+ function scrollToBottom() {
696
+ chatHistory.scrollTop = chatHistory.scrollHeight;
697
+ }
698
+
699
+ // --- Data Population ---
700
+ function populatePanel(d) {
701
+ // Header
702
+ document.getElementById('panel-company-name').textContent = d.company || 'Unknown';
703
+ document.getElementById('panel-source').textContent = d.source_label || 'Comprehensive Analysis';
704
+
705
+ // Verdict Badge
706
+ const verdictEl = document.getElementById('panel-verdict');
707
+ const v = (d.verdict || 'Mixed').toUpperCase();
708
+ verdictEl.textContent = v;
709
+ verdictEl.className = 'px-3 py-1 rounded-full text-xs font-bold uppercase tracking-wider border ';
710
+ if (v === 'POSITIVE') verdictEl.classList.add('bg-emerald-100', 'dark:bg-emerald-500/20', 'text-emerald-700', 'dark:text-emerald-400', 'border-emerald-200', 'dark:border-emerald-500/30');
711
+ else if (v === 'NEGATIVE') verdictEl.classList.add('bg-rose-100', 'dark:bg-rose-500/20', 'text-rose-700', 'dark:text-rose-400', 'border-rose-200', 'dark:border-rose-500/30');
712
+ else verdictEl.classList.add('bg-amber-100', 'dark:bg-amber-500/20', 'text-amber-700', 'dark:text-amber-400', 'border-amber-200', 'dark:border-amber-500/30');
713
+
714
+
715
+ // RAG Explanation
716
+ const expEl = document.getElementById('panel-explanation');
717
+ if (d.explanation) {
718
+ let html = d.explanation.replace(/\*\*(.*?)\*\*/g, '<strong class="text-slate-900 dark:text-white font-semibold">$1</strong>');
719
+ html = html.replace(/\n/g, '<br/>');
720
+ expEl.innerHTML = `<p>${html}</p>`;
721
+ } else {
722
+ expEl.innerHTML = '<p class="text-slate-500 italic">No explanation generated.</p>';
723
+ }
724
+
725
+ // Signals
726
+ const renderSignal = (s, isPos) => `
727
+ <div class="flex gap-3 text-sm">
728
+ <span class="font-mono text-xs px-1.5 py-0.5 rounded flex-shrink-0 self-start ${isPos ? 'bg-emerald-100 dark:bg-emerald-500/10 text-emerald-700 dark:text-emerald-400 border border-emerald-200 dark:border-emerald-500/20' : 'bg-rose-100 dark:bg-rose-500/10 text-rose-700 dark:text-rose-400 border border-rose-200 dark:border-rose-500/20'}">${s.index}</span>
729
+ <div>
730
+ <strong class="block text-slate-800 dark:text-slate-200 mb-1">${s.title}</strong>
731
+ <span class="text-slate-600 dark:text-slate-400">${s.body}</span>
732
+ </div>
733
+ </div>`;
734
+
735
+ document.getElementById('panel-positive').innerHTML = d.positive_signals?.length
736
+ ? d.positive_signals.map(s => renderSignal(s, true)).join('')
737
+ : '<p class="text-slate-500 text-sm italic">No positive signals detected.</p>';
738
+
739
+ document.getElementById('panel-negative').innerHTML = d.negative_signals?.length
740
+ ? d.negative_signals.map(s => renderSignal(s, false)).join('')
741
+ : '<p class="text-slate-500 text-sm italic">No negative signals detected.</p>';
742
+
743
+ // Metrics Grid
744
+ const metricsEl = document.getElementById('panel-metrics');
745
+ if (d.metrics && d.metrics.length) {
746
+ const trendIcon = { up: 'β†—', down: 'β†˜', flat: 'β†’' };
747
+ const trendColor = { up: 'text-emerald-600 dark:text-emerald-400', down: 'text-rose-600 dark:text-rose-400', flat: 'text-slate-500 dark:text-slate-400' };
748
+
749
+ metricsEl.innerHTML = d.metrics.map(m => `
750
+ <div class="bg-white dark:bg-slate-900/40 border border-slate-200 dark:border-white/5 rounded-2xl p-4 flex flex-col justify-between shadow-sm">
751
+ <span class="text-xs font-mono text-slate-500 dark:text-slate-400 uppercase">${m.label}</span>
752
+ <div class="mt-2 flex items-end justify-between">
753
+ <span class="text-2xl font-serif text-slate-900 dark:text-white">${m.value}</span>
754
+ <div class="flex flex-col items-end">
755
+ <span class="text-xs ${trendColor[m.trend]} font-bold flex items-center gap-1">${trendIcon[m.trend]} ${m.trend.toUpperCase()}</span>
756
+ <span class="text-[10px] text-slate-500">${m.sub}</span>
757
+ </div>
758
+ </div>
759
+ </div>
760
+ `).join('');
761
+ } else {
762
+ metricsEl.innerHTML = '<p class="text-slate-500 text-sm italic">No financial metrics available.</p>';
763
+ }
764
+
765
+ // Bar Metrics
766
+ const barsEl = document.getElementById('panel-bar-metrics');
767
+ if (d.bar_metrics && d.bar_metrics.length) {
768
+ barsEl.innerHTML = d.bar_metrics.map((b, i) => `
769
+ <div>
770
+ <div class="flex justify-between text-xs mb-1">
771
+ <span class="text-slate-600 dark:text-slate-300">${b.label}</span>
772
+ <span class="font-mono text-slate-900 dark:text-white">${b.value}</span>
773
+ </div>
774
+ <div class="w-full bg-slate-200 dark:bg-slate-800 rounded-full h-2 overflow-hidden">
775
+ <div class="h-full rounded-full transition-all duration-1000 ease-out" style="width: 0%; background-color: ${b.color}" data-width="${b.target_pct}%"></div>
776
+ </div>
777
+ </div>
778
+ `).join('');
779
+
780
+ // Animate bars after a short delay
781
+ setTimeout(() => {
782
+ barsEl.querySelectorAll('[data-width]').forEach(bar => {
783
+ bar.style.width = bar.getAttribute('data-width');
784
+ });
785
+ }, 100);
786
+ } else {
787
+ barsEl.innerHTML = '<p class="text-slate-500 text-sm italic">No QoQ data available.</p>';
788
+ }
789
+
790
+ // Claims Table
791
+ const claimsEl = document.getElementById('panel-claims');
792
+ if (d.claims && d.claims.length) {
793
+ const getDirHtml = (dir) => {
794
+ if (dir === 'increase') return '<span class="text-emerald-600 dark:text-emerald-400 font-bold">↑ INC</span>';
795
+ if (dir === 'decrease') return '<span class="text-rose-600 dark:text-rose-400 font-bold">↓ DEC</span>';
796
+ return '<span class="text-slate-500 dark:text-slate-400 font-bold">β†’ FLAT</span>';
797
+ };
798
+
799
+ const getResHtml = (res) => {
800
+ const r = (res || '').toUpperCase();
801
+ if (r === 'VERIFIED') return '<span class="px-2 py-1 bg-emerald-100 dark:bg-emerald-500/10 text-emerald-700 dark:text-emerald-400 border border-emerald-200 dark:border-emerald-500/20 rounded text-xs">Verified</span>';
802
+ if (r === 'NOT VERIFIED' || r === 'FAILED') return '<span class="px-2 py-1 bg-rose-100 dark:bg-rose-500/10 text-rose-700 dark:text-rose-400 border border-rose-200 dark:border-rose-500/20 rounded text-xs">Failed</span>';
803
+ if (r === 'PARTIAL') return '<span class="px-2 py-1 bg-amber-100 dark:bg-amber-500/10 text-amber-700 dark:text-amber-400 border border-amber-200 dark:border-amber-500/20 rounded text-xs">Partial</span>';
804
+ return `<span class="px-2 py-1 bg-slate-200 dark:bg-slate-500/10 text-slate-700 dark:text-slate-400 border border-slate-300 dark:border-slate-500/20 rounded text-xs">${r || 'UNKNOWN'}</span>`;
805
+ };
806
+
807
+ claimsEl.innerHTML = d.claims.map(c => `
808
+ <tr class="hover:bg-slate-50 dark:hover:bg-white/[0.02] transition-colors">
809
+ <td class="px-6 py-4 font-mono text-xs text-slate-500 dark:text-slate-400 border-b border-slate-100 dark:border-white/5">${c.quarter || '--'}</td>
810
+ <td class="px-6 py-4 whitespace-normal max-w-md border-b border-slate-100 dark:border-white/5">
811
+ <p class="text-sm text-slate-800 dark:text-slate-200 line-clamp-2" title="${c.sentence}">${c.sentence}</p>
812
+ </td>
813
+ <td class="px-6 py-4 border-b border-slate-100 dark:border-white/5">
814
+ <span class="px-2 py-1 bg-slate-100 dark:bg-white/5 border border-slate-200 dark:border-white/10 rounded text-xs text-slate-600 dark:text-slate-300 font-mono">${c.metric || 'N/A'}</span>
815
+ </td>
816
+ <td class="px-6 py-4 font-mono text-xs border-b border-slate-100 dark:border-white/5">${getDirHtml(c.direction)}</td>
817
+ <td class="px-6 py-4 font-mono border-b border-slate-100 dark:border-white/5">${getResHtml(c.result)}</td>
818
+ </tr>
819
+ `).join('');
820
+ } else {
821
+ claimsEl.innerHTML = `<tr><td colspan="5" class="px-6 py-8 text-center text-slate-500 italic">No claims data extracted.</td></tr>`;
822
+ }
823
+ }
824
+
825
+ // --- Loader Utility ---
826
+ function showLoader() {
827
+ loader.style.display = 'flex';
828
+
829
+ const sequence = ['Aggregating RAG intelligence...', 'Analyzing DistilBERT claims...', 'Verifying financial outcomes...'];
830
+ let msgIndex = 0;
831
+ loaderStatus.textContent = sequence[0];
832
+
833
+ window.loaderInterval = setInterval(() => {
834
+ msgIndex = (msgIndex + 1) % sequence.length;
835
+ loaderStatus.textContent = sequence[msgIndex];
836
+ }, 800);
837
+
838
+ setTimeout(() => {
839
+ loader.classList.remove('hidden');
840
+ loader.classList.add('opacity-100');
841
+ }, 10);
842
+ }
843
+
844
+ function hideLoader() {
845
+ clearInterval(window.loaderInterval);
846
+ loader.classList.remove('opacity-100');
847
+ setTimeout(() => {
848
+ loader.classList.add('hidden');
849
+ loader.style.display = 'none';
850
+ }, 300);
851
+ }
852
+
853
+ </script>
854
+ </body>
855
+ </html>
chroma_db/589bdc22-9a2d-4c25-b432-0b35caad7759/data_level0.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:200cb437a2ed2bf4c0160ba76ce7e5418aed10105ed04f03478116072089316b
3
+ size 7066016
chroma_db/589bdc22-9a2d-4c25-b432-0b35caad7759/header.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:cc614e2f95436bbe5dd290324470fbcf07e3e900271d2ef44a6a67899ec17ad4
3
+ size 100
chroma_db/589bdc22-9a2d-4c25-b432-0b35caad7759/index_metadata.pickle ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3ff8a9c564ac87a4b8627df17fc710ce229476401c0d71d1b7141bac763f00d1
3
+ size 211014
chroma_db/589bdc22-9a2d-4c25-b432-0b35caad7759/length.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:99708d3d149b9eabf69d9cb09e9f3666cf64a240e415aae996825bb71a55da93
3
+ size 16864
chroma_db/589bdc22-9a2d-4c25-b432-0b35caad7759/link_lists.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ab0d63d509bc1d2765f936d88086d18812ae42faf306d63eaa553c07cb12db33
3
+ size 37468
chroma_db/chroma.sqlite3 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9f8e37238672bb8ae6a37afb3cc5d201436ec94c501f72933363c7e307782f6b
3
+ size 10010624
claim_classification_model_distilbert_trained/claim_classifier_model/checkpoint-902/config.json ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "activation": "gelu",
3
+ "architectures": [
4
+ "DistilBertForSequenceClassification"
5
+ ],
6
+ "attention_dropout": 0.1,
7
+ "bos_token_id": null,
8
+ "dim": 768,
9
+ "dropout": 0.1,
10
+ "dtype": "float32",
11
+ "eos_token_id": null,
12
+ "hidden_dim": 3072,
13
+ "initializer_range": 0.02,
14
+ "max_position_embeddings": 512,
15
+ "model_type": "distilbert",
16
+ "n_heads": 12,
17
+ "n_layers": 6,
18
+ "pad_token_id": 0,
19
+ "problem_type": "single_label_classification",
20
+ "qa_dropout": 0.1,
21
+ "seq_classif_dropout": 0.2,
22
+ "sinusoidal_pos_embds": false,
23
+ "tie_weights_": true,
24
+ "tie_word_embeddings": true,
25
+ "transformers_version": "5.0.0",
26
+ "use_cache": false,
27
+ "vocab_size": 30522
28
+ }
claim_classification_model_distilbert_trained/claim_classifier_model/checkpoint-902/model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1b8ddfa04b35ed16b36bcfd77c01aa0e0cd34a3a9412ed6a4ffba6128b5b2c0f
3
+ size 267832560
claim_classification_model_distilbert_trained/claim_classifier_model/checkpoint-902/tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
claim_classification_model_distilbert_trained/claim_classifier_model/checkpoint-902/tokenizer_config.json ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "backend": "tokenizers",
3
+ "cls_token": "[CLS]",
4
+ "do_lower_case": true,
5
+ "is_local": false,
6
+ "mask_token": "[MASK]",
7
+ "model_max_length": 512,
8
+ "pad_token": "[PAD]",
9
+ "sep_token": "[SEP]",
10
+ "strip_accents": null,
11
+ "tokenize_chinese_chars": true,
12
+ "tokenizer_class": "DistilBertTokenizer",
13
+ "unk_token": "[UNK]"
14
+ }
claim_classification_model_distilbert_trained/claim_classifier_model/checkpoint-902/trainer_state.json ADDED
@@ -0,0 +1,184 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "best_global_step": null,
3
+ "best_metric": null,
4
+ "best_model_checkpoint": null,
5
+ "epoch": 2.0,
6
+ "eval_steps": 500,
7
+ "global_step": 902,
8
+ "is_hyper_param_search": false,
9
+ "is_local_process_zero": true,
10
+ "is_world_process_zero": true,
11
+ "log_history": [
12
+ {
13
+ "epoch": 0.11086474501108648,
14
+ "grad_norm": 3.2481462955474854,
15
+ "learning_rate": 4.864190687361419e-05,
16
+ "loss": 0.5528812789916993,
17
+ "step": 50
18
+ },
19
+ {
20
+ "epoch": 0.22172949002217296,
21
+ "grad_norm": 4.128503322601318,
22
+ "learning_rate": 4.725609756097561e-05,
23
+ "loss": 0.3858406448364258,
24
+ "step": 100
25
+ },
26
+ {
27
+ "epoch": 0.3325942350332594,
28
+ "grad_norm": 6.555556297302246,
29
+ "learning_rate": 4.587028824833703e-05,
30
+ "loss": 0.377481575012207,
31
+ "step": 150
32
+ },
33
+ {
34
+ "epoch": 0.4434589800443459,
35
+ "grad_norm": 3.6121280193328857,
36
+ "learning_rate": 4.448447893569845e-05,
37
+ "loss": 0.3342946624755859,
38
+ "step": 200
39
+ },
40
+ {
41
+ "epoch": 0.5543237250554324,
42
+ "grad_norm": 6.620821952819824,
43
+ "learning_rate": 4.309866962305987e-05,
44
+ "loss": 0.3004466438293457,
45
+ "step": 250
46
+ },
47
+ {
48
+ "epoch": 0.6651884700665188,
49
+ "grad_norm": 3.3040060997009277,
50
+ "learning_rate": 4.171286031042129e-05,
51
+ "loss": 0.3366293716430664,
52
+ "step": 300
53
+ },
54
+ {
55
+ "epoch": 0.7760532150776053,
56
+ "grad_norm": 4.973900318145752,
57
+ "learning_rate": 4.0327050997782706e-05,
58
+ "loss": 0.2817412567138672,
59
+ "step": 350
60
+ },
61
+ {
62
+ "epoch": 0.8869179600886918,
63
+ "grad_norm": 5.162442684173584,
64
+ "learning_rate": 3.8941241685144125e-05,
65
+ "loss": 0.25218795776367187,
66
+ "step": 400
67
+ },
68
+ {
69
+ "epoch": 0.9977827050997783,
70
+ "grad_norm": 3.353585720062256,
71
+ "learning_rate": 3.7555432372505545e-05,
72
+ "loss": 0.27799144744873044,
73
+ "step": 450
74
+ },
75
+ {
76
+ "epoch": 1.0,
77
+ "eval_accuracy": 0.8829728230726567,
78
+ "eval_f1": 0.8908432488360062,
79
+ "eval_loss": 0.2983216941356659,
80
+ "eval_precision": 0.8351115421920465,
81
+ "eval_recall": 0.9545454545454546,
82
+ "eval_runtime": 3.5538,
83
+ "eval_samples_per_second": 507.338,
84
+ "eval_steps_per_second": 31.797,
85
+ "step": 451
86
+ },
87
+ {
88
+ "epoch": 1.1086474501108647,
89
+ "grad_norm": 3.653623342514038,
90
+ "learning_rate": 3.6169623059866964e-05,
91
+ "loss": 0.1536967372894287,
92
+ "step": 500
93
+ },
94
+ {
95
+ "epoch": 1.2195121951219512,
96
+ "grad_norm": 2.036006212234497,
97
+ "learning_rate": 3.478381374722838e-05,
98
+ "loss": 0.1598629665374756,
99
+ "step": 550
100
+ },
101
+ {
102
+ "epoch": 1.3303769401330376,
103
+ "grad_norm": 4.857279300689697,
104
+ "learning_rate": 3.33980044345898e-05,
105
+ "loss": 0.16571538925170898,
106
+ "step": 600
107
+ },
108
+ {
109
+ "epoch": 1.441241685144124,
110
+ "grad_norm": 3.656660556793213,
111
+ "learning_rate": 3.201219512195122e-05,
112
+ "loss": 0.15497909545898436,
113
+ "step": 650
114
+ },
115
+ {
116
+ "epoch": 1.5521064301552108,
117
+ "grad_norm": 0.165361687541008,
118
+ "learning_rate": 3.062638580931264e-05,
119
+ "loss": 0.15916526794433594,
120
+ "step": 700
121
+ },
122
+ {
123
+ "epoch": 1.6629711751662972,
124
+ "grad_norm": 4.209650039672852,
125
+ "learning_rate": 2.924057649667406e-05,
126
+ "loss": 0.1957833671569824,
127
+ "step": 750
128
+ },
129
+ {
130
+ "epoch": 1.7738359201773837,
131
+ "grad_norm": 6.54930305480957,
132
+ "learning_rate": 2.7854767184035478e-05,
133
+ "loss": 0.1656125259399414,
134
+ "step": 800
135
+ },
136
+ {
137
+ "epoch": 1.8847006651884701,
138
+ "grad_norm": 5.860881805419922,
139
+ "learning_rate": 2.64689578713969e-05,
140
+ "loss": 0.1518951988220215,
141
+ "step": 850
142
+ },
143
+ {
144
+ "epoch": 1.9955654101995566,
145
+ "grad_norm": 11.93374252319336,
146
+ "learning_rate": 2.508314855875832e-05,
147
+ "loss": 0.15036168098449706,
148
+ "step": 900
149
+ },
150
+ {
151
+ "epoch": 2.0,
152
+ "eval_accuracy": 0.9162506932889628,
153
+ "eval_f1": 0.9172602739726028,
154
+ "eval_loss": 0.2625793516635895,
155
+ "eval_precision": 0.9068255687973997,
156
+ "eval_recall": 0.9279379157427938,
157
+ "eval_runtime": 3.7906,
158
+ "eval_samples_per_second": 475.647,
159
+ "eval_steps_per_second": 29.81,
160
+ "step": 902
161
+ }
162
+ ],
163
+ "logging_steps": 50,
164
+ "max_steps": 1804,
165
+ "num_input_tokens_seen": 0,
166
+ "num_train_epochs": 4,
167
+ "save_steps": 500,
168
+ "stateful_callbacks": {
169
+ "TrainerControl": {
170
+ "args": {
171
+ "should_epoch_stop": false,
172
+ "should_evaluate": false,
173
+ "should_log": false,
174
+ "should_save": true,
175
+ "should_training_stop": false
176
+ },
177
+ "attributes": {}
178
+ }
179
+ },
180
+ "total_flos": 252497609185452.0,
181
+ "train_batch_size": 16,
182
+ "trial_name": null,
183
+ "trial_params": null
184
+ }
claim_classification_model_distilbert_trained/claim_classifier_model/checkpoint-902/training_args.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:da6c8a57f0ec42580aa33138157c76bee46b4f0e32444da93a783274403b01a2
3
+ size 5201
config.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ config.py
3
+ Single source of truth for all paths, model settings, and environment config.
4
+ Import from here everywhere β€” never hardcode paths in pipeline files.
5
+ """
6
+
7
+ import os
8
+ from pathlib import Path
9
+ from dotenv import load_dotenv
10
+
11
+ load_dotenv()
12
+
13
+ # ── Directory layout ──────────────────────────────────────────────────────────
14
+
15
+ BASE_DIR = Path(__file__).resolve().parent
16
+ DATA_DIR = BASE_DIR / "data"
17
+ FRONTEND_DIR = BASE_DIR / "frontend"
18
+ CHROMA_DIR = BASE_DIR / "chroma_db"
19
+
20
+ # ── Model paths ───────────────────────────────────────────────────────────────
21
+
22
+ MODEL_PATH = str(
23
+ BASE_DIR
24
+ / "claim_classification_model_distilbert_trained"
25
+ / "claim_classifier_model"
26
+ / "checkpoint-902"
27
+ )
28
+
29
+ # ── External API keys ─────────────────────────────────────────────────────────
30
+
31
+ GROQ_API_KEY = os.getenv("GROQ_API_KEY", "")
32
+
33
+ # ── Inference settings ────────────────────────────────────────────────────────
34
+
35
+ CLAIM_MODEL_BATCH_SIZE = 64
36
+ CLAIM_MODEL_THRESHOLD = 0.6
37
+ CLAIM_MODEL_MAX_LENGTH = 128
38
+
39
+ # ── RAG settings ──────────────────────────────────────────────────────────────
40
+
41
+ RAG_TOP_K = 6
42
+ CHROMA_DB_PATH = str(CHROMA_DIR)
43
+ CHROMA_COLLECTION = "claims"
44
+ EMBEDDING_MODEL = "all-MiniLM-L6-v2"
45
+ GROQ_MODEL = "llama-3.3-70b-versatile"
46
+
47
+ # ── Flask settings ────────────────────────────────────────────────────────────
48
+
49
+ DEBUG = os.getenv("FLASK_DEBUG", "false").lower() == "true"
50
+ HOST = os.getenv("FLASK_HOST", "0.0.0.0")
51
+ PORT = int(os.getenv("FLASK_PORT", 5000))
52
+ SECRET_KEY = os.getenv("SECRET_KEY", "dev-secret-change-in-prod")
data/logos/AXIS_Bank.png ADDED
data/logos/BAJAJ_FINANCE.png ADDED
data/logos/Bharthi_Airtel.png ADDED
data/logos/HCL.png ADDED
data/logos/HDFC_Bank.png ADDED
data/logos/ICICI_Bank.png ADDED
data/logos/INFOSYS.png ADDED
data/logos/ITC.png ADDED
data/logos/Kotak_Mahindra_Bank.png ADDED
data/logos/L_T.png ADDED
data/logos/Mahindra___Mahindra.png ADDED
data/logos/Reliance__Industries.png ADDED
data/logos/SBI.png ADDED
data/logos/SUN_PHARMA.png ADDED
data/logos/TCS.png ADDED
db.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ db.py
3
+ SQLite database setup. Single source of truth for schema and connection.
4
+ All tables are created here on first run β€” import get_db() everywhere else.
5
+ """
6
+
7
+ import sqlite3
8
+ from pathlib import Path
9
+ from contextlib import contextmanager
10
+
11
+ import config
12
+
13
+ DB_PATH = Path(config.BASE_DIR) / "promisetrack.db"
14
+
15
+
16
+ def init_db() -> None:
17
+ """Create all tables if they don't exist. Call once in create_app()."""
18
+ with get_db() as conn:
19
+ conn.executescript("""
20
+ CREATE TABLE IF NOT EXISTS companies (
21
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
22
+ folder_name TEXT UNIQUE NOT NULL,
23
+ display_name TEXT NOT NULL,
24
+ status TEXT NOT NULL DEFAULT 'pending',
25
+ processed_at TIMESTAMP,
26
+ error_msg TEXT
27
+ );
28
+
29
+ CREATE TABLE IF NOT EXISTS analysis_cache (
30
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
31
+ company_id INTEGER NOT NULL REFERENCES companies(id),
32
+ mode TEXT NOT NULL,
33
+ result_json TEXT NOT NULL,
34
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
35
+ UNIQUE(company_id, mode)
36
+ );
37
+
38
+ CREATE TABLE IF NOT EXISTS claims (
39
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
40
+ company_id INTEGER NOT NULL REFERENCES companies(id),
41
+ quarter TEXT,
42
+ sentence TEXT,
43
+ metric TEXT,
44
+ direction TEXT,
45
+ magnitude TEXT,
46
+ result TEXT,
47
+ actual_change REAL,
48
+ confidence REAL
49
+ );
50
+
51
+ CREATE TABLE IF NOT EXISTS timeseries (
52
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
53
+ company_id INTEGER NOT NULL REFERENCES companies(id),
54
+ quarter TEXT,
55
+ revenue REAL,
56
+ net_profit REAL,
57
+ operating_profit REAL,
58
+ profit_margin REAL,
59
+ revenue_qoq_change REAL,
60
+ net_profit_qoq_change REAL,
61
+ operating_profit_qoq_change REAL,
62
+ profit_margin_qoq_change REAL,
63
+ revenue_yoy_change REAL,
64
+ net_profit_yoy_change REAL,
65
+ operating_profit_yoy_change REAL,
66
+ profit_margin_yoy_change REAL,
67
+ UNIQUE(company_id, quarter)
68
+ );
69
+
70
+ CREATE TABLE IF NOT EXISTS risk (
71
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
72
+ company_id INTEGER NOT NULL REFERENCES companies(id),
73
+ quarter TEXT,
74
+ total_claims INTEGER,
75
+ verification_rate REAL,
76
+ failure_rate REAL,
77
+ partial_rate REAL,
78
+ direction_mismatch_rate REAL,
79
+ consistency_score REAL,
80
+ risk_drift REAL,
81
+ warning_flag INTEGER,
82
+ UNIQUE(company_id, quarter)
83
+ );
84
+ """)
85
+
86
+
87
+ @contextmanager
88
+ def get_db():
89
+ """Context manager that yields a SQLite connection with row_factory set."""
90
+ conn = sqlite3.connect(str(DB_PATH))
91
+ conn.row_factory = sqlite3.Row
92
+ conn.execute("PRAGMA journal_mode=WAL") # safe for concurrent reads
93
+ conn.execute("PRAGMA foreign_keys=ON")
94
+ try:
95
+ yield conn
96
+ conn.commit()
97
+ except Exception:
98
+ conn.rollback()
99
+ raise
100
+ finally:
101
+ conn.close()
pipelines/__init__.py ADDED
File without changes
pipelines/finance/__init__.py ADDED
File without changes
pipelines/finance/extract_xbrl_data.py ADDED
@@ -0,0 +1,239 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ extract_xbrl_data.py
4
+ Extracts XBRL/XML financial data from year-subfolder structure.
5
+ Handles: CompanyName / YearFolder / *.xml
6
+ """
7
+
8
+ import re
9
+ import xml.etree.ElementTree as ET
10
+ from pathlib import Path
11
+ from typing import Optional
12
+
13
+ METRIC_MAPPING = {
14
+ "RevenueFromOperations": "revenue", "Revenue": "revenue",
15
+ "Income": "revenue", "TotalIncome": "revenue",
16
+ "ProfitLossForPeriod": "net_profit",
17
+ "ProfitLossFromOrdinaryActivitiesAfterTax": "net_profit",
18
+ "ProfitLossForPeriodFromContinuingOperations": "net_profit",
19
+ "OperatingProfitBeforeProvisionAndContingencies": "operating_profit",
20
+ "OperatingProfit": "operating_profit",
21
+ "ProfitBeforeTax": "operating_profit",
22
+ "ProfitBeforeExceptionalItemsAndTax": "operating_profit",
23
+ "ProfitBeforeTaxAndExceptionalItems": "operating_profit",
24
+ "Expenses": "total_expenses", "OperatingExpenses": "total_expenses",
25
+ "ExpenditureExcludingProvisionsAndContingencies": "total_expenses",
26
+ "TaxExpense": "tax_expense",
27
+ "BasicEarningsPerShareAfterExtraordinaryItems": "eps",
28
+ }
29
+
30
+ # Tags that should NEVER be mapped to revenue even if they fuzzy-match "income".
31
+ # OtherIncome is a sub-component, not total revenue.
32
+ _REVENUE_BLOCKLIST = {
33
+ "OtherIncome", "InterestEarned", "InterestOrDiscountOnAdvancesOrBills",
34
+ "RevenueOnInvestments", "InterestOnBalancesWithReserveBankOfIndiaAndOtherInterBankFunds",
35
+ "OtherInterest",
36
+ }
37
+
38
+ # Priority order for revenue: higher number = preferred when multiple tags map to "revenue".
39
+ # Only the highest-priority tag seen per (company, quarter, metric) is kept.
40
+ _METRIC_PRIORITY = {
41
+ "revenue": {
42
+ "Income": 100, "TotalIncome": 100,
43
+ "RevenueFromOperations": 90, "Revenue": 90,
44
+ },
45
+ "net_profit": {
46
+ "ProfitLossFromOrdinaryActivitiesAfterTax": 100,
47
+ "ProfitLossForPeriod": 90,
48
+ "ProfitLossForPeriodFromContinuingOperations": 80,
49
+ },
50
+ "operating_profit": {
51
+ "OperatingProfitBeforeProvisionAndContingencies": 100,
52
+ "OperatingProfit": 90,
53
+ "ProfitBeforeExceptionalItemsAndTax": 80,
54
+ "ProfitBeforeTaxAndExceptionalItems": 80,
55
+ "ProfitBeforeTax": 70,
56
+ },
57
+ }
58
+
59
+
60
+ def _parse_year_from_folder(folder_name: str) -> Optional[int]:
61
+ m = re.search(r'(\d{2})-(\d{2})', folder_name)
62
+ if m:
63
+ return 2000 + int(m.group(2))
64
+ m = re.search(r'[_\s](\d{2,4})$', folder_name)
65
+ if m:
66
+ y = int(m.group(1))
67
+ return y if y > 2000 else 2000 + y
68
+ m = re.search(r'(20\d{2})', folder_name)
69
+ if m:
70
+ return int(m.group(1))
71
+ return None
72
+
73
+
74
+ def _assign_weights(year_folders: list) -> dict:
75
+ sorted_f = sorted(year_folders, key=lambda x: x[1], reverse=True)
76
+ return {folder: round(max(1.0 - rank * 0.25, 0.1), 2)
77
+ for rank, (folder, _) in enumerate(sorted_f)}
78
+
79
+
80
+ def _quarter_from_context(root, context_id: str) -> Optional[str]:
81
+ try:
82
+ context = root.find(f".//*[@id='{context_id}']")
83
+ if context is None:
84
+ return None
85
+ period = context.find("{http://www.xbrl.org/2003/instance}period")
86
+ if period is None:
87
+ return None
88
+
89
+ start_date = period.find("{http://www.xbrl.org/2003/instance}startDate")
90
+ end_date = period.find("{http://www.xbrl.org/2003/instance}endDate")
91
+ instant = period.find("{http://www.xbrl.org/2003/instance}instant")
92
+
93
+ # Ignore cumulative/YTD data (e.g., 9 months, 6 months). We only want ~90 day quarters.
94
+ if start_date is not None and end_date is not None and start_date.text and end_date.text:
95
+ from datetime import datetime
96
+ try:
97
+ sd = datetime.strptime(start_date.text, "%Y-%m-%d")
98
+ ed = datetime.strptime(end_date.text, "%Y-%m-%d")
99
+ days = (ed - sd).days
100
+ if days > 105: # Skip periods longer than a quarter
101
+ return None
102
+ except Exception:
103
+ pass
104
+
105
+ node = end_date if end_date is not None else instant
106
+ if node is None or not node.text:
107
+ return None
108
+ m = re.match(r"(\d{4})-(\d{2})-\d{2}", node.text)
109
+ if not m:
110
+ return None
111
+
112
+ year, month = int(m.group(1)), int(m.group(2))
113
+
114
+ # ── FIX: Indian Financial Year Math ──
115
+ # If the calendar month is Jan-Mar, the FY matches the calendar year.
116
+ # If the calendar month is Apr-Dec, the FY is the NEXT calendar year.
117
+ fy_year = year if month <= 3 else year + 1
118
+ q = 4 if month <= 3 else (1 if month <= 6 else (2 if month <= 9 else 3))
119
+
120
+ return f"{fy_year}-Q{q}"
121
+
122
+ except Exception:
123
+ return None
124
+
125
+
126
+ def _normalize_metric(tag: str) -> Optional[str]:
127
+ clean = tag.split("}")[-1] if "}" in tag else tag.split(":")[-1]
128
+ # Exact match first
129
+ if clean in METRIC_MAPPING:
130
+ # Block sub-components that fuzzy-match revenue but aren't total revenue
131
+ if clean in _REVENUE_BLOCKLIST:
132
+ return None
133
+ return METRIC_MAPPING[clean]
134
+ # Fuzzy match β€” but never let blocklisted tags through
135
+ if clean in _REVENUE_BLOCKLIST:
136
+ return None
137
+ for key, val in METRIC_MAPPING.items():
138
+ if key.lower() in clean.lower():
139
+ return val
140
+ return None
141
+
142
+
143
+ def _tag_priority(clean_tag: str, metric: str) -> int:
144
+ """Return priority score for a tag within its metric group. Higher = preferred."""
145
+ return _METRIC_PRIORITY.get(metric, {}).get(clean_tag, 50)
146
+
147
+
148
+ def _is_segment(context_id: str) -> bool:
149
+ return any(x in context_id.lower()
150
+ for x in ["segment", "reportablesegment", "geographicsegment"])
151
+
152
+
153
+ def extract_numeric_data(xml_path: Path, company_name: str,
154
+ weight: float = 1.0) -> list:
155
+ try:
156
+ root = ET.parse(xml_path).getroot()
157
+ except Exception:
158
+ return []
159
+
160
+ # best[(quarter, metric)] = {"value": ..., "priority": ..., "weight": ...}
161
+ best: dict = {}
162
+
163
+ for elem in root.iter():
164
+ if not (elem.get("unitRef") and elem.get("contextRef") and elem.text):
165
+ continue
166
+ try:
167
+ value = float(elem.text)
168
+ except (ValueError, TypeError):
169
+ continue
170
+ if abs(value) < 1:
171
+ continue
172
+ ctx = elem.get("contextRef")
173
+ if _is_segment(ctx):
174
+ continue
175
+ quarter = _quarter_from_context(root, ctx)
176
+ if not quarter:
177
+ continue
178
+ metric = _normalize_metric(elem.tag)
179
+ if not metric:
180
+ continue
181
+ if abs(value) > 1_000_000:
182
+ value = value / 10_000_000
183
+
184
+ clean_tag = elem.tag.split("}")[-1] if "}" in elem.tag else elem.tag.split(":")[-1]
185
+ priority = _tag_priority(clean_tag, metric)
186
+ key = (quarter, metric)
187
+
188
+ if key not in best or priority > best[key]["priority"]:
189
+ best[key] = {"value": round(value, 4), "priority": priority}
190
+
191
+ return [
192
+ {"company": company_name, "quarter": q, "metric": m,
193
+ "value": v["value"], "weight": weight}
194
+ for (q, m), v in best.items()
195
+ ]
196
+
197
+
198
+ def run_xbrl_extraction_pipeline(companies_dir_path: str) -> list:
199
+ """
200
+ Scans: companies_dir/<Company>/<YearFolder>/*.xml
201
+ Assigns recency weights same as text pipeline.
202
+ """
203
+ companies_dir = Path(companies_dir_path)
204
+ if not companies_dir.exists():
205
+ raise FileNotFoundError(f"Not found: {companies_dir}")
206
+
207
+ all_records = []
208
+ for company_dir in sorted(companies_dir.iterdir()):
209
+ if not company_dir.is_dir():
210
+ continue
211
+ company_name = company_dir.name
212
+
213
+ year_folders = []
214
+ for sub in company_dir.iterdir():
215
+ if not sub.is_dir():
216
+ continue
217
+ year = _parse_year_from_folder(sub.name)
218
+ if year:
219
+ year_folders.append((sub, year))
220
+ if not year_folders:
221
+ year_folders = [(company_dir, 2024)]
222
+
223
+ weight_map = _assign_weights(year_folders)
224
+
225
+ for folder, year in year_folders:
226
+ weight = weight_map.get(folder, 0.1)
227
+ for xml_file in sorted(folder.glob("*.xml")):
228
+ records = extract_numeric_data(xml_file, company_name, weight)
229
+ all_records.extend(records)
230
+
231
+ # Deduplicate
232
+ seen, unique = set(), []
233
+ for r in all_records:
234
+ key = (r["company"], r["quarter"], r["metric"], r["value"])
235
+ if key not in seen:
236
+ unique.append(r)
237
+ seen.add(key)
238
+ unique.sort(key=lambda r: (r["company"], r["quarter"], r["metric"]))
239
+ return unique
pipelines/finance/prepare_timeseries_data.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ prepare_timeseries_data.py
4
+ Builds a wide time-series DataFrame from raw XBRL numeric records,
5
+ adding QoQ / YoY change features and profit margin.
6
+
7
+ Flask entry point: run_timeseries_pipeline(xbrl_records)
8
+ """
9
+
10
+ import numpy as np
11
+ import pandas as pd
12
+
13
+
14
+ # ── Config ────────────────────────────────────────────────────────────────────
15
+
16
+ KEY_METRICS = {
17
+ "revenue": ["revenue", "total_income"],
18
+ "net_profit": ["net_profit", "profit_after_tax"],
19
+ "operating_profit": ["operating_profit"],
20
+ }
21
+
22
+ _FEATURE_COLS = ["revenue", "net_profit", "operating_profit", "profit_margin"]
23
+
24
+
25
+ # ── Helpers ───────────────────────────────────────────────────────────────────
26
+
27
+ def _sort_key(q: str) -> tuple:
28
+ try:
29
+ year, qn = q.split("-Q")
30
+ return (int(year), int(qn))
31
+ except Exception:
32
+ return (0, 0)
33
+
34
+ def _safe_pct_change(series: pd.Series) -> pd.Series:
35
+ return (series - series.shift(1)) / series.shift(1) * 100
36
+
37
+
38
+ # ── Flask entry point ─────────────────────────────────────────────────────────
39
+
40
+ def run_timeseries_pipeline(xbrl_records: list[dict]) -> list[dict]:
41
+ """
42
+ Main entry point for the Flask app.
43
+
44
+ Input : list[dict] from run_xbrl_extraction_pipeline()
45
+ Keys: company, quarter, metric, value
46
+ Output: list[dict] β€” one row per (company, quarter) with derived
47
+ time-series features (QoQ, YoY, margin).
48
+ """
49
+ if not xbrl_records:
50
+ return []
51
+
52
+ df = pd.DataFrame(xbrl_records)
53
+
54
+ # ── Consolidate to wide format ────────────────────────────────────────────
55
+ records = []
56
+ for company in df["company"].unique():
57
+ cdf = df[df["company"] == company]
58
+ for quarter in cdf["quarter"].unique():
59
+ qdf = cdf[cdf["quarter"] == quarter]
60
+ row = {"company": company, "quarter": quarter}
61
+ for target, sources in KEY_METRICS.items():
62
+ val = None
63
+ for src in sources:
64
+ subset = qdf[qdf["metric"] == src]
65
+ if not subset.empty:
66
+ # Use max to get the consolidated/total figure,
67
+ # not a sub-component that may have snuck through.
68
+ val = subset["value"].max()
69
+ break
70
+ row[target] = val
71
+ records.append(row)
72
+
73
+ wide = pd.DataFrame(records)
74
+
75
+ # ── Sort chronologically ──────────────────────────────────────────────────
76
+ wide["_sort"] = wide["quarter"].apply(_sort_key)
77
+ wide = (
78
+ wide.sort_values(["company", "_sort"])
79
+ .drop(columns=["_sort"])
80
+ .reset_index(drop=True)
81
+ )
82
+
83
+ # ── Forward fill within each company ─────────────────────────────────────
84
+ wide = (
85
+ wide.groupby("company", group_keys=False)
86
+ .apply(lambda x: x.ffill())
87
+ .reset_index(drop=True)
88
+ )
89
+
90
+ # ── Derived metrics ───────────────────────────────────────────────────────
91
+ wide["profit_margin"] = wide["net_profit"] / wide["revenue"] * 100
92
+
93
+ # ── Time-series features (QoQ / YoY) ─────────────────────────────────────
94
+ for company in wide["company"].unique():
95
+ mask = wide["company"] == company
96
+ idx = wide[mask].index
97
+ for col in _FEATURE_COLS:
98
+ values = pd.to_numeric(wide.loc[mask, col], errors="coerce")
99
+ wide.loc[idx, f"{col}_qoq_change"] = _safe_pct_change(values)
100
+ wide.loc[idx, f"{col}_qoq_abs_change"] = values.diff()
101
+ wide.loc[idx, f"{col}_yoy_change"] = (
102
+ (values - values.shift(4)) / values.shift(4) * 100
103
+ )
104
+
105
+ # ── Drop all-NaN columns ──────────────────────────────────────────────────
106
+ wide = wide.dropna(how="all", axis=1)
107
+
108
+ return wide.to_dict(orient="records")
109
+
pipelines/finance/verify_claims.py ADDED
@@ -0,0 +1,197 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ verify_claims.py
4
+ Cross-references structured claim attributes against XBRL time-series data
5
+ to verify whether each claim's stated direction is supported by the numbers.
6
+
7
+ Flask entry point: run_claim_verification_pipeline(claim_records, timeseries_records)
8
+ """
9
+
10
+ import re
11
+ import pandas as pd
12
+
13
+
14
+ # ── Metric mapping ────────────────────────────────────────────────────────────
15
+
16
+ METRIC_COL = {
17
+ "revenue": "revenue",
18
+ "profit": "net_profit",
19
+ "ebitda": "operating_profit",
20
+ "margin": "profit_margin",
21
+ "expenses": "total_expenses",
22
+ # proxy mappings
23
+ "subscriber": "revenue",
24
+ "order": "revenue",
25
+ "deposit": "revenue",
26
+ "loan": "revenue",
27
+ "volume": "revenue",
28
+ "arpu": "revenue",
29
+ "return": "profit_margin",
30
+ "provision": "net_profit",
31
+ }
32
+
33
+
34
+ # ── Helpers ───────────────────────────────────────────────────────────────────
35
+
36
+ def _normalise_quarter(q: str) -> str:
37
+ """Cleans quarter strings and converts Calendar Years to Indian FY."""
38
+ q = str(q).strip().upper()
39
+ year, quarter = None, None
40
+
41
+ m = re.search(r"([1-4])[Qq].*?(?:FY|20)?(\d{2,4})", q)
42
+ if not m: m = re.search(r"[Qq]([1-4]).*?(?:FY|20)?(\d{2,4})", q)
43
+ if not m:
44
+ m = re.match(r"^\d{4}-Q[1-4]$", q)
45
+ if m: year, quarter = int(q[:4]), int(q[-1])
46
+
47
+ if m and not year:
48
+ y_val = int(m.group(2))
49
+ year = y_val + 2000 if y_val < 100 else y_val
50
+ quarter = int(m.group(1))
51
+
52
+ if year and quarter:
53
+ if "FY" in q: return f"{year}-Q{quarter}"
54
+ # Indian FY Math: Apr-Dec (Q1, Q2, Q3) -> Next Year. Jan-Mar (Q4) -> Same Year.
55
+ fy_year = year + 1 if quarter in [1, 2, 3] else year
56
+ return f"{fy_year}-Q{quarter}"
57
+
58
+ return q
59
+
60
+ def _infer_ref_type(sentence: str) -> str:
61
+ t = str(sentence).lower()
62
+ if "yoy" in t or "year" in t:
63
+ return "yoy"
64
+ return "qoq"
65
+
66
+
67
+ def _evaluate(direction: str, change: float) -> tuple[str, str]:
68
+ if direction == "increase":
69
+ if change > 2:
70
+ return "VERIFIED", "VERIFIED_STRONG"
71
+ if change < -2:
72
+ return "NOT VERIFIED", "CONTRADICTS_DIRECTION"
73
+ return "PARTIAL", "PARTIAL_NO_CHANGE"
74
+
75
+ if direction == "decrease":
76
+ if change < -2:
77
+ return "VERIFIED", "VERIFIED_STRONG"
78
+ if change > 2:
79
+ return "NOT VERIFIED", "CONTRADICTS_DIRECTION"
80
+ return "PARTIAL", "PARTIAL_NO_CHANGE"
81
+
82
+ return "PARTIAL", "UNKNOWN_DIRECTION"
83
+
84
+
85
+ def _get_nearest_row(ts_df: pd.DataFrame, company_key: str, quarter: str):
86
+ """
87
+ Returns (row, match_type). Tries exact match first, then falls back
88
+ to the previous quarter, then the latest available.
89
+ """
90
+ company_rows = ts_df[ts_df["_company_key"] == company_key]
91
+
92
+ if company_rows.empty:
93
+ return None, "NO_COMPANY_DATA"
94
+
95
+ exact = company_rows[company_rows["quarter"] == quarter]
96
+ if not exact.empty:
97
+ return exact.iloc[0], "EXACT_MATCH"
98
+
99
+ sorted_rows = company_rows.sort_values("quarter")
100
+ prev = sorted_rows[sorted_rows["quarter"] <= quarter]
101
+ if not prev.empty:
102
+ return prev.iloc[-1], "FALLBACK_PREV_QUARTER"
103
+
104
+ return sorted_rows.iloc[-1], "FALLBACK_LATEST"
105
+
106
+
107
+ # ── Flask entry point ─────────────────────────────────────────────────────────
108
+
109
+ def run_claim_verification_pipeline(
110
+ claim_records: list[dict],
111
+ timeseries_records: list[dict],
112
+ ) -> list[dict]:
113
+ """
114
+ Main entry point for the Flask app.
115
+
116
+ Input : claim_records β€” from run_attribute_extraction_pipeline()
117
+ Keys: company, quarter, sentence, metric,
118
+ direction, magnitude, direction_missing
119
+ timeseries_records β€” from run_timeseries_pipeline()
120
+ Keys: company, quarter, revenue, net_profit, ...
121
+ Output: list[dict] β€” one record per claim with verification result appended.
122
+ Added keys: actual_change, result, reason
123
+ """
124
+ if not claim_records or not timeseries_records:
125
+ return []
126
+
127
+ claims = pd.DataFrame(claim_records)
128
+ ts = pd.DataFrame(timeseries_records)
129
+
130
+ claims["quarter"] = claims["quarter"].apply(_normalise_quarter)
131
+ ts["quarter"] = ts["quarter"].apply(_normalise_quarter)
132
+ ts["_company_key"] = ts["company"].str.lower().str.strip()
133
+ claims["_company_key"] = claims["company"].str.lower().str.strip()
134
+
135
+ results = []
136
+
137
+ for _, row in claims.iterrows():
138
+ company_key = row["_company_key"]
139
+ quarter = row["quarter"]
140
+ metric = row["metric"]
141
+ direction = str(row.get("direction", "")).lower()
142
+ sentence = row.get("sentence", "")
143
+ base_col = METRIC_COL.get(metric)
144
+
145
+ # Unknown metric
146
+ if not base_col or base_col not in ts.columns:
147
+ results.append({
148
+ **row.drop("_company_key").to_dict(),
149
+ "actual_change": None,
150
+ "result": "SKIPPED",
151
+ "reason": "MISSING_METRIC_MAPPING",
152
+ })
153
+ continue
154
+
155
+ ts_row, match_type = _get_nearest_row(ts, company_key, quarter)
156
+
157
+ if ts_row is None:
158
+ results.append({
159
+ **row.drop("_company_key").to_dict(),
160
+ "actual_change": None,
161
+ "result": "SKIPPED",
162
+ "reason": "MISSING_TS_ROW",
163
+ })
164
+ continue
165
+
166
+ # Resolve change column (YoY or QoQ, with QoQ fallback)
167
+ ref_type = _infer_ref_type(sentence)
168
+ primary_col = f"{base_col}_{ref_type}_change"
169
+ fallback_col = f"{base_col}_qoq_change"
170
+
171
+ actual_change = ts_row.get(primary_col)
172
+ fallback_used = False
173
+
174
+ if pd.isna(actual_change):
175
+ actual_change = ts_row.get(fallback_col)
176
+ fallback_used = True
177
+
178
+ if pd.isna(actual_change):
179
+ results.append({
180
+ **row.drop("_company_key").to_dict(),
181
+ "actual_change": None,
182
+ "result": "SKIPPED",
183
+ "reason": "MISSING_VALUE",
184
+ })
185
+ continue
186
+
187
+ result, reason = _evaluate(direction, float(actual_change))
188
+ tag = match_type + ("|FALLBACK_CHANGE" if fallback_used else "")
189
+
190
+ results.append({
191
+ **row.drop("_company_key").to_dict(),
192
+ "actual_change": round(float(actual_change), 2),
193
+ "result": result,
194
+ "reason": f"{reason} | {tag}",
195
+ })
196
+
197
+ return results
pipelines/ml/__init__.py ADDED
File without changes
pipelines/ml/extract_attributes.py ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Optional
2
+ #!/usr/bin/env python3
3
+ """
4
+ extract_attributes.py
5
+ Extracts structured attributes (metric, direction, magnitude) from claim sentences.
6
+
7
+ Flask entry point: run_attribute_extraction_pipeline(claim_records)
8
+ """
9
+
10
+ import re
11
+
12
+
13
+ # ── Metric map ────────────────────────────────────────────────────────────────
14
+
15
+ METRIC_MAP = [
16
+ ("ebitda", ["ebitda"]),
17
+ ("revenue", ["revenue", "sales", "turnover", "topline"]),
18
+ ("profit", ["net profit", "profit", "pat", "pbt", "earnings"]),
19
+ ("margin", ["margin", "ebitda margin", "operating margin"]),
20
+ ("expenses", ["cost", "expense", "capex", "opex"]),
21
+ ("loan", ["loan", "loan book", "credit", "advances"]),
22
+ ("deposit", ["deposit", "casa"]),
23
+ ("npa", ["npa", "gnpa", "nnpa"]),
24
+ ("arpu", ["arpu"]),
25
+ ("aum", ["aum"]),
26
+ ("volume", ["volume"]),
27
+ ("subscriber", ["subscriber", "user", "customer base"]),
28
+ ("order", ["order", "order book", "backlog"]),
29
+ ("debt", ["debt", "net debt"]),
30
+ ("cash_flow", ["cash flow"]),
31
+ ("return", ["roe", "roce", "roa"]),
32
+ ("provision", ["provision"]),
33
+ ("market_share", ["market share"]),
34
+ ]
35
+
36
+ # ── Direction patterns (data-driven) ─────────────────────────────────────────
37
+
38
+ INCREASE_PATTERNS = r"(increase|growth|grew|improve|expanded|rise|higher|up|strong|robust|healthy|better)"
39
+ DECREASE_PATTERNS = r"(decline|decrease|fall|drop|loss|pressure|weak|compression|down|impact)"
40
+ NEUTRAL_PATTERNS = r"(stable|steady|flat|maintain|unchanged)"
41
+
42
+ # ── Magnitude ─────────────────────────────────────────────────────────────────
43
+
44
+ _MAG_RE = re.compile(
45
+ r'(\d+(\.\d+)?\s*(%|bps|basis points|crore|million|billion|lakh))',
46
+ re.IGNORECASE,
47
+ )
48
+
49
+
50
+ # ── Helpers ───────────────────────────────────────────────────────────────────
51
+
52
+ def _clean(s: str) -> str:
53
+ s = re.sub(r'[\r\n\t]+', ' ', s)
54
+ s = re.sub(r'\s+', ' ', s)
55
+ return s.strip().lower()
56
+
57
+ def _split_sentence(s: str) -> list[str]:
58
+ """Split on contrast conjunctions only β€” NOT on 'and'."""
59
+ parts = re.split(r'\bbut\b|\bwhile\b|\bhowever\b', s)
60
+ return [p.strip() for p in parts if len(p.strip()) > 20]
61
+
62
+ def extract_metrics(text: str) -> list[str]:
63
+ t = text.lower()
64
+ found = []
65
+ for name, kws in METRIC_MAP:
66
+ for kw in kws:
67
+ if kw in t:
68
+ found.append(name)
69
+ break
70
+ return list(set(found))
71
+
72
+ def extract_direction(text: str, metric: str) -> Optional[str]:
73
+ t = text.lower()
74
+
75
+ # Global direction β€” decrease takes priority
76
+ if re.search(DECREASE_PATTERNS, t):
77
+ return "decrease"
78
+ if re.search(r"(increase|growth|grew|improve|expanded|rise|higher|up)", t):
79
+ return "increase"
80
+ # Soft positive β†’ neutral
81
+ if re.search(r"(strong|robust|healthy|solid)", t):
82
+ return "neutral"
83
+ if re.search(NEUTRAL_PATTERNS, t):
84
+ return "neutral"
85
+
86
+ # Metric-local fallback
87
+ words = re.findall(r"[a-z]+", t)
88
+ metric_positions = [i for i, w in enumerate(words) if metric in w]
89
+ for pos in metric_positions:
90
+ window = " ".join(words[max(0, pos - 8) : pos + 8])
91
+ if re.search(DECREASE_PATTERNS, window):
92
+ return "decrease"
93
+ if re.search(INCREASE_PATTERNS, window):
94
+ return "increase"
95
+
96
+ return None
97
+
98
+ def extract_magnitude(text: str) -> Optional[str]:
99
+ m = _MAG_RE.findall(text)
100
+ return m[0][0] if m else None
101
+
102
+
103
+ # ── Flask entry point ─────────────────────────────────────────────────────────
104
+
105
+ def run_attribute_extraction_pipeline(claim_records: list[dict]) -> list[dict]:
106
+ """
107
+ Main entry point for the Flask app.
108
+
109
+ Input : List of dicts from run_claim_extraction_pipeline(),
110
+ each must have: sentence, company, quarter
111
+ Output: List of dicts β€” one per (sentence-part Γ— metric) combination.
112
+ """
113
+ records = []
114
+
115
+ for row in claim_records:
116
+ sentence = str(row.get("sentence", ""))
117
+
118
+ for part in _split_sentence(sentence):
119
+ clean_sent = _clean(part)
120
+ metrics = extract_metrics(clean_sent)
121
+
122
+ if not metrics:
123
+ continue
124
+
125
+ magnitude = extract_magnitude(clean_sent)
126
+
127
+ for metric in metrics:
128
+ direction = extract_direction(clean_sent, metric)
129
+ records.append({
130
+ "company": row.get("company", ""),
131
+ "quarter": row.get("quarter", ""),
132
+ "sentence": part,
133
+ "metric": metric,
134
+ "direction": direction,
135
+ "magnitude": magnitude,
136
+ "direction_missing": direction is None,
137
+ })
138
+
139
+ # Deduplicate on sentence + metric
140
+ seen = set()
141
+ unique = []
142
+ for r in records:
143
+ key = (r["sentence"], r["metric"])
144
+ if key not in seen:
145
+ unique.append(r)
146
+ seen.add(key)
147
+
148
+ return unique
pipelines/ml/merge_claim_dataset.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ merge_claim_dataset.py
4
+ Merges claims + non-claims into a balanced, shuffled training dataset.
5
+
6
+ Flask entry point: run_merge_dataset_pipeline(claim_records, non_claim_records)
7
+ """
8
+
9
+ import pandas as pd
10
+
11
+
12
+ def run_merge_dataset_pipeline(
13
+ claim_records: list[dict],
14
+ non_claim_records: list[dict],
15
+ ) -> list[dict]:
16
+ """
17
+ Main entry point for the Flask app.
18
+
19
+ Input : claim_records β€” list of dicts with at least a 'sentence' key
20
+ (from run_claim_extraction_pipeline())
21
+ non_claim_records β€” list of dicts with at least a 'sentence' key
22
+ (from run_non_claim_extraction_pipeline())
23
+ Output: Deduplicated, shuffled list of dicts with keys: sentence, label.
24
+ """
25
+ claims = pd.DataFrame(claim_records)
26
+ non_claims = pd.DataFrame(non_claim_records)
27
+
28
+ claims["label"] = "CLAIM"
29
+ non_claims["label"] = "NON_CLAIM"
30
+
31
+ df = pd.concat(
32
+ [claims[["sentence", "label"]], non_claims[["sentence", "label"]]],
33
+ ignore_index=True,
34
+ )
35
+
36
+ df = df.drop_duplicates(subset="sentence")
37
+ df = df.sample(frac=1, random_state=42).reset_index(drop=True)
38
+
39
+ return df.to_dict(orient="records")
40
+
pipelines/ml/non_claim_extractor.py ADDED
@@ -0,0 +1,193 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ non_claim_extractor.py
4
+
5
+ 1. Removes sentences already identified as claims from the pool.
6
+ 2. Runs a lightweight second-pass keyword score on the remainder.
7
+ 3. Sentences scoring above threshold are rescued into claims
8
+ (catches what the spaCy pass missed).
9
+ 4. Samples the clean remainder to balance the dataset.
10
+
11
+ Flask entry point: run_non_claim_extraction_pipeline(sentence_records, claim_records)
12
+ """
13
+ """
14
+ import re
15
+ import pandas as pd
16
+
17
+
18
+ # ── Keyword sets for second-pass scoring ──────────────────────────────────────
19
+
20
+ _PERF = {
21
+ "revenue", "revenues", "sales", "profit", "profits", "ebitda", "margin",
22
+ "margins", "income", "earnings", "growth", "decline", "volume", "volumes",
23
+ "cost", "costs", "loan", "loans", "deposit", "deposits", "credit",
24
+ "arpu", "aum", "subscriber", "subscribers", "customer", "customers",
25
+ "order", "orders", "capacity", "utilisation", "utilization", "share",
26
+ "contribution", "mix", "demand", "supply", "price", "pricing", "return",
27
+ "cash", "debt", "capex", "provision", "coverage", "ratio", "fee", "spread",
28
+ "collection", "disbursement", "addition", "base", "book", "output",
29
+ }
30
+
31
+ _DIR = {
32
+ "grew", "grow", "growth", "increase", "increased", "rise", "rose",
33
+ "improve", "improved", "improvement", "expand", "expanded", "expansion",
34
+ "decline", "declined", "decrease", "decreased", "fall", "fell", "drop",
35
+ "strong", "robust", "healthy", "solid", "stable", "steady", "record",
36
+ "higher", "lower", "better", "significant", "substantial", "momentum",
37
+ "recover", "recovered", "deliver", "delivered", "achieve", "achieved",
38
+ "surge", "surged", "jump", "jumped", "scale", "scaled", "accelerat",
39
+ "well", "good", "broad", "double", "triple", "outperform",
40
+ }
41
+
42
+ _NEGATION_RE = re.compile(r"\b(not|no|never|don't|do not|didn't|did not|won't|will not)\b", re.IGNORECASE)
43
+ _QUESTION_RE = re.compile(r"\?$")
44
+ _GARBAGE_RE = re.compile(r"^[\d\s\.\,\%\|\-\(\)\$\β‚Ή\/\:]+$")
45
+ _TRANSITION_RE = re.compile(
46
+ r"^(thank|thanks|good (morning|evening|afternoon)|hi |hello |"
47
+ r"let me (first|now|just)|moving on|turning to|coming to|over to|"
48
+ r"operator|moderator|so (let|shall) (me|us))",
49
+ re.IGNORECASE,
50
+ )
51
+
52
+
53
+ # ── Helpers ───────────────────────────────────────────────────────────────────
54
+
55
+ def _dedup_key(s: str) -> str:
56
+ return re.sub(r"[^a-z0-9 ]", " ", re.sub(r"\s+", " ", str(s).lower())).strip()
57
+
58
+ def keyword_score(sentence: str) -> int:
59
+
60
+ s = sentence.lower()
61
+ tokens = set(re.findall(r"[a-z]+", s))
62
+ perf_hits = sum(1 for kw in _PERF if kw in s)
63
+ dir_hits = sum(1 for kw in _DIR if any(t.startswith(kw) for t in tokens))
64
+ return perf_hits + dir_hits
65
+
66
+ def is_likely_claim(sentence: str) -> bool:
67
+
68
+ s = sentence.strip()
69
+ if len(s) < 25:
70
+ return False
71
+ if _QUESTION_RE.search(s) or _GARBAGE_RE.match(s) or _TRANSITION_RE.match(s):
72
+ return False
73
+ if _NEGATION_RE.search(s):
74
+ return False
75
+ return keyword_score(s) >= 3
76
+
77
+
78
+ # ── Flask entry point ─────────────────────────────────────────────────────────
79
+
80
+ def run_non_claim_extraction_pipeline(
81
+ sentence_records: list[dict],
82
+ claim_records: list[dict],
83
+ ) -> dict:
84
+
85
+
86
+ sentences_df = pd.DataFrame(sentence_records)
87
+ claims_df = pd.DataFrame(claim_records)
88
+
89
+ # Build exclusion set from existing claims
90
+ claim_keys = set(claims_df["sentence"].apply(_dedup_key))
91
+ sentences_df["_key"] = sentences_df["sentence"].apply(_dedup_key)
92
+
93
+ remainder = (
94
+ sentences_df[~sentences_df["_key"].isin(claim_keys)]
95
+ .drop_duplicates("_key")
96
+ .copy()
97
+ )
98
+
99
+ # Second-pass: rescue missed claims from the remainder
100
+ rescued_mask = remainder["sentence"].apply(is_likely_claim)
101
+ rescued = remainder[rescued_mask].drop(columns="_key")
102
+ true_non = remainder[~rescued_mask].drop(columns="_key")
103
+
104
+ # Merge rescued into claims, deduplicate
105
+ all_claims_df = pd.concat([claims_df, rescued], ignore_index=True)
106
+ all_claims_df["_key"] = all_claims_df["sentence"].apply(_dedup_key)
107
+ all_claims_df = (
108
+ all_claims_df.drop_duplicates("_key")
109
+ .drop(columns="_key")
110
+ .reset_index(drop=True)
111
+ )
112
+
113
+ # Sample non-claims to match claim count (balanced dataset)
114
+ true_non = true_non.drop_duplicates(subset="sentence")
115
+ n_sample = min(len(all_claims_df), len(true_non))
116
+ non_claims_df = true_non.sample(n=n_sample, random_state=42).copy()
117
+ non_claims_df["label"] = "NON_CLAIM"
118
+
119
+ return {
120
+ "claims": all_claims_df.to_dict(orient="records"),
121
+ "non_claims": non_claims_df.to_dict(orient="records"),
122
+ }
123
+ """
124
+
125
+ import re
126
+ import torch
127
+ import pandas as pd
128
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
129
+
130
+ # Load FinBERT - specifically tuned for sentiment & claim-like financial tones
131
+ # For a production agent, you'd use a local model to save on API costs
132
+ MODEL_NAME = "ProsusAI/finbert"
133
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
134
+ model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
135
+
136
+ def is_numeric_claim(sentence: str) -> bool:
137
+ """
138
+ Core logic: A financial claim must contain numeric proof
139
+ (dates, percentages, currency, or counts).
140
+ """
141
+ # Regex for: $10M, 5%, 2026, 1.5bn, β‚Ή500cr
142
+ numeric_pattern = r"(\d+\.?\d*)\s?([%m|bn|cr|k|%|β‚Ή|\$])"
143
+ return bool(re.search(numeric_pattern, sentence, re.IGNORECASE))
144
+
145
+ def get_finbert_score(sentence: str):
146
+ """Uses Transformer to check if the sentence has a 'factual' or 'positive/negative' tone."""
147
+ inputs = tokenizer(sentence, return_tensors="pt", truncation=True, padding=True, max_length=128)
148
+ with torch.no_grad():
149
+ outputs = model(**inputs)
150
+
151
+ # 0: Positive, 1: Negative, 2: Neutral
152
+ # Claims are rarely 'Neutral'β€”they are usually driving a narrative (Pos/Neg)
153
+ probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
154
+ conf, label = torch.max(probs, dim=-1)
155
+ return label.item(), conf.item()
156
+
157
+ def run_non_claim_extraction_pipeline(sentence_records, claim_records):
158
+ """
159
+ Main entry point - same I/O as your old code, but with deep learning logic.
160
+ """
161
+ sentences_df = pd.DataFrame(sentence_records)
162
+ claims_df = pd.DataFrame(claim_records)
163
+
164
+ # 1. First Pass: Numerical Density Filter
165
+ # Most management claims contain a hard number (Numeric Drift Detection)
166
+ sentences_df["is_claim_candidate"] = sentences_df["sentence"].apply(is_numeric_claim)
167
+
168
+ # 2. Second Pass: FinBERT Validation
169
+ # We only run the heavy transformer on candidates to save compute
170
+ def validate_claim(row):
171
+ if not row["is_claim_candidate"]:
172
+ return False
173
+ label, conf = get_finbert_score(row["sentence"])
174
+ # We accept sentences where FinBERT is confident it's NOT just neutral chatter
175
+ return label != 2 and conf > 0.85
176
+
177
+ sentences_df["is_verified_claim"] = sentences_df.apply(validate_claim, axis=1)
178
+
179
+ claims_df = sentences_df[sentences_df["is_verified_claim"]].copy()
180
+ non_claims_df = sentences_df[~sentences_df["is_verified_claim"]].copy()
181
+
182
+ # Labeling for your balanced dataset
183
+ claims_df["label"] = "CLAIM"
184
+ non_claims_df["label"] = "NON_CLAIM"
185
+
186
+ # Balancing the dataset (Sample non-claims to match claims count)
187
+ n_sample = min(len(claims_df), len(non_claims_df))
188
+ balanced_non_claims = non_claims_df.sample(n=n_sample, random_state=42)
189
+
190
+ return {
191
+ "claims": claims_df.to_dict(orient="records"),
192
+ "non_claims": balanced_non_claims.to_dict(orient="records")
193
+ }