Spaces:
Sleeping
Sleeping
KarenYYH Claude commited on
Commit ·
26bc5b6
1
Parent(s): 9cc6bfe
Fix: Monkey-patch aistudio_sdk.hub.download before importing PaddleNLP
Browse files- Set HUB_DISABLE_DOWNLOAD=1 environment variable
- Inject fake aistudio_sdk.hub.download into sys.modules
- Intercept calls to aistudio_sdk before PaddleNLP imports
- This prevents the 'cannot import name download' error
Co-Authored-By: Claude <noreply@anthropic.com>
- models/sentiment.py +23 -4
models/sentiment.py
CHANGED
|
@@ -95,9 +95,12 @@ class SentimentAnalyzer:
|
|
| 95 |
|
| 96 |
def _load_paddlenlp_model(self, model_path: str):
|
| 97 |
"""使用 PaddleNLP 加载模型"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
try:
|
| 99 |
import paddle
|
| 100 |
-
from paddlenlp.transformers import BertModel, BertTokenizer
|
| 101 |
from paddle.nn import Layer
|
| 102 |
from paddle.nn import Linear, Dropout
|
| 103 |
|
|
@@ -131,7 +134,23 @@ class SentimentAnalyzer:
|
|
| 131 |
with open(tokenizer_config_path, 'r') as f:
|
| 132 |
tokenizer_config = json.load(f)
|
| 133 |
|
| 134 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
from paddlenlp.transformers import BertTokenizer as BT
|
| 136 |
self.tokenizer = BT(
|
| 137 |
vocab,
|
|
@@ -147,8 +166,8 @@ class SentimentAnalyzer:
|
|
| 147 |
class BertClassificationModel(Layer):
|
| 148 |
def __init__(self, config_dict, num_labels=3):
|
| 149 |
super().__init__()
|
| 150 |
-
#
|
| 151 |
-
from paddlenlp.transformers import BertConfig
|
| 152 |
bert_config = BertConfig(
|
| 153 |
vocab_size=config_dict.get('vocab_size', 21128),
|
| 154 |
hidden_size=config_dict.get('hidden_size', 768),
|
|
|
|
| 95 |
|
| 96 |
def _load_paddlenlp_model(self, model_path: str):
|
| 97 |
"""使用 PaddleNLP 加载模型"""
|
| 98 |
+
# 先尝试禁用 aistudio_sdk,然后再导入 PaddleNLP
|
| 99 |
+
import os
|
| 100 |
+
os.environ['HUB_DISABLE_DOWNLOAD'] = '1'
|
| 101 |
+
|
| 102 |
try:
|
| 103 |
import paddle
|
|
|
|
| 104 |
from paddle.nn import Layer
|
| 105 |
from paddle.nn import Linear, Dropout
|
| 106 |
|
|
|
|
| 134 |
with open(tokenizer_config_path, 'r') as f:
|
| 135 |
tokenizer_config = json.load(f)
|
| 136 |
|
| 137 |
+
# 延迟导入 - 只在真正需要时才导入
|
| 138 |
+
# 使用 exec 来避免模块级导入
|
| 139 |
+
import sys
|
| 140 |
+
import types
|
| 141 |
+
|
| 142 |
+
# 创建一个假的模块来拦截 aistudio_sdk 调用
|
| 143 |
+
class FakeAistudioHub:
|
| 144 |
+
@staticmethod
|
| 145 |
+
def download(*args, **kwargs):
|
| 146 |
+
return None
|
| 147 |
+
|
| 148 |
+
# 注入假模块
|
| 149 |
+
sys.modules['aistudio_sdk'] = types.ModuleType('aistudio_sdk', ())
|
| 150 |
+
sys.modules['aistudio_sdk.hub'] = types.ModuleType('aistudio_sdk.hub', ())
|
| 151 |
+
sys.modules['aistudio_sdk.hub'].download = FakeAistudioHub.download
|
| 152 |
+
|
| 153 |
+
# 现在可以安全地导入 BertTokenizer
|
| 154 |
from paddlenlp.transformers import BertTokenizer as BT
|
| 155 |
self.tokenizer = BT(
|
| 156 |
vocab,
|
|
|
|
| 166 |
class BertClassificationModel(Layer):
|
| 167 |
def __init__(self, config_dict, num_labels=3):
|
| 168 |
super().__init__()
|
| 169 |
+
# 延迟导入 BertModel
|
| 170 |
+
from paddlenlp.transformers import BertConfig, BertModel
|
| 171 |
bert_config = BertConfig(
|
| 172 |
vocab_size=config_dict.get('vocab_size', 21128),
|
| 173 |
hidden_size=config_dict.get('hidden_size', 768),
|