Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from deepface import DeepFace
|
| 3 |
+
import easyocr
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
+
import pandas as pd
|
| 7 |
+
import sqlite3
|
| 8 |
+
import requests
|
| 9 |
+
import random # for simulate QTE
|
| 10 |
+
|
| 11 |
+
conn = sqlite3.connect('kyc_logs.db')
|
| 12 |
+
c = conn.cursor()
|
| 13 |
+
c.execute('''CREATE TABLE IF NOT EXISTS verifications
|
| 14 |
+
(id INTEGER PRIMARY KEY, name TEXT, phone TEXT, address TEXT, trs REAL, status TEXT, qte_details TEXT, timestamp DATETIME DEFAULT CURRENT_TIMESTAMP)''')
|
| 15 |
+
conn.commit()
|
| 16 |
+
|
| 17 |
+
reader = easyocr.Reader(['en'], gpu=False)
|
| 18 |
+
|
| 19 |
+
st.set_page_config(page_title="Bright KYC AI", layout="wide")
|
| 20 |
+
st.title("🔒 Bright KYC AI – Strict Enforcer with Quantum Trust Echo 🚀")
|
| 21 |
+
st.write("Ghana-Focused | No Shortcuts | Founder: Gov")
|
| 22 |
+
|
| 23 |
+
tab1, tab2 = st.tabs(["Customer Onboarding", "Admin Dashboard"])
|
| 24 |
+
|
| 25 |
+
with tab1:
|
| 26 |
+
st.header("Strict Verification Flow")
|
| 27 |
+
full_name = st.text_input("Full Name")
|
| 28 |
+
phone = st.text_input("Phone Number (+233...)")
|
| 29 |
+
gps_address = st.text_input("GhanaPost Digital Address (e.g., GA-123-4567)")
|
| 30 |
+
|
| 31 |
+
col1, col2 = st.columns(2)
|
| 32 |
+
with col1: id_file = st.file_uploader("Upload ID (Ghana Card/Passport/Voter’s ID)", type=['jpg', 'jpeg', 'png'])
|
| 33 |
+
with col2: selfie_file = st.file_uploader("Upload Live Selfie", type=['jpg', 'jpeg', 'png'])
|
| 34 |
+
|
| 35 |
+
if st.button("Run Strict Verification") and all([full_name, phone, gps_address, id_file, selfie_file]):
|
| 36 |
+
with st.spinner("Enforcing + Quantum Trust Echo... 🔍"):
|
| 37 |
+
id_bytes = id_file.read()
|
| 38 |
+
selfie_bytes = selfie_file.read()
|
| 39 |
+
|
| 40 |
+
id_img = Image.open(io.BytesIO(id_bytes))
|
| 41 |
+
selfie_img = Image.open(io.BytesIO(selfie_bytes))
|
| 42 |
+
|
| 43 |
+
col1, col2 = st.columns(2)
|
| 44 |
+
with col1: st.image(id_img, caption="ID")
|
| 45 |
+
with col2: st.image(selfie_img, caption="Selfie")
|
| 46 |
+
|
| 47 |
+
# Step 2: ID Verification (OCR) - Simple extract for name match
|
| 48 |
+
extracted = reader.readtext(id_bytes)
|
| 49 |
+
full_text = " ".join([text for _, text, _ in extracted]).lower()
|
| 50 |
+
if full_name.lower() not in full_text:
|
| 51 |
+
st.warning("Name no match ID text! Flag ⚠️")
|
| 52 |
+
|
| 53 |
+
# Step 3: Address Confirmation
|
| 54 |
+
try:
|
| 55 |
+
resp = requests.get(f"https://ghanapostgps.sperixlabs.org/get-address/{gps_address}")
|
| 56 |
+
address_valid = resp.status_code == 200 and resp.json().get("status")
|
| 57 |
+
address_score = 20 if address_valid else 0
|
| 58 |
+
st.success("Address Valid ✅") if address_valid else st.warning("Invalid Address ⚠️")
|
| 59 |
+
except:
|
| 60 |
+
address_score = 10
|
| 61 |
+
st.info("Address check skipped")
|
| 62 |
+
|
| 63 |
+
# Step 4: Biometric Capture
|
| 64 |
+
try:
|
| 65 |
+
live_result = DeepFace.extract_faces(img_path=selfie_bytes, anti_spoofing=True)
|
| 66 |
+
is_live = live_result[0]['is_real']
|
| 67 |
+
live_score = live_result[0]['antispoof_score']
|
| 68 |
+
if not is_live:
|
| 69 |
+
st.error("SPOOF DETECTED → BLOCKED 🚫")
|
| 70 |
+
status = "BLOCKED"
|
| 71 |
+
trs = 0
|
| 72 |
+
else:
|
| 73 |
+
verify = DeepFace.verify(img1_path=id_bytes, img2_path=selfie_bytes)
|
| 74 |
+
confidence = (1 - verify["distance"]) * 100
|
| 75 |
+
except Exception as e:
|
| 76 |
+
confidence = 0
|
| 77 |
+
live_score = 0
|
| 78 |
+
status = "ERROR"
|
| 79 |
+
st.error(f"Biometric error: {e}")
|
| 80 |
+
|
| 81 |
+
# Step 5: Quantum Trust Echo (QTE) Simulation - Your Brilliant Feature!
|
| 82 |
+
# Temporal Consistency: Check duplicate in DB (identity exist before?)
|
| 83 |
+
c.execute("SELECT COUNT(*) FROM verifications WHERE name=? OR phone=?", (full_name, phone))
|
| 84 |
+
duplicate_count = c.fetchone()[0]
|
| 85 |
+
temporal_score = 30 if duplicate_count == 0 else -20 # New good, duplicate bad
|
| 86 |
+
|
| 87 |
+
# Digital Gravity: How many systems know am? (Simulate with phone check + random)
|
| 88 |
+
gravity_score = 25 if phone.startswith("+233") and len(phone) == 13 else -10
|
| 89 |
+
gravity_score += random.randint(-5, 5) # Simulate other systems
|
| 90 |
+
|
| 91 |
+
# Echo Stability: Consistency across devices/locations (Simulate with address + name)
|
| 92 |
+
stability_score = 20 if len(gps_address) > 5 and address_score > 0 else -15
|
| 93 |
+
|
| 94 |
+
qte_details = f"Temporal: {temporal_score} | Gravity: {gravity_score} | Stability: {stability_score}"
|
| 95 |
+
|
| 96 |
+
# Final TRS with QTE + Biometric
|
| 97 |
+
biometric_base = confidence * 0.5 if 'confidence' in locals() else 0
|
| 98 |
+
trs = biometric_base + address_score + (live_score * 100 * 0.2) + temporal_score + gravity_score + stability_score
|
| 99 |
+
|
| 100 |
+
if trs >= 70:
|
| 101 |
+
status = "AUTO-APPROVED ✅"
|
| 102 |
+
elif trs >= 40:
|
| 103 |
+
status = "MANUAL REVIEW ⚠️"
|
| 104 |
+
else:
|
| 105 |
+
status = "BLOCKED 🚫"
|
| 106 |
+
|
| 107 |
+
# Step 6: Risk Scoring (Simple, add more later)
|
| 108 |
+
risk_flag = duplicate_count > 0 or confidence < 50
|
| 109 |
+
if risk_flag:
|
| 110 |
+
status = "FLAGGED FOR REVIEW ⚠️" if "APPROVED" in status else status
|
| 111 |
+
trs -= 10
|
| 112 |
+
|
| 113 |
+
st.write(f"**Trust Resonance Score (TRS): {trs:.2f}/100**")
|
| 114 |
+
st.write(f"QTE Breakdown: {qte_details}")
|
| 115 |
+
st.write(f"Final Decision: {status}")
|
| 116 |
+
|
| 117 |
+
# Save to DB
|
| 118 |
+
c.execute("INSERT INTO verifications (name, phone, address, trs, status, qte_details) VALUES (?, ?, ?, ?, ?, ?)",
|
| 119 |
+
(full_name, phone, gps_address, trs, status, qte_details))
|
| 120 |
+
conn.commit()
|
| 121 |
+
|
| 122 |
+
with tab2:
|
| 123 |
+
st.header("Admin & Compliance Dashboard")
|
| 124 |
+
df = pd.read_sql_query("SELECT * FROM verifications ORDER BY timestamp DESC", conn)
|
| 125 |
+
st.dataframe(df, use_container_width=True)
|
| 126 |
+
|
| 127 |
+
st.subheader("Manual Override")
|
| 128 |
+
selected_id = st.number_input("Enter Verification ID to Override", min_value=1, step=1)
|
| 129 |
+
new_status = st.selectbox("New Status", ["APPROVED ✅", "MANUAL REVIEW ⚠️", "BLOCKED 🚫"])
|
| 130 |
+
|
| 131 |
+
if st.button("Apply Override"):
|
| 132 |
+
if selected_id:
|
| 133 |
+
c.execute("UPDATE verifications SET status=? WHERE id=?", (new_status, selected_id))
|
| 134 |
+
conn.commit()
|
| 135 |
+
st.success(f"ID {selected_id} updated to {new_status}")
|
| 136 |
+
st.experimental_rerun() # Refresh table
|
| 137 |
+
else:
|
| 138 |
+
st.warning("Enter valid ID")
|
| 139 |
+
|
| 140 |
+
st.write(f"Total Verifications: {len(df)} | Flagged/Blocked: {len(df[df['status'].str.contains('REVIEW|BLOCKED|ERROR')])}")
|