Azure AI サービス

Azure AI サービスは、開発者と組織が、すぐに使用できる事前構築済みのカスタマイズ可能な API とモデルを使用して、インテリジェントで最先端の市場対応の責任あるアプリケーションを迅速に作成するのに役立ちます。

SynapseML を使用すると、効果的で拡張性の高い予測と分析のモデルをさまざまな Spark データ ソースから構築できます。 Synapse Spark には、synapse.ml.services などの組み込みの SynapseML ライブラリが用意されています。

重要

2023 年 9 月 20 日以降は、新しい Anomaly Detector リソースを作成できなくなります。 Anomaly Detector サービスは、2026 年 10 月 1 日に廃止されます。

Azure Synapse Analytics の前提条件

チュートリアル「Azure Synapse で Azure AI サービスを使用するための前提条件」では、Synapse Analytics で Azure AI サービスを使用する前に行う必要があるいくつかの手順について説明しています。

Azure AI サービスは、開発者がアプリケーションにインテリジェントな機能を追加するために使用できる、一連の API、SDK、サービスです。 AI サービスは、AI やデータ サイエンスの直接的なスキルや知識を持っていない場合でも開発者が機能を利用できるようにします。 Azure AI サービスでは、開発者が、聞いたり、話したり、理解したり、推論し始めたりできるアプリケーションの作成を支援します。 Azure AI サービス内のサービス カタログは、5 つの主要な柱として視覚、音声、言語、Web 検索、意思決定に分類できます。

使用方法

視覚

Computer Vision

  • 説明: 人間が判読できる言語で画像の説明を提供します (ScalaPython)
  • 分析 (色、画像の種類、顔、成人向け/わいせつなコンテンツ): 画像の視覚的特徴を分析します (ScalaPython)
  • OCR: 画像からテキストを読み取ります (ScalaPython)
  • テキスト認識: 画像からテキストを読み取ります (ScalaPython)
  • サムネイル: 画像からユーザー指定のサイズのサムネイルを生成します (ScalaPython)
  • ドメイン固有のコンテンツの認識: ドメイン固有のコンテンツ (有名人、ランドマーク) を認識します (ScalaPython)
  • タグ: 入力画像に関連する単語の一覧を識別します (ScalaPython)

Face

  • 検出: 画像内の人間の顔を検出します (ScalaPython)
  • 検証: 2 つの顔が同じ人物のものであるか、顔がある人物のものであるかを確認します (ScalaPython)
  • 識別: 人物のグループから特定のクエリ対象の顔に最も近い一致を見つけます (ScalaPython)
  • 類似検索: 顔リスト内でクエリの顔と似ている顔を見つけます (ScalaPython)
  • グループ: 顔のグループを類似性に基づいて別々のグループに分割します (ScalaPython)

音声

音声サービス

  • 音声テキスト変換: 音声ストリームを文字に起こします (ScalaPython)
  • 会話の文字起こし: 音声ストリームを、識別された話者のライブ音声テキストに文字起こしします。 (ScalaPython)
  • テキスト読み上げ: テキストをリアルな音声に変換します (ScalaPython)

Language

AI 言語

  • 言語検出: 入力テキストの言語を検出します (ScalaPython)
  • キー フレーズ抽出: 入力テキスト内の主な話の要点を識別します (ScalaPython)
  • 名前付きエンティティ認識: 入力テキスト内の既知のエンティティと一般的な名前付きエンティティを識別します (ScalaPython)
  • 感情分析: 入力テキスト内のセンチメントを示す 0 から 1 のスコアを返します (ScalaPython)
  • 医療エンティティ抽出: テキストから医療エンティティとリレーションシップを抽出します。 (ScalaPython)

翻訳

Translator

  • 翻訳: テキストを翻訳します。 (ScalaPython)
  • 文字変換: ある言語のテキストをあるスクリプトから別のスクリプトに変換します。 (ScalaPython)
  • 検出: テキストの一部の言語を識別します。 (ScalaPython)
  • 文の分割: 文章内で文の境界の位置を識別します。 (ScalaPython)
  • 辞書検索: 単語や少数の慣用句に対して代替の翻訳を提供します。 (ScalaPython)
  • 辞書の例: 辞書内の用語がコンテキストで使用される方法を示す例を提供します。 (ScalaPython)
  • ドキュメント翻訳: ドキュメントの構造とデータ形式を維持しながら、サポートされているすべての言語および方言との間でドキュメントを翻訳します。 (ScalaPython)

Document Intelligence

