Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,84 +7,116 @@ 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"π
|
| 11 |
-
# Start Prism as a background process
|
| 12 |
subprocess.Popen(["prism", "mock", OPENAPI_URL, "-p", "4010", "-h", "0.0.0.0"])
|
| 13 |
|
| 14 |
-
# 2.
|
| 15 |
-
max_retries =
|
| 16 |
prism_ready = False
|
| 17 |
for i in range(max_retries):
|
| 18 |
try:
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
print("β
Prism is
|
| 22 |
prism_ready = True
|
| 23 |
break
|
| 24 |
except:
|
| 25 |
print(f"β³ Waiting for Prism... ({i+1}/{max_retries})")
|
| 26 |
time.sleep(2)
|
| 27 |
|
| 28 |
-
if not prism_ready:
|
| 29 |
-
print("β Prism failed to start. App might crash.")
|
| 30 |
-
|
| 31 |
# 3. Setup Client
|
| 32 |
-
client = Jocall3(base_url="http://127.0.0.1:4010", api_key="
|
| 33 |
|
| 34 |
-
#
|
| 35 |
RESOURCES = {
|
| 36 |
"Accounts": client.accounts,
|
|
|
|
|
|
|
|
|
|
| 37 |
"AI - Oracle": client.ai.oracle,
|
|
|
|
|
|
|
| 38 |
"AI - Ads": client.ai.ads,
|
|
|
|
| 39 |
"AI - Incubator": client.ai.incubator,
|
|
|
|
| 40 |
"AI - Advisor": client.ai.advisor,
|
|
|
|
|
|
|
| 41 |
"AI - Agent": client.ai.agent,
|
|
|
|
| 42 |
"Corporate": client.corporate,
|
|
|
|
|
|
|
| 43 |
"Corporate - Risk": client.corporate.risk,
|
| 44 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
"Payments": client.payments,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
"System": client.system,
|
|
|
|
|
|
|
| 47 |
"Transactions": client.transactions,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
}
|
| 49 |
|
| 50 |
def get_methods(res_name):
|
| 51 |
res = RESOURCES.get(res_name)
|
| 52 |
methods = [m for m, _ in inspect.getmembers(res, predicate=inspect.ismethod) if not m.startswith('_')]
|
| 53 |
-
return gr.Dropdown(choices=methods)
|
| 54 |
|
| 55 |
-
def
|
| 56 |
res = RESOURCES.get(res_name)
|
| 57 |
method = getattr(res, method_name)
|
| 58 |
sig = inspect.signature(method)
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
def
|
| 63 |
try:
|
| 64 |
res = RESOURCES.get(res_name)
|
| 65 |
method = getattr(res, method_name)
|
| 66 |
kwargs = json.loads(params_json) if params_json.strip() else {}
|
| 67 |
output = method(**kwargs)
|
| 68 |
if hasattr(output, 'to_dict'): return json.dumps(output.to_dict(), indent=2)
|
| 69 |
-
return json.dumps(output, indent=2) if output is not None else "β
Success
|
| 70 |
except Exception as e:
|
| 71 |
-
return f"β Error: {str(e)}"
|
| 72 |
|
| 73 |
-
#
|
| 74 |
-
with gr.Blocks(theme=gr.themes.
|
| 75 |
-
gr.Markdown("#
|
|
|
|
|
|
|
| 76 |
with gr.Row():
|
| 77 |
with gr.Column(scale=1):
|
| 78 |
-
res_choice = gr.Dropdown(list(RESOURCES.keys()), label="1. Resource")
|
| 79 |
-
method_choice = gr.Dropdown([], label="2. Method")
|
| 80 |
-
params_input = gr.TextArea(label="3. Arguments (JSON)", lines=
|
| 81 |
-
run_btn = gr.Button("π EXECUTE", variant="primary")
|
|
|
|
| 82 |
with gr.Column(scale=2):
|
| 83 |
-
output_display = gr.Code(label="
|
| 84 |
|
| 85 |
res_choice.change(get_methods, inputs=res_choice, outputs=method_choice)
|
| 86 |
-
method_choice.change(
|
| 87 |
-
run_btn.click(
|
| 88 |
|
| 89 |
if __name__ == "__main__":
|
| 90 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 7 |
stats = yaml.safe_load(f)
|
| 8 |
OPENAPI_URL = stats.get('openapi_spec_url')
|
| 9 |
|
| 10 |
+
print(f"π Launching Prism for all endpoints...")
|
|
|
|
| 11 |
subprocess.Popen(["prism", "mock", OPENAPI_URL, "-p", "4010", "-h", "0.0.0.0"])
|
| 12 |
|
| 13 |
+
# 2. FIX: Wait for a VALID endpoint (/system/status) instead of root (/)
|
| 14 |
+
max_retries = 30
|
| 15 |
prism_ready = False
|
| 16 |
for i in range(max_retries):
|
| 17 |
try:
|
| 18 |
+
# Hit a known endpoint to verify readiness
|
| 19 |
+
requests.get("http://127.0.0.1:4010/system/status", timeout=1)
|
| 20 |
+
print("β
Prism is verified healthy!")
|
| 21 |
prism_ready = True
|
| 22 |
break
|
| 23 |
except:
|
| 24 |
print(f"β³ Waiting for Prism... ({i+1}/{max_retries})")
|
| 25 |
time.sleep(2)
|
| 26 |
|
|
|
|
|
|
|
|
|
|
| 27 |
# 3. Setup Client
|
| 28 |
+
client = Jocall3(base_url="http://127.0.0.1:4010", api_key="boss-mode")
|
| 29 |
|
| 30 |
+
# 4. Map the entire SDK tree
|
| 31 |
RESOURCES = {
|
| 32 |
"Accounts": client.accounts,
|
| 33 |
+
"Accounts - Statements": client.accounts.statements,
|
| 34 |
+
"Accounts - Transactions": client.accounts.transactions,
|
| 35 |
+
"Accounts - Overdraft": client.accounts.overdraft_settings,
|
| 36 |
"AI - Oracle": client.ai.oracle,
|
| 37 |
+
"AI - Oracle Predictions": client.ai.oracle.predictions,
|
| 38 |
+
"AI - Oracle Simulations": client.ai.oracle.simulations,
|
| 39 |
"AI - Ads": client.ai.ads,
|
| 40 |
+
"AI - Ads Generate": client.ai.ads.generate,
|
| 41 |
"AI - Incubator": client.ai.incubator,
|
| 42 |
+
"AI - Incubator Pitch": client.ai.incubator.pitch,
|
| 43 |
"AI - Advisor": client.ai.advisor,
|
| 44 |
+
"AI - Advisor Chat": client.ai.advisor.chat,
|
| 45 |
+
"AI - Advisor Tools": client.ai.advisor.tools,
|
| 46 |
"AI - Agent": client.ai.agent,
|
| 47 |
+
"AI - Models": client.ai.models,
|
| 48 |
"Corporate": client.corporate,
|
| 49 |
+
"Corporate - Compliance": client.corporate.compliance,
|
| 50 |
+
"Corporate - Treasury": client.corporate.treasury,
|
| 51 |
"Corporate - Risk": client.corporate.risk,
|
| 52 |
+
"Corporate - Fraud": client.corporate.risk.fraud,
|
| 53 |
+
"Corporate - Governance": client.corporate.governance,
|
| 54 |
+
"Investments": client.investments,
|
| 55 |
+
"Investments - Portfolios": client.investments.portfolios,
|
| 56 |
+
"Lending": client.lending,
|
| 57 |
+
"Lending - Applications": client.lending.applications,
|
| 58 |
+
"Marketplace": client.marketplace,
|
| 59 |
"Payments": client.payments,
|
| 60 |
+
"Payments - Domestic": client.payments.domestic,
|
| 61 |
+
"Payments - International": client.payments.international,
|
| 62 |
+
"Payments - FX": client.payments.fx,
|
| 63 |
+
"Sustainability": client.sustainability,
|
| 64 |
"System": client.system,
|
| 65 |
+
"System - Notifications": client.system.notifications,
|
| 66 |
+
"System - Verification": client.system.verification,
|
| 67 |
"Transactions": client.transactions,
|
| 68 |
+
"Users": client.users,
|
| 69 |
+
"Users - Me": client.users.me,
|
| 70 |
+
"Web3": client.web3,
|
| 71 |
+
"Web3 - Wallets": client.web3.wallets,
|
| 72 |
}
|
| 73 |
|
| 74 |
def get_methods(res_name):
|
| 75 |
res = RESOURCES.get(res_name)
|
| 76 |
methods = [m for m, _ in inspect.getmembers(res, predicate=inspect.ismethod) if not m.startswith('_')]
|
| 77 |
+
return gr.Dropdown(choices=sorted(methods))
|
| 78 |
|
| 79 |
+
def generate_payload_template(res_name, method_name):
|
| 80 |
res = RESOURCES.get(res_name)
|
| 81 |
method = getattr(res, method_name)
|
| 82 |
sig = inspect.signature(method)
|
| 83 |
+
payload = {}
|
| 84 |
+
for name, param in sig.parameters.items():
|
| 85 |
+
if name in ['extra_headers', 'extra_query', 'extra_body', 'timeout', 'self']:
|
| 86 |
+
continue
|
| 87 |
+
# provide a smart hint based on type
|
| 88 |
+
payload[name] = "string" if param.annotation == str else 0.0
|
| 89 |
+
return json.dumps(payload, indent=2)
|
| 90 |
|
| 91 |
+
def run_api(res_name, method_name, params_json):
|
| 92 |
try:
|
| 93 |
res = RESOURCES.get(res_name)
|
| 94 |
method = getattr(res, method_name)
|
| 95 |
kwargs = json.loads(params_json) if params_json.strip() else {}
|
| 96 |
output = method(**kwargs)
|
| 97 |
if hasattr(output, 'to_dict'): return json.dumps(output.to_dict(), indent=2)
|
| 98 |
+
return json.dumps(output, indent=2) if output is not None else "β
Success: Command accepted by server."
|
| 99 |
except Exception as e:
|
| 100 |
+
return f"β SDK Error: {str(e)}"
|
| 101 |
|
| 102 |
+
# 5. The UI
|
| 103 |
+
with gr.Blocks(theme=gr.themes.Default(primary_hue="cyan")) as demo:
|
| 104 |
+
gr.Markdown("# ποΈ Jocall3 AI Banking: The Complete 152-Endpoint Console")
|
| 105 |
+
gr.Markdown("This interface dynamically inspects the `aibanking` SDK and generates requests for the local Prism Mock.")
|
| 106 |
+
|
| 107 |
with gr.Row():
|
| 108 |
with gr.Column(scale=1):
|
| 109 |
+
res_choice = gr.Dropdown(list(RESOURCES.keys()), label="1. Select API Resource Group")
|
| 110 |
+
method_choice = gr.Dropdown([], label="2. Select SDK Method")
|
| 111 |
+
params_input = gr.TextArea(label="3. Arguments (Auto-Generated JSON)", lines=12)
|
| 112 |
+
run_btn = gr.Button("π EXECUTE REMOTE CALL", variant="primary")
|
| 113 |
+
|
| 114 |
with gr.Column(scale=2):
|
| 115 |
+
output_display = gr.Code(label="Mock Server Response", language="json", lines=30)
|
| 116 |
|
| 117 |
res_choice.change(get_methods, inputs=res_choice, outputs=method_choice)
|
| 118 |
+
method_choice.change(generate_payload_template, inputs=[res_choice, method_choice], outputs=params_input)
|
| 119 |
+
run_btn.click(run_api, inputs=[res_choice, method_choice, params_input], outputs=output_display)
|
| 120 |
|
| 121 |
if __name__ == "__main__":
|
| 122 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|