我有一個 fastapi 來進行預測并將輸出作為回應回傳,但是我已經實作了輸入檢查,如果用戶提供不受支持的輸入,它會回傳 aInvalid smile但這里的問題是回應字典沒有被替換。
當我做預測時,我得到了這個回應
{"result":{"interaction_map":[[15.0,5.0,14.0,15.0,15.0],[19.0,7.0,20.0,19.0,19.0],[13.0,6.0,18.0,13.0,13.0],[15.0,5.0,14.0,15.0,15.0],[15.0,5.0,14.0,15.0,15.0]],"predictions":-3.405024290084839}}
但是當我輸入錯誤時,我得到了這個回應
{"result":{"interaction_map":[[15.0,5.0,14.0,15.0,15.0],[19.0,7.0,20.0,19.0,19.0],[13.0,6.0,18.0,13.0,13.0],[15.0,5.0,14.0,15.0,15.0],[15.0,5.0,14.0,15.0,15.0]],"predictions":"invalid SMILES"}}
但我期待這個回應
{"predictions":"invalid SMILES"}
這是我正在使用的代碼
response = {}
async def predictions(solute, solvent):
m = Chem.MolFromSmiles(solute,sanitize=False)
n = Chem.MolFromSmiles(solvent,sanitize=False)
if (m == None or n == None):
response['predictions']= 'invalid SMILES'
print('invalid SMILES')
else:
mol = Chem.MolFromSmiles(solute)
mol = Chem.AddHs(mol)
solute = Chem.MolToSmiles(mol)
solute_graph = get_graph_from_smile(solute)
mol = Chem.MolFromSmiles(solvent)
mol = Chem.AddHs(mol)
solvent = Chem.MolToSmiles(mol)
solvent_graph = get_graph_from_smile(solvent)
delta_g, interaction_map = model([solute_graph.to(device), solvent_graph.to(device)])
interaction_map_one = torch.trunc(interaction_map)
response["interaction_map"] = (interaction_map_one.detach().numpy()).tolist()
response["predictions"] = delta_g.item()
@app.get('/predict_solubility')
async def post():
return {'result': response}
@app.get('/predict')
async def predict(background_tasks: BackgroundTasks,solute,solvent):
background_tasks.add_task(predictions,solute,solvent)
return {'success'}
uj5u.com熱心網友回復:
問題是這response是一個全域變數,所以你在第一個請求中寫入的元素在第二個請求中仍然保留在那里。
一種快速修復方法是在請求開始時清除responsedict 以/predict_solubility:
async def predictions(solute, solvent):
response.clear()
/predict但總的來說,對我來說,設定某種可能被覆寫的全域狀態的單個請求似乎是有問題的,而不是回傳一個jobid可以檢查特定作業/任務的位置。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/434489.html
下一篇:C#字典到物件陣列
