File size: 1,835 Bytes
ffbd55f
 
 
 
 
 
 
 
 
1b65420
 
 
 
 
 
ffbd55f
b5e8aa6
 
 
 
ffbd55f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5e8aa6
 
 
ffbd55f
 
 
b5e8aa6
ffbd55f
 
1b65420
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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