Spaces:
Sleeping
Sleeping
Upload app_new.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844import streamlit as stimport pandas as pdimport numpy as npimport joblibimport jsonimport timefrom datetime import datetime
# ==========================================# 1. ุฅุนุฏุงุฏ ุงูุตูุญุฉ ูุงููุบุฉ (UI Layer ููุท)# ==========================================st.set_page_config( page_title="FraudGuard AI | ูุธุงู
ูุดู ุงูุงุญุชูุงู ุงูู
ุชูุฏู
", page_icon="๐ก๏ธ", layout="wide", initial_sidebar_state="expanded",)
st.session_state.setdefault("language", "ar")LANG = st.session_state["language"]IS_AR = LANG == "ar"DIRECTION = "rtl" if IS_AR else "ltr"
TRANSLATIONS = { "ar": { "language": "ุงููุบุฉ / Language", "arabic": "ุงูุนุฑุจูุฉ", "english": "English", "brand": "FraudGuard AI", "tagline": "ูุธุงู
ูุดู ุงูุงุญุชูุงู ุงูุฐูู", "eyebrow": "ู
ูุตุฉ ุงุณุชุฎุจุงุฑุงุช ู
ุฎุงุทุฑ ุงูู
ุนุงู
ูุงุช", "hero_title": "ุญู
ุงูุฉ ู
ุงููุฉ ุจูุฑุงุฑ ุฃุฐูู", "hero_subtitle": "ุญููู ุงูู
ุนุงู
ูุฉ ูู ุงูููุช ุงูุญููููุ ูุงูุดู ุฅุดุงุฑุงุช ุงูุงุญุชูุงู ูุจู ุฃู ุชุชุญูู ุฅูู ุฎุณุงุฑุฉ.", "system_status": "ุงููุธุงู
ู
ุชุตู", "model_ready": "ุงููู
ูุฐุฌ ุฌุงูุฒ ููุชุญููู", "optimal_threshold": "ุงูุนุชุจุฉ ุงูู
ุซูู", "threshold_caption": "ู
ุณุชูู ุงููุฑุงุฑ ุงูุฃู
ุซู", "transaction_details": "ุชูุงุตูู ุงูู
ุนุงู
ูุฉ", "financial_data": "ุงูุจูุงูุงุช ุงูู
ุงููุฉ", "amount": "ู
ุจูุบ ุงูู
ุนุงู
ูุฉ ($)", "transaction_time": "ููุช ุงูู
ุนุงู
ูุฉ", "account_identity": "ูููุฉ ุงูุนู
ูู", "email_domain": "ูุทุงู ุงูุจุฑูุฏ ุงูุฅููุชุฑููู", "account_age": "ุนู
ุฑ ุงูุญุณุงุจ (ุฃูุงู
)", "account_age_help": "ุนุฏุฏ ุงูุฃูุงู
ู
ูุฐ ุฅูุดุงุก ุงูุญุณุงุจ", "location_device": "ุงูู
ููุน ูุงูุฌูุงุฒ", "distance": "ุงูู
ุณุงูุฉ ุนู ุงูู
ููุน ุงูู
ุนุชุงุฏ (ูู
)", "device_type": "ููุน ุงูุฌูุงุฒ", "country": "ุฏููุฉ ุงูู
ุนุงู
ูุฉ", "behavior": "ุงูุฃูู
ุงุท ุงูุณููููุฉ", "frequency": "ุนุฏุฏ ุงูู
ุนุงู
ูุงุช (ุขุฎุฑ ุณุงุนุฉ)", "avg_transaction": "ู
ุชูุณุท ุงูู
ุจูุบ ุงูุณุงุจู ($)", "failed_attempts": "ู
ุญุงููุงุช ูุงุดูุฉ (ุขุฎุฑ 24 ุณุงุนุฉ)", "summary": "ู
ูุฎุต ุงูู
ุนุงู
ูุฉ ุงูู
ุฏุฎูุฉ", "amount_value": "ููู
ุฉ ุงูู
ุนุงู
ูุฉ", "time": "ุงูููุช", "hour": "ุงูุณุงุนุฉ", "location": "ุงูู
ููุน", "from": "ู
ู", "speed": "ุงูุณุฑุนุฉ", "last_hour": "ูู ุขุฎุฑ ุณุงุนุฉ", "start_analysis": "ุจุฏุก ุชุญููู ุงูุงุญุชูุงู", "analyzing": "ุฌุงุฑู ุชุญููู ุงูู
ุนุงู
ูุฉ...", "analysis_results": "ูุชุงุฆุฌ ุงูุชุญููู", "risk_rate": "ูุณุจุฉ ุงูู
ุฎุงุทุฑุฉ", "probability": "ุงูุงุญุชู
ุงู", "decision_threshold": "ุงูุนุชุจุฉ", "safe": "ุขู
ูุฉ (ู
ูุจููุฉ)", "warning": "ุงุดุชุจุงู (ุชุญุช ุงูู
ุฑุงุฌุนุฉ)", "danger": "ุงุญุชูุงู (ู
ุฑููุถุฉ)", "decision_explanation": "ุชูุณูุฑ ุงููุฑุงุฑ", "technical_details": "ุชูุงุตูู ุชูููุฉ (ููู
ุทูุฑูู)", "top_features": "ุฃูู
10 ุณู
ุงุช ู
ุคุซุฑุฉ:", "value": "ุงูููู
ุฉ", "effect": "ุงูุชุฃุซูุฑ", "positive": "ู
ูุฌุจ", "negative": "ุณุงูุจ", "neutral": "ู
ุญุงูุฏ", "technical_note": "ุงูููู
ุงูุณุงูุจุฉ ูู V14, V17, V10, V12, V16 ุชุฒูุฏ ู
ู ุงุญุชู
ุงููุฉ ุงูุงุญุชูุงู", "about": "ู
ุนููู
ุงุช ุนู ุงููุธุงู
", "how_works": "ููู ูุนู
ู ูุธุงู
FraudGuard AIุ", "collect_data": "1. ุฌู
ุน ุงูุจูุงูุงุช:", "collect_data_body": "ูููู
ุจู
ุญุงูุงุฉ 10 ุณู
ุงุช ุฑุฆูุณูุฉ ุชู
ุชุญุฏูุฏูุง ุจูุงุกู ุนูู ุชุญููู ุขูุงู ุงูู
ุนุงู
ูุงุช. ูู ุณู
ุฉ ุชู
ุซู ูู
ุทุงู ุณููููุงู ุฃู ุชูููุงู ู
ุฑุชุจุทุงู ุจุงูุงุญุชูุงู.", "analyze_features": "2. ุชุญููู ุงูุณู
ุงุช:", "analyze_features_body": "V14, V17, V10, V12, V16: ููู
ุณุงูุจุฉ ุชุดูุฑ ุฅูู ุงูุงุญุชูุงู. V4, V11: ููู
ู
ูุฌุจุฉ ุชุดูุฑ ุฅูู ุงูุงุญุชูุงู. Amount, Time: ูุชู
ุชูููุณูุง ูู
ูุงุฑูุชูุง ู
ุน ุงูุฃูู
ุงุท ุงูุณุงุจูุฉ.", "make_decision": "3. ุงุชุฎุงุฐ ุงููุฑุงุฑ:", "explain": "4. ุงูุชูุณูุฑ:", "explain_body": "ููุถุญ ุงูุนูุงู
ู ุงูุชู ุฃุซุฑุช ุนูู ุงููุฑุงุฑ ูููุฏู
ูุฑุงุกุฉ ุนู
ููุฉ ููุชุนุงู
ู ู
ุน ุงูุญุงูุฉ.", "tip": "ูุตูุญุฉ: ุงูู
ุนุงู
ูุงุช ุงูุขู
ูุฉ ุนุงุฏุฉู ู
ุง ุชููู ู
ู ุฃุฌูุฒุฉ ู
ุนุฑููุฉุ ูู ุฃููุงุช ุงุนุชูุงุฏูุฉุ ูุจููู
ู
ุชูุณุทุฉุ ูู
ู ู
ูุงูุน ู
ุฃูููุฉ.", "footer": "FraudGuard AI v2.0 | ูุธุงู
ูุดู ุงูุงุญุชูุงู ุงูู
ุตุฑูู | ยฉ 2024 ุฌู
ูุน ุงูุญููู ู
ุญููุธุฉ", "safe_reason_normal": "ูุง ุชูุฌุฏ ู
ุคุดุฑุงุช ุฎุทูุฑุฉ: ุฌู
ูุน ุงูู
ุนุงู
ูุงุช ุถู
ู ุงููุทุงู ุงูุทุจูุนู", "safe_reason_email": "ุจุฑูุฏ ุฅููุชุฑููู ู
ูุซูู: ูุทุงู ุจุฑูุฏ ุฑุณู
ู ุฃู ุดุฎุตู ู
ุนุฑูู", "safe_reason_location": "ู
ููุน ุงุนุชูุงุฏู: ุงูู
ุนุงู
ูุฉ ู
ู ุงูู
ููุน ุงูู
ุนุชุงุฏ ููุนู
ูู", "safe_reason_device": "ุฌูุงุฒ ู
ูุซูู: ุชู
ุงูุชุนุฑู ุนูู ุงูุฌูุงุฒ ู
ู ูุจู", "fraud_classification": "ุชุตููู ุงุญุชูุงู: ุชุฌุงูุฒุช ูุณุจุฉ ุงูู
ุฎุงุทุฑุฉ ุงูุนุชุจุฉ ุงูู
ุซูู", "v14_low": "V14 ู
ูุฎูุถ ุฌุฏุงู: ู
ุคุดุฑ ููู ุนูู ุงูุงุญุชูุงู (ุงูููู
ุฉ: {})", "suspicious_email": "ุจุฑูุฏ ุฅููุชุฑููู ู
ุดุจูู: ุงุณุชุฎุฏุงู
ุจุฑูุฏ ู
ุคูุช ุฃู ุนูู ุงูุดุจูุฉ ุงูู
ุธูู
ุฉ", "anonymity": "ุฅุฎูุงุก ุงููููุฉ: ุงุณุชุฎุฏุงู
VPN ุฃู ุจุฑููุณู ูุฅุฎูุงุก ุงูู
ููุน", "unusual_location": "ู
ููุน ุบูุฑ ุงุนุชูุงุฏู: ุงูู
ุนุงู
ูุฉ ู
ู ู
ููุน ุจุนูุฏ ุนู ุงูู
ุนุชุงุฏ ({:,} ูู
)", "high_frequency": "ุชูุฑุงุฑ ุนุงูู: {} ู
ุนุงู
ูุฉ ูู ุณุงุนุฉ ูุงุญุฏุฉ (ูุดุจู Card Testing)", "unusual_time": "ุชูููุช ุบูุฑ ุงุนุชูุงุฏู: ุงูู
ุนุงู
ูุฉ ูู ุณุงุนุฉ ู
ุชุฃุฎุฑุฉ ({:.1f} ุณุงุนุฉ)", "card_testing": "ุงุฎุชุจุงุฑ ุจุทุงูุฉ: ู
ุจูุบ ุตุบูุฑ ุฌุฏุงู ูุดุจู ุงุฎุชุจุงุฑ ุงูุจุทุงูุฉ", "failed_attempts_reason": "ู
ุญุงููุงุช ูุงุดูุฉ: {} ู
ุญุงููุฉ ูุงุดูุฉ ุณุงุจูุฉ", "load_error": "ุฎุทุฃ ูู ุชุญู
ูู ุงูู
ููุงุช: {}", "missing_model": "ูู
ูุชู
ุชุญู
ูู ุงููู
ูุฐุฌ. ุชุฃูุฏ ู
ู ูุฌูุฏ ู
ููุงุช ุงููู
ูุฐุฌ ูู ุงูู
ุฌูุฏ.", "analysis_error": "ุฎุทุฃ ูู ุงูุชุญููู: {}", "column_error": "ูุฑุฌู ุงูุชุฃูุฏ ู
ู ุชุทุงุจู ุฃุณู
ุงุก ุงูุฃุนู
ุฏุฉ ู
ุน ุงููู
ูุฐุฌ ุงูู
ุฏุฑุจ", }, "en": { "language": "Language / ุงููุบุฉ", "arabic": "ุงูุนุฑุจูุฉ", "english": "English", "brand": "FraudGuard AI", "tagline": "Intelligent Fraud Detection System", "eyebrow": "TRANSACTION RISK INTELLIGENCE PLATFORM", "hero_title": "Smarter decisions. Stronger protection.", "hero_subtitle": "Analyze every transaction in real time and surface fraud signals before they become losses.", "system_status": "SYSTEM ONLINE", "model_ready": "Model ready for analysis", "optimal_threshold": "Optimal threshold", "threshold_caption": "Recommended decision level", "transaction_details": "Transaction details", "financial_data": "Financial data", "amount": "Transaction amount ($)", "transaction_time": "Transaction time", "account_identity": "Customer identity", "email_domain": "Email domain", "account_age": "Account age (days)", "account_age_help": "Number of days since the account was created", "location_device": "Location & device", "distance": "Distance from usual location (km)", "device_type": "Device type", "country": "Transaction country", "behavior": "Behavior patterns", "frequency": "Transactions in the last hour", "avg_transaction": "Previous average amount ($)", "failed_attempts": "Failed attempts in the last 24 hours", "summary": "Submitted transaction summary", "amount_value": "Transaction value", "time": "Time", "hour": "Hour", "location": "Location", "from": "From", "speed": "Velocity", "last_hour": "During the last hour", "start_analysis": "Start fraud analysis", "analyzing": "Analyzing transaction...", "analysis_results": "Analysis results", "risk_rate": "Risk rate", "probability": "Probability", "decision_threshold": "Threshold", "safe": "Safe (approved)", "warning": "Review required", "danger": "Fraud (declined)", "decision_explanation": "Decision explanation", "technical_details": "Technical details (developers)", "top_features": "Top 10 influential features:", "value": "Value", "effect": "Effect", "positive": "Positive", "negative": "Negative", "neutral": "Neutral", "technical_note": "Negative values in V14, V17, V10, V12 and V16 increase the likelihood of fraud", "about": "About the system", "how_works": "How does FraudGuard AI work?", "collect_data": "1. Data collection:", "collect_data_body": "We simulate 10 key features identified through analysis of thousands of transactions. Each feature represents a behavioral or technical pattern associated with fraud.", "analyze_features": "2. Feature analysis:", "analyze_features_body": "V14, V17, V10, V12 and V16: negative values indicate fraud. V4 and V11: positive values indicate fraud. Amount and Time: scaled and compared with previous patterns.", "make_decision": "3. Decision:", "explain": "4. Explainability:", "explain_body": "We surface the factors that influenced the decision and provide a practical reading of the case.", "tip": "Tip: safer transactions typically come from known devices, at usual times, with moderate amounts and familiar locations.", "footer": "FraudGuard AI v2.0 | Banking Fraud Detection System | ยฉ 2024 All rights reserved", "safe_reason_normal": "No critical indicators: the transaction is within the normal range", "safe_reason_email": "Trusted email: a recognized business or personal domain", "safe_reason_location": "Usual location: the transaction matches the customerโs normal location", "safe_reason_device": "Trusted device: this device has been recognized before", "fraud_classification": "Fraud classification: the risk rate exceeded the optimal threshold", "v14_low": "Very low V14: a strong fraud indicator (value: {})", "suspicious_email": "Suspicious email: temporary mail or dark-web domain detected", "anonymity": "Anonymity masking: VPN or proxy usage detected", "unusual_location": "Unusual location: the transaction is {:,} km from the usual location", "high_frequency": "High frequency: {} transactions in one hour (similar to card testing)", "unusual_time": "Unusual time: the transaction occurred late ({:.1f} hours)", "card_testing": "Card testing: an unusually small amount resembles a card test", "failed_attempts_reason": "Failed attempts: {} previous failed attempts", "load_error": "Error loading artifacts: {}", "missing_model": "The model was not loaded. Confirm that the model files exist in the project folder.", "analysis_error": "Analysis error: {}", "column_error": "Confirm that the feature names match the trained model", },}
def t(key): return TRANSLATIONS[LANG][key]
def option_label(value): """Translate display labels while retaining the original Arabic values internally.""" option_map = { "gmail.com (ุดุฎุตู)": {"ar": "gmail.com (ุดุฎุตู)", "en": "gmail.com (Personal)"}, "company.com (ุฑุณู
ู)": {"ar": "company.com (ุฑุณู
ู)", "en": "company.com (Business)"}, "outlook.com (ุดุฎุตู)": {"ar": "outlook.com (ุดุฎุตู)", "en": "outlook.com (Personal)"}, "yahoo.com (ุดุฎุตู)": {"ar": "yahoo.com (ุดุฎุตู)", "en": "yahoo.com (Personal)"}, "temp-mail.org (ู
ุคูุช)": {"ar": "temp-mail.org (ู
ุคูุช)", "en": "temp-mail.org (Temporary)"}, "darkweb.onion (ู
ุดุจูู)": {"ar": "darkweb.onion (ู
ุดุจูู)", "en": "darkweb.onion (Suspicious)"}, "protonmail.com (ู
ุดูุฑ)": {"ar": "protonmail.com (ู
ุดูุฑ)", "en": "protonmail.com (Encrypted)"}, "ุฌูุงุฒ ู
ูุซูู (ุชุณุฌูู ุณุงุจู)": {"ar": "ุฌูุงุฒ ู
ูุซูู (ุชุณุฌูู ุณุงุจู)", "en": "Trusted device (previously seen)"}, "ุฌูุงุฒ ุฌุฏูุฏ (ุฃูู ุงุณุชุฎุฏุงู
)": {"ar": "ุฌูุงุฒ ุฌุฏูุฏ (ุฃูู ุงุณุชุฎุฏุงู
)", "en": "New device (first use)"}, "ู
ุชุตูุญ ุฎุงุต/Incognito": {"ar": "ู
ุชุตูุญ ุฎุงุต/Incognito", "en": "Private browser / Incognito"}, "VPN/ุจุฑููุณู": {"ar": "VPN/ุจุฑููุณู", "en": "VPN / Proxy"}, "ุฌูุงุฒ ู
ู ุฏููุฉ ู
ุฎุชููุฉ": {"ar": "ุฌูุงุฒ ู
ู ุฏููุฉ ู
ุฎุชููุฉ", "en": "Device from another country"}, "ุงูููุงูุงุช ุงูู
ุชุญุฏุฉ": {"ar": "ุงูููุงูุงุช ุงูู
ุชุญุฏุฉ", "en": "United States"}, "ุงูู
ู
ููุฉ ุงูู
ุชุญุฏุฉ": {"ar": "ุงูู
ู
ููุฉ ุงูู
ุชุญุฏุฉ", "en": "United Kingdom"}, "ุฃูู
ุงููุง": {"ar": "ุฃูู
ุงููุง", "en": "Germany"}, "ูุฑูุณุง": {"ar": "ูุฑูุณุง", "en": "France"}, "ููุฏุง": {"ar": "ููุฏุง", "en": "Canada"}, "ุฑูุณูุง": {"ar": "ุฑูุณูุง", "en": "Russia"}, "ุงูุตูู": {"ar": "ุงูุตูู", "en": "China"}, "ููุฌูุฑูุง": {"ar": "ููุฌูุฑูุง", "en": "Nigeria"}, "ุงูููุฏ": {"ar": "ุงูููุฏ", "en": "India"}, "ุงูุจุฑุงุฒูู": {"ar": "ุงูุจุฑุงุฒูู", "en": "Brazil"}, } return option_map.get(value, {}).get(LANG, value)
# ุงูุชุตู
ูู
ุงูู
ุนุฏูู: ูุฐู ุงูุทุจูุฉ ุจุตุฑูุฉ ููุท ููุง ุชุบููุฑ ู
ุฏุฎูุงุช ุงููู
ูุฐุฌ ุฃู ุทุฑููุฉ ุงูุชูุจุค.st.markdown( f""" <style> :root {{ --metal-950: #080b10; --metal-900: #10151d; --metal-850: #171e28; --metal-800: #202a36; --metal-700: #334150; --metal-500: #718096; --silver: #d7e0e8; --silver-bright: #f5f8fa; --cyan: #59d5e6; --gold: #d7ad67; --green: #55d6a2; --amber: #f0ba62; --red: #f07373; }}
.stApp {{ direction: {DIRECTION}; background: radial-gradient(circle at 8% 4%, rgba(89,213,230,.09), transparent 26%), radial-gradient(circle at 90% 10%, rgba(215,173,103,.08), transparent 22%), linear-gradient(135deg, #080b10 0%, #10151d 48%, #0b0f15 100%); color: var(--silver-bright); }}
[data-testid="stHeader"] {{ background: transparent; }} [data-testid="stToolbar"] {{ visibility: hidden; }} [data-testid="stSidebar"] {{ background: linear-gradient(180deg, #0d131a 0%, #111923 100%); border-right: 1px solid rgba(215,224,232,.12); border-left: 1px solid rgba(215,224,232,.08); }} [data-testid="stSidebarContent"] {{ padding: 1.25rem 1rem 2rem; }} [data-testid="stSidebar"] * {{ color: var(--silver); }}
h1, h2, h3, h4, p, label, [data-testid="stMetricLabel"] {{ font-family: Inter, "Segoe UI", Tahoma, Arial, sans-serif; }} h1, h2, h3, h4 {{ color: var(--silver-bright) !important; letter-spacing: -.02em; }} p, label, [data-testid="stCaptionContainer"] {{ color: #aebdca !important; }} .block-container {{ max-width: 1500px; padding-top: 2rem; padding-bottom: 1rem; }}
.brand-panel, .hero-panel, .metal-card, .status-panel {{ position: relative; overflow: hidden; border: 1px solid rgba(215,224,232,.18); border-radius: 20px; background: linear-gradient(145deg, rgba(255,255,255,.105), rgba(255,255,255,.018) 38%, rgba(0,0,0,.18)), linear-gradient(145deg, #293542, #111821 68%, #0c1117); box-shadow: 0 18px 45px rgba(0,0,0,.30), inset 0 1px 0 rgba(255,255,255,.13), inset 0 -1px 0 rgba(0,0,0,.45); }} .brand-panel::before, .hero-panel::before, .metal-card::before, .status-panel::before {{ content: ""; position: absolute; inset: 0; pointer-events: none; background: linear-gradient(112deg, transparent 15%, rgba(255,255,255,.08) 32%, transparent 48%); opacity: .65; }} .brand-panel {{ padding: 1.15rem; margin-bottom: 1.3rem; border-color: rgba(89,213,230,.26); }} .brand-mark {{ display:flex; align-items:center; gap:.7rem; position:relative; z-index:1; }} .shield {{ width: 42px; height: 42px; display:grid; place-items:center; border-radius: 13px; color: #081116; background: linear-gradient(145deg, #e8f8fb, #59d5e6 55%, #2e8d9b); box-shadow: 0 0 22px rgba(89,213,230,.26), inset 0 1px 2px rgba(255,255,255,.8); font-size: 1.35rem; }} .brand-title {{ margin:0; color:var(--silver-bright); font-size:1.15rem; font-weight:800; }} .brand-subtitle {{ margin:.25rem 0 0; color:#8fa6b7; font-size:.78rem; }} .eyebrow {{ color:var(--cyan); font-size:.72rem; letter-spacing:.16em; font-weight:800; margin:0 0 .5rem; }} .hero-panel {{ padding: 2.2rem 2.3rem; margin-bottom: 1.3rem; min-height: 205px; border-color: rgba(215,173,103,.25); }} .hero-panel h1 {{ position:relative; z-index:1; margin:.3rem 0 .65rem; font-size:clamp(2rem, 4vw, 4rem); line-height:1.05; }} .hero-panel p {{ position:relative; z-index:1; max-width:780px; font-size:1.03rem; line-height:1.8; margin:0; }} .hero-chip {{ position:absolute; z-index:1; inset-inline-end:1.5rem; top:1.5rem; border:1px solid rgba(85,214,162,.35); color:var(--green); border-radius:99px; padding:.45rem .75rem; font-size:.76rem; font-weight:700; background:rgba(85,214,162,.07); }} .hero-chip span {{ display:inline-block; width:7px; height:7px; border-radius:50%; background:var(--green); box-shadow:0 0 11px var(--green); margin-inline-end:.45rem; }} .threshold-card {{ height:100%; padding:1.15rem; border:1px solid rgba(215,173,103,.27); border-radius:18px; background:linear-gradient(145deg, rgba(215,173,103,.14), rgba(0,0,0,.18)); box-shadow:inset 0 1px 0 rgba(255,255,255,.1); }} .threshold-label {{ color:#b9c6d0; font-size:.82rem; }} .threshold-value {{ color:var(--gold); font-size:2rem; font-weight:800; margin:.35rem 0; }} .threshold-caption {{ color:#8d9ca9; font-size:.76rem; }} .section-title {{ display:flex; align-items:center; gap:.6rem; color:var(--silver-bright); font-size:1.2rem; font-weight:750; margin:1.35rem 0 .8rem; }} .section-title::before {{ content:""; width:4px; height:22px; border-radius:9px; background:linear-gradient(var(--cyan), var(--gold)); box-shadow:0 0 12px rgba(89,213,230,.35); }} .metric-card {{ padding:1.1rem 1.2rem; min-height:120px; border:1px solid rgba(215,224,232,.14); border-radius:17px; background:linear-gradient(145deg, rgba(255,255,255,.08), rgba(255,255,255,.018)); box-shadow:inset 0 1px 0 rgba(255,255,255,.09), 0 12px 28px rgba(0,0,0,.18); }} .metric-label {{ color:#8fa2b2; font-size:.78rem; margin-bottom:.5rem; }} .metric-value {{ color:var(--silver-bright); font-size:1.42rem; font-weight:800; line-height:1.15; }} .metric-caption {{ color:#718493; font-size:.74rem; margin-top:.45rem; }} .accent-cyan {{ border-top:2px solid var(--cyan); }} .accent-gold {{ border-top:2px solid var(--gold); }} .accent-green {{ border-top:2px solid var(--green); }} .accent-amber {{ border-top:2px solid var(--amber); }}
[data-testid="stExpander"] {{ border:1px solid rgba(215,224,232,.14); border-radius:15px; background:rgba(255,255,255,.025); }} [data-testid="stExpander"] summary {{ color:var(--silver-bright) !important; font-weight:700; }} [data-testid="stNumberInput"], [data-testid="stSelectbox"], [data-testid="stSlider"], [data-testid="stTimeInput"] {{ margin-bottom:.35rem; }} [data-baseweb="select"] > div, [data-baseweb="input"] > div, [data-testid="stTimeInput"] input {{ background:#18222d !important; border-color:#3b4b5b !important; color:var(--silver-bright) !important; border-radius:10px !important; }} [data-testid="stSlider"] [role="slider"] {{ background:var(--cyan); border-color:var(--cyan); }} [data-testid="stProgressBar"] > div > div {{ background:linear-gradient(90deg, var(--cyan), var(--gold)); }} [data-testid="stMetricValue"] {{ color:var(--silver-bright) !important; }} [data-testid="stMetricLabel"] {{ color:#9cafbd !important; }} [data-testid="stButton"] button {{ width:100%; min-height:56px; border-radius:14px; border:1px solid rgba(215,173,103,.45); color:#0c1117; font-size:1rem; font-weight:850; background:linear-gradient(145deg, #f0d49b, #c79548 52%, #8b602b); box-shadow:0 12px 25px rgba(215,173,103,.16), inset 0 1px 0 rgba(255,255,255,.65); transition:transform .2s ease, box-shadow .2s ease; }} [data-testid="stButton"] button:hover {{ transform:translateY(-2px); box-shadow:0 17px 32px rgba(215,173,103,.28), inset 0 1px 0 rgba(255,255,255,.7); color:#080b10; }} [data-testid="stAlert"] {{ border-radius:13px; background:rgba(255,255,255,.045); }} [data-testid="stDataFrame"] {{ border:1px solid rgba(215,224,232,.13); border-radius:14px; overflow:hidden; }} hr {{ border-color:rgba(215,224,232,.13) !important; }} .footer {{ text-align:center; color:#718493; padding:1rem 0 .2rem; font-size:.78rem; }} .sidebar-note {{ color:#718493 !important; font-size:.75rem; line-height:1.7; margin-top:1rem; }} @media (max-width: 800px) {{ .hero-panel {{ padding:1.5rem; }} .hero-chip {{ position:relative; inset-inline-end:auto; top:auto; display:inline-block; margin-top:1.2rem; }} }} </style> """, unsafe_allow_html=True,)
def metric_card(label, value, caption, accent="cyan"): st.markdown( f""" <div class="metric-card accent-{accent}" dir="{DIRECTION}"> <div class="metric-label">{label}</div> <div class="metric-value">{value}</div> <div class="metric-caption">{caption}</div> </div> """, unsafe_allow_html=True, )
# ==========================================# 2. ุชุญู
ูู ุงูู
ููุงุช (Model Layer) โ ู
ุญููุธ ูู
ุง ูู# ==========================================@st.cache_resourcedef load_artifacts(): """ุชุญู
ูู ุฌู
ูุน ู
ููุงุช ุงููู
ูุฐุฌ ูุงูุถุจุท""" try: model = joblib.load("fraud_model.joblib") scaler = joblib.load("scaler.joblib") features = joblib.load("feature_names.joblib")
with open("top_features.json", "r") as f: top_features = json.load(f)
with open("threshold.json", "r") as f: threshold_data = json.load(f) optimal_threshold = threshold_data.get("threshold", 0.5)
return model, scaler, features, top_features, optimal_threshold except Exception as e: st.error(t("load_error").format(str(e))) return None, None, None, [], 0.5
model, scaler, all_features, top_features, OPTIMAL_THRESHOLD = load_artifacts()
if not model: st.error(t("missing_model")) st.stop()
# ==========================================# 3. ุงููุงุฆู
ุฉ ุงูุฌุงูุจูุฉ (Input Layer) โ ุงูููู
ุงูุฏุงุฎููุฉ ู
ุญููุธุฉ# ==========================================with st.sidebar: st.markdown( f""" <div class="brand-panel" dir="{DIRECTION}"> <div class="brand-mark"> <div class="shield">โ</div> <div> <p class="brand-title">{t('brand')}</p> <p class="brand-subtitle">{t('tagline')}</p> </div> </div> </div> """, unsafe_allow_html=True, )
st.radio( t("language"), options=["ar", "en"], format_func=lambda value: t("arabic") if value == "ar" else t("english"), key="language", horizontal=True, )
st.markdown("---") st.markdown(f'<div class="section-title">{t("transaction_details")}</div>', unsafe_allow_html=True)
with st.expander(f"๐ฐ {t('financial_data')}", expanded=True): amount = st.number_input( t("amount"), min_value=0.01, max_value=100000.0, value=149.99, step=0.01, format="%.2f", key="amount_input", ) transaction_time = st.time_input( t("transaction_time"), value=datetime.now().time(), key="transaction_time_input", ) time_hour = transaction_time.hour + transaction_time.minute / 60.0
with st.expander(f"๐ค {t('account_identity')}", expanded=True): email_domain = st.selectbox( t("email_domain"), [ "gmail.com (ุดุฎุตู)", "company.com (ุฑุณู
ู)", "outlook.com (ุดุฎุตู)", "yahoo.com (ุดุฎุตู)", "temp-mail.org (ู
ุคูุช)", "darkweb.onion (ู
ุดุจูู)", "protonmail.com (ู
ุดูุฑ)", ], format_func=option_label, key="email_domain_input", ) customer_age = st.slider( t("account_age"), min_value=1, max_value=365 * 5, value=180, help=t("account_age_help"), key="customer_age_input", )
with st.expander(f"๐ {t('location_device')}", expanded=True): distance_km = st.slider( t("distance"), min_value=0, max_value=10000, value=5, step=10, key="distance_input", ) device_type = st.selectbox( t("device_type"), [ "ุฌูุงุฒ ู
ูุซูู (ุชุณุฌูู ุณุงุจู)", "ุฌูุงุฒ ุฌุฏูุฏ (ุฃูู ุงุณุชุฎุฏุงู
)", "ู
ุชุตูุญ ุฎุงุต/Incognito", "VPN/ุจุฑููุณู", "ุฌูุงุฒ ู
ู ุฏููุฉ ู
ุฎุชููุฉ", ], format_func=option_label, key="device_type_input", ) country = st.selectbox( t("country"), [ "ุงูููุงูุงุช ุงูู
ุชุญุฏุฉ", "ุงูู
ู
ููุฉ ุงูู
ุชุญุฏุฉ", "ุฃูู
ุงููุง", "ูุฑูุณุง", "ููุฏุง", "ุฑูุณูุง", "ุงูุตูู", "ููุฌูุฑูุง", "ุงูููุฏ", "ุงูุจุฑุงุฒูู", ], format_func=option_label, key="country_input", )
with st.expander(f"๐ {t('behavior')}", expanded=False): frequency_1h = st.slider( t("frequency"), min_value=1, max_value=50, value=1, key="frequency_input" ) avg_transaction = st.slider( t("avg_transaction"), min_value=1.0, max_value=5000.0, value=120.0, step=10.0, key="avg_transaction_input", ) failed_attempts = st.slider( t("failed_attempts"), min_value=0, max_value=20, value=0, key="failed_attempts_input", )
st.markdown( f'<p class="sidebar-note">{t("tip")}</p>', unsafe_allow_html=True )
# ==========================================# 4. ู
ูุทู ุงูู
ุญุงูุงุฉ (Logic Layer) โ ูู
ูุชุบูุฑ# ==========================================def simulate_features(): """ ู
ุญุงูุงุฉ ูุงูุนูุฉ ูุฃูุถู 10 ุฃุนู
ุฏุฉ ุจูุงุกู ุนูู ุงูู
ุฏุฎูุงุช """ features_dict = {feature: 0.0 for feature in all_features}
# 1. Amount ู Time (ุณูุชู
ุชูููุณูู
ุง ูุงุญููุง) features_dict["Amount"] = float(amount) features_dict["Time"] = time_hour
# 2. V14 (ู
ูู
ููุบุงูุฉ - ุนุงุฏุฉ ุณุงูุจ ููุงุญุชูุงู) v14_value = 1.5
# ุนูุงู
ู ุชุฎูุถ V14 (ุชุฒูุฏ ุงูุงุญุชูุงู) if "temp-mail" in email_domain or "darkweb" in email_domain: v14_value -= 8.5 if "VPN" in device_type or "ุฎุงุต" in device_type: v14_value -= 5.2 if distance_km > 1000: v14_value -= (distance_km / 500) * 0.8 if country in ["ููุฌูุฑูุง", "ุฑูุณูุง", "ุงูุตูู"]: v14_value -= 3.0 if frequency_1h > 10: v14_value -= 2.5 if failed_attempts > 3: v14_value -= 4.0
# ุนูุงู
ู ุชุฑูุน V14 (ุชููู ุงูุงุญุชูุงู) if "company.com" in email_domain: v14_value += 4.0 if customer_age > 365: v14_value += 3.0 if amount < 100 and amount > 10: v14_value += 1.5
features_dict["V14"] = np.clip(v14_value, -15, 15)
# 3. V17 (ุนุงุฏุฉ ุณุงูุจ ููุงุญุชูุงู) v17_value = 0.8 if frequency_1h > 5: v17_value -= (frequency_1h / 10) * 1.2 if "ู
ุคูุช" in email_domain: v17_value -= 3.5 features_dict["V17"] = np.clip(v17_value, -10, 10)
# 4. V10 (ุนุงุฏุฉ ุณุงูุจ ููุงุญุชูุงู) v10_value = 0.5 if amount > 1000: v10_value -= 2.0 if failed_attempts > 0: v10_value -= failed_attempts * 0.7 features_dict["V10"] = np.clip(v10_value, -15, 10)
# 5. V12 (ุนุงุฏุฉ ุณุงูุจ ููุงุญุชูุงู) v12_value = 0.3 if distance_km > 500: v12_value -= (distance_km / 1000) * 3.0 features_dict["V12"] = np.clip(v12_value, -15, 8)
# 6. V16 (ุนุงุฏุฉ ุณุงูุจ ููุงุญุชูุงู) v16_value = 0.4 if time_hour < 6 or time_hour > 22: v16_value -= 2.5 features_dict["V16"] = np.clip(v16_value, -10, 8)
# 7. V11 (ุนุงุฏุฉ ู
ูุฌุจ ููุงุญุชูุงู) v11_value = 0.2 if frequency_1h > 15: v11_value += 3.0 if abs(amount - avg_transaction) > (avg_transaction * 0.5): v11_value += 1.8 features_dict["V11"] = np.clip(v11_value, -2, 12)
# 8. V4 (ุนุงุฏุฉ ู
ูุฌุจ ููุงุญุชูุงู) v4_value = 0.3 if "VPN" in device_type: v4_value += 4.5 if amount < 1.0: v4_value += 3.0 features_dict["V4"] = np.clip(v4_value, -5, 15)
# 9. V2 (ู
ุชุบูุฑ) v2_value = 0.1 if customer_age < 30: v2_value += 2.0 features_dict["V2"] = np.clip(v2_value, -5, 8)
# 10. V19 (ู
ุชุบูุฑ) v19_value = -0.1 if "ุฌูุงุฒ ุฌุฏูุฏ" in device_type: v19_value += 1.5 features_dict["V19"] = np.clip(v19_value, -7, 7)
return features_dict
# ==========================================# 5. ุงููุงุฌูุฉ ุงูุฑุฆูุณูุฉ# ==========================================col1, col2 = st.columns([3.5, 1])with col1: st.markdown( f""" <div class="hero-panel" dir="{DIRECTION}"> <div class="hero-chip"><span></span>{t('system_status')}</div> <p class="eyebrow">{t('eyebrow')}</p> <h1>{t('hero_title')}</h1> <p>{t('hero_subtitle')}</p> </div> """, unsafe_allow_html=True, )with col2: st.markdown( f""" <div class="threshold-card" dir="{DIRECTION}"> <div class="threshold-label">{t('optimal_threshold')}</div> <div class="threshold-value">{OPTIMAL_THRESHOLD:.3f}</div> <div class="threshold-caption">{t('threshold_caption')}</div> </div> """, unsafe_allow_html=True, )
st.markdown(f'<div class="section-title">{t("summary")}</div>', unsafe_allow_html=True)col1, col2, col3, col4 = st.columns(4)with col1: metric_card(t("amount"), f"${amount:,.2f}", t("amount_value"), "gold")with col2: metric_card( t("time"), f"{time_hour:.1f}", f"{t('hour')} {transaction_time.strftime('%H:%M')}", "cyan", )with col3: metric_card( t("location"), f"{distance_km:,} km", f"{t('from')} {option_label(country)}", "green", )with col4: if "VPN" in device_type or "ู
ุคูุช" in email_domain: velocity_accent = "amber" elif "ุฌุฏูุฏ" in device_type or distance_km > 1000: velocity_accent = "gold" else: velocity_accent = "green" metric_card( t("speed"), f"{frequency_1h}", t("last_hour"), velocity_accent, )
st.markdown("<div style='height:.5rem'></div>", unsafe_allow_html=True)
# ==========================================# 6. ุงูุชุญููู ูุงููุชุงุฆุฌ โ ู
ูุทู ุงูุชูุจุค ู
ุญููุธ# ==========================================if st.button(f"โฆ {t('start_analysis')}", type="primary"): with st.spinner(t("analyzing")): time.sleep(1.5)
features_dict = simulate_features() df_input = pd.DataFrame([features_dict])
# ุชูููุณ Amount ู Time ููุท โ ู
ุญููุธ ูู
ุง ูู if "Amount" in df_input.columns and "Time" in df_input.columns: df_input[["Amount", "Time"]] = scaler.transform(df_input[["Amount", "Time"]])
try: probability = model.predict_proba(df_input)[0][1]
st.markdown( f'<div class="section-title">{t("analysis_results")}</div>', unsafe_allow_html=True, ) col_prog1, col_prog2, col_prog3 = st.columns([2, 1, 1]) with col_prog1: st.progress(float(probability), text=f"{t('risk_rate')}: {probability:.2%}") with col_prog2: st.metric(t("probability"), f"{probability:.4f}") with col_prog3: st.metric(t("decision_threshold"), f"{OPTIMAL_THRESHOLD:.4f}")
decision_threshold = OPTIMAL_THRESHOLD half_threshold = decision_threshold / 2
if probability < half_threshold: status = "safe" status_text = t("safe") status_color = "#55d6a2" icon = "โ" elif probability < decision_threshold: status = "warning" status_text = t("warning") status_color = "#f0ba62" icon = "!" else: status = "danger" status_text = t("danger") status_color = "#f07373" icon = "ร"
st.markdown( f""" <div class="status-panel" dir="{DIRECTION}" style="margin:1.2rem 0; padding:1.6rem; text-align:center; border-color:{status_color}80; background:linear-gradient(145deg, {status_color}20, rgba(0,0,0,.2));"> <div style="font-size:3rem; line-height:1; color:{status_color}; font-weight:900;">{icon}</div> <h2 style="color:{status_color} !important; margin:.55rem 0 .35rem;">{status_text}</h2> <p style="margin:0; color:#c8d4dc !important;">{t('risk_rate')}: <strong>{probability:.2%}</strong></p> </div> """, unsafe_allow_html=True, )
st.markdown( f'<div class="section-title">{t("decision_explanation")}</div>', unsafe_allow_html=True, ) reasons = []
if probability >= decision_threshold: reasons.append(f"๐จ **{t('fraud_classification')}**") if "V14" in features_dict and features_dict["V14"] < -5: reasons.append(f"๐ **{t('v14_low').format(format(features_dict['V14'], '.2f'))}**") if "temp-mail" in email_domain or "darkweb" in email_domain: reasons.append(f"๐ง **{t('suspicious_email')}**") if "VPN" in device_type: reasons.append(f"๐ก๏ธ **{t('anonymity')}**") if distance_km > 1000: reasons.append(f"๐ **{t('unusual_location').format(distance_km)}**") if frequency_1h > 15: reasons.append(f"โก **{t('high_frequency').format(frequency_1h)}**") if time_hour < 6 or time_hour > 22: reasons.append(f"๐ **{t('unusual_time').format(time_hour)}**") if amount < 1.0: reasons.append(f"๐ณ **{t('card_testing')}**") if failed_attempts > 2: reasons.append(f"โ **{t('failed_attempts_reason').format(failed_attempts)}**")
if not reasons and probability < half_threshold: reasons.extend( [ f"โ
**{t('safe_reason_normal')}**", f"โ
**{t('safe_reason_email')}**", f"โ
**{t('safe_reason_location')}**", f"โ
**{t('safe_reason_device')}**", ] )
for reason in reasons: if "โ
" in reason: st.success(reason) elif "โ ๏ธ" in reason: st.warning(reason) elif any(token in reason for token in ["๐จ", "โ", "๐"]): st.error(reason) else: st.info(reason)
with st.expander(f"๐ {t('technical_details')}", expanded=False): st.write(f"**{t('top_features')}**") important_features = { "V14": features_dict.get("V14", 0), "V17": features_dict.get("V17", 0), "V10": features_dict.get("V10", 0), "V12": features_dict.get("V12", 0), "V16": features_dict.get("V16", 0), "V11": features_dict.get("V11", 0), "V4": features_dict.get("V4", 0), "V2": features_dict.get("V2", 0), "V19": features_dict.get("V19", 0), "Amount": features_dict.get("Amount", 0), } df_important = pd.DataFrame.from_dict( important_features, orient="index", columns=[t("value")], ) df_important[t("effect")] = df_important[t("value")].apply( lambda x: f"๐ข {t('positive')}" if x > 0 else f"๐ด {t('negative')}" if x < 0 else f"โช {t('neutral')}" ) st.dataframe(df_important, use_container_width=True) st.caption(f"๐ก {t('technical_note')}")
except Exception as e: st.error(t("analysis_error").format(str(e))) st.info(t("column_error"))
# ==========================================# 7. ู
ุนููู
ุงุช ุฅุถุงููุฉ# ==========================================st.markdown("---")with st.expander(f"โน๏ธ {t('about')}", expanded=False): st.markdown( f""" ### {t('how_works')}
**{t('collect_data')}** {t('collect_data_body')}
**{t('analyze_features')}** {t('analyze_features_body')}
**{t('make_decision')}** - **ุฃูู ู
ู {OPTIMAL_THRESHOLD / 2:.3f}**: {t('safe')} - **ุจูู {OPTIMAL_THRESHOLD / 2:.3f} ู {OPTIMAL_THRESHOLD:.3f}**: {t('warning')} - **ุฃุนูู ู
ู {OPTIMAL_THRESHOLD:.3f}**: {t('danger')}
**{t('explain')}** {t('explain_body')} """ ) st.info(f"๐ก {t('tip')}")
st.markdown("---")st.markdown(f'<div class="footer">๐ก๏ธ {t("footer")}</div>', unsafe_allow_html=True)app_new.pyAdded+844