Document Intelligence

  • レイアウト分析: 指定したドキュメントからテキストとレイアウトの情報を抽出します。 (ScalaPython)
  • 領収書分析: 光学式文字認識 (OCR) と領収書モデルを使用して領収書からデータを検出して抽出します。これらを使用すると、業者名、業者の電話番号、取引日、取引合計など、領収書から構造化データを簡単に抽出できます。 (ScalaPython)
  • 名刺分析: 光学式文字認識 (OCR) と名刺モデルを使用して名刺からデータを検出して抽出します。これらを使用すると、連絡先の名前、会社名、電話番号、メールアドレスなど、名刺から構造化データを簡単に抽出できます。 (ScalaPython)
  • 請求書分析: 光学式文字認識 (OCR) と請求書を解釈するディープ ラーニング モデルを使用して、請求書からデータを検出して抽出します。これらを使用すると、顧客、ベンダー、請求書 ID、請求書の期限、合計、請求金額の期限、税額、出荷先、請求先、明細項目など、請求書から構造化データを簡単に抽出できます。 (ScalaPython)
  • 身分証明書分析: 光学式文字認識 (OCR) と身分証明書モデルを使用して識別ドキュメントからデータを検出して抽出します。これらを使用すると、姓、名、生年月日、ドキュメント番号など、身分証明書から構造化データを簡単に抽出できます。 (ScalaPython)
  • カスタム フォーム分析: 一連の代表的なトレーニング フォームから作成されたモデルに基づいて、フォーム (PDF と画像) から構造化データに情報を抽出します。 (ScalaPython)
  • カスタム モデルの取得: カスタム モデルに関する詳細情報を取得します。 (ScalaPython)
  • カスタム モデルの一覧表示: すべてのカスタム モデルに関する情報を取得します。 (ScalaPython)

決定

Anomaly Detector

  • 最新のポイントの異常状態: 前のポイントを使用してモデルを生成し、最新のポイントが異常であるかどうかを判断します (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 の間のスコアが返されます。低いスコアは否定的なセンチメントを示し、高いスコアは肯定的なセンチメントを示します。 このサンプルでは、3 つの単純な文を使用し、それぞれのセンチメントを返します。

# 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")
    )
)

正常性データのテキスト分析を実行する

Text Analytics for Health サービス により、医師のメモ、退院要約、臨床ドキュメント、電子健康記録などの非構造化テキストから関連する医療情報が抽出されて、ラベルが付けられます。

次のコード サンプルでは、医師のメモのテキストを分析して構造化データに変換します。

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))

別の言語にテキストを翻訳する

Translator はクラウドベースの機械翻訳サービスであり、インテリジェント アプリを構築するたに使用される AI API の Azure AI サービス ファミリの一部です。 Translator は、皆さんのアプリケーションや Web サイト、ツール、ソリューションに簡単に統合することができます。 これを使用すると、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 Document Intelligence は、機械学習テクノロジを利用して自動データ処理ソフトウェアを構築できる Azure Applied AI Services の一部です。 Azure AI Document Intelligence を使用すると、ドキュメントに存在するテキスト、キーと値の組み合わせ、選択マーク、表、構造を特定、抽出します。 このサービスは、ファイル、バウンディング ボックス、機密情報などの元データの要素同士の関係を含む、構造化されたデータを出力します。

次のコード サンプルでは、ビジネス カードイメージを分析し、その情報を構造化データに抽出します。

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")
)

Computer Vision のサンプル

Azure AI Vision では、画像を分析して、顔、オブジェクト、自然言語による記述などの構造を識別します。

次のコード例では、イメージを分析し、タグでラベルを付けます。 タグは、認識可能な物体、人物、風景、アクションなど、画像内のものを 1 単語で表したものです。

# 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 Image Search では、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))

音声をテキストに変換する

音声テキスト変換サービスでは、音声のストリームまたはファイルをテキストに変換します。 次のコード サンプルでは、1 つのオーディオ ファイルをテキストに文字起こしします。

# 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 日より前に作成された異常検出リソースがない場合は、作成できません。 この部分はスキップしてもかまいません。

Anomaly Detector は、時系列データ内の不規則性を検出するのに適しています。 次のコード サンプルでは、Anomaly Detector サービスを使用して、時系列の異常を検出します。

# 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 から情報を取得する

HTTP on Spark を使用すると、任意の Web サービスを自分のビッグ データ パイプラインで使用することができます。 この例では、World Bank 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 インデックスに書き込む方法を示します。

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",
)