Spaces:
Running
config: Use env var for proxy URL
Browse files- [add] Implement `get_proxy_url` and `validate_proxy_url` functions (utils.py:82)
- [config] Import `get_proxy_url` and `validate_proxy_url` (hf_token_utils.py:10)
- [config] Change `proxy_url` default to `None` in `get_proxy_token` (hf_token_utils.py:18)
- [config] Use `validate_proxy_url` and `get_proxy_url` in `get_proxy_token` when `proxy_url` is `None` (hf_token_utils.py:29-33)
- [config] Change `proxy_url` default to `None` in `report_token_status` (hf_token_utils.py:90)
- [config] Use `validate_proxy_url` and `get_proxy_url` in `report_token_status` when `proxy_url` is `None` (hf_token_utils.py:100-105)
- [docs] Update setup instructions to include `PROXY_URL` secret (README.md:36)
- [docs] Remove hardcoded proxy server URL from documentation (README.md:40)
- [docs] Add `PROXY_URL Missing` to troubleshooting section (README.md:219)
- README.md +8 -4
- hf_token_utils.py +20 -4
- utils.py +13 -0
|
@@ -36,14 +36,17 @@ A comprehensive AI platform that combines conversational AI and text-to-image ge
|
|
| 36 |
|
| 37 |
### 1. HuggingFace Space Secrets
|
| 38 |
|
| 39 |
-
Add the following
|
| 40 |
|
| 41 |
- **Key**: `PROXY_KEY`
|
| 42 |
- **Value**: Your HF-Inferoxy proxy API key
|
| 43 |
|
|
|
|
|
|
|
|
|
|
| 44 |
### 2. HF-Inferoxy Server
|
| 45 |
|
| 46 |
-
The app
|
| 47 |
|
| 48 |
### 3. Dependencies
|
| 49 |
|
|
@@ -219,8 +222,9 @@ Prompt: "Help me debug this Python code: [paste code]"
|
|
| 219 |
|
| 220 |
#### Setup Issues
|
| 221 |
1. **PROXY_KEY Missing**: Ensure the secret is set in your HuggingFace Space settings
|
| 222 |
-
2. **
|
| 223 |
-
3. **
|
|
|
|
| 224 |
|
| 225 |
#### Chat Issues
|
| 226 |
1. **No Response**: Check model name format and provider availability
|
|
|
|
| 36 |
|
| 37 |
### 1. HuggingFace Space Secrets
|
| 38 |
|
| 39 |
+
Add the following secrets to your HuggingFace Space:
|
| 40 |
|
| 41 |
- **Key**: `PROXY_KEY`
|
| 42 |
- **Value**: Your HF-Inferoxy proxy API key
|
| 43 |
|
| 44 |
+
- **Key**: `PROXY_URL`
|
| 45 |
+
- **Value**: Your HF-Inferoxy proxy server URL (e.g., `https://hf-proxy.example.com`)
|
| 46 |
+
|
| 47 |
### 2. HF-Inferoxy Server
|
| 48 |
|
| 49 |
+
The app will use the HF-Inferoxy server URL specified in the `PROXY_URL` secret.
|
| 50 |
|
| 51 |
### 3. Dependencies
|
| 52 |
|
|
|
|
| 222 |
|
| 223 |
#### Setup Issues
|
| 224 |
1. **PROXY_KEY Missing**: Ensure the secret is set in your HuggingFace Space settings
|
| 225 |
+
2. **PROXY_URL Missing**: Ensure the proxy server URL secret is set in your HuggingFace Space settings
|
| 226 |
+
3. **Connection Errors**: Verify the HF-Inferoxy server is accessible
|
| 227 |
+
4. **Import Errors**: Check that all dependencies are properly installed
|
| 228 |
|
| 229 |
#### Chat Issues
|
| 230 |
1. **No Response**: Check model name format and provider availability
|
|
@@ -5,18 +5,19 @@ import json
|
|
| 5 |
import time
|
| 6 |
from typing import Dict, Optional, Any, Tuple
|
| 7 |
from requests.exceptions import ConnectionError, Timeout, RequestException
|
|
|
|
| 8 |
|
| 9 |
# Timeout and retry configuration
|
| 10 |
REQUEST_TIMEOUT = 30 # 30 seconds timeout
|
| 11 |
RETRY_ATTEMPTS = 2
|
| 12 |
RETRY_DELAY = 1 # 1 second delay between retries
|
| 13 |
|
| 14 |
-
def get_proxy_token(proxy_url: str =
|
| 15 |
"""
|
| 16 |
Get a valid token from the proxy server with timeout and retry logic.
|
| 17 |
|
| 18 |
Args:
|
| 19 |
-
proxy_url: URL of the HF-Inferoxy server
|
| 20 |
api_key: Your API key for authenticating with the proxy server
|
| 21 |
|
| 22 |
Returns:
|
|
@@ -27,6 +28,13 @@ def get_proxy_token(proxy_url: str = "http://scw.nazdev.tech:11155", api_key: st
|
|
| 27 |
TimeoutError: If request times out
|
| 28 |
Exception: If token provisioning fails
|
| 29 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
headers = {}
|
| 31 |
if api_key:
|
| 32 |
headers["Authorization"] = f"Bearer {api_key}"
|
|
@@ -90,7 +98,7 @@ def report_token_status(
|
|
| 90 |
token_id: str,
|
| 91 |
status: str = "success",
|
| 92 |
error: Optional[str] = None,
|
| 93 |
-
proxy_url: str =
|
| 94 |
api_key: str = None
|
| 95 |
) -> bool:
|
| 96 |
"""
|
|
@@ -100,12 +108,20 @@ def report_token_status(
|
|
| 100 |
token_id: ID of the token to report (from get_proxy_token)
|
| 101 |
status: Status to report ('success' or 'error')
|
| 102 |
error: Error message if status is 'error'
|
| 103 |
-
proxy_url: URL of the HF-Inferoxy server
|
| 104 |
api_key: Your API key for authenticating with the proxy server
|
| 105 |
|
| 106 |
Returns:
|
| 107 |
True if report was accepted, False otherwise
|
| 108 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
payload = {"token_id": token_id, "status": status}
|
| 110 |
|
| 111 |
if error:
|
|
|
|
| 5 |
import time
|
| 6 |
from typing import Dict, Optional, Any, Tuple
|
| 7 |
from requests.exceptions import ConnectionError, Timeout, RequestException
|
| 8 |
+
from utils import get_proxy_url, validate_proxy_url
|
| 9 |
|
| 10 |
# Timeout and retry configuration
|
| 11 |
REQUEST_TIMEOUT = 30 # 30 seconds timeout
|
| 12 |
RETRY_ATTEMPTS = 2
|
| 13 |
RETRY_DELAY = 1 # 1 second delay between retries
|
| 14 |
|
| 15 |
+
def get_proxy_token(proxy_url: str = None, api_key: str = None) -> Tuple[str, str]:
|
| 16 |
"""
|
| 17 |
Get a valid token from the proxy server with timeout and retry logic.
|
| 18 |
|
| 19 |
Args:
|
| 20 |
+
proxy_url: URL of the HF-Inferoxy server (optional, will use PROXY_URL env var if not provided)
|
| 21 |
api_key: Your API key for authenticating with the proxy server
|
| 22 |
|
| 23 |
Returns:
|
|
|
|
| 28 |
TimeoutError: If request times out
|
| 29 |
Exception: If token provisioning fails
|
| 30 |
"""
|
| 31 |
+
# Get proxy URL from environment if not provided
|
| 32 |
+
if proxy_url is None:
|
| 33 |
+
is_valid, error_msg = validate_proxy_url()
|
| 34 |
+
if not is_valid:
|
| 35 |
+
raise Exception(error_msg)
|
| 36 |
+
proxy_url = get_proxy_url()
|
| 37 |
+
|
| 38 |
headers = {}
|
| 39 |
if api_key:
|
| 40 |
headers["Authorization"] = f"Bearer {api_key}"
|
|
|
|
| 98 |
token_id: str,
|
| 99 |
status: str = "success",
|
| 100 |
error: Optional[str] = None,
|
| 101 |
+
proxy_url: str = None,
|
| 102 |
api_key: str = None
|
| 103 |
) -> bool:
|
| 104 |
"""
|
|
|
|
| 108 |
token_id: ID of the token to report (from get_proxy_token)
|
| 109 |
status: Status to report ('success' or 'error')
|
| 110 |
error: Error message if status is 'error'
|
| 111 |
+
proxy_url: URL of the HF-Inferoxy server (optional, will use PROXY_URL env var if not provided)
|
| 112 |
api_key: Your API key for authenticating with the proxy server
|
| 113 |
|
| 114 |
Returns:
|
| 115 |
True if report was accepted, False otherwise
|
| 116 |
"""
|
| 117 |
+
# Get proxy URL from environment if not provided
|
| 118 |
+
if proxy_url is None:
|
| 119 |
+
is_valid, error_msg = validate_proxy_url()
|
| 120 |
+
if not is_valid:
|
| 121 |
+
print(f"β {error_msg}")
|
| 122 |
+
return False
|
| 123 |
+
proxy_url = get_proxy_url()
|
| 124 |
+
|
| 125 |
payload = {"token_id": token_id, "status": status}
|
| 126 |
|
| 127 |
if error:
|
|
@@ -82,6 +82,19 @@ def validate_proxy_key():
|
|
| 82 |
return True, ""
|
| 83 |
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
def parse_model_and_provider(model_name):
|
| 86 |
"""
|
| 87 |
Parse model name and provider from a string like 'model:provider'.
|
|
|
|
| 82 |
return True, ""
|
| 83 |
|
| 84 |
|
| 85 |
+
def get_proxy_url():
|
| 86 |
+
"""Get the proxy URL from environment variables."""
|
| 87 |
+
return os.getenv("PROXY_URL")
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
def validate_proxy_url():
|
| 91 |
+
"""Validate that the proxy URL is available."""
|
| 92 |
+
proxy_url = get_proxy_url()
|
| 93 |
+
if not proxy_url:
|
| 94 |
+
return False, "β Error: PROXY_URL not found in environment variables. Please set it in your HuggingFace Space secrets."
|
| 95 |
+
return True, ""
|
| 96 |
+
|
| 97 |
+
|
| 98 |
def parse_model_and_provider(model_name):
|
| 99 |
"""
|
| 100 |
Parse model name and provider from a string like 'model:provider'.
|