我有一個在 score.py 中使用的 json 檔案,但是沒有找到。向端點發出發布請求時,我收到以下錯誤“沒有這樣的檔案或目錄:'/var/azureml-app/model_adjustments.json'”
json 檔案與 score.py 位于同一檔案夾中,并從腳本中呼叫它
path_to_source="path/example"
inf_conf = InferenceConfig(entry_script="score.py",environment=environment,source_directory=path_to_source)
在我的 score.py 檔案中,我有以下內容
def init():
global model
# note you can pass in multiple rows for scoring
def run(raw_data):
try:
# parse the features from the json doc
dat = json.loads(raw_data)
#deserialize the model file back into a sklearn mode
model_name = "{0}_{1}_{2}".format(dat["Name"], dat["model"], dat["EDP"])
model_path = Model.get_model_path(model_name = model_name)
model = load(model_path)
# parse the savings json file
current_directory = os.getcwd()
file_name="model_adjustments.json"
json_file=os.path.join(current_directory,file_name)
with open(json_file, "r") as fr:
adjustments_dat = json.loads(fr.read())
modelAdjustment = adjustments_dat[model_name]
# create a dataframe of the features
dfPredict = pd.DataFrame(
columns = [
"feature1",
"feature2",
"feature3",
"feature4",
"feature5"
]
)
# predict the outcome for these features
prediction = model.predict(dfPredict)
Predicted_val = prediction[0]
#prediction adjustment for buffer
final_val= Predicted_val modelAdjustment
return {
"final_val": final_val}
except Exception as e:
error = str(e)
return {"debug": -1, "error": error}
我知道它與 json 檔案有關,因為當我洗掉與它有關的所有內容時,在執行發布請求時沒有錯誤。我得到了我預期的值。不知道為什么它不讀取 json 檔案
uj5u.com熱心網友回復:
我沒有時間嘗試這個,但我會假設該score.py檔案(以及同一目錄中的任何檔案)不會位于os.getcwd().
我會嘗試使用__file__來獲取路徑score.py:
所以,而不是:
current_directory = os.getcwd()
file_name="model_adjustments.json"
json_file=os.path.join(current_directory,file_name)
而是嘗試:
import pathlib
model_directory = pathlib.Path(os.path.realpath(__file__)).parent.resolve()
file_name="model_adjustments.json"
json_file=os.path.join(model_directory,file_name)
(注意:realpath是否可以確保正確決議符號鏈接)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/519941.html
標籤:Pythonjson天蓝色机器学习服务azureml-python-sdkazuremlsdk
