Share via


Azure AI 服務

Azure AI 服務可協助開發人員和組織使用現成、預先建置且可自訂的 API 和模型,快速建立智慧型、最先進的市場就緒及負責任應用程式。

SynapseML 可讓您從各種 Spark 資料來源建置功能強大且可調整程度高的預測和分析模型。 Synapse Spark 提供內建的 SynapseML 連結庫,包括 synapse.ml.services。

重要

從 2023 年 9 月 20 日起,您將無法建立新的 異常偵測程式 資源。 異常偵測程式 服務將於 2026 年 10 月 1 日淘汰。

Azure Synapse Analytics 的必要條件

在 Azure Synapse 中使用 Azure AI 服務的必要條件教學課程會逐步引導您完成在 Synapse Analytics 中使用 Azure AI 服務之前必須執行的幾個步驟。

Azure AI 服務 是一套 API、SDK 和服務,開發人員可用來將智慧型手機新增至其應用程式。 即使開發人員沒有直接 AI 或數據科學技能或知識,AI 服務也能賦予開發人員權力。 Azure AI 服務可協助開發人員建立可查看、聆聽、說話、瞭解,甚至開始推理的應用程式。 Azure AI 服務內的服務目錄可分為五個主要要素:視覺、語音、語言、Web 搜尋和決策。

使用方式

視覺

電腦視覺

  • 描述:以人類可讀語言 (ScalaPython) 提供影像的描述
  • 分析 (色彩、影像類型、臉部、成人/猥褻內容):分析影像的視覺特徵 (ScalaPython)
  • OCR:從影像讀取文字 (ScalaPython)
  • 辨識文字:從影像讀取文字 (ScalaPython)
  • 影片縮圖:從影像產生使用者指定大小的縮圖 (ScalaPython)
  • 辨識網域特定內容:辨識網域特定內容 (名人、地標)(ScalaPython)
  • 標籤:識別與輸入影像相關的單字清單 (ScalaPython)

臉部

  • 偵測:偵測影像中的人臉 (ScalaPython)
  • 驗證:確認兩個兩部是否屬於同一個人,或一個臉部是否屬於某個人 (ScalaPython)
  • 識別:從人員群組尋找特定查詢人員臉部最相符的項目 (ScalaPython)
  • 尋找類似:在臉部清單中尋找與查詢類似的臉部 (ScalaPython)
  • 群組:根據相似性將一組臉部分成不相交的群組 (ScalaPython)

Speech

語音服務

  • 語音轉換文字:轉譯音訊串流 (ScalaPython)
  • 對話謄寫:透過已識別的說話者將音訊串流轉譯為即時文字記錄。 (ScalaPython)
  • 文字轉換語音:將文字轉換為真實音訊 (ScalaPython)

語言

AI 語言

  • 語言偵測:偵測輸入文字的語言 (ScalaPython)
  • 關鍵片語擷取:識別輸入文字中的重要話題 (ScalaPython)
  • 具名實體辨識:識別輸入文字中的已知實體和一般具名實體 (ScalaPython)
  • 情感分析:傳回介於 0 到 1 之間的分數,指出輸入文字中的情感 (ScalaPython)
  • 醫療保健實體擷取:從文字擷取醫療實體和關聯性。 (ScalaPython)

翻譯

翻譯工具

  • 翻譯:翻譯文字。 (ScalaPython)
  • 轉換功能:將某種語言的文字從某個指令碼轉換成另一個指令碼。 (ScalaPython)
  • 偵測:識別一段文字的語言。 (ScalaPython)
  • 中斷句子:識別句子界限在一段文字裡的位置。 (ScalaPython)
  • 字典查閱:提供字組和少數慣用語的替代翻譯。 (ScalaPython)
  • 字典範例:提供範例以說明字典中的字詞在內容中的使用方式。 (ScalaPython)
  • 文件翻譯:跨所有支援的語言和方言翻譯文件,同時保留文件結構和資料格式。 (ScalaPython)

文件智慧服務

