Skip to content

Commit

Permalink
feat: update translation endpoint to be compatible for different endp…
Browse files Browse the repository at this point in the history
…oints
  • Loading branch information
ebubae committed Nov 11, 2024
1 parent c33de71 commit 0244e95
Showing 1 changed file with 31 additions and 17 deletions.
48 changes: 31 additions & 17 deletions src/controllers/translation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from 'axios';
import { MiddleWare } from '../types';
import { IGBO_TO_ENGLISH_API, MAIN_KEY } from '../config';
import { ENGLIGH_TO_IGBO_API, IGBO_TO_ENGLISH_API, MAIN_KEY } from '../config';
import { z } from 'zod';
import { fromError } from 'zod-validation-error';
import LanguageEnum from '../shared/constants/LanguageEnum';
Expand All @@ -19,9 +19,18 @@ export interface Translation {
translation: string;
}

// Due to limit on inputs used to train the model, the maximum
// Igbo translation input is 120 characters
const IGBO_ENGLISH_TRANSLATION_INPUT_MAX_LENGTH = 120;
// TODO: move this information to remote config
const SUPPORTED_TRANSLATIONS = {
[`${LanguageEnum.IGBO}to${LanguageEnum.ENGLISH}`]: {
maxInputLength: 120,
translationAPI: IGBO_TO_ENGLISH_API,
},
[`${LanguageEnum.ENGLISH}to${LanguageEnum.IGBO}`]: {
maxInputLength: 150,
translationAPI: ENGLIGH_TO_IGBO_API,
},
} as const;
type SupportedTranslationCodes = keyof typeof SUPPORTED_TRANSLATIONS;

/**
* Talks to Igbo-to-English translation model to translate the provided text.
Expand All @@ -32,43 +41,48 @@ const IGBO_ENGLISH_TRANSLATION_INPUT_MAX_LENGTH = 120;
*/
export const getTranslation: MiddleWare = async (req, res, next) => {
try {
console.log('entered translation endpoint');
console.log('request body', req.body);
const requestBodyValidation = TranslationRequestBody.safeParse(req.body);
if (!requestBodyValidation.success) {
throw fromError(requestBodyValidation.error);
}

const requestBody = requestBodyValidation.data;
console.log(`validation succeeded, body: ${requestBody}`);

if (requestBody.sourceLanguageCode === requestBody.destinationLanguageCode) {
throw new Error('Source and destination languages must be different');
}

if (
requestBody.sourceLanguageCode !== LanguageEnum.IGBO ||
requestBody.destinationLanguageCode !== LanguageEnum.ENGLISH
!(
`${requestBody.sourceLanguageCode}to${requestBody.destinationLanguageCode}` in
SUPPORTED_TRANSLATIONS
)
) {
throw new Error(
`${requestBody.sourceLanguageCode} to ${requestBody.destinationLanguageCode} translation is not yet supported`
);
}
const igboText = requestBody.text;
if (!igboText) {
const textToTranslate = requestBody.text;
if (!textToTranslate) {
throw new Error('Cannot translate empty string');
}

if (igboText.length > IGBO_ENGLISH_TRANSLATION_INPUT_MAX_LENGTH) {
throw new Error('Cannot translate text greater than 120 characters');
const translationKey =
`${requestBody.sourceLanguageCode}to${requestBody.destinationLanguageCode}` as SupportedTranslationCodes;
// TODO: update this use map based on language
if (textToTranslate.length > SUPPORTED_TRANSLATIONS[translationKey].maxInputLength) {
throw new Error(
`Cannot translate text greater than ${SUPPORTED_TRANSLATIONS[translationKey].maxInputLength} characters`
);
}

const payload: IgboEnglishTranslationMetadata = { igbo: igboText };
// TODO: joint model will standardize request
const payload =
translationKey == 'ibotoeng' ? { igbo: textToTranslate } : { english: textToTranslate };

// Talks to translation endpoint
console.log('making translation request');
const { data: response } = await axios.request<Translation>({
method: 'POST',
url: IGBO_TO_ENGLISH_API,
url: SUPPORTED_TRANSLATIONS[translationKey].translationAPI,
headers: {
'Content-Type': 'application/json',
'X-API-Key': MAIN_KEY,
Expand Down

0 comments on commit 0244e95

Please sign in to comment.