Spaces:
Running
Running
优化预测函数的错误处理逻辑,返回空列表以替代错误信息;增加对空文本和股票代码的处理,确保输入有效性
Browse files- app.py +1 -1
- blkeras.py +6 -2
- preprocess.py +4 -0
app.py
CHANGED
|
@@ -76,7 +76,7 @@ async def predict(request: PredictRequest):
|
|
| 76 |
)
|
| 77 |
return result
|
| 78 |
except Exception as e:
|
| 79 |
-
return
|
| 80 |
|
| 81 |
@app.get("/")
|
| 82 |
async def root():
|
|
|
|
| 76 |
)
|
| 77 |
return result
|
| 78 |
except Exception as e:
|
| 79 |
+
return []
|
| 80 |
|
| 81 |
@app.get("/")
|
| 82 |
async def root():
|
blkeras.py
CHANGED
|
@@ -99,7 +99,11 @@ def predict(text: str, stock_codes: list):
|
|
| 99 |
from preprocess import get_document_vector, get_stock_info, process_entities, process_pos_tags, processing_entry
|
| 100 |
|
| 101 |
try:
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
print(f"Input Text Length: {len(text)}, Start with: {text[:200] if len(text) > 200 else text}")
|
| 104 |
print("Input stock codes:", stock_codes)
|
| 105 |
|
|
@@ -406,7 +410,7 @@ def predict(text: str, stock_codes: list):
|
|
| 406 |
traceback_str = traceback.print_exc()
|
| 407 |
print(f"predict() error: {e}")
|
| 408 |
print(traceback_str)
|
| 409 |
-
return
|
| 410 |
finally:
|
| 411 |
end_time = datetime.now()
|
| 412 |
print(f"predict() Text: {input_text[:200] if len(input_text) > 200 else input_text} \n execution time: {end_time - start_time}, Text Length: {len(input_text)} \n")
|
|
|
|
| 99 |
from preprocess import get_document_vector, get_stock_info, process_entities, process_pos_tags, processing_entry
|
| 100 |
|
| 101 |
try:
|
| 102 |
+
if text.strip() == "" and not stock_codes:
|
| 103 |
+
return []
|
| 104 |
+
if stock_codes and text.strip() == "":
|
| 105 |
+
text = "EMPTY_TEXT"
|
| 106 |
+
|
| 107 |
print(f"Input Text Length: {len(text)}, Start with: {text[:200] if len(text) > 200 else text}")
|
| 108 |
print("Input stock codes:", stock_codes)
|
| 109 |
|
|
|
|
| 410 |
traceback_str = traceback.print_exc()
|
| 411 |
print(f"predict() error: {e}")
|
| 412 |
print(traceback_str)
|
| 413 |
+
return []
|
| 414 |
finally:
|
| 415 |
end_time = datetime.now()
|
| 416 |
print(f"predict() Text: {input_text[:200] if len(input_text) > 200 else input_text} \n execution time: {end_time - start_time}, Text Length: {len(input_text)} \n")
|
preprocess.py
CHANGED
|
@@ -266,6 +266,10 @@ def process_long_text(text, tokenizer, max_length=512):
|
|
| 266 |
return segments
|
| 267 |
|
| 268 |
def get_sentiment_score(text):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
try:
|
| 270 |
import torch
|
| 271 |
|
|
|
|
| 266 |
return segments
|
| 267 |
|
| 268 |
def get_sentiment_score(text):
|
| 269 |
+
if text and text.strip() == "EMPTY_TEXT":
|
| 270 |
+
return 0.0
|
| 271 |
+
|
| 272 |
+
|
| 273 |
try:
|
| 274 |
import torch
|
| 275 |
|