文件智慧

  • 分析版面配置:擷取指定文件的文字和版面配置資訊。 (ScalaPython)
  • 分析收據:使用光學字元辨識 (OCR) 和收據模型偵測並擷取收據的資料,讓您輕鬆擷取收據的結構化資料,例如商家名稱、商家電話號碼、交易日期、交易總額等。 (ScalaPython)
  • 分析名片:使用光學字元辨識 (OCR) 和名片模型偵測並擷取名片的資料,讓您輕鬆擷取名片的結構化資料,例如連絡人姓名、公司名稱、電話號碼、電子郵件等。 (ScalaPython)
  • 分析發票:使用光學字元辨識 (OCR) 和發票理解深度學習模型,偵測並擷取發票的資料,讓您輕鬆擷取發票的結構化資料,例如客戶、供應商、發票識別碼、發票到期日、總額、發票應付金額、稅額、收件人、付款人、明細項目等。 (ScalaPython)
  • 分析身分證明文件:使用光學字元辨識 (OCR) 和身分證明文件模型偵測並擷取身份證明文件的資料,讓您輕鬆擷取身份證明文件的結構化資料,例如名字、姓氏、出生日期、文件編號等。 (ScalaPython)
  • 分析自訂表單:根據從一組代表性定型表單建立的模型,從表單 (PDF 和影像) 擷取資訊到結構化資料。 (ScalaPython)
  • 取得自訂模型:取得自訂模型的詳細資訊。 (ScalaPython)
  • 列出自訂模型:取得所有自訂模型的相關資訊。 (ScalaPython)

Decision

異常偵測器

  • 最新點的異常狀態:使用上述點產生模型,並判斷最新點是否異常 (ScalaPython)
  • 尋找異常:使用整個數列產生模型,並尋找數列中的異常狀況 (ScalaPython)

準備您的系統

若要開始,請匯入必要的連結庫,並初始化 Spark 工作階段。

from pyspark.sql.functions import udf, col
from synapse.ml.io.http import HTTPTransformer, http_udf
from requests import Request
from pyspark.sql.functions import lit
from pyspark.ml import PipelineModel
from pyspark.sql.functions import col

匯入 Azure AI 服務連結庫,並將下列代碼段中的密鑰和位置取代為您的 Azure AI 服務密鑰和位置。

from synapse.ml.services import *
from synapse.ml.core.platform import *

# A general AI services key for AI Language, Computer Vision and Document Intelligence (or use separate keys that belong to each service)
service_key = find_secret(
    secret_name="ai-services-api-key", keyvault="mmlspark-build-keys"
)  # Replace the call to find_secret with your key as a python string. e.g. service_key="27snaiw..."
service_loc = "eastus"

# A Bing Search v7 subscription key
bing_search_key = find_secret(
    secret_name="bing-search-key", keyvault="mmlspark-build-keys"
)  # Replace the call to find_secret with your key as a python string.

# An Anomaly Detector subscription key
anomaly_key = find_secret(
    secret_name="anomaly-api-key", keyvault="mmlspark-build-keys"
)  # Replace the call to find_secret with your key as a python string. If you don't have an anomaly detection resource created before Sep 20th 2023, you won't be able to create one.
anomaly_loc = "westus2"

# A Translator subscription key
translator_key = find_secret(
    secret_name="translator-key", keyvault="mmlspark-build-keys"
)  # Replace the call to find_secret with your key as a python string.
translator_loc = "eastus"

# An Azure search key
search_key = find_secret(
    secret_name="azure-search-key", keyvault="mmlspark-build-keys"
)  # Replace the call to find_secret with your key as a python string.

對文字執行情感分析

AI 語言服務提供數種演算法,可從文字擷取智慧型手機深入解析。 例如,我們可以找到指定輸入文字的情感。 服務會傳回介於 0.0 和 1.0 之間的分數,低分表示負面情感,而高分表示正面情感。 此範例會使用三個簡單的句子,並傳回每個句子的情感。

# Create a dataframe that's tied to it's column names
df = spark.createDataFrame(
    [
        ("I am so happy today, its sunny!", "en-US"),
        ("I am frustrated by this rush hour traffic", "en-US"),
        ("The AI services on spark aint bad", "en-US"),
    ],
    ["text", "language"],
)

