-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
225 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { HarmBlockThreshold, HarmCategory } from '@google-cloud/vertexai'; | ||
|
||
export const SAFETY_SETTINGS = [ | ||
{ | ||
category: HarmCategory.HARM_CATEGORY_HATE_SPEECH, | ||
threshold: HarmBlockThreshold.BLOCK_NONE, | ||
}, | ||
{ | ||
category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT, | ||
threshold: HarmBlockThreshold.BLOCK_NONE, | ||
}, | ||
{ | ||
category: HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT, | ||
threshold: HarmBlockThreshold.BLOCK_NONE, | ||
}, | ||
{ | ||
category: HarmCategory.HARM_CATEGORY_HARASSMENT, | ||
threshold: HarmBlockThreshold.BLOCK_NONE, | ||
}, | ||
]; |
76 changes: 76 additions & 0 deletions
76
src/lib/parser/experimental/VertexAPI/convertImageToHTML.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { VertexAI } from '@google-cloud/vertexai'; | ||
import { SAFETY_SETTINGS } from './constants'; | ||
|
||
export const convertImageToHTML = async ( | ||
imageData: string | ||
): Promise<string> => { | ||
const vertexAI = new VertexAI({ | ||
project: 'notion-to-anki', | ||
location: 'europe-west3', | ||
}); | ||
const model = 'gemini-1.5-flash-002'; | ||
|
||
const generativeModel = vertexAI.preview.getGenerativeModel({ | ||
model: model, | ||
generationConfig: { | ||
maxOutputTokens: 8192, | ||
temperature: 1, | ||
topP: 0.95, | ||
}, | ||
safetySettings: SAFETY_SETTINGS, | ||
}); | ||
|
||
const text1 = { | ||
text: `Convert the text in this image to the following format: | ||
<ul class=\"toggle\"> | ||
<li> | ||
<details> | ||
<summary> | ||
n) question | ||
</summary> | ||
<p>A) ..., </p> | ||
<p>B)... </p> | ||
etc. | ||
<p>and finally Answer: D</p> | ||
</details> | ||
</li> | ||
</ul> | ||
— | ||
- Extra rules: n=is the number for the question, question=the question text | ||
- Add newline between the options | ||
- If you are not able to detect the pattern above, try converting this into a question and answer format`, | ||
}; | ||
|
||
const image1 = { | ||
inlineData: { | ||
mimeType: 'image/png', | ||
data: imageData, | ||
}, | ||
}; | ||
|
||
const req = { | ||
contents: [{ role: 'user', parts: [text1, image1] }], | ||
}; | ||
|
||
let htmlContent = ''; | ||
try { | ||
const streamingResp = await generativeModel.generateContentStream(req); | ||
for await (const item of streamingResp.stream) { | ||
if ( | ||
item.candidates && | ||
item.candidates[0].content && | ||
item.candidates[0].content.parts | ||
) { | ||
htmlContent += item.candidates[0].content.parts | ||
.map((part) => part.text) | ||
.join(''); | ||
} | ||
} | ||
} catch (error) { | ||
console.error('Error generating content stream:', error); | ||
} | ||
|
||
return htmlContent; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.