0

我想在python上使用Azure webservice發佈機器學習模型。我能夠成功地部署代碼,但是當我嘗試通過URL調用它時,它會拋出我'Azure'模塊不存在。代碼基本上從容器中檢索TFIDF模型(blob)並使用它來預測新值。錯誤清楚地表明,嘗試在web服務上運行時Azure包丟失,我不知道如何解決它。這裏有雲代碼:web服務上的Azure模塊

對於部署:

from azureml import services 
from azure.storage.blob import BlobService 
@services.publish('7c94eb2d9e4c01cbe7ce1063','f78QWNcOXHt9J+Qt1GMzgdEt+m3NXby9JL`npT7XX8ZAGdRZIX/NZ4lL2CkRkGQ==') 
@services.types(res=unicode) 
@services.returns(str) 
def TechBot(res): 
    from azure.storage.blob import BlobService 
    from gensim.similarities import SparseMatrixSimilarity, MatrixSimilarity, Similarity 
    blob_service = BlobService(account_name='tfidf', account_key='RU4R/NIVPsPOoR0bgiJMtosHJMbK1+AVHG0sJCHT6jIdKPRz3cIMYTsrQ5BBD5SELKHUXgBHNmvsIlhEdqUCzw==') 
    blob_service.get_blob_to_path('techbot',"2014.csv","df") 
    df=pd.read_csv("df") 
    doct = res 

要訪問我用Python代碼從

service.azureml.net

import urllib2 
import json 
import requests 
data = { 
     "Inputs": { 
       "input1": 
       [ 
        { 
          'res': "wifi wnable", 
        } 
       ], 
     }, 
    "GlobalParameters": { 
    } 
} 

body = str.encode(json.dumps(data)) 

#proxies = {"http":"http://%s" % proxy} 
url = 'http://ussouthcentral.services.azureml.net/workspaces/7c94eb2de26a45399e4c01cbe7ce1063/services/11943e537e0741beb466cd91f738d073/execute?api-version=2.0&format=swagger' 
api_key = '8fH9kp67pEt3C6XK9sXDLbyYl5cBNEwYg9VY92xvkxNd+cd2w46sF1ckC3jqrL/m8joV7o3rsTRUydkzRGDYig==' # Replace this with the API key for the web service 
headers = {'Content-Type':'application/json', 'Authorization':('Bearer '+ api_key)} 

#proxy_support = urllib2.ProxyHandler(proxies) 
#opener = urllib2.build_opener(proxy_support, urllib2.HTTPHandler(debuglevel=1)) 
#urllib2.install_opener(opener) 

req = urllib2.Request(url, body, headers) 

try: 
    response = urllib2.urlopen(req, timeout=60) 

    result = response.read() 
    print(result) 
except urllib2.HTTPError, error: 
    print("The request failed with status code: " + str(error.code)) 

    # Print the headers - they include the requert ID and the timestamp, which are useful for debugging the failure 
    print(error.info()) 
    print(json.loads(error.read())) 

字符串「水庫」的網址將在年底預測。正如我所說,如果我通過調用azure模塊運行python,它運行得非常好,當我訪問url時會發生問題。 任何幫助表示讚賞,請讓我知道如果你需要更多的信息(我的代碼,我只sohwcased一半)

回答

1

我試圖通過POSTMAN重現該問題,然後我照你說的下面的錯誤信息。

{ 
    "error": { 
    "code": "ModuleExecutionError", 
    "message": "Module execution encountered an error.", 
    "details": [ 
     { 
     "code": "85", 
     "target": "Execute Python Script RRS", 
     "message": "Error 0085: The following error occurred during script evaluation, please view the output log for more information:\r\n---------- Start of error message from Python interpreter ----------\r\nCaught exception while executing function: Traceback (most recent call last):\n File \"\\server\\InvokePy.py\", line 120, in executeScript\n outframe = mod.azureml_main(*inframes)\n File \"\\temp\\1280677032.py\", line 1094, in azureml_main\n File \"<ipython-input-15-bd03d199b8d9>\", line 6, in TechBot_2\nImportError: No module named azure\n\r\n\r\n---------- End of error message from Python interpreter ----------" 
     } 
    ] 
    } 
} 

根據錯誤代碼00085 &信息ImportError: No module named azure,我認爲這個問題是通過導入Python模塊azure-storage引起的。有類似的SO線程Access Azure blog storage from within an Azure ML experiment其中有相同的問題,我想你可以參考它的回答,嘗試在你的代碼中用HTTP協議代替HTTPS來解決問題,代碼爲client = BlobService(STORAGE_ACCOUNT, STORAGE_KEY, protocol="http")

希望它有幫助。任何關注&更新,請隨時讓我知道。


更新:使用HTTP協議BlobService

from azureml import services 
from azure.storage.blob import BlobService 
@services.publish('7c94eb2d9e4c01cbe7ce1063','f78QWNcOXHt9J+Qt1GMzgdEt+m3NXby9JL`npT7XX8ZAGdRZIX/NZ4lL2CkRkGQ==') 
@services.types(res=unicode) 
@services.returns(str) 
def TechBot(res): 
    from azure.storage.blob import BlobService 
    from gensim.similarities import SparseMatrixSimilarity, MatrixSimilarity, Similarity 
# Begin: Update code 
# Using `HTTP` protocol for BlobService 
    blob_service = BlobService(account_name='tfidf', 
       account_key='RU4R/NIVPsPOoR0bgiJMtosHJMbK1+AVHG0sJCHT6jIdKPRz3cIMYTsrQ5BBD5SELKHUXgBHNmvsIlhEdqUCzw==', 
       protocol='http') 
# End 
    blob_service.get_blob_to_path('techbot',"2014.csv","df") 
    df=pd.read_csv("df") 
    doct = res 
+0

我更新的blobservice的協議和在url使用HTTP,現在蔭收到「URLError:<的urlopen錯誤[錯誤10060]連接嘗試失敗因爲關聯方沒有正確迴應。「假設某些連接沒有成功建立。我正在使用'req = urllib2.Request(url,body,headers)'來拉取url請求。我在我的網絡中檢查了prozy,並且沒有任何 –

+0

已更新腳本以反映'Http'和超時變量 –

+0

@SincoleBrans You誤解了我的意思。我建議您嘗試使用'HTTP'協議訪問SDK中的Azure Blob存儲,而不是在azureml Web服務網址中使用'http://'而不是'https://'。請參閱我的Azureml部署更新代碼。 –