Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:A616567126 GPT WEB JAVA BaiduServiceImpl

From Leeroopedia
Knowledge Sources
Domains Translation, Content_Moderation
Last Updated 2026-10-13 00:00 GMT

Overview

Concrete service for Baidu API integration providing Chinese-to-English translation and text content moderation with Redis-cached access tokens.

Description

The BaiduServiceImpl class implements `BaiDuService` with two key functions: `translateToEnglish()` checks if text contains Chinese characters, then calls the Baidu Translate API with MD5 signature authentication (appid+prompt+salt+secret). `textToExamine()` calls Baidu's content censorship API to check if text is compliant, using a Redis-cached access token (29-day TTL) obtained via OAuth client credentials.

Usage

Inject this service when you need to translate Chinese prompts to English for Midjourney, or validate user input for content compliance.

Code Reference

Source Location

Signature

@Service
public class BaiduServiceImpl implements BaiDuService {

    @Override
    public String translateToEnglish(String prompt);

    @Override
    public boolean textToExamine(String prompt);

    static String getAccessToken();

    boolean containsChinese(String prompt);
}

Import

import com.intelligent.bot.service.baidu.impl.BaiduServiceImpl;

I/O Contract

Inputs

Name Type Required Description
prompt String Yes Text to translate or moderate

Outputs

Name Type Description
translateToEnglish String English translation (or original if no Chinese)
textToExamine boolean true if compliant, false if "不合规"

Usage Examples

Translate Chinese to English

// Translate MJ prompt
String english = baiduService.translateToEnglish("一只可爱的猫");
// Returns: "a cute cat"

// Non-Chinese text returns as-is
String same = baiduService.translateToEnglish("hello world");
// Returns: "hello world"

Content Moderation

// Check if text is compliant
boolean compliant = baiduService.textToExamine("正常文本");
// Returns: true

boolean blocked = baiduService.textToExamine("违规内容");
// Returns: false

Access Token Caching

// Token cached in Redis for 29 days
String token = RedisUtil.getCacheObject(CommonConst.BAIDU_TO_EXAMINE_REDIS_KEY);
if (StringUtils.isEmpty(token)) {
    // Fetch new token via OAuth client_credentials
    // Cache with 29-day TTL
    RedisUtil.setCacheObject(key, token, 29L, TimeUnit.DAYS);
}

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment