我想在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一半)
我更新的blobservice的協議和在url使用HTTP,現在蔭收到「URLError:<的urlopen錯誤[錯誤10060]連接嘗試失敗因爲關聯方沒有正確迴應。「假設某些連接沒有成功建立。我正在使用'req = urllib2.Request(url,body,headers)'來拉取url請求。我在我的網絡中檢查了prozy,並且沒有任何 –
已更新腳本以反映'Http'和超時變量 –
@SincoleBrans You誤解了我的意思。我建議您嘗試使用'HTTP'協議訪問SDK中的Azure Blob存儲,而不是在azureml Web服務網址中使用'http://'而不是'https://'。請參閱我的Azureml部署更新代碼。 –