# Run the Text Analytics service with options
sentiment = (
    AnalyzeText()
    .setKind("SentimentAnalysis")
    .setTextCol("text")
    .setLocation(service_loc)
    .setSubscriptionKey(service_key)
    .setOutputCol("sentiment")
    .setErrorCol("error")
    .setLanguageCol("language")
)

# Show the results of your text query in a table format
display(
    sentiment.transform(df).select(
        "text", col("sentiment.documents.sentiment").alias("sentiment")
    )
)

執行健康情況數據的文字分析

健全狀況服務 文字分析 從醫生筆記、出院摘要、臨床文件和電子健康記錄等非結構化文字中擷取和標記相關的醫療資訊。

下列程式代碼範例會分析並將醫生筆記中的文字轉換成結構化數據。

df = spark.createDataFrame(
    [
        ("20mg of ibuprofen twice a day",),
        ("1tsp of Tylenol every 4 hours",),
        ("6-drops of Vitamin B-12 every evening",),
    ],
    ["text"],
)

healthcare = (
    AnalyzeHealthText()
    .setSubscriptionKey(service_key)
    .setLocation(service_loc)
    .setLanguage("en")
    .setOutputCol("response")
)

display(healthcare.transform(df))

將文字翻譯成不同的語言

翻譯工具 是雲端式機器翻譯服務,是用來建置智慧型手機應用程式的 Azure AI 服務系列 AI API 的一部分。 您可以輕鬆地在應用程式、網站、工具和解決方案中整合翻譯工具。 它可讓您以90種語言和方言新增多語言用戶體驗,並可用來翻譯文字,而不需要裝載您自己的演算法。

下列程式代碼範例會提供您想要翻譯的句子,以及您想要翻譯的目標語言,藉以執行簡單的文字翻譯。

from pyspark.sql.functions import col, flatten

# Create a dataframe including sentences you want to translate
df = spark.createDataFrame(
    [(["Hello, what is your name?", "Bye"],)],
    [
        "text",
    ],
)

# Run the Translator service with options
translate = (
    Translate()
    .setSubscriptionKey(translator_key)
    .setLocation(translator_loc)
    .setTextCol("text")
    .setToLanguage(["zh-Hans"])
    .setOutputCol("translation")
)

# Show the results of the translation.
display(
    translate.transform(df)
    .withColumn("translation", flatten(col("translation.translations")))
    .withColumn("translation", col("translation.text"))
    .select("translation")
)

從檔擷取資訊到結構化數據

Azure AI 檔智慧 是 Azure 應用 AI 服務的一部分,可讓您使用機器學習技術建置自動化數據處理軟體。 透過 Azure AI 檔智慧,您可以從文件中識別及擷取文字、索引鍵/值組、選取標記、數據表和結構。 服務輸出的結構化資料包含在原始文件中的關係、週框方塊和信賴度等等。

下列程式代碼範例會分析名片影像,並將其資訊擷取至結構化數據。

from pyspark.sql.functions import col, explode

# Create a dataframe containing the source files
imageDf = spark.createDataFrame(
    [
        (
            "https://mmlspark.blob.core.windows.net/datasets/FormRecognizer/business_card.jpg",
        )
    ],
    [
        "source",
    ],
)

# Run the Form Recognizer service
analyzeBusinessCards = (
    AnalyzeBusinessCards()
    .setSubscriptionKey(service_key)
    .setLocation(service_loc)
    .setImageUrlCol("source")
    .setOutputCol("businessCards")
)

# Show the results of recognition.
display(
    analyzeBusinessCards.transform(imageDf)
    .withColumn(
        "documents", explode(col("businessCards.analyzeResult.documentResults.fields"))
    )
    .select("source", "documents")
)

電腦視覺範例

Azure AI 視覺 會分析影像,以識別臉部、物件和自然語言描述等結構。

下列程式代碼範例會分析影像,並使用標籤影像。 卷標是影像中事物的單字描述,例如可辨識的對象、人員、風景和動作。

