Skip to content

Commit b382ff1

Browse files
authored
Ability to try different models
https://gist.github.com/simonw/d77ab3ef497ed79f353633322cc6d38a I tweaked it so the initial image preview would be hidden on prompt response, and changed the sort order of the select box options.
1 parent 6655282 commit b382ff1

File tree

1 file changed

+55
-11
lines changed

1 file changed

+55
-11
lines changed

gemini-bbox.html

+55-11
Original file line numberDiff line numberDiff line change
@@ -74,29 +74,38 @@
7474
const fileInput = document.getElementById('imageInput');
7575
const promptInput = document.getElementById('promptInput');
7676
const resultDiv = document.getElementById('result');
77+
const modelSelect = document.getElementById('modelSelect');
7778

78-
if (!fileInput.files[0] || !promptInput.value) {
79-
alert('Please select an image and enter a prompt.');
79+
if (!promptInput.value) {
80+
alert('Please enter a prompt.');
8081
return;
8182
}
8283

8384
resultDiv.innerHTML = 'Processing...';
8485

8586
try {
86-
const model = await getGenerativeModel({ model: "gemini-1.5-pro" });
87-
const compressedImage = await resizeAndCompressImage(fileInput.files[0]);
88-
const imagePart = await fileToGenerativePart(compressedImage);
87+
const model = await getGenerativeModel({ model: modelSelect.value });
88+
let content = [promptInput.value];
89+
90+
if (fileInput.files[0]) {
91+
const compressedImage = await resizeAndCompressImage(fileInput.files[0]);
92+
const imagePart = await fileToGenerativePart(compressedImage);
93+
content.push(imagePart);
94+
}
8995

90-
const result = await model.generateContent([promptInput.value, imagePart]);
96+
const result = await model.generateContent(content);
9197
const response = await result.response;
9298
const text = response.text();
9399

94100
resultDiv.innerHTML = marked.parse(text);
101+
document.getElementById('imagePreview').style.display = 'none';
95102

96-
// Extract coordinates from the response
97-
const coordinates = extractCoordinates(text);
98-
if (coordinates.length > 0) {
99-
displayImageWithBoundingBoxes(compressedImage, coordinates);
103+
if (fileInput.files[0]) {
104+
// Extract coordinates from the response
105+
const coordinates = extractCoordinates(text);
106+
if (coordinates.length > 0) {
107+
displayImageWithBoundingBoxes(fileInput.files[0], coordinates);
108+
}
100109
}
101110
} catch (error) {
102111
resultDiv.innerHTML = `Error: ${error.message}`;
@@ -201,8 +210,30 @@
201210
reader.readAsDataURL(file);
202211
}
203212

204-
// Attach event listener to the submit button
213+
function clearImage() {
214+
document.getElementById('imageInput').value = '';
215+
document.getElementById('canvas').getContext('2d').clearRect(0, 0, canvas.width, canvas.height);
216+
document.getElementById('imagePreview').src = '';
217+
document.getElementById('imagePreview').style.display = 'none';
218+
}
219+
220+
function previewImage(event) {
221+
const file = event.target.files[0];
222+
if (file) {
223+
const reader = new FileReader();
224+
reader.onload = function(e) {
225+
const img = document.getElementById('imagePreview');
226+
img.src = e.target.result;
227+
img.style.display = 'block';
228+
}
229+
reader.readAsDataURL(file);
230+
}
231+
}
232+
233+
// Attach event listeners
205234
document.getElementById('submitBtn').addEventListener('click', processImageAndPrompt);
235+
document.getElementById('clearImageBtn').addEventListener('click', clearImage);
236+
document.getElementById('imageInput').addEventListener('change', previewImage);
206237
</script>
207238
<style>
208239
body {
@@ -224,16 +255,29 @@
224255
max-width: 100%;
225256
height: auto;
226257
}
258+
#imagePreview {
259+
max-width: 100%;
260+
display: none;
261+
margin-top: 10px;
262+
}
227263
</style>
228264
</head>
229265
<body>
230266
<h1>Gemini API Image Bounding Box Visualization</h1>
267+
<select id="modelSelect">
268+
<option value="gemini-1.5-pro">gemini-1.5-pro (default)</option>
269+
<option value="gemini-1.5-pro-exp-0827">gemini-1.5-pro-exp-0827</option>
270+
<option value="gemini-1.5-flash-exp-0827">gemini-1.5-flash-exp-0827</option>
271+
<option value="gemini-1.5-flash-8b-exp-0827">gemini-1.5-flash-8b-exp-0827</option>
272+
</select>
231273
<input type="file" id="imageInput" accept="image/*">
274+
<button id="clearImageBtn">Clear Image</button>
232275
<textarea id="promptInput">Return bounding boxes as JSON arrays [ymin, xmin, ymax, xmax]
233276
</textarea>
234277
<button id="submitBtn">Process</button>
235278
<div id="result"></div>
236279
<div id="imageContainer">
280+
<img id="imagePreview" alt="Image preview" />
237281
<canvas id="canvas"></canvas>
238282
</div>
239283
</body>

0 commit comments

Comments
 (0)