# Sample script to get Zero GPU balance using your credentials

**URL:** <https://discuss.huggingface.co/t/sample-script-to-get-zero-gpu-balance-using-your-credentials/168463>\
**Category:** Amazon Inferentia & Trainium\
**Created:** [September 16, 2025, 6:03am UTC](https://discuss.huggingface.co/t/sample-script-to-get-zero-gpu-balance-using-your-credentials/168463 "2025-09-16T06:03:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![EvgenyZaretskiy](https://avatars.discourse-cdn.com/v4/letter/e/f6c823/32.png) [@EvgenyZaretskiy](https://discuss.huggingface.co/u/EvgenyZaretskiy)\
**Post date:** [September 16, 2025, 6:03am UTC](https://discuss.huggingface.co/t/sample-script-to-get-zero-gpu-balance-using-your-credentials/168463/1 "2025-09-16T06:03:09Z")

</div>

**import requests**  
**import re**  
**import logging**  
**from datetime import datetime**

**# Any questions address to [Telegram: Contact @Eugeny\_Zaretskiy](https://t.me/Eugeny_Zaretskiy)**

**def get\_hf\_gpu\_balance(log\_id=None):**  
**“”"**  
**Fetch Hugging Face billing page and extract current GPU balance.**

**```**  
**Args:**  
\*\* log\_id (str): Optional identifier for logging\*\*

**Returns:**  
\*\* float: Current GPU balance in seconds, or None if failed\*\*  
**“”"**  
**# Configure logging format**  
**log\_prefix = f"[HFQUOTA][{log\_id}]" if log\_id else “[HFQUOTA]”**

**# Your authentication tokens, find it using F12 in Google Chrome**  
**cookies = {**  
\*\* ‘token’: ‘your\_token1’,\*\*  
\*\* ‘\_stripe\_mid’: ‘your\_token2’,\*\*  
\*\* ‘\_stripe\_sid’: ‘your\_token3’,\*\*  
\*\* ‘aws-waf-token’: ‘your\_token4’\*\*  
**}**

**url = ‘[Hugging Face – The AI community building the future.](https://huggingface.co/settings/billing)’**

**try:**  
**```**

**# logging.info(f"{log\_prefix} Fetching billing page…")**

**```**  
\*\* response = requests.get(url, cookies=cookies, timeout=30)\*\*  
\*\* response.raise\_for\_status()\*\*

* * *

\*\* html\_content = response.text\*\*  
**```**

**# logging.info(f"{log\_prefix} Page fetched successfully (size: {len(html\_content)} bytes)")**

**```**  
\*\* # Extract GPU balance using regex\*\*  
\*\* pattern = r’current":([0-9]+(?:.[0-9]+)?)'\*\*  
\*\* match = re.search(pattern, html\_content)\*\*

* * *

\*\* if match:\*\*  
\*\* gpu\_balance = str(int(float(match.group(1))))\*\*  
**```**

**# logging.info(f"{log\_prefix} GPU balance extracted: {gpu\_balance} seconds")**

**```**  
\*\* return gpu\_balance\*\*  
\*\* else:\*\*  
\*\* logging.warning(f"{log\_prefix} GPU balance not found in HTML")\*\*  
\*\* return None\*\*

* * *

**except requests.exceptions.RequestException as e:**  
\*\* logging.error(f"{log\_prefix} HTTP request failed: {e}“)\*\*  
\*\* return None\*\*  
**except ValueError as e:**  
\*\* logging.error(f”{log\_prefix} Failed to convert balance to float: {e}“)\*\*  
\*\* return None\*\*  
**except Exception as e:**  
\*\* logging.error(f”{log\_prefix} Unexpected error: {e}")\*\*  
\*\* return None\*\*  
**```**

**# Example usage**

**if name == “main”:**

**# Setup logging**

**logging.basicConfig(level=logging.INFO, format=‘%(asctime)s - %(levelname)s - %(message)s’)**

**`**  **# Get GPU balance with log ID** **balance = get_hf_gpu_balance("GPU-MONITOR-001")**  **if balance is not None:** **print(datetime.now().strftime("%d%m%y %H:%M:%S"), "Current GPU balance:", balance, "seconds")**  **else:** **print("Failed to retrieve GPU balance")**  **`**

**Example of output:**

**160925 12:45:02 Current GPU balance: 1500 seconds**  
**160925 12:50:03 Current GPU balance: 1492 seconds**

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/hellohellohello/original/2X/d/de4155eb4aa4108ecb32a1389d7cc37ae69f88b7.png) [@system](https://discuss.huggingface.co/u/system)\
**Post date:** [September 16, 2025, 6:04am UTC](https://discuss.huggingface.co/t/sample-script-to-get-zero-gpu-balance-using-your-credentials/168463/2 "2025-09-16T06:04:39Z")

</div>



---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/hellohellohello/original/2X/d/de4155eb4aa4108ecb32a1389d7cc37ae69f88b7.png) [@system](https://discuss.huggingface.co/u/system)\
**Post date:** [September 16, 2025, 10:19am UTC](https://discuss.huggingface.co/t/sample-script-to-get-zero-gpu-balance-using-your-credentials/168463/3 "2025-09-16T10:19:51Z")

</div>


