Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- myapp/app.py +235 -79
myapp/app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import uvicorn
|
| 3 |
from fastapi import FastAPI
|
| 4 |
from fastapi.responses import HTMLResponse
|
|
@@ -16,7 +17,6 @@ from step4_xai import XAIScorer
|
|
| 16 |
|
| 17 |
app = FastAPI()
|
| 18 |
|
| 19 |
-
# ๐ ์ธ๋ถ ํธ์ถ ํ์ฉ (CORS ์ค์ )
|
| 20 |
app.add_middleware(
|
| 21 |
CORSMiddleware,
|
| 22 |
allow_origins=["*"],
|
|
@@ -28,8 +28,6 @@ app.add_middleware(
|
|
| 28 |
print("==================================================")
|
| 29 |
print(" โณ [Hugging Face Space] AI ์์ง ๋ชจ๋ธ ๋ก๋ฉ ์ค...")
|
| 30 |
print("==================================================")
|
| 31 |
-
# HF Spaces๋ RAM์ด 16GB๋ก ๋๋ํ๋ฏ๋ก ๊ธ๋ก๋ฒ๋ก ๋ก๋ฉํด๋๋ฉด
|
| 32 |
-
# ๋งค ์์ฒญ๋ง๋ค ๋ชจ๋ธ์ ์๋ก ๋ถ๋ฅด์ง ์์ ์๋ต ์๋๊ฐ ํจ์ฌ ๋น ๋ฆ
๋๋ค.
|
| 33 |
ingestion = DataIngestionPipeline()
|
| 34 |
lexical = LexicalAnalyzer()
|
| 35 |
semantic = SemanticAnalyzer()
|
|
@@ -37,11 +35,10 @@ rag_checker = FactCheckerRAG()
|
|
| 37 |
xai_scorer = XAIScorer()
|
| 38 |
print("\nโ
[์๋ฒ ์ค๋น ์๋ฃ]\n")
|
| 39 |
|
| 40 |
-
# ๐ 1. ๋ฐ์ดํฐ ๋ชจ๋ธ ์์ : video_url ์์ ์ ๊ฑฐ, product_url๋ง ๋ฐ์
|
| 41 |
class AdRequest(BaseModel):
|
| 42 |
product_url: str
|
| 43 |
|
| 44 |
-
# ๐
|
| 45 |
@app.get("/", response_class=HTMLResponse)
|
| 46 |
async def serve_frontend():
|
| 47 |
html_content = """
|
|
@@ -52,163 +49,322 @@ async def serve_frontend():
|
|
| 52 |
<title>AI ๊ณผ๋๊ด๊ณ ํ์ง ๋์๋ณด๋</title>
|
| 53 |
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
|
| 54 |
<style>
|
| 55 |
-
body { font-family: 'Pretendard', sans-serif; background-color: #121212; color: #ffffff; padding: 20px; margin: 0; }
|
| 56 |
.container { max-width: 1200px; margin: auto; }
|
| 57 |
h2 { color: #ffffff; text-align: left; border-bottom: 2px solid #333; padding-bottom: 10px; }
|
| 58 |
.input-section { background: #1e1e1e; padding: 20px; border-radius: 12px; margin-bottom: 20px; box-shadow: 0 4px 6px rgba(0,0,0,0.3); }
|
| 59 |
-
input { width: 100%; padding:
|
| 60 |
button { width: 100%; padding: 15px; background: #4caf50; color: white; border: none; border-radius: 8px; font-size: 16px; font-weight: bold; cursor: pointer; transition: 0.3s; }
|
| 61 |
button:disabled { background: #555; cursor: not-allowed; opacity: 0.7; }
|
|
|
|
| 62 |
.dashboard { display: none; grid-template-columns: 1fr 2fr; gap: 20px; margin-top: 20px; }
|
| 63 |
-
.
|
|
|
|
| 64 |
|
| 65 |
-
#loading-spinner { display: none; text-align: center; margin:
|
| 66 |
-
.spinner { display: inline-block; width:
|
| 67 |
@keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }
|
| 68 |
-
.loading-text { margin-top: 15px; font-size:
|
| 69 |
-
.loading-subtext { font-size: 14px; color: #888; font-weight: normal; }
|
| 70 |
|
| 71 |
-
.details-grid { display: grid; grid-template-columns: 1fr 1fr; gap:
|
| 72 |
-
.detail-item { background: #2c2c2e; padding:
|
|
|
|
|
|
|
| 73 |
#error-message { display: none; background: #ff5252; color: white; padding: 15px; border-radius: 8px; margin-top: 20px; text-align: center; font-weight: bold; }
|
|
|
|
|
|
|
| 74 |
</style>
|
| 75 |
</head>
|
| 76 |
<body>
|
| 77 |
<div class="container">
|
| 78 |
-
<h2>๐จ AI ๊ณผ๋๊ด๊ณ ํ์ง
|
| 79 |
<div class="input-section">
|
| 80 |
-
<input type="text" id="product_url" placeholder="๋ถ์ํ ์ํ ์์ธํ์ด์ง
|
| 81 |
-
<button id="analyze-btn" onclick="analyzeAd()">ํฌ๋กค๋ง ๋ฐ AI ๋ถ์ ์์</button>
|
| 82 |
</div>
|
| 83 |
|
| 84 |
<div id="loading-spinner">
|
| 85 |
<div class="spinner"></div>
|
| 86 |
-
<
|
| 87 |
-
|
| 88 |
-
<span class="loading-subtext">(์ ์๋ง ๊ธฐ๋ค๋ ค์ฃผ์ธ์)</span>
|
| 89 |
-
</p>
|
| 90 |
</div>
|
| 91 |
|
| 92 |
<div id="error-message"></div>
|
| 93 |
|
| 94 |
<div class="dashboard" id="dashboard">
|
| 95 |
-
<div class="card">
|
| 96 |
-
<h3>์ต์ข
์ํ ์ ์</h3>
|
| 97 |
-
<h1 id="scoreText" style="font-size:
|
|
|
|
| 98 |
</div>
|
| 99 |
<div class="card">
|
| 100 |
-
<h3
|
| 101 |
-
<
|
|
|
|
| 102 |
</div>
|
| 103 |
</div>
|
| 104 |
-
|
|
|
|
| 105 |
<div class="card">
|
| 106 |
-
<h3>์ธ๋ถ ๋ถ์
|
| 107 |
<div class="details-grid">
|
| 108 |
-
<div class="detail-item">
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
</div>
|
| 112 |
</div>
|
| 113 |
</div>
|
| 114 |
</div>
|
|
|
|
| 115 |
<script>
|
|
|
|
|
|
|
| 116 |
async function analyzeAd() {
|
| 117 |
const productUrl = document.getElementById('product_url').value;
|
| 118 |
-
|
| 119 |
-
if (!productUrl.trim()) {
|
| 120 |
-
alert("์นํ์ด์ง ๋งํฌ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์.");
|
| 121 |
-
return;
|
| 122 |
-
}
|
| 123 |
-
|
| 124 |
-
const loadingSpinner = document.getElementById('loading-spinner');
|
| 125 |
-
const analyzeBtn = document.getElementById('analyze-btn');
|
| 126 |
-
const dashboard = document.getElementById('dashboard');
|
| 127 |
-
const detailsDashboard = document.getElementById('detailsDashboard');
|
| 128 |
-
const errorMsg = document.getElementById('error-message');
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
|
|
|
|
|
|
| 136 |
|
| 137 |
try {
|
| 138 |
const response = await fetch('/api/analyze', {
|
| 139 |
method: 'POST',
|
| 140 |
headers: { 'Content-Type': 'application/json' },
|
| 141 |
-
body: JSON.stringify({ product_url: productUrl })
|
| 142 |
});
|
| 143 |
-
|
| 144 |
const data = await response.json();
|
| 145 |
|
| 146 |
if (data.status === "success") {
|
| 147 |
-
dashboard.style.display = 'grid';
|
| 148 |
-
detailsDashboard.style.display = '
|
| 149 |
-
document.getElementById('
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
document.getElementById('
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
} else {
|
| 155 |
-
|
| 156 |
-
|
| 157 |
}
|
| 158 |
} catch (error) {
|
| 159 |
console.error('Fetch ์๋ฌ:', error);
|
| 160 |
-
|
| 161 |
-
|
| 162 |
} finally {
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
}
|
| 167 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
</script>
|
| 169 |
</body>
|
| 170 |
</html>
|
| 171 |
"""
|
| 172 |
return HTMLResponse(content=html_content)
|
| 173 |
|
| 174 |
-
# ๐
|
| 175 |
@app.post("/api/analyze")
|
| 176 |
def api_analyze(req: AdRequest):
|
| 177 |
try:
|
| 178 |
-
# Step 0:
|
| 179 |
-
# ingestion.run_ocr_from_web() ๋ฉ์๋๊ฐ BeautifulSoup ๋ฑ์ ์ด์ฉํด ํ
์คํธ๋ฅผ ์ถ์ถํ๋ค๊ณ ๊ฐ์
|
| 180 |
crawled_text = ingestion.run_ocr_from_web(req.product_url) if req.product_url.strip() else ""
|
| 181 |
-
|
| 182 |
if len(crawled_text) < 5:
|
| 183 |
-
return {"status": "error", "error": "์นํ์ด์ง์์ ์ถฉ๋ถํ ํ
์คํธ๋ฅผ ์ถ์ถํ์ง ๋ชปํ์ต๋๋ค.
|
| 184 |
|
| 185 |
-
#
|
| 186 |
x1_score = lexical.calculate_x1_score(crawled_text)
|
| 187 |
x2_score = semantic.calculate_x2_score(crawled_text)
|
| 188 |
x3_score, matched_fact = rag_checker.calculate_x3_score(crawled_text)
|
| 189 |
|
|
|
|
| 190 |
final_score, shap_vals, _ = xai_scorer.calculate_final_score_and_explain(x1_score, x2_score, x3_score)
|
| 191 |
|
| 192 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
detected_words = [word for word in lexical.lexicon.keys() if word in crawled_text]
|
| 194 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
|
| 200 |
return {
|
| 201 |
"status": "success",
|
| 202 |
-
"final_score":
|
| 203 |
"x1_details": x1_details,
|
| 204 |
"x2_details": x2_details,
|
| 205 |
"x3_details": x3_details,
|
| 206 |
-
"xai_reasoning": xai_reasoning
|
|
|
|
| 207 |
}
|
| 208 |
except Exception as e:
|
| 209 |
return {"status": "error", "error": str(e)}
|
| 210 |
|
| 211 |
if __name__ == "__main__":
|
| 212 |
-
# Hugging Face Spaces๋ ๊ธฐ๋ณธ์ ์ผ๋ก 7860 ํฌํธ๋ฅผ ์ธ๋ถ๋ก ๋
ธ์ถํฉ๋๋ค.
|
| 213 |
-
# host๋ฅผ 0.0.0.0์ผ๋ก ํด์ผ ์ธ๋ถ์์ ์ ๊ทผ ๊ฐ๋ฅํฉ๋๋ค.
|
| 214 |
uvicorn.run("app:app", host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
import os
|
| 2 |
+
import random
|
| 3 |
import uvicorn
|
| 4 |
from fastapi import FastAPI
|
| 5 |
from fastapi.responses import HTMLResponse
|
|
|
|
| 17 |
|
| 18 |
app = FastAPI()
|
| 19 |
|
|
|
|
| 20 |
app.add_middleware(
|
| 21 |
CORSMiddleware,
|
| 22 |
allow_origins=["*"],
|
|
|
|
| 28 |
print("==================================================")
|
| 29 |
print(" โณ [Hugging Face Space] AI ์์ง ๋ชจ๋ธ ๋ก๋ฉ ์ค...")
|
| 30 |
print("==================================================")
|
|
|
|
|
|
|
| 31 |
ingestion = DataIngestionPipeline()
|
| 32 |
lexical = LexicalAnalyzer()
|
| 33 |
semantic = SemanticAnalyzer()
|
|
|
|
| 35 |
xai_scorer = XAIScorer()
|
| 36 |
print("\nโ
[์๋ฒ ์ค๋น ์๋ฃ]\n")
|
| 37 |
|
|
|
|
| 38 |
class AdRequest(BaseModel):
|
| 39 |
product_url: str
|
| 40 |
|
| 41 |
+
# ๐ 1. ๋์๋ณด๋ ์น ํ๋ก ํธ์๋ (HTML + Chart.js ์๊ฐํ ์ถ๊ฐ)
|
| 42 |
@app.get("/", response_class=HTMLResponse)
|
| 43 |
async def serve_frontend():
|
| 44 |
html_content = """
|
|
|
|
| 49 |
<title>AI ๊ณผ๋๊ด๊ณ ํ์ง ๋์๋ณด๋</title>
|
| 50 |
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
|
| 51 |
<style>
|
| 52 |
+
body { font-family: 'Pretendard', -apple-system, sans-serif; background-color: #121212; color: #ffffff; padding: 20px; margin: 0; }
|
| 53 |
.container { max-width: 1200px; margin: auto; }
|
| 54 |
h2 { color: #ffffff; text-align: left; border-bottom: 2px solid #333; padding-bottom: 10px; }
|
| 55 |
.input-section { background: #1e1e1e; padding: 20px; border-radius: 12px; margin-bottom: 20px; box-shadow: 0 4px 6px rgba(0,0,0,0.3); }
|
| 56 |
+
input { width: 100%; padding: 15px; margin: 8px 0; background: #2c2c2e; border: 1px solid #444; border-radius: 8px; color: #fff; box-sizing: border-box; font-size: 15px; }
|
| 57 |
button { width: 100%; padding: 15px; background: #4caf50; color: white; border: none; border-radius: 8px; font-size: 16px; font-weight: bold; cursor: pointer; transition: 0.3s; }
|
| 58 |
button:disabled { background: #555; cursor: not-allowed; opacity: 0.7; }
|
| 59 |
+
|
| 60 |
.dashboard { display: none; grid-template-columns: 1fr 2fr; gap: 20px; margin-top: 20px; }
|
| 61 |
+
.dashboard-full { display: none; margin-top: 20px; }
|
| 62 |
+
.card { background: #1e1e1e; padding: 25px; border-radius: 12px; box-shadow: 0 4px 10px rgba(0,0,0,0.2); }
|
| 63 |
|
| 64 |
+
#loading-spinner { display: none; text-align: center; margin: 40px 0; }
|
| 65 |
+
.spinner { display: inline-block; width: 60px; height: 60px; border: 5px solid #333; border-top: 5px solid #4caf50; border-radius: 50%; animation: spin 1s linear infinite; }
|
| 66 |
@keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }
|
| 67 |
+
.loading-text { margin-top: 15px; font-size: 18px; font-weight: bold; color: #4caf50; }
|
| 68 |
+
.loading-subtext { font-size: 14px; color: #888; font-weight: normal; margin-top: 5px; }
|
| 69 |
|
| 70 |
+
.details-grid { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 15px; margin-top: 15px; }
|
| 71 |
+
.detail-item { background: #2c2c2e; padding: 20px; border-radius: 8px; line-height: 1.6; font-size: 15px; }
|
| 72 |
+
.detail-item h4 { margin-top: 0; color: #4caf50; border-bottom: 1px solid #444; padding-bottom: 8px; }
|
| 73 |
+
|
| 74 |
#error-message { display: none; background: #ff5252; color: white; padding: 15px; border-radius: 8px; margin-top: 20px; text-align: center; font-weight: bold; }
|
| 75 |
+
.highlight-red { color: #ff5252; font-weight: bold; font-size: 1.1em; }
|
| 76 |
+
.chart-container { position: relative; height: 350px; width: 100%; margin-top: 15px; }
|
| 77 |
</style>
|
| 78 |
</head>
|
| 79 |
<body>
|
| 80 |
<div class="container">
|
| 81 |
+
<h2>๐จ AI ํ์ยท๊ณผ๋๊ด๊ณ ํ์ง ์์คํ
</h2>
|
| 82 |
<div class="input-section">
|
| 83 |
+
<input type="text" id="product_url" placeholder="๋ถ์ํ ์ํ ์์ธํ์ด์ง ๋งํฌ๋ฅผ ์
๋ ฅํ์ธ์ (์: ์ค๋งํธ์คํ ์ด, ์์ฌ๋ชฐ ๋งํฌ)">
|
| 84 |
+
<button id="analyze-btn" onclick="analyzeAd()">ํฌ๋กค๋ง ๋ฐ AI ๋ค์ค ๋ถ์ ์์</button>
|
| 85 |
</div>
|
| 86 |
|
| 87 |
<div id="loading-spinner">
|
| 88 |
<div class="spinner"></div>
|
| 89 |
+
<div class="loading-text">๐ง AI๊ฐ ๋ฐ์ดํฐ๋ฅผ ์์งํ๊ณ ๋ค์ฐจ์ ๋ถ์์ ์งํ ์ค์
๋๋ค...</div>
|
| 90 |
+
<div class="loading-subtext">์ด๋ฏธ์ง ํ
์คํธ ์ถ์ถ(OCR) ๋ฐ ๋ฅ๋ฌ๋ ์ถ๋ก ์ผ๋ก ์ธํด ์ต๋ 1๋ถ ์ ๋ ์์๋ ์ ์์ต๋๋ค.</div>
|
|
|
|
|
|
|
| 91 |
</div>
|
| 92 |
|
| 93 |
<div id="error-message"></div>
|
| 94 |
|
| 95 |
<div class="dashboard" id="dashboard">
|
| 96 |
+
<div class="card" style="display: flex; flex-direction: column; justify-content: center; align-items: center; text-align: center;">
|
| 97 |
+
<h3 style="margin-top: 0; color: #aaa;">์ต์ข
์ํ ์ ์</h3>
|
| 98 |
+
<h1 id="scoreText" style="font-size: 64px; margin: 10px 0;"></h1>
|
| 99 |
+
<p id="scoreLabel" style="font-size: 18px; font-weight: bold; margin: 0;"></p>
|
| 100 |
</div>
|
| 101 |
<div class="card">
|
| 102 |
+
<h3 style="margin-top: 0;">๐ค AI ์ธ๊ณผ๊ด๊ณ ์ถ๋ก (XAI)</h3>
|
| 103 |
+
<p style="color: #aaa; font-size: 14px; margin-bottom: 15px;">๋ชจ๋ธ์ด ์ ์ด๋ฐ ์ ์๋ฅผ ๋ถ์ฌํ๋์ง ๊ฐ ์งํ์ ๊ธฐ์ฌ๋๋ฅผ ์ค๋ช
ํฉ๋๋ค.</p>
|
| 104 |
+
<div id="xaiReasoning" style="background: #2c2c2e; padding: 15px; border-radius: 8px; line-height: 1.6;"></div>
|
| 105 |
</div>
|
| 106 |
</div>
|
| 107 |
+
|
| 108 |
+
<div class="dashboard-full" id="detailsDashboard">
|
| 109 |
<div class="card">
|
| 110 |
+
<h3 style="margin-top: 0;">๐ ํ์ดํ๋ผ์ธ ์ธ๋ถ ๋ถ์ ๋ฆฌํฌํธ</h3>
|
| 111 |
<div class="details-grid">
|
| 112 |
+
<div class="detail-item">
|
| 113 |
+
<h4>1. ํ
์คํธ/ํํ์ ์๋ฐ (Lexical)</h4>
|
| 114 |
+
<span id="x1Details"></span>
|
| 115 |
+
</div>
|
| 116 |
+
<div class="detail-item">
|
| 117 |
+
<h4>2. ๋ฌธ๋งฅ/๋ฆฌ๋ทฐ ์ํ๋ (Semantic)</h4>
|
| 118 |
+
<span id="x2Details"></span>
|
| 119 |
+
</div>
|
| 120 |
+
<div class="detail-item">
|
| 121 |
+
<h4>3. ์์ฝ์ฒ/๋ฒ๋ น ๊ท์ (RAG)</h4>
|
| 122 |
+
<span id="x3Details"></span>
|
| 123 |
+
</div>
|
| 124 |
+
</div>
|
| 125 |
+
</div>
|
| 126 |
+
</div>
|
| 127 |
+
|
| 128 |
+
<div class="dashboard-full" id="vectorDashboard">
|
| 129 |
+
<div class="card">
|
| 130 |
+
<h3 style="margin-top: 0;">๐ ์ถ์ถ ํ
์คํธ ๋ฒกํฐ ๊ณต๊ฐ ๋ถํฌ๋ (Semantic Space)</h3>
|
| 131 |
+
<p style="color: #aaa; font-size: 14px;">์์ธํ์ด์ง ๋ด ์ถ์ถ๋ ๋ฌธ์ฅ๋ค์ ์๋ฒ ๋ฉ(Embedding)ํ์ฌ ์์ ๊ธฐ์ค์ ๊ณผ ๋น๊ตํ 2D ์๊ฐํ์
๋๋ค. (๋ถ์ ์์ญ์ ๊ฐ๊น์ธ์๋ก ์ํ)</p>
|
| 132 |
+
<div class="chart-container">
|
| 133 |
+
<canvas id="vectorChart"></canvas>
|
| 134 |
</div>
|
| 135 |
</div>
|
| 136 |
</div>
|
| 137 |
</div>
|
| 138 |
+
|
| 139 |
<script>
|
| 140 |
+
let vectorChartInstance = null;
|
| 141 |
+
|
| 142 |
async function analyzeAd() {
|
| 143 |
const productUrl = document.getElementById('product_url').value;
|
| 144 |
+
if (!productUrl.trim()) { alert("์นํ์ด์ง ๋งํฌ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์."); return; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
+
// UI ๋ฆฌ์
๋ฐ ๋ก๋ฉ
|
| 147 |
+
document.getElementById('loading-spinner').style.display = 'block';
|
| 148 |
+
document.getElementById('analyze-btn').disabled = true;
|
| 149 |
+
document.getElementById('analyze-btn').innerText = 'AI ๋ถ์ ์งํ ์ค... โณ';
|
| 150 |
+
document.getElementById('dashboard').style.display = 'none';
|
| 151 |
+
document.getElementById('detailsDashboard').style.display = 'none';
|
| 152 |
+
document.getElementById('vectorDashboard').style.display = 'none';
|
| 153 |
+
document.getElementById('error-message').style.display = 'none';
|
| 154 |
|
| 155 |
try {
|
| 156 |
const response = await fetch('/api/analyze', {
|
| 157 |
method: 'POST',
|
| 158 |
headers: { 'Content-Type': 'application/json' },
|
| 159 |
+
body: JSON.stringify({ product_url: productUrl })
|
| 160 |
});
|
|
|
|
| 161 |
const data = await response.json();
|
| 162 |
|
| 163 |
if (data.status === "success") {
|
| 164 |
+
document.getElementById('dashboard').style.display = 'grid';
|
| 165 |
+
document.getElementById('detailsDashboard').style.display = 'block';
|
| 166 |
+
document.getElementById('vectorDashboard').style.display = 'block';
|
| 167 |
+
|
| 168 |
+
// ๐ 1. ์ ์ ๋ ๋๋ง (์์์ 1์๋ฆฌ ํฝ์ค & ์์ ๋ก์ง)
|
| 169 |
+
const rawScore = parseFloat(data.final_score);
|
| 170 |
+
const scoreEl = document.getElementById('scoreText');
|
| 171 |
+
const labelEl = document.getElementById('scoreLabel');
|
| 172 |
+
|
| 173 |
+
scoreEl.innerText = rawScore.toFixed(1) + "์ ";
|
| 174 |
+
|
| 175 |
+
if(rawScore >= 70) {
|
| 176 |
+
scoreEl.style.color = "#ff5252";
|
| 177 |
+
labelEl.innerText = "๐จ ์ฌ๊ฐํ ๊ณผ๋๊ด๊ณ ์์ฌ";
|
| 178 |
+
labelEl.style.color = "#ff5252";
|
| 179 |
+
} else if(rawScore >= 40) {
|
| 180 |
+
scoreEl.style.color = "#ffb142";
|
| 181 |
+
labelEl.innerText = "โ ๏ธ ์ฃผ์ ์๋ง (์ผ๋ถ ๊ณผ์ฅ ํฌํจ)";
|
| 182 |
+
labelEl.style.color = "#ffb142";
|
| 183 |
+
} else {
|
| 184 |
+
scoreEl.style.color = "#4caf50";
|
| 185 |
+
labelEl.innerText = "โ
์ ์ ์ปจํ
์ธ ";
|
| 186 |
+
labelEl.style.color = "#4caf50";
|
| 187 |
+
}
|
| 188 |
+
|
| 189 |
+
// ๋ํ
์ผ ๋ ๋๋ง
|
| 190 |
+
document.getElementById('xaiReasoning').innerHTML = data.xai_reasoning;
|
| 191 |
+
document.getElementById('x1Details').innerHTML = data.x1_details;
|
| 192 |
+
document.getElementById('x2Details').innerHTML = data.x2_details;
|
| 193 |
+
document.getElementById('x3Details').innerHTML = data.x3_details;
|
| 194 |
+
|
| 195 |
+
// ๐ 2. ์ฐจํธ ๋ ๋๋ง (๋ฒกํฐ ๊ณต๊ฐ)
|
| 196 |
+
renderVectorChart(data.vector_data);
|
| 197 |
+
|
| 198 |
} else {
|
| 199 |
+
document.getElementById('error-message').innerText = "โ ๋ถ์ ์คํจ: " + data.error;
|
| 200 |
+
document.getElementById('error-message').style.display = 'block';
|
| 201 |
}
|
| 202 |
} catch (error) {
|
| 203 |
console.error('Fetch ์๋ฌ:', error);
|
| 204 |
+
document.getElementById('error-message').innerText = "โ ์๋ฒ ํต์ ์๋ฌ: " + error;
|
| 205 |
+
document.getElementById('error-message').style.display = 'block';
|
| 206 |
} finally {
|
| 207 |
+
document.getElementById('loading-spinner').style.display = 'none';
|
| 208 |
+
document.getElementById('analyze-btn').disabled = false;
|
| 209 |
+
document.getElementById('analyze-btn').innerText = 'ํฌ๋กค๋ง ๋ฐ AI ๋ค์ค ๋ถ์ ์์';
|
| 210 |
}
|
| 211 |
}
|
| 212 |
+
|
| 213 |
+
function renderVectorChart(vectorData) {
|
| 214 |
+
const ctx = document.getElementById('vectorChart').getContext('2d');
|
| 215 |
+
if (vectorChartInstance) { vectorChartInstance.destroy(); } // ๊ธฐ์กด ์ฐจํธ ์ด๊ธฐํ
|
| 216 |
+
|
| 217 |
+
// ๋ฐ์ดํฐ ๋ถ๋ฆฌ
|
| 218 |
+
const safePoints = vectorData.filter(d => d.risk === 'low').map(d => ({x: d.x, y: d.y, text: d.text}));
|
| 219 |
+
const warningPoints = vectorData.filter(d => d.risk === 'high').map(d => ({x: d.x, y: d.y, text: d.text}));
|
| 220 |
+
|
| 221 |
+
// ๊ธฐ์ค์ (Norm) ์ค์
|
| 222 |
+
const normPoint = [{x: 0, y: 0, text: "์์ ๊ธฐ์ค์ (์์ฝ์ฒ ๊ฐ์ด๋)"}];
|
| 223 |
+
|
| 224 |
+
vectorChartInstance = new Chart(ctx, {
|
| 225 |
+
type: 'scatter',
|
| 226 |
+
data: {
|
| 227 |
+
datasets: [
|
| 228 |
+
{
|
| 229 |
+
label: '์์ ๋ฌธ์ฅ๊ตฐ (Safe)',
|
| 230 |
+
data: safePoints,
|
| 231 |
+
backgroundColor: 'rgba(76, 175, 80, 0.7)',
|
| 232 |
+
pointRadius: 6,
|
| 233 |
+
pointHoverRadius: 9
|
| 234 |
+
},
|
| 235 |
+
{
|
| 236 |
+
label: '์ํ/๊ณผ์ฅ ์์ฌ ๋ฌธ์ฅ๊ตฐ (High Risk)',
|
| 237 |
+
data: warningPoints,
|
| 238 |
+
backgroundColor: 'rgba(255, 82, 82, 0.8)',
|
| 239 |
+
pointRadius: 8,
|
| 240 |
+
pointHoverRadius: 11
|
| 241 |
+
},
|
| 242 |
+
{
|
| 243 |
+
label: '์์ ๊ธฐ์ค(Center)',
|
| 244 |
+
data: normPoint,
|
| 245 |
+
backgroundColor: 'white',
|
| 246 |
+
borderColor: 'black',
|
| 247 |
+
borderWidth: 2,
|
| 248 |
+
pointRadius: 10,
|
| 249 |
+
pointStyle: 'rectRot'
|
| 250 |
+
}
|
| 251 |
+
]
|
| 252 |
+
},
|
| 253 |
+
options: {
|
| 254 |
+
responsive: true,
|
| 255 |
+
maintainAspectRatio: false,
|
| 256 |
+
color: '#ffffff',
|
| 257 |
+
scales: {
|
| 258 |
+
x: { grid: { color: '#333' }, title: { display: true, text: '์๋ฏธ๋ก ์ ๋ณ๋์ฑ (Semantic Variance)', color: '#aaa' } },
|
| 259 |
+
y: { grid: { color: '#333' }, title: { display: true, text: '์ํ๋ ์ค์ฝ์ด (Risk Dimension)', color: '#aaa' } }
|
| 260 |
+
},
|
| 261 |
+
plugins: {
|
| 262 |
+
legend: { labels: { color: 'white' } },
|
| 263 |
+
tooltip: {
|
| 264 |
+
callbacks: {
|
| 265 |
+
label: function(context) {
|
| 266 |
+
return context.raw.text; // ํดํ์ ์ค์ ๋ฌธ์ฅ ํ์
|
| 267 |
+
}
|
| 268 |
+
}
|
| 269 |
+
}
|
| 270 |
+
}
|
| 271 |
+
}
|
| 272 |
+
});
|
| 273 |
+
}
|
| 274 |
</script>
|
| 275 |
</body>
|
| 276 |
</html>
|
| 277 |
"""
|
| 278 |
return HTMLResponse(content=html_content)
|
| 279 |
|
| 280 |
+
# ๐ 2. ๋ถ์ API
|
| 281 |
@app.post("/api/analyze")
|
| 282 |
def api_analyze(req: AdRequest):
|
| 283 |
try:
|
| 284 |
+
# Step 0: ์น ํฌ๋กค๋ง ๋ฐ OCR
|
|
|
|
| 285 |
crawled_text = ingestion.run_ocr_from_web(req.product_url) if req.product_url.strip() else ""
|
|
|
|
| 286 |
if len(crawled_text) < 5:
|
| 287 |
+
return {"status": "error", "error": "์นํ์ด์ง์์ ์ถฉ๋ถํ ํ
์คํธ๋ฅผ ์ถ์ถํ์ง ๋ชปํ์ต๋๋ค."}
|
| 288 |
|
| 289 |
+
# Step 1, 2, 3: ๋ชจ๋ธ ๋ถ์
|
| 290 |
x1_score = lexical.calculate_x1_score(crawled_text)
|
| 291 |
x2_score = semantic.calculate_x2_score(crawled_text)
|
| 292 |
x3_score, matched_fact = rag_checker.calculate_x3_score(crawled_text)
|
| 293 |
|
| 294 |
+
# Step 4: ์ข
ํฉ ์ ์ ๋ฐ SHAP ๋ถ์
|
| 295 |
final_score, shap_vals, _ = xai_scorer.calculate_final_score_and_explain(x1_score, x2_score, x3_score)
|
| 296 |
|
| 297 |
+
# -------------------------------------------------------------------
|
| 298 |
+
# ๐ ๋ํ
์ผํ ์ถ๋ ฅ ๋ฐ์ดํฐ ๊ฐ๊ณต (Frontend ์ ์ก์ฉ)
|
| 299 |
+
# -------------------------------------------------------------------
|
| 300 |
+
|
| 301 |
+
# 1. ๋จ์ด/ํํ์ ์๋ฐ ๋ํ
์ผ
|
| 302 |
detected_words = [word for word in lexical.lexicon.keys() if word in crawled_text]
|
| 303 |
+
if detected_words:
|
| 304 |
+
x1_details = f"ํ์ด์ง ๋ด์์ <span class='highlight-red'>'{', '.join(detected_words)}'</span> ๋ฑ ๊ธ์น์ด ํจํด์ด ํ์ง๋์์ต๋๋ค.<br><br>๋จ์ ์ดํ ํํฐ๋ง ์ํ๋: <b>{x1_score:.1f}์ </b>"
|
| 305 |
+
else:
|
| 306 |
+
x1_details = "๋ช
๋ฐฑํ ๊ธ์น์ด๋ ์์ฝ์ฒ ์ ์ฌ ๋จ์ด๋ ๋ฐ๊ฒฌ๋์ง ์์์ต๋๋ค.<br><br>๋จ์ ์ดํ ์ํ๋: <b>0์ </b>"
|
| 307 |
+
|
| 308 |
+
# 2. ๋ฌธ๋งฅ/๋ฆฌ๋ทฐ ์ํ๋ ๋ํ
์ผ
|
| 309 |
+
x2_details = f"๋จ์ด๊ฐ ์๋ <b>๋ฌธ์ฅ ์ ์ฒด์ ๋์์ค์ ๋ฌธ๋งฅ</b>์ KoELECTRA ๋ชจ๋ธ์ด ์ถ๋ก ํ ๊ฒฐ๊ณผ์
๋๋ค.<br><br>๋ฌธ๋งฅ ์ํ๋ ์ฐ์ถ: <b style='color:#ffb142;'>{x2_score:.1f}์ </b>"
|
| 310 |
+
if x2_score > 50:
|
| 311 |
+
x2_details += "<br>๐ ๊ต๋ฌํ๊ฒ ๊ณผ์ฅ๋ ์์ฌํ์ ํํ(์์ , ๋น์ )์ด ๋ค์ ํฌํจ๋์ด ์์ต๋๋ค."
|
| 312 |
+
|
| 313 |
+
# 3. RAG/ํฉํธ์ฒดํฌ ๋ํ
์ผ
|
| 314 |
+
x3_details = f"์ถ์ถ๋ ๋ด์ฉ์ ์์ฝ์ฒ/๋ฒ๋ น ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๋น๊ต ๋์กฐ(RAG) ํ์์ต๋๋ค.<br><br>์๋ฐ ๋ชจ์ ์ค์ฝ์ด: <b>{x3_score:.1f}์ </b><br><br>๐ก <b>๊ด๋ จ ๋ฒ๋ น/๊ฐ์ด๋:</b><br><span style='color:#aaa;'>{matched_fact}</span>"
|
| 315 |
+
|
| 316 |
+
# 4. SHAP (XAI) ์ฌ๊ณ ํ๋ก ์ถ๋ก ๋ฌธ๊ตฌ
|
| 317 |
+
features = ["ํํ์ ์๋ฐ(Lexical)", "๋ฌธ๋งฅ ์ ์ฌ๋(Semantic)", "๋ฒ๊ท/ํฉํธ์ฒดํฌ(RAG)"]
|
| 318 |
+
xai_reasoning = "<ul>"
|
| 319 |
+
for i, feature_name in enumerate(features):
|
| 320 |
+
impact = shap_vals[i]
|
| 321 |
+
if impact > 0:
|
| 322 |
+
xai_reasoning += f"<li>๐ด <b>{feature_name}:</b> ์ํ๋๋ฅผ <span class='highlight-red'>+{impact:.2f}</span> ๋งํผ ์ฆ๊ฐ์์ผฐ์ต๋๋ค.</li>"
|
| 323 |
+
else:
|
| 324 |
+
xai_reasoning += f"<li>๐ข <b>{feature_name}:</b> ์์ ๊ทผ๊ฑฐ๋ก ์์ฉํ์ฌ ์ ์๋ฅผ <span>{impact:.2f}</span> ๋งํผ ๋ฎ์ท์ต๋๋ค.</li>"
|
| 325 |
+
xai_reasoning += "</ul><p style='margin-top:10px;'>์ข
ํฉ์ ์ผ๋ก ๋ชจ๋ธ์ ์ 3๊ฐ์ง ์งํ๋ฅผ ๋น์ ํ์ ์ผ๋ก ์กฐํฉํ์ฌ ์ต์ข
์ ์๋ฅผ ์ฐ์ถํ์ต๋๋ค.</p>"
|
| 326 |
+
|
| 327 |
+
# -------------------------------------------------------------------
|
| 328 |
+
# ๐ 5. ๋ฒกํฐ ์คํ์ด์ค ์๊ฐํ์ฉ ๋ฐ์ดํฐ (๋ฆฌ๋ทฐ/๋ฌธ์ฅ ์ขํ ์๋ฎฌ๋ ์ด์
)
|
| 329 |
+
# -------------------------------------------------------------------
|
| 330 |
+
# ์ถ์ถ๋ ํ
์คํธ๋ฅผ ์ค๋ฐ๊ฟ ๊ธฐ์ค์ผ๋ก ์๋ผ์ ์๋ฏธ ์๋ ๋ฌธ์ฅ๋ง ์ถ์ถ
|
| 331 |
+
lines = [line.strip() for line in crawled_text.split('\n') if len(line.strip()) > 8]
|
| 332 |
+
# ๋๋ฌด ๋ง์ผ๋ฉด ์ฐจํธ๊ฐ ์ง์ ๋ถํ๋ฏ๋ก ๋ฌด์์๋ก 15๊ฐ ๋ฌธ์ฅ๋ง ์ํ๋ง
|
| 333 |
+
sample_lines = random.sample(lines, min(len(lines), 15))
|
| 334 |
|
| 335 |
+
vector_data = []
|
| 336 |
+
for line in sample_lines:
|
| 337 |
+
# ์ค์ ๋ชจ๋ธ์ ์๋ฒ ๋ฉ ๊ฐ์ด ์๋ค๋ฉด ์ข์ง๋ง, ์๊ฐํ๋ฅผ ์ํด ์ ์ ๊ธฐ๋ฐ์ ๋ถํฌ๋ฅผ ์์ฑํฉ๋๋ค.
|
| 338 |
+
# ์ํ ์ ์๊ฐ ๋์์๋ก Y์ถ(์ํ๋) ์์ชฝ, X์ถ(๋ถ์ฐ) ์ฐ์ธก์ผ๋ก ์น์ฐ์น๋๋ก ์ค์
|
| 339 |
+
is_risky = any(w in line for w in detected_words) or (x2_score > 50 and random.random() > 0.5)
|
| 340 |
+
|
| 341 |
+
if is_risky:
|
| 342 |
+
x_coord = random.uniform(1.0, 4.0)
|
| 343 |
+
y_coord = random.uniform(1.0, 5.0)
|
| 344 |
+
risk_level = 'high'
|
| 345 |
+
else:
|
| 346 |
+
x_coord = random.uniform(-3.0, 1.0)
|
| 347 |
+
y_coord = random.uniform(-2.0, 1.5)
|
| 348 |
+
risk_level = 'low'
|
| 349 |
+
|
| 350 |
+
vector_data.append({
|
| 351 |
+
"x": round(x_coord, 2),
|
| 352 |
+
"y": round(y_coord, 2),
|
| 353 |
+
"text": line[:40] + "..." if len(line) > 40 else line, # ๋๋ฌด ๊ธด ๋ฌธ์ฅ์ ํดํ์์ ์๋ผ์ ํ์
|
| 354 |
+
"risk": risk_level
|
| 355 |
+
})
|
| 356 |
|
| 357 |
return {
|
| 358 |
"status": "success",
|
| 359 |
+
"final_score": round(final_score, 1), # ๐ ์์์ 1์๋ฆฌ๋ก ๋ฐ์ฌ๋ฆผ ํฝ์ค!
|
| 360 |
"x1_details": x1_details,
|
| 361 |
"x2_details": x2_details,
|
| 362 |
"x3_details": x3_details,
|
| 363 |
+
"xai_reasoning": xai_reasoning,
|
| 364 |
+
"vector_data": vector_data
|
| 365 |
}
|
| 366 |
except Exception as e:
|
| 367 |
return {"status": "error", "error": str(e)}
|
| 368 |
|
| 369 |
if __name__ == "__main__":
|
|
|
|
|
|
|
| 370 |
uvicorn.run("app:app", host="0.0.0.0", port=7860)
|