Spaces:
Runtime error
Runtime error
CassiopeiaCode commited on
Commit ·
1bdfa3a
1
Parent(s): 59d6efd
feat: 添加HTTP代理支持
Browse files- 添加HTTP_PROXY环境变量配置
- 在所有requests请求中使用代理(留空则不使用)
- 支持replicate.py、app.py和auth_flow.py中的所有HTTP请求
Files changed (4)
- .env.example+5-1
- app.py+7-1
- auth_flow.py+8-1
- replicate.py+9-1
3 unmodified lines45673 unmodified linesOPENAI_KEYS=""# 出错次数阈值,超过此值自动禁用账号MAX_ERROR_COUNT=1003 unmodified lines45678910113 unmodified linesOPENAI_KEYS=""# 出错次数阈值,超过此值自动禁用账号MAX_ERROR_COUNT=100# HTTP代理设置(留空不使用代理)# 例如:HTTP_PROXY="http://127.0.0.1:7890"HTTP_PROXY=""
192 unmodified lines19319419519619719821 unmodified lines220221222223224225226192 unmodified linesOIDC_BASE = "https://oidc.us-east-1.amazonaws.com"TOKEN_URL = f"{OIDC_BASE}/token"def _oidc_headers() -> Dict[str, str]:return {"content-type": "application/json",21 unmodified lines}try:r = requests.post(TOKEN_URL, headers=_oidc_headers(), json=payload, timeout=(15, 60))r.raise_for_status()data = r.json()new_access = data.get("accessToken")192 unmodified lines19319419519619719819920020120220320421 unmodified lines226227228229230231232192 unmodified linesOIDC_BASE = "https://oidc.us-east-1.amazonaws.com"TOKEN_URL = f"{OIDC_BASE}/token"def _get_proxies() -> Optional[Dict[str, str]]:proxy = os.getenv("HTTP_PROXY", "").strip()if proxy:return {"http": proxy, "https": proxy}return Nonedef _oidc_headers() -> Dict[str, str]:return {"content-type": "application/json",21 unmodified lines}try:r = requests.post(TOKEN_URL, headers=_oidc_headers(), json=payload, timeout=(15, 60), proxies=_get_proxies())r.raise_for_status()data = r.json()new_access = data.get("accessToken")
1234567891020 unmodified lines31323334353637import jsonimport timeimport uuidfrom typing import Dict, Tuple, Optionalimport requests# OIDC endpoints and constants (aligned with v1/auth_client.py)OIDC_BASE = "https://oidc.us-east-1.amazonaws.com"REGISTER_URL = f"{OIDC_BASE}/client/register"20 unmodified lines# Keep JSON order and mimic body closely to v1payload_str = json.dumps(payload, ensure_ascii=False)headers = make_headers()resp = requests.post(url, headers=headers, data=payload_str, timeout=(15, 60))return resp123456789101112131415161720 unmodified lines38394041424344import jsonimport timeimport uuidimport osfrom typing import Dict, Tuple, Optionalimport requestsdef _get_proxies() -> Optional[Dict[str, str]]:proxy = os.getenv("HTTP_PROXY", "").strip()if proxy:return {"http": proxy, "https": proxy}return None# OIDC endpoints and constants (aligned with v1/auth_client.py)OIDC_BASE = "https://oidc.us-east-1.amazonaws.com"REGISTER_URL = f"{OIDC_BASE}/client/register"20 unmodified lines# Keep JSON order and mimic body closely to v1payload_str = json.dumps(payload, ensure_ascii=False)headers = make_headers()resp = requests.post(url, headers=headers, data=payload_str, timeout=(15, 60), proxies=_get_proxies())return resp
123459 unmodified lines151617181920177 unmodified lines198199200201202203204import jsonimport uuidfrom pathlib import Pathfrom typing import Dict, Optional, Tuple, Iterator, List, Generator, Anyimport struct9 unmodified linesself.has_content = Trueyield itemBASE_DIR = Path(__file__).resolve().parentTEMPLATE_PATH = BASE_DIR / "templates" / "streaming_request.json"177 unmodified linespayload_str = json.dumps(body_json, ensure_ascii=False)headers = _merge_headers(headers_from_log, access_token)session = requests.Session()resp = session.post(url, headers=headers, data=payload_str, stream=True, timeout=timeout)if resp.status_code >= 400:try:err = resp.text1234569 unmodified lines161718192021222324252627177 unmodified lines205206207208209210211212import jsonimport uuidimport osfrom pathlib import Pathfrom typing import Dict, Optional, Tuple, Iterator, List, Generator, Anyimport struct9 unmodified linesself.has_content = Trueyield itemdef _get_proxies() -> Optional[Dict[str, str]]:proxy = os.getenv("HTTP_PROXY", "").strip()if proxy:return {"http": proxy, "https": proxy}return NoneBASE_DIR = Path(__file__).resolve().parentTEMPLATE_PATH = BASE_DIR / "templates" / "streaming_request.json"177 unmodified linespayload_str = json.dumps(body_json, ensure_ascii=False)headers = _merge_headers(headers_from_log, access_token)session = requests.Session()proxies = _get_proxies()resp = session.post(url, headers=headers, data=payload_str, stream=True, timeout=timeout, proxies=proxies)if resp.status_code >= 400:try:err = resp.text
.env.example+5-1
app.py+7-1
auth_flow.py+8-1
replicate.py+9-1