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 # ========================================== # ๐ AI ๋ ๊ณ ๋ธ๋ก ๋ถ๋ฌ์ค๊ธฐ # ========================================== 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] AI ์์ง ๋ชจ๋ธ ๋ก๋ฉ ์ค...") print("==================================================") ingestion = DataIngestionPipeline() lexical = LexicalAnalyzer() semantic = SemanticAnalyzer() rag_checker = FactCheckerRAG() xai_scorer = XAIScorer() print("\nโ [์๋ฒ ์ค๋น ์๋ฃ]\n") class AdRequest(BaseModel): product_url: str # ๐ 1. ๋์๋ณด๋ ์น ํ๋ก ํธ์๋ (HTML + Chart.js ์๊ฐํ ์ถ๊ฐ) @app.get("/", response_class=HTMLResponse) async def serve_frontend(): html_content = """
๋ชจ๋ธ์ด ์ ์ด๋ฐ ์ ์๋ฅผ ๋ถ์ฌํ๋์ง ๊ฐ ์งํ์ ๊ธฐ์ฌ๋๋ฅผ ์ค๋ช ํฉ๋๋ค.
์์ธํ์ด์ง ๋ด ์ถ์ถ๋ ๋ฌธ์ฅ๋ค์ ์๋ฒ ๋ฉ(Embedding)ํ์ฌ ์์ ๊ธฐ์ค์ ๊ณผ ๋น๊ตํ 2D ์๊ฐํ์ ๋๋ค. (๋ถ์ ์์ญ์ ๊ฐ๊น์ธ์๋ก ์ํ)
์ข ํฉ์ ์ผ๋ก ๋ชจ๋ธ์ ์ 3๊ฐ์ง ์งํ๋ฅผ ๋น์ ํ์ ์ผ๋ก ์กฐํฉํ์ฌ ์ต์ข ์ ์๋ฅผ ์ฐ์ถํ์ต๋๋ค.
" # ------------------------------------------------------------------- # ๐ 5. ๋ฒกํฐ ์คํ์ด์ค ์๊ฐํ์ฉ ๋ฐ์ดํฐ (๋ฆฌ๋ทฐ/๋ฌธ์ฅ ์ขํ ์๋ฎฌ๋ ์ด์ ) # ------------------------------------------------------------------- # ์ถ์ถ๋ ํ ์คํธ๋ฅผ ์ค๋ฐ๊ฟ ๊ธฐ์ค์ผ๋ก ์๋ผ์ ์๋ฏธ ์๋ ๋ฌธ์ฅ๋ง ์ถ์ถ lines = [line.strip() for line in crawled_text.split('\n') if len(line.strip()) > 8] # ๋๋ฌด ๋ง์ผ๋ฉด ์ฐจํธ๊ฐ ์ง์ ๋ถํ๋ฏ๋ก ๋ฌด์์๋ก 15๊ฐ ๋ฌธ์ฅ๋ง ์ํ๋ง sample_lines = random.sample(lines, min(len(lines), 15)) vector_data = [] for line in sample_lines: # ์ค์ ๋ชจ๋ธ์ ์๋ฒ ๋ฉ ๊ฐ์ด ์๋ค๋ฉด ์ข์ง๋ง, ์๊ฐํ๋ฅผ ์ํด ์ ์ ๊ธฐ๋ฐ์ ๋ถํฌ๋ฅผ ์์ฑํฉ๋๋ค. # ์ํ ์ ์๊ฐ ๋์์๋ก Y์ถ(์ํ๋) ์์ชฝ, X์ถ(๋ถ์ฐ) ์ฐ์ธก์ผ๋ก ์น์ฐ์น๋๋ก ์ค์ is_risky = any(w in line for w in detected_words) or (x2_score > 50 and random.random() > 0.5) if is_risky: x_coord = random.uniform(1.0, 4.0) y_coord = random.uniform(1.0, 5.0) risk_level = 'high' else: x_coord = random.uniform(-3.0, 1.0) y_coord = random.uniform(-2.0, 1.5) risk_level = 'low' vector_data.append({ "x": round(x_coord, 2), "y": round(y_coord, 2), "text": line[:40] + "..." if len(line) > 40 else line, # ๋๋ฌด ๊ธด ๋ฌธ์ฅ์ ํดํ์์ ์๋ผ์ ํ์ "risk": risk_level }) return { "status": "success", "final_score": float(round(final_score, 1)), # ๐ float()๋ก ๊ฐ์ธ์ฃผ๊ธฐ! "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)