| import sys |
| import subprocess |
| import pandas as pd |
|
|
| |
| for package in ["transformers", "torch", "shap", "pandas"]: |
| try: |
| __import__(package) |
| except ModuleNotFoundError: |
| subprocess.check_call([sys.executable, "-m", "pip", "install", package]) |
|
|
| from transformers import pipeline |
| import shap |
|
|
| |
| |
| |
| print("๐ FDS ๋น์ ํ ๋งฅ๋ฝ ๋ถ์์ ์ํ LLM ํ์ดํ๋ผ์ธ์ ์ด๊ธฐํ ์ค์
๋๋ค...") |
| classifier = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english", top_k=None) |
|
|
| |
| |
| |
| def fds_llm_predict(texts): |
| """ |
| SHAP์ด ๋ด๋ถ์ ์ผ๋ก ํ
์คํธ๋ฅผ ๋ง์คํนํ์ฌ numpy.ndarray ํํ๋ก ์ ์กํ๋ฏ๋ก, |
| Hugging Face ํ์ดํ๋ผ์ธ์ด ์ธ์ํ ์ ์๋๋ก ๊ฐ์ ๋ก ํ์ด์ฌ list ํ์
์ผ๋ก ์บ์คํ
ํฉ๋๋ค. |
| """ |
| |
| if not isinstance(texts, list): |
| texts = list(texts) |
| |
| results = classifier(texts) |
| fraud_probabilities = [] |
| for res in results: |
| |
| risk_score = next(item['score'] for item in res if item['label'] == 'NEGATIVE') |
| fraud_probabilities.append(risk_score) |
| return fraud_probabilities |
|
|
| |
| explainer = shap.Explainer(fds_llm_predict, shap.maskers.Text(tokenizer=r"\W+")) |
|
|
| |
| |
| |
| suspicious_transaction_context = ( |
| "The elderly customer requested an urgent transfer of $45,000 to an unknown account. " |
| "She appears extremely nervous, continually checking her smartphone, and mentioned that " |
| "a stranger instructed her via an unverified remote control app to complete this transaction immediately." |
| ) |
|
|
| print("\n" + "="*60) |
| print("๐ฅ [์์ง๋ ๋น์ ํ ๋ฐ์ดํฐ ๋ถ์ ๋์]") |
| print(suspicious_transaction_context) |
| print("="*60) |
|
|
| |
| |
| |
| print("\n๐ค LLM ๋ถ์ ๋ฐ SHAP ๊ฐ์น ๊ณ์ฐ ๊ฐ๋ ์ค...") |
|
|
| |
| final_risk_score = fds_llm_predict([suspicious_transaction_context])[0] |
|
|
| |
| shap_values = explainer([suspicious_transaction_context]) |
|
|
| |
| |
| |
| words = shap_values.data[0] |
| contributions = shap_values.values[0] |
|
|
| fds_report = pd.DataFrame({ |
| 'Detected_Word': words, |
| 'Risk_Contribution': contributions |
| }) |
|
|
| fds_report = fds_report[fds_report['Detected_Word'].str.strip() != ""] |
| fds_report_sorted = fds_report.sort_values(by='Risk_Contribution', ascending=False) |
|
|
| |
| |
| |
| print("\n๐จ [FDS 5์ธ๋ ๊ด์ ์์คํ
์๋ฆผ]") |
| if final_risk_score > 0.85: |
| print(f"โถ ์ต์ข
์กฐ์น: โ [์ฆ์ ์ฐจ๋จ] ๊ธ์ต์ฌ๊ธฐ ์์ฌ ๋ฌธ๋งฅ ํฌ์ฐฉ") |
| elif final_risk_score > 0.50: |
| print(f"โถ ์ต์ข
์กฐ์น: โ ๏ธ [์ถ๊ฐ ์ธ์ฆ] ์์ฌ ์งํ ํ์ง") |
| else: |
| print(f"โถ ์ต์ข
์กฐ์น: โ
[์ ์ ์น์ธ]") |
|
|
| print(f"โถ ์ข
ํฉ ๋ฆฌ์คํฌ ์ค์ฝ์ด: {round(final_risk_score * 100, 2)}%\n") |
|
|
| print("๐ก [XAI ์๋ช
๊ฐ์ด๋ - ์ํ ๊ธฐ์ฌ๋ Top 5 ๋จ์ด]") |
| print("-" * 50) |
| print(fds_report_sorted.head(5).to_string(index=False)) |
| print("-" * 50) |