|
|
import gradio as gr |
|
|
import requests |
|
|
import json |
|
|
import os |
|
|
|
|
|
def get_balance(): |
|
|
""" |
|
|
Fetch balance from Bineric API using API key from secrets |
|
|
""" |
|
|
try: |
|
|
|
|
|
api_key = os.environ.get("Key") |
|
|
|
|
|
|
|
|
if not api_key or api_key == "YOUR_API_KEY": |
|
|
return "❌ API Key not found. Please add your API key in the 'Secrets' tab (Key = 'YOUR_API_KEY')" |
|
|
|
|
|
headers = { |
|
|
'api-key': api_key |
|
|
} |
|
|
|
|
|
|
|
|
response = requests.get( |
|
|
'https://api.bineric.com/api/v1/monitoring/balance', |
|
|
headers=headers |
|
|
) |
|
|
|
|
|
|
|
|
response.raise_for_status() |
|
|
|
|
|
|
|
|
balance_data = response.json() |
|
|
|
|
|
|
|
|
formatted_response = json.dumps(balance_data, indent=2) |
|
|
|
|
|
return f"✅ Balance retrieved successfully:\n\n{formatted_response}" |
|
|
|
|
|
except requests.exceptions.RequestException as e: |
|
|
return f"❌ Error making API request: {str(e)}" |
|
|
except json.JSONDecodeError as e: |
|
|
return f"❌ Error parsing response: {str(e)}" |
|
|
except Exception as e: |
|
|
return f"❌ Unexpected error: {str(e)}" |
|
|
|
|
|
|
|
|
with gr.Blocks(title="Bineric Balance Checker") as demo: |
|
|
gr.Markdown("# 🔍 Bineric API Balance Checker") |
|
|
|
|
|
gr.Markdown("Click the button below to fetch your balance using the API key from Secrets.") |
|
|
|
|
|
|
|
|
with gr.Accordion("ℹ️ Setup Instructions", open=False): |
|
|
gr.Markdown(""" |
|
|
1. **Add your API key in Gradio Secrets:** |
|
|
- Go to the "Secrets" tab |
|
|
- Add a new secret with: |
|
|
- Key: `Key` |
|
|
- Value: `YOUR_API_KEY_HERE` |
|
|
|
|
|
2. **Click the button below to fetch balance** |
|
|
""") |
|
|
|
|
|
|
|
|
output_text = gr.Textbox( |
|
|
label="API Response", |
|
|
placeholder="Click 'Get Balance' to see the response...", |
|
|
lines=20 |
|
|
) |
|
|
|
|
|
|
|
|
get_balance_btn = gr.Button("🔄 Get Balance", variant="primary") |
|
|
|
|
|
|
|
|
get_balance_btn.click( |
|
|
fn=get_balance, |
|
|
inputs=[], |
|
|
outputs=output_text |
|
|
) |
|
|
|
|
|
|
|
|
clear_btn = gr.Button("🗑️ Clear Output") |
|
|
clear_btn.click(lambda: "", None, output_text) |
|
|
|
|
|
|
|
|
gr.Markdown(""" |
|
|
--- |
|
|
**Note:** This app uses the Bineric API monitoring endpoint to fetch balance information. |
|
|
Make sure your API key has the necessary permissions. |
|
|
""") |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo.launch() |