# Create a dataframe with the image URLs
base_url = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/"
df = spark.createDataFrame(
    [
        (base_url + "objects.jpg",),
        (base_url + "dog.jpg",),
        (base_url + "house.jpg",),
    ],
    [
        "image",
    ],
)

# Run the Computer Vision service. Analyze Image extracts information from/about the images.
analysis = (
    AnalyzeImage()
    .setLocation(service_loc)
    .setSubscriptionKey(service_key)
    .setVisualFeatures(
        ["Categories", "Color", "Description", "Faces", "Objects", "Tags"]
    )
    .setOutputCol("analysis_results")
    .setImageUrlCol("image")
    .setErrorCol("error")
)

# Show the results of what you wanted to pull out of the images.
display(analysis.transform(df).select("image", "analysis_results.description.tags"))

Bing 影像搜尋會搜尋 Web,以擷取與使用者的自然語言查詢相關的影像。

下列程式代碼範例會使用文字查詢來尋找具有引號的影像。 程式代碼的輸出是影像 URL 清單,其中包含與查詢相關的相片。

# Number of images Bing will return per query
imgsPerBatch = 10
# A list of offsets, used to page into the search results
offsets = [(i * imgsPerBatch,) for i in range(100)]
# Since web content is our data, we create a dataframe with options on that data: offsets
bingParameters = spark.createDataFrame(offsets, ["offset"])

# Run the Bing Image Search service with our text query
bingSearch = (
    BingImageSearch()
    .setSubscriptionKey(bing_search_key)
    .setOffsetCol("offset")
    .setQuery("Martin Luther King Jr. quotes")
    .setCount(imgsPerBatch)
    .setOutputCol("images")
)

# Transformer that extracts and flattens the richly structured output of Bing Image Search into a simple URL column
getUrls = BingImageSearch.getUrlTransformer("images", "url")

# This displays the full results returned, uncomment to use
# display(bingSearch.transform(bingParameters))

# Since we have two services, they are put into a pipeline
pipeline = PipelineModel(stages=[bingSearch, getUrls])

# Show the results of your search: image URLs
display(pipeline.transform(bingParameters))

將語音轉換成文字

語音轉換文字服務會將說話音訊的串流或檔案轉換成文字。 下列程式代碼範例會將一個音訊檔案轉譯為文字。

# Create a dataframe with our audio URLs, tied to the column called "url"
df = spark.createDataFrame(
    [("https://mmlspark.blob.core.windows.net/datasets/Speech/audio2.wav",)], ["url"]
)

# Run the Speech-to-text service to translate the audio into text
speech_to_text = (
    SpeechToTextSDK()
    .setSubscriptionKey(service_key)
    .setLocation(service_loc)
    .setOutputCol("text")
    .setAudioDataCol("url")
    .setLanguage("en-US")
    .setProfanity("Masked")
)

# Show the results of the translation
display(speech_to_text.transform(df).select("url", "text.DisplayText"))

將文字轉換成語音

文字到語音 轉換是一項服務,可讓您建置自然說話的應用程式和服務,從 119 種語言和變體的 270 多個神經語音中選擇。

下列程式代碼範例會將文字轉換成包含文字內容的音訊檔案。

from synapse.ml.services.speech import TextToSpeech

fs = ""
if running_on_databricks():
    fs = "dbfs:"
elif running_on_synapse_internal():
    fs = "Files"

# Create a dataframe with text and an output file location
df = spark.createDataFrame(
    [
        (
            "Reading out loud is fun! Check out aka.ms/spark for more information",
            fs + "/output.mp3",
        )
    ],
    ["text", "output_file"],
)

tts = (
    TextToSpeech()
    .setSubscriptionKey(service_key)
    .setTextCol("text")
    .setLocation(service_loc)
    .setVoiceName("en-US-JennyNeural")
    .setOutputFileCol("output_file")
)

# Check to make sure there were no errors during audio creation
display(tts.transform(df))

偵測時間序列數據中的異常

如果您沒有在 2023 年 9 月 20 日之前建立異常偵測資源,您將無法建立一個。 您可能想要略過此部分。

