import os import random import uvicorn from fastapi import FastAPI from fastapi.responses import HTMLResponse from fastapi.middleware.cors import CORSMiddleware from pydantic import BaseModel # ========================================== # Load AI pipeline components # ========================================== from step0_ingestion import DataIngestionPipeline from step1_lexical import LexicalAnalyzer from step2_semantic import SemanticAnalyzer from step3_rag import FactCheckerRAG from step4_xai import XAIScorer app = FastAPI() app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) print("==================================================") print(" ā³ [Hugging Face Space] Loading AI engine models...") print("==================================================") ingestion = DataIngestionPipeline() lexical = LexicalAnalyzer() semantic = SemanticAnalyzer() rag_checker = FactCheckerRAG() xai_scorer = XAIScorer() print("\nā [Server Ready]\n") class AdRequest(BaseModel): product_url: str # 1. Dashboard frontend (HTML + Chart.js visualization) @app.get("/", response_class=HTMLResponse) async def serve_frontend(): html_content = """
How individual signals contributed to the hidden billing risk assessment.
Extracted sentences mapped against deceptive intent patterns. Red zone indicates high-pressure or obscured billing language.
Combined assessment of deceptive UX and legal non-compliance.
" # 5. Vector-space visualization (Simulated for English context) lines = [line.strip() for line in crawled_text.split('\n') if len(line.strip()) > 10] sample_lines = random.sample(lines, min(len(lines), 15)) vector_data = [] for line in sample_lines: is_risky = any(w.lower() in line.lower() for w in detected_words) or (x2_score > 50 and random.random() > 0.5) if is_risky: x_coord = random.uniform(1.0, 5.0) y_coord = random.uniform(1.0, 5.0) risk_level = 'high' else: x_coord = random.uniform(-5.0, 1.0) y_coord = random.uniform(-3.0, 1.5) risk_level = 'low' vector_data.append({ "x": round(x_coord, 2), "y": round(y_coord, 2), "text": line[:50] + "..." if len(line) > 50 else line, "risk": risk_level }) return { "status": "success", "final_score": float(round(final_score, 1)), "x1_details": x1_details, "x2_details": x2_details, "x3_details": x3_details, "xai_reasoning": xai_reasoning, "vector_data": vector_data } except Exception as e: return {"status": "error", "error": str(e)} if __name__ == "__main__": uvicorn.run("app:app", host="0.0.0.0", port=7860)