grid-backtest / hf-space /patch_cors.py
shaun07's picture
Upload folder using huggingface_hub
61ca2b1 verified
Raw History Blame Contribute Delete
682 Bytes
"""
补丁脚本:更新 CORS 配置,允许 HuggingFace Spaces 域名访问。
"""
import re
filepath = '/app/backend/server.py'
with open(filepath, 'r') as f:
content = f.read()
# 替换 CORS allow_origins 行
old_cors = '''allow_origins=["http://localhost:3000", "http://127.0.0.1:3000", "http://localhost:3001", "http://127.0.0.1:3001"],'''
new_cors = '''allow_origins=["*"], # HF Spaces: 允许所有来源'''
if old_cors in content:
content = content.replace(old_cors, new_cors)
print('✅ CORS 补丁已应用:允许所有来源')
else:
print('⚠️ 未找到 CORS 配置,可能已被修改')
with open(filepath, 'w') as f:
f.write(content)