Skip to content

Commit

Permalink
Add multiple languages to ocr, try catch gpt
Browse files Browse the repository at this point in the history
  • Loading branch information
TamTH committed Apr 7, 2023
1 parent 4b0535c commit b9fad20
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 40 deletions.
30 changes: 18 additions & 12 deletions dist/commands/gpt.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/commands/gpt.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 15 additions & 5 deletions dist/commands/ocr.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/commands/ocr.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions dist/slash_commands.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/slash_commands.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 20 additions & 15 deletions ts/commands/gpt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,25 @@ export default async function gpt(interaction: ChatInputCommandInteraction) {
}

async function getChatGPTResponse(query: string): Promise<string> {
const response = await axios.post(
CHAT_GPT_CHAT_URL,
{
model: 'gpt-3.5-turbo',
messages: [{role: 'user', content: query}],
},
{
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${OPENAI_API_KEY}`,
try {
const response = await axios.post(
CHAT_GPT_CHAT_URL,
{
model: 'gpt-3.5-turbo',
messages: [{role: 'user', content: query}],
},
}
);
console.log(response.data.choices);
const content = response.data.choices[0].message.content;
return content;
{
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${OPENAI_API_KEY}`,
},
}
);
console.log(response.data.choices);
const content = response.data.choices[0].message.content;
return content;
} catch (error) {
console.log('> Error', error);
return 'Can not call ChatGPT now, please try again later.';
}
}
19 changes: 14 additions & 5 deletions ts/commands/ocr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,25 @@ export default async function (msg: Message, tokens: any) {
export async function ocr(interaction: ChatInputCommandInteraction) {
await interaction.deferReply();
interaction.followUp('OCR with OCR Space...');
var image = interaction.options.get('image');
const image = interaction.options.get('image');
var language = 'eng';
try {
language = interaction.options.get('language')!.value as string;
} catch (error) {
console.log('> Error', error);
}
console.log(image);
const imageUrl = image?.attachment?.url as string;
const text = await getOcrText(imageUrl);
const text = await getOcrText(imageUrl, language);
interaction.editReply(text);
}

async function getOcrText(imageUrl: string): Promise<string> {
const ocrUrl = `${OCR_SPACE_IMAGEURL_URL}?apikey=${OCR_SPACE_API_KEY}&url=${imageUrl}&isOverlayRequired=true&iscreatesearchablepdf=true&issearchablepdfhidetextlayer=true`;
// const response = await fetch(ocrUrl);
async function getOcrText(imageUrl: string, language: string): Promise<string> {
var ocrUrl = '';
ocrUrl = `${OCR_SPACE_IMAGEURL_URL}?apikey=${OCR_SPACE_API_KEY}&url=${imageUrl}&isOverlayRequired=true&iscreatesearchablepdf=true&issearchablepdfhidetextlayer=true&language=${language}`;
if (language === 'vie') {
ocrUrl += `&OCREngine=3`;
}
try {
const response = await axios.get(ocrUrl);
const text = response.data.ParsedResults[0].ParsedText as string;
Expand Down
Loading

0 comments on commit b9fad20

Please sign in to comment.