Spaces:
Sleeping
Sleeping
File size: 8,240 Bytes
fc3fac6 c4d9988 2ec8814 c4d9988 2ec8814 c4d9988 2ec8814 c4d9988 2ec8814 c4d9988 2ec8814 c4d9988 2ec8814 c4d9988 2ec8814 c4d9988 2ec8814 c4d9988 2ec8814 c4d9988 2ec8814 c4d9988 2ec8814 c4d9988 2ec8814 c4d9988 2ec8814 c4d9988 2ec8814 c4d9988 2ec8814 c4d9988 fc3fac6 2ec8814 fc3fac6 c4d9988 fc3fac6 c4d9988 2ec8814 c4d9988 2ec8814 fc3fac6 2ec8814 fc3fac6 c4d9988 fc3fac6 2ec8814 fc3fac6 2ec8814 fc3fac6 c4d9988 2ec8814 c4d9988 fc3fac6 2ec8814 fc3fac6 c4d9988 fc3fac6 c4d9988 fc3fac6 2ec8814 c4d9988 2ec8814 c4d9988 fc3fac6 2ec8814 fc3fac6 c4d9988 2ec8814 fc3fac6 c4d9988 fc3fac6 c4d9988 2ec8814 fc3fac6 2ec8814 fc3fac6 c4d9988 2ec8814 c4d9988 fc3fac6 c4d9988 2ec8814 fc3fac6 2ec8814 fc3fac6 c4d9988 fc3fac6 2ec8814 fc3fac6 2ec8814 fc3fac6 c4d9988 fc3fac6 2ec8814 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
import streamlit as st
import json
import hashlib
from web3 import Web3
from google import genai
from google.genai import types
# --------------------------------------------------
# PAGE CONFIG
# --------------------------------------------------
st.set_page_config(
page_title="Invoice-Flow AI",
layout="wide",
initial_sidebar_state="collapsed"
)
# --------------------------------------------------
# PROFESSIONAL FINTECH STYLING
# --------------------------------------------------
st.markdown("""
<style>
:root {
--primary: #0f2a44;
--secondary: #1e3a8a;
--accent: #047857;
--muted: #475569;
--bg: #f5f7fb;
}
.stApp {
background-color: var(--bg);
}
.block-container {
max-width: 1200px;
padding-top: 2rem;
}
.card {
background: #ffffff;
border-radius: 14px;
padding: 1.6rem;
margin-bottom: 1.5rem;
border: 1px solid #e6eaf0;
box-shadow: 0 12px 28px rgba(15, 42, 68, 0.06);
}
h1 {
color: var(--primary);
font-weight: 700;
letter-spacing: -0.02em;
}
h2, h3 {
color: var(--primary);
font-weight: 600;
}
.caption {
color: var(--muted);
font-size: 0.95rem;
}
.badge-live {
background: #e0f2fe;
color: #0369a1;
padding: 0.35rem 0.8rem;
border-radius: 999px;
font-size: 0.75rem;
font-weight: 600;
}
.badge-success {
background: #ecfdf5;
color: #047857;
padding: 0.35rem 0.8rem;
border-radius: 999px;
font-size: 0.75rem;
font-weight: 600;
}
.badge-pending {
background: #fff7ed;
color: #9a3412;
padding: 0.35rem 0.8rem;
border-radius: 999px;
font-size: 0.75rem;
font-weight: 600;
}
.stButton > button {
background-color: var(--secondary);
color: white;
border-radius: 10px;
font-weight: 600;
border: none;
}
.stButton > button:hover {
background-color: #1e40af;
}
.stLinkButton a {
border-radius: 10px;
font-weight: 600;
}
</style>
""", unsafe_allow_html=True)
# --------------------------------------------------
# HERO / BRAND HEADER
# --------------------------------------------------
st.markdown("""
<div class="card">
<h1>Invoice-Flow AI</h1>
<p class="caption">
Agentic invoice verification & on-chain trust infrastructure for MSME finance
</p>
<span class="badge-live">HACKATHON LIVE PROTOTYPE</span>
</div>
""", unsafe_allow_html=True)
# --------------------------------------------------
# SECRETS (HF SAFE)
# --------------------------------------------------
try:
GEMINI_API_KEY = st.secrets["GEMINI_API_KEY"]
RPC_URL = st.secrets["RPC_URL"]
PRIVATE_KEY = st.secrets["PRIVATE_KEY"]
except Exception:
st.error("Missing secrets. Add them in Hugging Face → Settings → Variables.")
st.stop()
# --------------------------------------------------
# CLIENT SETUP
# --------------------------------------------------
genai_client = genai.Client(api_key=GEMINI_API_KEY)
w3 = Web3(Web3.HTTPProvider(RPC_URL))
account = w3.eth.account.from_key(PRIVATE_KEY)
CONTRACT_ADDRESS = "0x3af7C33f4D0303E6eb94c2D24fB0bD82B54c6914"
contract = w3.eth.contract(
address=Web3.to_checksum_address(CONTRACT_ADDRESS),
abi=json.load(open("abi.json"))
)
# --------------------------------------------------
# SESSION STATE
# --------------------------------------------------
for key, default in {
"invoice": None,
"approved": False,
"minted": False,
"tx_hash": None
}.items():
if key not in st.session_state:
st.session_state[key] = default
# --------------------------------------------------
# FALLBACK
# --------------------------------------------------
def mock_invoice():
return {
"seller_name": "TechFlow Solutions Pvt Ltd",
"seller_gstin": "29AAACF5724R1Z5",
"invoice_amount": 75000,
"invoice_number": "INV-2024-001"
}
# --------------------------------------------------
# MAIN LAYOUT
# --------------------------------------------------
col1, col2 = st.columns([1, 1])
# ---------------- LEFT -----------------
with col1:
st.markdown("<div class='card'>", unsafe_allow_html=True)
st.subheader("Invoice Ingestion")
st.markdown("<p class='caption'>Upload an invoice PDF for automated trust verification</p>", unsafe_allow_html=True)
uploaded = st.file_uploader("Invoice PDF", type=["pdf"])
if uploaded:
pdf_bytes = uploaded.getvalue()
if st.button("Analyze Invoice"):
with st.spinner("Extracting structured data..."):
try:
res = genai_client.models.generate_content(
model="gemini-1.5-flash",
contents=[
types.Part.from_bytes(pdf_bytes, "application/pdf"),
"Extract invoice as JSON with keys seller_name, seller_gstin, invoice_amount, invoice_number. Return ONLY JSON."
]
)
clean = res.text.replace("```json", "").replace("```", "").strip()
st.session_state.invoice = json.loads(clean)
except Exception:
st.warning("AI extraction failed. Using demo-safe fallback.")
st.session_state.invoice = mock_invoice()
st.markdown("</div>", unsafe_allow_html=True)
# ---------------- RIGHT ----------------
with col2:
if st.session_state.invoice:
st.markdown("<div class='card'>", unsafe_allow_html=True)
st.subheader("Verification Summary")
st.json(st.session_state.invoice)
st.markdown("</div>", unsafe_allow_html=True)
st.markdown("<div class='card'>", unsafe_allow_html=True)
st.subheader("Buyer Consent")
if not st.session_state.approved:
st.markdown("<span class='badge-pending'>Awaiting Buyer Confirmation</span>", unsafe_allow_html=True)
if st.button("Approve Invoice"):
st.session_state.approved = True
st.rerun()
else:
st.markdown("<span class='badge-success'>Buyer Approved</span>", unsafe_allow_html=True)
st.markdown("</div>", unsafe_allow_html=True)
if st.session_state.approved:
st.markdown("<div class='card'>", unsafe_allow_html=True)
st.subheader("On-Chain Settlement")
if not st.session_state.minted:
if st.button("Mint Verified Receipt"):
with st.spinner("Finalizing on blockchain..."):
inv = st.session_state.invoice
payload = f"{inv['invoice_number']}{inv['seller_gstin']}{inv['invoice_amount']}"
receipt_hash = hashlib.sha256(payload.encode()).hexdigest()
tx = contract.functions.mintReceipt(
Web3.to_bytes(hexstr="0x" + receipt_hash),
inv["seller_gstin"],
int(inv["invoice_amount"])
).build_transaction({
"from": account.address,
"nonce": w3.eth.get_transaction_count(account.address),
"gas": 250000,
"gasPrice": w3.eth.gas_price,
"chainId": 80002
})
signed = w3.eth.account.sign_transaction(tx, PRIVATE_KEY)
tx_hash = w3.eth.send_raw_transaction(signed.raw_transaction)
st.session_state.tx_hash = w3.to_hex(tx_hash)
st.session_state.minted = True
st.rerun()
if st.session_state.minted:
st.success("Receipt minted successfully")
st.link_button(
"View Transaction on PolygonScan",
f"https://amoy.polygonscan.com/tx/{st.session_state.tx_hash}"
)
st.markdown("</div>", unsafe_allow_html=True)
# --------------------------------------------------
# FOOTER
# --------------------------------------------------
st.markdown("---")
st.caption(
"Invoice-Flow AI • Trust Infrastructure for MSME Finance • "
"Gemini 1.5 Flash • Polygon Amoy"
)
|