Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,65 +1,109 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import subprocess
|
| 3 |
-
import time
|
| 4 |
-
import yaml
|
| 5 |
import gradio as gr
|
| 6 |
from aibanking import Jocall3
|
| 7 |
|
| 8 |
-
# 1.
|
| 9 |
with open(".stats.yml", 'r') as f:
|
| 10 |
stats = yaml.safe_load(f)
|
| 11 |
OPENAPI_URL = stats.get('openapi_spec_url')
|
| 12 |
|
| 13 |
-
# 2. Start Prism in the background on port 4010
|
| 14 |
print(f"Starting Prism Mock Server...")
|
| 15 |
-
subprocess.Popen([
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
# 3. Setup the SDK Client
|
| 25 |
-
# It handles the 'x-api-key' header automatically
|
| 26 |
-
client = Jocall3(
|
| 27 |
-
base_url="http://127.0.0.1:4010",
|
| 28 |
-
api_key="mock-key-123"
|
| 29 |
-
)
|
| 30 |
-
|
| 31 |
-
# 4. App Functions
|
| 32 |
-
def check_status():
|
| 33 |
try:
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
return
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
|
|
|
| 40 |
try:
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
except Exception as e:
|
| 48 |
-
return f"❌ Error: {str(e)}"
|
| 49 |
|
| 50 |
-
#
|
| 51 |
-
with gr.Blocks(
|
| 52 |
-
gr.Markdown("#
|
| 53 |
-
gr.Markdown("
|
| 54 |
|
| 55 |
with gr.Row():
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
|
| 64 |
if __name__ == "__main__":
|
| 65 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
+
import os, subprocess, time, yaml, json
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from aibanking import Jocall3
|
| 4 |
|
| 5 |
+
# 1. Start Prism (using the spec URL in your .stats.yml)
|
| 6 |
with open(".stats.yml", 'r') as f:
|
| 7 |
stats = yaml.safe_load(f)
|
| 8 |
OPENAPI_URL = stats.get('openapi_spec_url')
|
| 9 |
|
|
|
|
| 10 |
print(f"Starting Prism Mock Server...")
|
| 11 |
+
subprocess.Popen(["prism", "mock", OPENAPI_URL, "-p", "4010", "-h", "0.0.0.0"])
|
| 12 |
+
time.sleep(5)
|
| 13 |
+
|
| 14 |
+
# 2. Setup SDK Client
|
| 15 |
+
client = Jocall3(base_url="http://127.0.0.1:4010", api_key="demo-key")
|
| 16 |
+
|
| 17 |
+
# --- Helper for pretty printing ---
|
| 18 |
+
def format_res(data):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
try:
|
| 20 |
+
# If it's a Pydantic model (SDK response), convert to dict
|
| 21 |
+
if hasattr(data, 'to_dict'):
|
| 22 |
+
return json.dumps(data.to_dict(), indent=2)
|
| 23 |
+
return json.dumps(data, indent=2)
|
| 24 |
+
except:
|
| 25 |
+
return str(data)
|
| 26 |
|
| 27 |
+
# --- Resource Handlers ---
|
| 28 |
+
def api_router(category, action, param1=None, param2=None):
|
| 29 |
try:
|
| 30 |
+
# SYSTEM
|
| 31 |
+
if category == "System":
|
| 32 |
+
if action == "Status": return format_res(client.system.get_status())
|
| 33 |
+
if action == "Audit Logs": return format_res(client.system.get_audit_logs(limit=5))
|
| 34 |
+
if action == "Webhooks": return format_res(client.system.webhooks.list())
|
| 35 |
+
|
| 36 |
+
# USERS
|
| 37 |
+
if category == "Users":
|
| 38 |
+
if action == "Me": return format_res(client.users.me.retrieve())
|
| 39 |
+
if action == "Preferences": return format_res(client.users.me.preferences.retrieve())
|
| 40 |
+
if action == "Devices": return format_res(client.users.me.devices.list())
|
| 41 |
+
if action == "Security Log": return format_res(client.users.me.security.retrieve_log())
|
| 42 |
+
|
| 43 |
+
# ACCOUNTS
|
| 44 |
+
if category == "Accounts":
|
| 45 |
+
if action == "List All": return format_res(client.accounts.retrieve_me())
|
| 46 |
+
if action == "Details": return format_res(client.accounts.retrieve_details(param1 or "acc_123"))
|
| 47 |
+
if action == "History": return format_res(client.accounts.retrieve_balance_history(param1 or "acc_123"))
|
| 48 |
+
|
| 49 |
+
# AI ORACLE
|
| 50 |
+
if category == "AI Oracle":
|
| 51 |
+
if action == "Market Crash Prob": return format_res(client.ai.oracle.predictions.retrieve_market_crash_probability())
|
| 52 |
+
if action == "Inflation": return format_res(client.ai.oracle.predictions.retrieve_inflation(region=param1 or "GLOBAL"))
|
| 53 |
+
if action == "Simulate": return format_res(client.ai.oracle.simulate.create(prompt=param1 or "Recession scenario"))
|
| 54 |
+
|
| 55 |
+
# CORPORATE
|
| 56 |
+
if category == "Corporate":
|
| 57 |
+
if action == "Risk Exposure": return format_res(client.corporate.risk.get_risk_exposure())
|
| 58 |
+
if action == "List Cards": return format_res(client.corporate.cards.list_all())
|
| 59 |
+
if action == "Anomalies": return format_res(client.corporate.anomalies.list_detected())
|
| 60 |
+
if action == "Liquidity": return format_res(client.corporate.treasury.get_liquidity_positions())
|
| 61 |
+
|
| 62 |
+
# WEB3
|
| 63 |
+
if category == "Web3":
|
| 64 |
+
if action == "Wallets": return format_res(client.web3.wallets.list())
|
| 65 |
+
if action == "Network": return format_res(client.web3.network.get_status())
|
| 66 |
+
if action == "NFTs": return format_res(client.web3.nfts.list())
|
| 67 |
+
|
| 68 |
+
# PAYMENTS / FX
|
| 69 |
+
if category == "Payments":
|
| 70 |
+
if action == "FX Rates": return format_res(client.payments.fx.get_rates(pair=param1 or "EURUSD"))
|
| 71 |
+
if action == "List Payments": return format_res(client.payments.list())
|
| 72 |
+
|
| 73 |
+
return "Action not implemented in UI yet, but endpoint is live in Prism."
|
| 74 |
except Exception as e:
|
| 75 |
+
return f"❌ SDK Error: {str(e)}"
|
| 76 |
|
| 77 |
+
# --- UI Layout ---
|
| 78 |
+
with gr.Blocks(title="Jocall3 Full API Explorer") as demo:
|
| 79 |
+
gr.Markdown("# 🏛️ Jocall3 AI Banking: 152 Endpoints Live")
|
| 80 |
+
gr.Markdown("Select a category to test the SDK against the Prism Mock Server.")
|
| 81 |
|
| 82 |
with gr.Row():
|
| 83 |
+
with gr.Column(scale=1):
|
| 84 |
+
cat = gr.Radio(["System", "Users", "Accounts", "AI Oracle", "Corporate", "Web3", "Payments"], label="Resource Category")
|
| 85 |
+
act = gr.Dropdown(choices=[], label="Action / Endpoint")
|
| 86 |
+
p1 = gr.Textbox(label="Parameter 1 (ID/Prompt/Region)", visible=True)
|
| 87 |
+
run_btn = gr.Button("Execute Request", variant="primary")
|
| 88 |
+
|
| 89 |
+
with gr.Column(scale=2):
|
| 90 |
+
output = gr.Code(label="SDK JSON Response", language="json", lines=25)
|
| 91 |
+
|
| 92 |
+
# Dynamic Dropdown Logic
|
| 93 |
+
def update_actions(category):
|
| 94 |
+
actions = {
|
| 95 |
+
"System": ["Status", "Audit Logs", "Webhooks"],
|
| 96 |
+
"Users": ["Me", "Preferences", "Devices", "Security Log"],
|
| 97 |
+
"Accounts": ["List All", "Details", "History"],
|
| 98 |
+
"AI Oracle": ["Market Crash Prob", "Inflation", "Simulate"],
|
| 99 |
+
"Corporate": ["Risk Exposure", "List Cards", "Anomalies", "Liquidity"],
|
| 100 |
+
"Web3": ["Wallets", "Network", "NFTs"],
|
| 101 |
+
"Payments": ["FX Rates", "List Payments"]
|
| 102 |
+
}
|
| 103 |
+
return gr.Dropdown(choices=actions.get(category, []))
|
| 104 |
|
| 105 |
+
cat.change(update_actions, inputs=cat, outputs=act)
|
| 106 |
+
run_btn.click(api_router, inputs=[cat, act, p1], outputs=output)
|
| 107 |
|
| 108 |
if __name__ == "__main__":
|
| 109 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|