-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-gemini-api.html
More file actions
173 lines (145 loc) · 5.73 KB
/
Copy pathtest-gemini-api.html
File metadata and controls
173 lines (145 loc) · 5.73 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Test Gemini API</title>
<style>
body {
font-family: monospace;
padding: 20px;
max-width: 800px;
margin: 0 auto;
}
button {
padding: 10px 20px;
font-size: 16px;
margin: 10px 0;
}
#output {
white-space: pre-wrap;
background: #f5f5f5;
padding: 15px;
border-radius: 5px;
margin-top: 20px;
}
input {
width: 100%;
padding: 8px;
margin: 10px 0;
}
</style>
</head>
<body>
<h1>Gemini API Test</h1>
<p>This page tests if the Gemini API is working correctly with audio.</p>
<label>API Key:</label>
<input type="password" id="apiKey" placeholder="Enter your Gemini API key">
<button onclick="testTextAPI()">Test Text API</button>
<button onclick="testAudioAPI()">Test Audio API (with sample)</button>
<div id="output">Results will appear here...</div>
<script>
async function testTextAPI() {
const apiKey = document.getElementById('apiKey').value;
const output = document.getElementById('output');
if (!apiKey) {
output.textContent = 'Please enter an API key';
return;
}
output.textContent = 'Testing text API...\n';
try {
const response = await fetch(
`https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key=${apiKey}`,
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
contents: [{
parts: [{ text: "Say hello" }]
}]
})
}
);
output.textContent += `Status: ${response.status}\n`;
const data = await response.json();
output.textContent += `Response:\n${JSON.stringify(data, null, 2)}`;
} catch (error) {
output.textContent += `Error: ${error.message}\n${error.stack}`;
}
}
async function testAudioAPI() {
const apiKey = document.getElementById('apiKey').value;
const output = document.getElementById('output');
if (!apiKey) {
output.textContent = 'Please enter an API key';
return;
}
output.textContent = 'Testing audio API...\n';
// Create a simple test audio (silence)
const sampleRate = 16000;
const duration = 1; // 1 second
const numSamples = sampleRate * duration;
const audioData = new Int16Array(numSamples);
// Convert to base64
const bytes = new Uint8Array(audioData.buffer);
let binary = '';
for (let i = 0; i < bytes.byteLength; i++) {
binary += String.fromCharCode(bytes[i]);
}
const base64Audio = btoa(binary);
output.textContent += `Created ${numSamples} samples (${duration}s)\n`;
output.textContent += `Base64 length: ${base64Audio.length}\n\n`;
// Try different MIME types
const mimeTypes = [
"audio/pcm;rate=16000",
"audio/l16;rate=16000",
"audio/raw;rate=16000",
"audio/pcm",
"audio/wav"
];
for (const mimeType of mimeTypes) {
output.textContent += `\n--- Testing MIME type: ${mimeType} ---\n`;
try {
const requestBody = {
contents: [{
parts: [{
inline_data: {
mime_type: mimeType,
data: base64Audio
}
}]
}]
};
// Only add system instruction for non-wav formats
if (!mimeType.includes('wav')) {
requestBody.systemInstruction = {
parts: [{
text: "Transcribe the audio."
}]
};
}
const response = await fetch(
`https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key=${apiKey}`,
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(requestBody)
}
);
output.textContent += `Status: ${response.status}\n`;
const data = await response.json();
if (response.ok) {
output.textContent += `✓ SUCCESS!\n`;
output.textContent += `Response:\n${JSON.stringify(data, null, 2)}\n`;
break; // Stop on first success
} else {
output.textContent += `Error: ${data.error?.message || 'Unknown error'}\n`;
}
} catch (error) {
output.textContent += `Exception: ${error.message}\n`;
}
}
}
</script>
</body>
</html>