将 Fabric 中预生成的 Azure AI 翻译与 REST API 和 SynapseML 结合使用(预览版)

重要

此功能目前为预览版

Azure AI 翻译是一项 Azure AI 服务,可用于执行语言翻译和其他与语言相关的操作。

此示例演示如何在 Fabric 中使用预构建的 Azure AI 翻译器和 RESTful API:

  • 翻译文本
  • 直译文本
  • 获取支持的语言

先决条件

  • 创建新的笔记本
  • 将笔记本附加到湖屋。 在笔记本的左侧,选择 添加 来添加已有的湖仓或创建一个新的湖仓。

注释

本文使用 Microsoft Fabric 的内置预生成 AI 服务,这些服务会自动处理身份验证。 无需获取单独的 Azure AI 服务密钥 - 身份验证是通过 Fabric 工作区管理的。 有关详细信息,请参阅 Fabric 中的预生成 AI 模型(预览版)。

本文中的代码示例使用预安装在 Microsoft Fabric 笔记本中的库:

  • SynapseML:预安装在 Fabric 笔记本中,用于机器学习功能
    • synapse.ml.core - 核心 SynapseML 功能
    • synapse.ml.fabric.service_discovery - Fabric 服务发现实用工具
    • synapse.ml.fabric.token_utils - 身份验证令牌实用工具
    • synapse.ml.services - AI 服务集成(包括 Translate、Transliterate 类)
  • PySpark:默认情况下在 Fabric Spark 计算中可用
    • pyspark.sql.functions - 数据帧转换函数 (colflatten
  • 标准 Python 库:内置于 Python 运行时
    • json - JSON 分析和格式设置
    • requests - 用于 REST API 调用的 HTTP 客户端
# Get workload endpoints and access token

from synapse.ml.fabric.service_discovery import get_fabric_env_config
from synapse.ml.fabric.token_utils import TokenUtils
import json
import requests


fabric_env_config = get_fabric_env_config().fabric_env_config
auth_header = TokenUtils().get_openai_auth_header()

# Make a RESTful request to AI service
prebuilt_AI_base_host = fabric_env_config.ml_workload_endpoint + "cognitive/texttranslation/"
print("Workload endpoint for AI service: \n" + prebuilt_AI_base_host)

service_url = prebuilt_AI_base_host + "language/:analyze-text?api-version=2022-05-01"
print("Service URL: \n" + service_url)

auth_headers = {
    "Authorization" : auth_header
}

def print_response(response):
    print(f"HTTP {response.status_code}")
    if response.status_code == 200:
        try:
            result = response.json()
            print(json.dumps(result, indent=2, ensure_ascii=False))
        except:
            print(f"pasre error {response.content}")
    else:
        print(f"error message: {response.content}")

文本翻译

文本翻译是翻译服务的核心工作。

service_url = prebuilt_AI_base_host + "translate?api-version=3.0&to=fr"
post_body = [{'Text':'Hello, friend.'}]

response = requests.post(service_url, json=post_body, headers=auth_headers)

# Output all information of the request process
print_response(response)

输出

    HTTP 200
    [
      {
        "detectedLanguage": {
          "language": "en",
          "score": 1.0
        },
        "translations": [
          {
            "text": "Bonjour cher ami.",
            "to": "fr"
          }
        ]
      }
    ]

文本音译

音译根据拼音相似性将一种语言的脚本(字母)中的单词或短语转换为另一种语言。

service_url = prebuilt_AI_base_host + "transliterate?api-version=3.0&language=ja&fromScript=Jpan&toScript=Latn"
post_body = [
    {"Text":"こんにちは"},
    {"Text":"さようなら"}
]

response = requests.post(service_url, json=post_body, headers=auth_headers)

# Output all information of the request process
print_response(response)

输出

    HTTP 200
    [
      {
        "text": "Kon'nichiwa​",
        "script": "Latn"
      },
      {
        "text": "sayonara",
        "script": "Latn"
      }
    ]

支持的语言检索

返回翻译器支持的语言列表。

service_url = prebuilt_AI_base_host + "languages?api-version=3.0"

response = requests.get(service_url, headers=auth_headers)

# Output all information of the request process
print_response(response)