Spaces:
Running
Running
优化生成唯一键值函数,支持传入符号参数以增强缓存机制;改进预测函数中的缓存检查逻辑,确保输入有效性并优化缓存管理
Browse files- blkeras.py +18 -14
blkeras.py
CHANGED
@@ -83,11 +83,11 @@ CACHE_MAX_SIZE = 512
|
|
83 |
|
84 |
|
85 |
# 生成唯一键值函数
|
86 |
-
def generate_key(lemmatized_entry):
|
87 |
# 获取当前日期,例如 '20241010'
|
88 |
current_date = datetime.now().strftime('%Y%m%d')
|
89 |
# 将 lemmatized_entry 中的单词连接成字符串,并与当前日期组合生成 MD5 哈希值
|
90 |
-
combined_text = f"{''.join(lemmatized_entry)}{current_date}"
|
91 |
return hashlib.md5(combined_text.encode()).hexdigest()
|
92 |
|
93 |
# 生成符合正态分布的伪精准度值
|
@@ -145,13 +145,7 @@ def predict(text: str, stock_codes: list):
|
|
145 |
# 从 NER 结果中提取相关的股票代码或公司名称
|
146 |
affected_stock_codes = find_stock_codes_or_names(ner)
|
147 |
|
148 |
-
|
149 |
-
cache_key = generate_key(lemmatized_entry)
|
150 |
-
# 检查缓存中是否已有结果
|
151 |
-
if input_text != "EMPTY_TEXT" and cache_key in prediction_cache:
|
152 |
-
print(f"Cache hit: {cache_key}" )
|
153 |
-
return prediction_cache[cache_key]
|
154 |
-
|
155 |
|
156 |
|
157 |
# Final Result
|
@@ -160,6 +154,16 @@ def predict(text: str, stock_codes: list):
|
|
160 |
# 调用 get_stock_info 函数
|
161 |
|
162 |
for stock_code in affected_stock_codes:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
previous_stock_history, _, previous_stock_inx_index_history, previous_stock_dj_index_history, previous_stock_ixic_index_history, previous_stock_ndx_index_history, _, _, _, _ = get_stock_info(stock_code)
|
164 |
|
165 |
|
@@ -409,12 +413,12 @@ def predict(text: str, stock_codes: list):
|
|
409 |
}
|
410 |
final_result_list.append(result)
|
411 |
|
412 |
-
|
413 |
-
|
414 |
|
415 |
-
|
416 |
-
|
417 |
-
|
418 |
|
419 |
|
420 |
# 返回预测结果
|
|
|
83 |
|
84 |
|
85 |
# 生成唯一键值函数
|
86 |
+
def generate_key(lemmatized_entry, symbol: str = ""):
|
87 |
# 获取当前日期,例如 '20241010'
|
88 |
current_date = datetime.now().strftime('%Y%m%d')
|
89 |
# 将 lemmatized_entry 中的单词连接成字符串,并与当前日期组合生成 MD5 哈希值
|
90 |
+
combined_text = f"{''.join(lemmatized_entry)}{current_date}{symbol}"
|
91 |
return hashlib.md5(combined_text.encode()).hexdigest()
|
92 |
|
93 |
# 生成符合正态分布的伪精准度值
|
|
|
145 |
# 从 NER 结果中提取相关的股票代码或公司名称
|
146 |
affected_stock_codes = find_stock_codes_or_names(ner)
|
147 |
|
148 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
149 |
|
150 |
|
151 |
# Final Result
|
|
|
154 |
# 调用 get_stock_info 函数
|
155 |
|
156 |
for stock_code in affected_stock_codes:
|
157 |
+
|
158 |
+
# 生成唯一键值
|
159 |
+
cache_key = generate_key(lemmatized_entry, stock_code)
|
160 |
+
# 检查缓存中是否已有结果
|
161 |
+
if input_text != "EMPTY_TEXT" and cache_key in prediction_cache:
|
162 |
+
print(f"Cache hit: {cache_key}" )
|
163 |
+
# 从缓存中获取结果并添加到最终结果列表
|
164 |
+
final_result_list.append(prediction_cache[cache_key])
|
165 |
+
continue
|
166 |
+
|
167 |
previous_stock_history, _, previous_stock_inx_index_history, previous_stock_dj_index_history, previous_stock_ixic_index_history, previous_stock_ndx_index_history, _, _, _, _ = get_stock_info(stock_code)
|
168 |
|
169 |
|
|
|
413 |
}
|
414 |
final_result_list.append(result)
|
415 |
|
416 |
+
# 缓存预测结果
|
417 |
+
prediction_cache[cache_key] = result
|
418 |
|
419 |
+
# 如果缓存大小超过最大限制,移除最早的缓存项
|
420 |
+
if len(prediction_cache) > CACHE_MAX_SIZE:
|
421 |
+
prediction_cache.popitem(last=False)
|
422 |
|
423 |
|
424 |
# 返回预测结果
|