-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
62 lines (53 loc) · 1.91 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { GoogleGenerativeAI, HarmBlockThreshold, HarmCategory } from "@google/generative-ai";
import Base64 from 'base64-js';
import MarkdownIt from 'markdown-it';
import { maybeShowApiKeyBanner } from './gemini-api-banner';
import './style.css';
let API_KEY = 'AIzaSyAyGwtvwDPwaFDwlVKEOXjPlH4DFktzF3Q';
let form = document.querySelector('form');
let promptInput = document.querySelector('input[name="prompt"]');
let output = document.querySelector('.output');
form.onsubmit = async (ev) => {
ev.preventDefault();
output.textContent = 'Generating...';
try {
// Load the image as a base64 string
// let imageUrl = form.elements.namedItem('chosen-image').value;
// let imageBase64 = await fetch(imageUrl)
// .then(r => r.arrayBuffer())
// .then(a => Base64.fromByteArray(new Uint8Array(a)));
// Assemble the prompt by combining the text with the chosen image
let contents = [
{
role: 'user',
parts: [
// { inline_data: { mime_type: 'image/jpeg', data: imageBase64, } },
{ text: promptInput.value }
]
}
];
// Call the gemini-pro-vision model, and get a stream of results
const genAI = new GoogleGenerativeAI(API_KEY);
const model = genAI.getGenerativeModel({
model: "gemini-pro",
safetySettings: [
{
category: HarmCategory.HARM_CATEGORY_HARASSMENT,
threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH,
},
],
});
const result = await model.generateContentStream({ contents });
// Read from the stream and interpret the output as markdown
let buffer = [];
let md = new MarkdownIt();
for await (let response of result.stream) {
buffer.push(response.text());
output.innerHTML = md.render(buffer.join(''));
}
} catch (e) {
output.innerHTML += '<hr>' + e;
}
};
// You can delete this once you've filled out an API key
maybeShowApiKeyBanner(API_KEY);