EricSam's picture
Update app.py
1b65420 verified
raw
history blame
1.84 kB
import gradio as gr
from transformers import pipeline
import hmac
import hashlib
import requests
import time
import os
from datetime import datetime
# Initialize hotdog classification pipeline with error handling
hotdog_pipeline = None
try:
hotdog_pipeline = pipeline(task="image-classification", model="julien-c/hotdog-not-hotdog")
except RuntimeError as e:
print(f"Warning: Hotdog classifier failed to initialize: {e}. Image classification will be unavailable.")
# Global state for API credentials (updated by user input)
api_key = gr.State(os.getenv('BINGX_API_KEY', ""))
api_secret = gr.State(os.getenv('BINGX_API_SECRET', ""))
# Configuration
API_BASE_URL = "https://open-api.bingx.com"
# Generate Signature
def generate_signature(api_secret, params_str):
return hmac.new(api_secret.encode("utf-8"), params_str.encode("utf-8"), hashlib.sha256).hexdigest()
# Parse Parameters
def parse_params(params):
sorted_keys = sorted(params.keys())
param_pairs = [f"{key}={params[key]}" for key in sorted_keys]
params_str = "&".join(param_pairs)
return params_str + "&timestamp=" + str(int(time.time() * 1000)) if params_str else "timestamp=" + str(int(time.time() * 1000))
# Fetch from BingX API
def fetch_from_api(endpoint, params=None, api_key=api_key, api_secret=api_secret):
if not api_key.value or not api_secret.value:
raise ValueError("BingX API Key and Secret are not set. Please enter them in the Manage API Credentials section.")
params = params or {}
params['recvWindow'] = params.get('recvWindow', '5000')
params_str = parse_params(params)
signature = generate_signature(api_secret.value, params_str)
url = f"{API_BASE_URL}{endpoint}?{params_str}&signature={signature}"
try:
response = requests.get(url, headers={'X-BX-APIKEY': api_key.value, 'Content