1.5.1 Java sdk

优质
小牛编辑
135浏览
2023-12-01

Java sdk获取

如果有需求请联系生态云团队: eco-cloud@xiaomi.com

调用示例

翻译

package com.mycompany.app;

import com.google.gson.Gson;

import com.xiaomi.infra.galaxy.ai.common.Credential;
import com.xiaomi.infra.galaxy.vision.client.GalaxyVisionClient;
import com.xiaomi.infra.galaxy.vision.client.VisionConfig;
import com.xiaomi.infra.galaxy.vision.model.TranslationRequest;
import com.xiaomi.infra.galaxy.vision.model.TranslationResult;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.io.IOException;

public class App 
{
    public static void main( String[] args )
    {
        translationTest();
    }

    public static void translationTest() {
        Credential credential = new Credential("id", "secret key");
        VisionConfig config = new VisionConfig("cnbj2.vision.api.xiaomi.net");
        GalaxyVisionClient visionClient = new GalaxyVisionClient(credential, config);

        // send translation request
        TranslationRequest translationRequest = new TranslationRequest();
        translationRequest.setAppId("iam_test_app");
        translationRequest.setRequestId(UUID.randomUUID().toString());
        translationRequest.setFrom("en");
        translationRequest.setTo("zh-chs");
        translationRequest.setQuery("good");

        try {
            TranslationResult result = visionClient.translate(translationRequest);
            System.out.println("translation result: " + new Gson().toJson(result));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

结果:

translation result: {"landingPageUrl":"https://dictweb.translator.qq.com/?id\u003d44d35c0d5b0bb0252821f55f432a3c59","translation":["合意的,满意的"],"basic":{"explains":["adj.合意的,满意的","n.善;正义","adv.好地"]},"quickAppUrl":"https://dictweb.translator.qq.com/?id\u003d44d35c0d5b0bb0252821f55f432a3c59","engine":"TENCENT","status":0,"requestId":"ad52e7f7-e10c-4549-819e-ea8d917752b7"}

语言检测

package com.mycompany.app;

import com.google.gson.Gson;

import com.xiaomi.infra.galaxy.ai.common.Credential;
import com.xiaomi.infra.galaxy.vision.client.GalaxyVisionClient;
import com.xiaomi.infra.galaxy.vision.client.VisionConfig;

import com.xiaomi.infra.galaxy.vision.model.LanguageDetectRequest;
import com.xiaomi.infra.galaxy.vision.model.LanguageDetectResult;
import com.xiaomi.infra.galaxy.vision.model.LanguageDetectText;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.io.IOException;

public class App 
{
    public static void main( String[] args )
    {
        languageDetectTest();
    }

    public static void languageDetectTest() {
        Credential credential = new Credential("id", "secret key");
        VisionConfig config = new VisionConfig("cnbj2.vision.api.xiaomi.net");
        GalaxyVisionClient visionClient = new GalaxyVisionClient(credential, config);

        // send translation request
        LanguageDetectRequest languageDetectRequest = new LanguageDetectRequest();
        languageDetectRequest.setAppId("iam_test_app");
        languageDetectRequest.setRequestId(UUID.randomUUID().toString());

        List<LanguageDetectText> texts = new ArrayList<LanguageDetectText>();
        LanguageDetectText text = new LanguageDetectText();
        text.setText("it is a fine day. 今天天气真好.");
        texts.add(text);
        languageDetectRequest.setTexts(texts);

        LanguageDetectResult result;
        try {
            result = visionClient.languageDetect(languageDetectRequest);
            System.out.println("language detect result: " + new Gson().toJson(result));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

结果:

language detect result: {"languages":[{"score":0.55,"language":"zh-chs"}]}