異常偵測器非常適合用來偵測時間序列資料中的異常狀況。 下列程式代碼範例會使用 異常偵測程式 服務來尋找時間序列中的異常狀況。

# Create a dataframe with the point data that Anomaly Detector requires
df = spark.createDataFrame(
    [
        ("1972-01-01T00:00:00Z", 826.0),
        ("1972-02-01T00:00:00Z", 799.0),
        ("1972-03-01T00:00:00Z", 890.0),
        ("1972-04-01T00:00:00Z", 900.0),
        ("1972-05-01T00:00:00Z", 766.0),
        ("1972-06-01T00:00:00Z", 805.0),
        ("1972-07-01T00:00:00Z", 821.0),
        ("1972-08-01T00:00:00Z", 20000.0),
        ("1972-09-01T00:00:00Z", 883.0),
        ("1972-10-01T00:00:00Z", 898.0),
        ("1972-11-01T00:00:00Z", 957.0),
        ("1972-12-01T00:00:00Z", 924.0),
        ("1973-01-01T00:00:00Z", 881.0),
        ("1973-02-01T00:00:00Z", 837.0),
        ("1973-03-01T00:00:00Z", 9000.0),
    ],
    ["timestamp", "value"],
).withColumn("group", lit("series1"))

# Run the Anomaly Detector service to look for irregular data
anamoly_detector = (
    SimpleDetectAnomalies()
    .setSubscriptionKey(anomaly_key)
    .setLocation(anomaly_loc)
    .setTimestampCol("timestamp")
    .setValueCol("value")
    .setOutputCol("anomalies")
    .setGroupbyCol("group")
    .setGranularity("monthly")
)

# Show the full results of the analysis with the anomalies marked as "True"
display(
    anamoly_detector.transform(df).select("timestamp", "value", "anomalies.isAnomaly")
)

從任意 Web API 取得資訊

在 Spark 上使用 HTTP 時,任何 Web 服務都可以在您的管線中使用。 在此範例中 ,我們使用世界銀行 API 來取得世界各國的相關信息。

# Use any requests from the python requests library

def world_bank_request(country):
    return Request(
        "GET", "http://api.worldbank.org/v2/country/{}?format=json".format(country)
    )

# Create a dataframe with specifies which countries we want data on
df = spark.createDataFrame([("br",), ("usa",)], ["country"]).withColumn(
    "request", http_udf(world_bank_request)(col("country"))
)

# Much faster for big data because of the concurrency :)
client = (
    HTTPTransformer().setConcurrency(3).setInputCol("request").setOutputCol("response")
)

# Get the body of the response

def get_response_body(resp):
    return resp.entity.content.decode()

# Show the details of the country data returned
display(
    client.transform(df).select(
        "country", udf(get_response_body)(col("response")).alias("response")
    )
)

Azure AI 搜尋範例

在此範例中,我們會示範如何使用認知技能擴充資料,並使用 SynapseML 寫入 Azure 搜尋服務索引。

search_service = "mmlspark-azure-search"
search_index = "test-33467690"

df = spark.createDataFrame(
    [
        (
            "upload",
            "0",
            "https://mmlspark.blob.core.windows.net/datasets/DSIR/test1.jpg",
        ),
        (
            "upload",
            "1",
            "https://mmlspark.blob.core.windows.net/datasets/DSIR/test2.jpg",
        ),
    ],
    ["searchAction", "id", "url"],
)

tdf = (
    AnalyzeImage()
    .setSubscriptionKey(service_key)
    .setLocation(service_loc)
    .setImageUrlCol("url")
    .setOutputCol("analyzed")
    .setErrorCol("errors")
    .setVisualFeatures(
        ["Categories", "Tags", "Description", "Faces", "ImageType", "Color", "Adult"]
    )
    .transform(df)
    .select("*", "analyzed.*")
    .drop("errors", "analyzed")
)

tdf.writeToAzureSearch(
    subscriptionKey=search_key,
    actionCol="searchAction",
    serviceName=search_service,
    indexName=search_index,
    keyCol="id",
)