Spaces:
Sleeping
Sleeping
Download scripts/test_hf_token.py from Ducdata1808/AGV_simulator: direct link, hf CLI and curl.
- Browser
- Download file 2.31 kB
-
https://huggingface.co/spaces/Ducdata1808/AGV_simulator/resolve/main/scripts/test_hf_token.py
- Command line
-
hf download hf://spaces/Ducdata1808/AGV_simulator/scripts/test_hf_token.py
-
curl -L -o test_hf_token.py https://huggingface.co/spaces/Ducdata1808/AGV_simulator/resolve/main/scripts/test_hf_token.py
2.31 kB
| import os | |
| import requests | |
| from dotenv import load_dotenv | |
| # Set console output encoding to UTF-8 to prevent cp1252 errors on Windows | |
| import sys | |
| import io | |
| sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8') | |
| dotenv_path = os.path.join(os.getcwd(), ".env") | |
| print("Loading .env from:", dotenv_path) | |
| load_dotenv(dotenv_path=dotenv_path, override=True) | |
| token = os.getenv("HF_TOKEN") | |
| if not token: | |
| print("HF_TOKEN not found in .env file!") | |
| exit(1) | |
| url = "https://api-inference.huggingface.co/models/Qwen/Qwen2.5-Coder-1.5B-Instruct" | |
| headers = {"Authorization": f"Bearer {token}"} | |
| payload = {"inputs": "Hi, who are you?"} | |
| # Danh sách các proxy để thử nghiệm | |
| # 127.0.0.1:40000 là cổng mặc định của Cloudflare WARP ở chế độ Local Proxy | |
| proxy_options = [ | |
| {"name": "Không dùng Proxy (Mặc định)", "proxies": None}, | |
| {"name": "SOCKS5 Proxy (Cloudflare WARP - socks5h://127.0.0.1:40000)", | |
| "proxies": {"http": "socks5h://127.0.0.1:40000", "https": "socks5h://127.0.0.1:40000"}}, | |
| {"name": "HTTP Proxy (Cloudflare WARP - http://127.0.0.1:40000)", | |
| "proxies": {"http": "http://127.0.0.1:40000", "https": "http://127.0.0.1:40000"}}, | |
| {"name": "SOCKS5 Proxy (Cổng 10808 phổ biến)", | |
| "proxies": {"http": "socks5h://127.0.0.1:10808", "https": "socks5h://127.0.0.1:10808"}}, | |
| {"name": "HTTP Proxy (Cổng 7890 phổ biến)", | |
| "proxies": {"http": "http://127.0.0.1:7890", "https": "http://127.0.0.1:7890"}} | |
| ] | |
| print("Bắt đầu chẩn đoán kết nối tới Hugging Face...") | |
| for opt in proxy_options: | |
| print(f"\n--- Thử nghiệm: {opt['name']} ---") | |
| try: | |
| response = requests.post(url, headers=headers, json=payload, proxies=opt["proxies"], timeout=10) | |
| print(f"HTTP Status Code: {response.status_code}") | |
| if response.status_code == 200: | |
| print("=> SUCCESS: Kết nối THÀNH CÔNG!") | |
| print(f"Phản hồi từ model: {response.json()}") | |
| print(f"\n[KHUYÊN DÙNG] Hãy sử dụng cấu hình proxy này!") | |
| # Kết thúc sớm nếu thành công | |
| break | |
| else: | |
| print(f"=> Thất bại (Mã lỗi HTTP: {response.status_code})") | |
| except Exception as e: | |
| print(f"=> Lỗi kết nối: {str(e)[:150]}...") | |