Skip to content

Instantly share code, notes, and snippets.

View tsh-code's full-sized avatar

TSH code sharing tsh-code

View GitHub Profile
{
"transfer": {
"properties": {
"transfer_type_of_transportation": {
"type": "string",
"description": "if it is car or aircraft or ship"
},
"transfer_person_name": { "type": "string" },
"transfer_departure_from_location": { "type": "string" },
"transfer_arrival_to_location": { "type": "string" },
@tsh-code
tsh-code / Api.ts
Created May 6, 2024 09:44
Preloaded translation lambdas API stack
import { Api, Function, StackContext, use } from "sst/constructs";
import { Translations } from "./Translations";
export function API({ stack }: StackContext) {
const { translationFn, translationFns } = use(Translations);
const staticTranslationLambdas: Record<string, { function: Function }> = Object.fromEntries(
translationFns.map(({ key, fn }) => [`POST /api/translate/${key}`, { function: fn }])
);
@tsh-code
tsh-code / Translations.ts
Created May 6, 2024 09:43
multiple preloaded lambdas stack
import { StackContext, Function, Stack } from "sst/constructs";
const LANGUAGE_PAIRS = [
["es", "en"],
["pl", "en"],
] as const;
interface UnidirectionalTranslation {
from: string;
to: string;
}
@tsh-code
tsh-code / handler.py
Created May 6, 2024 09:42
Preloaded translation lambdas handler
from easynmt import EasyNMT
import json
import os
def main(event, context):
source_lang = os.environ.get("FROM_LANGUAGE_CODE")
target_lang = os.environ.get("TO_LANGUAGE_CODE")
try:
body_dict = json.loads(event.get("body", "{}"))
@tsh-code
tsh-code / Dockerfile
Created May 6, 2024 09:41
Preloaded translation lambdas
FROM alpine:latest AS model
ARG LANGUAGE_CODE_PAIR
ENV LANGUAGE_CODE_PAIR=$LANGUAGE_CODE_PAIR
RUN test -n "$LANGUAGE_CODE_PAIR" || (echo "ERROR: LANGUAGE_CODE_PAIR is empty!" && exit 1)
RUN apk update && apk add --no-cache curl git git-lfs
RUN git lfs install
RUN git clone https://huggingface.co/Helsinki-NLP/opus-mt-${LANGUAGE_CODE_PAIR} /tmp/models
import { SSTConfig } from "sst";
import { Translations } from "./stacks/Translations";
import { API } from "./stacks/Api";
export default {
config(_input) {
return {
name: "easynmt-opus-translation",
region: "eu-west-1",
};
@tsh-code
tsh-code / Api.ts
Created May 6, 2024 09:39
Single function Api stack
import { Api, StackContext, use } from "sst/constructs";
import { Translations } from "./Translations";
export function API({ stack }: StackContext) {
const { translationFn } = use(Translations);
new Api(stack, "translation-api", {
routes: {
"POST /api/translate/{languageCode}": {
cdk: {
@tsh-code
tsh-code / Translations.ts
Created May 6, 2024 09:38
Single translation function stack.
import { StackContext, Function } from "sst/constructs";
export function Translations({ stack }: StackContext) {
const translationFn = new Function(stack, "TranslationLambda", {
runtime: "container",
handler: "packages/functions/src/translation",
memorySize: 6000,
diskSize: 1024,
timeout: "10 minutes",
});
import { StackContext, Function, Stack } from "sst/constructs";
const LANGUAGE_PAIRS = [
["es", "en"],
["pl", "en"],
] as const;
interface UnidirectionalTranslation {
from: string;
to: string;
}
from easynmt import EasyNMT
import json
def main(event, context):
path_params = event.get("pathParameters", {})
try:
body_dict = json.loads(event.get("body", "{}"))
except json.JSONDecodeError as e:
print(f"Error parsing body JSON: {e}")