MySafeCode's picture
Update app.py
bf4134a verified
import gradio as gr
import requests
import json
import os
def get_balance():
"""
Fetch balance from Bineric API using API key from secrets
"""
try:
# Get API key from secrets - Gradio will look for "Key" in the secrets tab
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
}
# Make API request
response = requests.get(
'https://api.bineric.com/api/v1/monitoring/balance',
headers=headers
)
# Check if request was successful
response.raise_for_status()
# Parse and format the response
balance_data = response.json()
# Format the JSON response for better readability
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)}"
# Create Gradio interface
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.")
# Add some information about setup
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 area
output_text = gr.Textbox(
label="API Response",
placeholder="Click 'Get Balance' to see the response...",
lines=20
)
# Button to trigger the API call
get_balance_btn = gr.Button("🔄 Get Balance", variant="primary")
# Connect button to function
get_balance_btn.click(
fn=get_balance,
inputs=[],
outputs=output_text
)
# Add a clear button
clear_btn = gr.Button("🗑️ Clear Output")
clear_btn.click(lambda: "", None, output_text)
# Add some examples or additional info
gr.Markdown("""
---
**Note:** This app uses the Bineric API monitoring endpoint to fetch balance information.
Make sure your API key has the necessary permissions.
""")
# Launch the app
if __name__ == "__main__":
demo.launch()