Skip to content

Commit

Permalink
updated route and url
Browse files Browse the repository at this point in the history
  • Loading branch information
CodingWithTashi committed Jul 6, 2024
1 parent 2644de6 commit 9941aea
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 20 deletions.
10 changes: 10 additions & 0 deletions src/common/data-param.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,14 @@ export interface DataParam {
* from bo to en and passing bo translate from en to bo.
*/
direction?: "en" | "bo";

/**
* The token to be used for the translation.
*/
csrfToken: string;

/**
* The cookie to be used for the translation.
*/
cookie: string;
}
72 changes: 58 additions & 14 deletions src/common/tb-conversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,34 @@ export async function getTranslateSyncResponse(dataParam: DataParam) {
const formData = new FormData();
formData.append("input", dataParam.input ?? ""); // 'input' is the query
formData.append("direction", dataParam.direction ?? "en"); // 'direction' is set to 'en'
if (!dataParam.input.endsWith(".")) {
dataParam.input = dataParam.input + ".";
}

// Make a POST request to the stream API with custom headers
const url = `https://monlam.ai/api/translation/stream?text=${encodeURIComponent(
dataParam.input
)}&target=${dataParam.direction}&token=${dataParam.csrfToken}`;

// Make a GET request to the stream API with custom headers
const response = await axios({
method: "post",
url: "https://monlam-file-api-latest.onrender.com/mt/playground/stream",
data: formData,
method: "get",
url: url,
headers: {
Accept: "*/*",
Accept: "text/event-stream",
"Accept-Encoding": "gzip, deflate, br, zstd",
"Accept-Language": "en-US,en;q=0.9",
Origin: "https://monlam.ai",
Referer: "https://monlam.ai/",
"Sec-Ch-Ua":
'"Brave";v="125", "Chromium";v="125", "Not.A/Brand";v="24"',
"Sec-Ch-Ua-Mobile": "?1",
"Sec-Ch-Ua-Platform": '"Android"',
"Cache-Control": "no-cache",
Cookie: dataParam.cookie,
Referer: "https://monlam.ai/model/mt?source=en",
"Sec-Ch-Ua": '"Not/A)Brand";v="8", "Chromium";v="126", "Brave";v="126"',
"Sec-Ch-Ua-Mobile": "?0",
"Sec-Ch-Ua-Platform": '"Windows"',
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "cross-site",
"Sec-Fetch-Site": "same-origin",
"Sec-Gpc": "1",
"User-Agent":
"Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Mobile Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36",
},
responseType: "stream",
});
Expand All @@ -53,7 +59,7 @@ export async function getTranslateSyncResponse(dataParam: DataParam) {
lines.forEach((line: any) => {
if (line.trim().startsWith("data:")) {
const json = JSON.parse(line.replace("data:", "").trim());
if (json.token && json.token.special) {
if (json.token.special) {
finalData = json;
}
}
Expand Down Expand Up @@ -84,3 +90,41 @@ export async function getTranslateSyncResponse(dataParam: DataParam) {
throw error;
}
}

export async function fetchCsrfToken() {
try {
const response = await axios({
method: "get",
url: "https://monlam.ai/model/mt",
params: {
source: "en",
_data: "root",
},
headers: {
Accept: "*/*",
"Accept-Encoding": "gzip, deflate, br, zstd",
"Accept-Language": "en-US,en;q=0.9",
Referer: "https://monlam.ai/model/mt?source=fr",
"Sec-Ch-Ua": '"Not/A)Brand";v="8", "Chromium";v="126", "Brave";v="126"',
"Sec-Ch-Ua-Mobile": "?0",
"Sec-Ch-Ua-Platform": '"Windows"',
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
"Sec-Gpc": "1",
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36",
},
});
const csrfToken = response.data.csrfToken;
const setCookie = response.headers["set-cookie"];

// Extract the first Set-Cookie value
const firstSetCookie = setCookie ? setCookie[0].split(";")[0] : null;

return { csrfToken, cookie: firstSetCookie };
} catch (error) {
console.error("Error fetching CSRF token:", error);
throw error;
}
}
21 changes: 18 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const bodyParser = require("body-parser");
import http from "http";
import { TibetanMlKit } from "./tibetan-ml-kit";
import cors from "cors";
import { fetchCsrfToken } from "./common/tb-conversion";

const app = express();
/**
Expand Down Expand Up @@ -47,15 +48,29 @@ app.get("/", (req, res) => {
/**
* Translates API
*/
app.get("/api/translate", async (req, res) => {
app.post("/api/translate", async (req, res) => {
try {
const data = await TibetanMlKit.translateWithSync({
input: "test",
input: "working now",
direction: "bo",
cookie: req.body.cookie,
csrfToken: req.body.csrfToken,
});
return res.json(data);
} catch (error) {
return res.status(500).send("Error calling API");
return res.status(500).send({ error: "error while translating data" });
}
});

app.get("/api/token", async (req, res) => {
try {
const { csrfToken, cookie } = await fetchCsrfToken();
return res.json({
csrfToken,
cookie,
});
} catch (error) {
return res.status(500).send({ error: "error while fetching token" });
}
});

Expand Down
22 changes: 19 additions & 3 deletions src/tibetan-ml-kit.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import axios from "axios";
import { DataParam } from "./common/data-param";
import { getTranslateSyncResponse } from "./common/tb-conversion";
import {
fetchCsrfToken,
getTranslateSyncResponse,
} from "./common/tb-conversion";
import { Server, WebSocketServer } from "ws";
import { defaultPromtMessage, genAI } from "./constant";
import { ModelConfig } from "./common/model-config";
Expand Down Expand Up @@ -67,10 +70,13 @@ class TibetanMlKit {
if (response && response.candidates) {
let en = response.candidates[0].content.parts[0].text;
console.log("en===>", en);
const { csrfToken, cookie } = await fetchCsrfToken();

let tb: any = await getTranslateSyncResponse({
input: en ?? "",
direction: "bo",
cookie: cookie ?? "",
csrfToken: csrfToken,
});
ws.send(
JSON.stringify({
Expand Down Expand Up @@ -158,8 +164,18 @@ class TibetanMlKit {
* @param DataParam
* @returns translateWithSync
*/
static async translateWithSync({ input, direction = "en" }: DataParam) {
return await getTranslateSyncResponse({ input, direction });
static async translateWithSync({
input,
direction = "en",
csrfToken,
cookie,
}: DataParam) {
return await getTranslateSyncResponse({
input,
direction,
csrfToken,
cookie,
});
}

/**
Expand Down

0 comments on commit 9941aea

Please sign in to comment.