Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
| 1 |
# Import necessary libraries
|
| 2 |
import streamlit as st
|
| 3 |
import requests
|
|
@@ -6,7 +8,6 @@ import uuid
|
|
| 6 |
import os
|
| 7 |
import hashlib
|
| 8 |
from datetime import datetime, date, timedelta
|
| 9 |
-
from dotenv import load_dotenv
|
| 10 |
import pandas as pd
|
| 11 |
from sklearn.ensemble import IsolationForest # For anomaly detection
|
| 12 |
from cryptography.fernet import Fernet # For encryption
|
|
@@ -24,9 +25,18 @@ from huggingface_hub import HfApi, HfFolder, Repository # For Hugging Face API
|
|
| 24 |
from requests.auth import HTTPBasicAuth
|
| 25 |
from oauthlib.oauth1 import Client # For MasterCard OAuth1
|
| 26 |
import json
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
# Set page config
|
| 32 |
st.set_page_config(
|
|
@@ -37,7 +47,6 @@ st.set_page_config(
|
|
| 37 |
|
| 38 |
# Load environment variables securely
|
| 39 |
def load_env_vars():
|
| 40 |
-
load_dotenv()
|
| 41 |
env_vars = os.environ
|
| 42 |
return env_vars
|
| 43 |
|
|
@@ -90,27 +99,30 @@ if huggingface_token:
|
|
| 90 |
else:
|
| 91 |
huggingface_token = HfFolder.get_token()
|
| 92 |
|
| 93 |
-
# Initialize Plaid client
|
| 94 |
-
|
| 95 |
-
from plaid.
|
| 96 |
-
from plaid.
|
| 97 |
-
from plaid.
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
plaid_config = Configuration(
|
| 106 |
-
host=plaid_environment.get(plaid_env, plaid.Environment.Sandbox),
|
| 107 |
-
api_key={
|
| 108 |
-
'clientId': plaid_client_id,
|
| 109 |
-
'secret': plaid_secret,
|
| 110 |
}
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
# JWT Authentication
|
| 116 |
def authenticate_user():
|
|
@@ -161,109 +173,112 @@ else:
|
|
| 161 |
if service_choice == "In-Store Scanner & Payment":
|
| 162 |
st.header("In-Store Item Scanner and Payment")
|
| 163 |
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
# Function to get item details
|
| 172 |
-
def get_item_details(code):
|
| 173 |
-
return ITEM_DATABASE.get(code)
|
| 174 |
-
|
| 175 |
-
# Video transformer for barcode scanning
|
| 176 |
-
class BarcodeScanner(VideoTransformerBase):
|
| 177 |
-
def __init__(self):
|
| 178 |
-
self.result = None
|
| 179 |
-
|
| 180 |
-
def transform(self, frame):
|
| 181 |
-
image = frame.to_ndarray(format="bgr24")
|
| 182 |
-
barcodes = pyzbar.decode(image)
|
| 183 |
-
|
| 184 |
-
for barcode in barcodes:
|
| 185 |
-
(x, y, w, h) = barcode.rect
|
| 186 |
-
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
|
| 187 |
-
|
| 188 |
-
barcode_data = barcode.data.decode("utf-8")
|
| 189 |
-
barcode_type = barcode.type
|
| 190 |
-
|
| 191 |
-
text = f"{barcode_data} ({barcode_type})"
|
| 192 |
-
cv2.putText(image, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
| 193 |
-
self.result = barcode_data
|
| 194 |
-
|
| 195 |
-
return image
|
| 196 |
-
|
| 197 |
-
st.write("Scan an item to view details and purchase.")
|
| 198 |
-
|
| 199 |
-
# Start barcode scanner
|
| 200 |
-
barcode_scanner = BarcodeScanner()
|
| 201 |
-
webrtc_ctx = webrtc_streamer(
|
| 202 |
-
key="barcode-scanner",
|
| 203 |
-
video_transformer_factory=lambda: barcode_scanner,
|
| 204 |
-
async_transform=True,
|
| 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 |
else:
|
| 234 |
-
st.error("
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
credentials = f"{mt_organization_id}:{mt_api_key}"
|
| 239 |
-
base64_credentials = base64.b64encode(credentials.encode()).decode('utf-8')
|
| 240 |
-
headers = {
|
| 241 |
-
"Authorization": f"Basic {base64_credentials}",
|
| 242 |
-
"Content-Type": "application/json",
|
| 243 |
-
"Accept": "application/json"
|
| 244 |
-
}
|
| 245 |
-
data = {
|
| 246 |
-
"description": description,
|
| 247 |
-
"status": "posted",
|
| 248 |
-
"ledger_entries": [
|
| 249 |
-
{
|
| 250 |
-
"amount": int(amount * 100), # Convert to cents
|
| 251 |
-
"direction": "debit",
|
| 252 |
-
"ledger_account_id": "ledger_account_id_debit", # Replace with your ledger account ID
|
| 253 |
-
},
|
| 254 |
-
{
|
| 255 |
-
"amount": int(amount * 100),
|
| 256 |
-
"direction": "credit",
|
| 257 |
-
"ledger_account_id": "ledger_account_id_credit", # Replace with your ledger account ID
|
| 258 |
-
},
|
| 259 |
-
]
|
| 260 |
-
}
|
| 261 |
-
response = requests.post(url, headers=headers, json=data)
|
| 262 |
-
if response.status_code in [200, 201]:
|
| 263 |
-
return response.json()
|
| 264 |
-
else:
|
| 265 |
-
st.error(f"Modern Treasury Error: {response.text}")
|
| 266 |
-
return None
|
| 267 |
|
| 268 |
# Citibank Services
|
| 269 |
elif service_choice == "Citibank Services":
|
|
@@ -384,8 +399,8 @@ else:
|
|
| 384 |
# Plaid Services
|
| 385 |
elif service_choice == "Plaid Services":
|
| 386 |
st.header("Plaid Services")
|
| 387 |
-
if not
|
| 388 |
-
st.error("
|
| 389 |
else:
|
| 390 |
plaid_action = st.selectbox("Choose an Action", [
|
| 391 |
"Link Account",
|
|
@@ -769,7 +784,6 @@ else:
|
|
| 769 |
st.error(f"Error: {response.text}")
|
| 770 |
elif visa_action == "Initiate Payment":
|
| 771 |
st.subheader("Initiate a Payment")
|
| 772 |
-
# Implement payment initiation with Visa API
|
| 773 |
st.info("Payment initiation with Visa API is not implemented in this example.")
|
| 774 |
|
| 775 |
# MasterCard Services
|
|
@@ -810,7 +824,6 @@ else:
|
|
| 810 |
st.error(f"Error: {response.text}")
|
| 811 |
elif mastercard_action == "Process Payment":
|
| 812 |
st.subheader("Process a Payment")
|
| 813 |
-
# Implement payment processing with MasterCard API
|
| 814 |
st.info("Payment processing with MasterCard API is not implemented in this example.")
|
| 815 |
|
| 816 |
# Pipedream Integration
|
|
@@ -926,4 +939,4 @@ else:
|
|
| 926 |
|
| 927 |
# Display footer
|
| 928 |
st.sidebar.markdown("Powered by Streamlit")
|
| 929 |
-
st.sidebar.markdown("Made by Citibank Demo Business Inc.")
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
# Import necessary libraries
|
| 4 |
import streamlit as st
|
| 5 |
import requests
|
|
|
|
| 8 |
import os
|
| 9 |
import hashlib
|
| 10 |
from datetime import datetime, date, timedelta
|
|
|
|
| 11 |
import pandas as pd
|
| 12 |
from sklearn.ensemble import IsolationForest # For anomaly detection
|
| 13 |
from cryptography.fernet import Fernet # For encryption
|
|
|
|
| 25 |
from requests.auth import HTTPBasicAuth
|
| 26 |
from oauthlib.oauth1 import Client # For MasterCard OAuth1
|
| 27 |
import json
|
| 28 |
+
|
| 29 |
+
# Optional imports for barcode scanning
|
| 30 |
+
try:
|
| 31 |
+
import cv2
|
| 32 |
+
from pyzbar import pyzbar
|
| 33 |
+
from streamlit_webrtc import webrtc_streamer, VideoTransformerBase
|
| 34 |
+
except ImportError:
|
| 35 |
+
st.warning("OpenCV and pyzbar are not available. Barcode scanning will be disabled.")
|
| 36 |
+
cv2 = None
|
| 37 |
+
pyzbar = None
|
| 38 |
+
webrtc_streamer = None
|
| 39 |
+
VideoTransformerBase = object
|
| 40 |
|
| 41 |
# Set page config
|
| 42 |
st.set_page_config(
|
|
|
|
| 47 |
|
| 48 |
# Load environment variables securely
|
| 49 |
def load_env_vars():
|
|
|
|
| 50 |
env_vars = os.environ
|
| 51 |
return env_vars
|
| 52 |
|
|
|
|
| 99 |
else:
|
| 100 |
huggingface_token = HfFolder.get_token()
|
| 101 |
|
| 102 |
+
# Initialize Plaid client if credentials are provided
|
| 103 |
+
if plaid_client_id and plaid_secret:
|
| 104 |
+
from plaid.api import plaid_api
|
| 105 |
+
from plaid.model import *
|
| 106 |
+
from plaid.api_client import ApiClient
|
| 107 |
+
from plaid.configuration import Configuration
|
| 108 |
+
|
| 109 |
+
plaid_environment = {
|
| 110 |
+
'sandbox': plaid.Environment.Sandbox,
|
| 111 |
+
'development': plaid.Environment.Development,
|
| 112 |
+
'production': plaid.Environment.Production,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
}
|
| 114 |
+
|
| 115 |
+
plaid_config = Configuration(
|
| 116 |
+
host=plaid_environment.get(plaid_env, plaid.Environment.Sandbox),
|
| 117 |
+
api_key={
|
| 118 |
+
'clientId': plaid_client_id,
|
| 119 |
+
'secret': plaid_secret,
|
| 120 |
+
}
|
| 121 |
+
)
|
| 122 |
+
plaid_api_client = ApiClient(plaid_config)
|
| 123 |
+
plaid_client = plaid_api.PlaidApi(plaid_api_client)
|
| 124 |
+
else:
|
| 125 |
+
plaid_client = None
|
| 126 |
|
| 127 |
# JWT Authentication
|
| 128 |
def authenticate_user():
|
|
|
|
| 173 |
if service_choice == "In-Store Scanner & Payment":
|
| 174 |
st.header("In-Store Item Scanner and Payment")
|
| 175 |
|
| 176 |
+
if cv2 and pyzbar and webrtc_streamer and VideoTransformerBase:
|
| 177 |
+
# Mock database of items
|
| 178 |
+
ITEM_DATABASE = {
|
| 179 |
+
"123456789012": {"name": "Product A", "price": 9.99},
|
| 180 |
+
"987654321098": {"name": "Product B", "price": 19.99},
|
| 181 |
+
# Add more items as needed
|
| 182 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
|
| 184 |
+
# Function to get item details
|
| 185 |
+
def get_item_details(code):
|
| 186 |
+
return ITEM_DATABASE.get(code)
|
| 187 |
+
|
| 188 |
+
# Video transformer for barcode scanning
|
| 189 |
+
class BarcodeScanner(VideoTransformerBase):
|
| 190 |
+
def __init__(self):
|
| 191 |
+
self.result = None
|
| 192 |
+
|
| 193 |
+
def transform(self, frame):
|
| 194 |
+
image = frame.to_ndarray(format="bgr24")
|
| 195 |
+
barcodes = pyzbar.decode(image)
|
| 196 |
+
|
| 197 |
+
for barcode in barcodes:
|
| 198 |
+
(x, y, w, h) = barcode.rect
|
| 199 |
+
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
|
| 200 |
+
|
| 201 |
+
barcode_data = barcode.data.decode("utf-8")
|
| 202 |
+
barcode_type = barcode.type
|
| 203 |
+
|
| 204 |
+
text = f"{barcode_data} ({barcode_type})"
|
| 205 |
+
cv2.putText(image, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
| 206 |
+
self.result = barcode_data
|
| 207 |
+
|
| 208 |
+
return image
|
| 209 |
+
|
| 210 |
+
st.write("Scan an item to view details and purchase.")
|
| 211 |
+
|
| 212 |
+
# Start barcode scanner
|
| 213 |
+
barcode_scanner = BarcodeScanner()
|
| 214 |
+
webrtc_ctx = webrtc_streamer(
|
| 215 |
+
key="barcode-scanner",
|
| 216 |
+
video_transformer_factory=lambda: barcode_scanner,
|
| 217 |
+
async_transform=True,
|
| 218 |
+
)
|
| 219 |
+
|
| 220 |
+
if webrtc_ctx.video_transformer:
|
| 221 |
+
barcode_data = barcode_scanner.result
|
| 222 |
+
if barcode_data:
|
| 223 |
+
item_details = get_item_details(barcode_data)
|
| 224 |
+
if item_details:
|
| 225 |
+
st.success(f"Item Scanned: {item_details['name']}")
|
| 226 |
+
st.write(f"Price: ${item_details['price']:.2f}")
|
| 227 |
+
|
| 228 |
+
if st.button("Purchase"):
|
| 229 |
+
# Process payment
|
| 230 |
+
try:
|
| 231 |
+
# In a real application, collect payment details securely
|
| 232 |
+
# For demonstration, we'll simulate a successful payment
|
| 233 |
+
st.write("Payment processed successfully.")
|
| 234 |
+
|
| 235 |
+
# Update Modern Treasury ledger
|
| 236 |
+
mt_response = create_mt_ledger_transaction(
|
| 237 |
+
amount=item_details['price'],
|
| 238 |
+
description=f"Purchase of {item_details['name']}"
|
| 239 |
+
)
|
| 240 |
+
if mt_response:
|
| 241 |
+
st.write("Ledger updated successfully.")
|
| 242 |
+
else:
|
| 243 |
+
st.write("Failed to update ledger.")
|
| 244 |
+
except Exception as e:
|
| 245 |
+
st.error(f"Error processing payment: {e}")
|
| 246 |
+
else:
|
| 247 |
+
st.error("Item not found in database.")
|
| 248 |
|
| 249 |
+
def create_mt_ledger_transaction(amount, description):
|
| 250 |
+
url = "https://app.moderntreasury.com/api/ledger_transactions"
|
| 251 |
+
credentials = f"{mt_organization_id}:{mt_api_key}"
|
| 252 |
+
base64_credentials = base64.b64encode(credentials.encode()).decode('utf-8')
|
| 253 |
+
headers = {
|
| 254 |
+
"Authorization": f"Basic {base64_credentials}",
|
| 255 |
+
"Content-Type": "application/json",
|
| 256 |
+
"Accept": "application/json"
|
| 257 |
+
}
|
| 258 |
+
data = {
|
| 259 |
+
"description": description,
|
| 260 |
+
"status": "posted",
|
| 261 |
+
"ledger_entries": [
|
| 262 |
+
{
|
| 263 |
+
"amount": int(amount * 100), # Convert to cents
|
| 264 |
+
"direction": "debit",
|
| 265 |
+
"ledger_account_id": "ledger_account_id_debit", # Replace with your ledger account ID
|
| 266 |
+
},
|
| 267 |
+
{
|
| 268 |
+
"amount": int(amount * 100),
|
| 269 |
+
"direction": "credit",
|
| 270 |
+
"ledger_account_id": "ledger_account_id_credit", # Replace with your ledger account ID
|
| 271 |
+
},
|
| 272 |
+
]
|
| 273 |
+
}
|
| 274 |
+
response = requests.post(url, headers=headers, json=data)
|
| 275 |
+
if response.status_code in [200, 201]:
|
| 276 |
+
return response.json()
|
| 277 |
else:
|
| 278 |
+
st.error(f"Modern Treasury Error: {response.text}")
|
| 279 |
+
return None
|
| 280 |
+
else:
|
| 281 |
+
st.error("Barcode scanning features are not available in this environment.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 282 |
|
| 283 |
# Citibank Services
|
| 284 |
elif service_choice == "Citibank Services":
|
|
|
|
| 399 |
# Plaid Services
|
| 400 |
elif service_choice == "Plaid Services":
|
| 401 |
st.header("Plaid Services")
|
| 402 |
+
if not plaid_client:
|
| 403 |
+
st.error("Plaid API credentials are not configured.")
|
| 404 |
else:
|
| 405 |
plaid_action = st.selectbox("Choose an Action", [
|
| 406 |
"Link Account",
|
|
|
|
| 784 |
st.error(f"Error: {response.text}")
|
| 785 |
elif visa_action == "Initiate Payment":
|
| 786 |
st.subheader("Initiate a Payment")
|
|
|
|
| 787 |
st.info("Payment initiation with Visa API is not implemented in this example.")
|
| 788 |
|
| 789 |
# MasterCard Services
|
|
|
|
| 824 |
st.error(f"Error: {response.text}")
|
| 825 |
elif mastercard_action == "Process Payment":
|
| 826 |
st.subheader("Process a Payment")
|
|
|
|
| 827 |
st.info("Payment processing with MasterCard API is not implemented in this example.")
|
| 828 |
|
| 829 |
# Pipedream Integration
|
|
|
|
| 939 |
|
| 940 |
# Display footer
|
| 941 |
st.sidebar.markdown("Powered by Streamlit")
|
| 942 |
+
st.sidebar.markdown("Made by Citibank Demo Business Inc.")
|