Spaces:
Sleeping
Sleeping
Update gemini-api.js
Browse files- gemini-api.js +71 -71
gemini-api.js
CHANGED
@@ -1,72 +1,72 @@
|
|
1 |
-
/**
|
2 |
-
* Calls the given Gemini model with the given image and/or text
|
3 |
-
* parts, streaming output (as a generator function).
|
4 |
-
*/
|
5 |
-
export async function* streamGemini({
|
6 |
-
model = 'gemini-
|
7 |
-
contents = [],
|
8 |
-
} = {}) {
|
9 |
-
// Send the prompt to the Python backend
|
10 |
-
// Call API defined in main.py
|
11 |
-
let response = await fetch("/api/generate", {
|
12 |
-
method: "POST",
|
13 |
-
headers: { "content-type": "application/json" },
|
14 |
-
body: JSON.stringify({ model, contents })
|
15 |
-
});
|
16 |
-
|
17 |
-
yield* streamResponseChunks(response);
|
18 |
-
}
|
19 |
-
|
20 |
-
/**
|
21 |
-
* A helper that streams text output chunks from a fetch() response.
|
22 |
-
*/
|
23 |
-
async function* streamResponseChunks(response) {
|
24 |
-
let buffer = '';
|
25 |
-
|
26 |
-
const CHUNK_SEPARATOR = '\n\n';
|
27 |
-
|
28 |
-
let processBuffer = async function* (streamDone = false) {
|
29 |
-
while (true) {
|
30 |
-
let flush = false;
|
31 |
-
let chunkSeparatorIndex = buffer.indexOf(CHUNK_SEPARATOR);
|
32 |
-
if (streamDone && chunkSeparatorIndex < 0) {
|
33 |
-
flush = true;
|
34 |
-
chunkSeparatorIndex = buffer.length;
|
35 |
-
}
|
36 |
-
if (chunkSeparatorIndex < 0) {
|
37 |
-
break;
|
38 |
-
}
|
39 |
-
|
40 |
-
let chunk = buffer.substring(0, chunkSeparatorIndex);
|
41 |
-
buffer = buffer.substring(chunkSeparatorIndex + CHUNK_SEPARATOR.length);
|
42 |
-
chunk = chunk.replace(/^data:\s*/, '').trim();
|
43 |
-
if (!chunk) {
|
44 |
-
if (flush) break;
|
45 |
-
continue;
|
46 |
-
}
|
47 |
-
let { error, text } = JSON.parse(chunk);
|
48 |
-
if (error) {
|
49 |
-
console.error(error);
|
50 |
-
throw new Error(error?.message || JSON.stringify(error));
|
51 |
-
}
|
52 |
-
yield text;
|
53 |
-
if (flush) break;
|
54 |
-
}
|
55 |
-
};
|
56 |
-
|
57 |
-
const reader = response.body.getReader();
|
58 |
-
try {
|
59 |
-
while (true) {
|
60 |
-
const { done, value } = await reader.read()
|
61 |
-
if (done) break;
|
62 |
-
buffer += new TextDecoder().decode(value);
|
63 |
-
console.log(new TextDecoder().decode(value));
|
64 |
-
yield* processBuffer();
|
65 |
-
}
|
66 |
-
} finally {
|
67 |
-
reader.releaseLock();
|
68 |
-
}
|
69 |
-
|
70 |
-
yield* processBuffer(true);
|
71 |
-
}
|
72 |
|
|
|
1 |
+
/**
|
2 |
+
* Calls the given Gemini model with the given image and/or text
|
3 |
+
* parts, streaming output (as a generator function).
|
4 |
+
*/
|
5 |
+
export async function* streamGemini({
|
6 |
+
model = 'gemini-2.0-flash-001', // use 'gemini-pro' for text -> text
|
7 |
+
contents = [],
|
8 |
+
} = {}) {
|
9 |
+
// Send the prompt to the Python backend
|
10 |
+
// Call API defined in main.py
|
11 |
+
let response = await fetch("/api/generate", {
|
12 |
+
method: "POST",
|
13 |
+
headers: { "content-type": "application/json" },
|
14 |
+
body: JSON.stringify({ model, contents })
|
15 |
+
});
|
16 |
+
|
17 |
+
yield* streamResponseChunks(response);
|
18 |
+
}
|
19 |
+
|
20 |
+
/**
|
21 |
+
* A helper that streams text output chunks from a fetch() response.
|
22 |
+
*/
|
23 |
+
async function* streamResponseChunks(response) {
|
24 |
+
let buffer = '';
|
25 |
+
|
26 |
+
const CHUNK_SEPARATOR = '\n\n';
|
27 |
+
|
28 |
+
let processBuffer = async function* (streamDone = false) {
|
29 |
+
while (true) {
|
30 |
+
let flush = false;
|
31 |
+
let chunkSeparatorIndex = buffer.indexOf(CHUNK_SEPARATOR);
|
32 |
+
if (streamDone && chunkSeparatorIndex < 0) {
|
33 |
+
flush = true;
|
34 |
+
chunkSeparatorIndex = buffer.length;
|
35 |
+
}
|
36 |
+
if (chunkSeparatorIndex < 0) {
|
37 |
+
break;
|
38 |
+
}
|
39 |
+
|
40 |
+
let chunk = buffer.substring(0, chunkSeparatorIndex);
|
41 |
+
buffer = buffer.substring(chunkSeparatorIndex + CHUNK_SEPARATOR.length);
|
42 |
+
chunk = chunk.replace(/^data:\s*/, '').trim();
|
43 |
+
if (!chunk) {
|
44 |
+
if (flush) break;
|
45 |
+
continue;
|
46 |
+
}
|
47 |
+
let { error, text } = JSON.parse(chunk);
|
48 |
+
if (error) {
|
49 |
+
console.error(error);
|
50 |
+
throw new Error(error?.message || JSON.stringify(error));
|
51 |
+
}
|
52 |
+
yield text;
|
53 |
+
if (flush) break;
|
54 |
+
}
|
55 |
+
};
|
56 |
+
|
57 |
+
const reader = response.body.getReader();
|
58 |
+
try {
|
59 |
+
while (true) {
|
60 |
+
const { done, value } = await reader.read()
|
61 |
+
if (done) break;
|
62 |
+
buffer += new TextDecoder().decode(value);
|
63 |
+
console.log(new TextDecoder().decode(value));
|
64 |
+
yield* processBuffer();
|
65 |
+
}
|
66 |
+
} finally {
|
67 |
+
reader.releaseLock();
|
68 |
+
}
|
69 |
+
|
70 |
+
yield* processBuffer(true);
|
71 |
+
}
|
72 |
|