Spaces:
Running
Running
Update index.html
Browse files- index.html +131 -99
index.html
CHANGED
|
@@ -534,111 +534,143 @@ ${userPersona ? "Strict Personalization: " + userPersona : ""}`.trim();
|
|
| 534 |
}
|
| 535 |
|
| 536 |
async function handleAudioSend(audioBlob, mimeType) {
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
|
| 553 |
-
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
]
|
| 571 |
-
};
|
| 572 |
-
|
| 573 |
-
abortController = new AbortController();
|
| 574 |
-
let fullAiText = '';
|
| 575 |
-
|
| 576 |
-
try {
|
| 577 |
-
const res = await fetch(STREAM_API_URL, {
|
| 578 |
-
method: 'POST',
|
| 579 |
-
headers: { 'Content-Type': 'application/json' },
|
| 580 |
-
signal: abortController.signal,
|
| 581 |
-
body: JSON.stringify({
|
| 582 |
-
system_instruction: { parts: [{ text: getSystemInstruction() }] },
|
| 583 |
-
contents: apiHistory,
|
| 584 |
-
generationConfig: { temperature: 0.5, maxOutputTokens: 2000 }
|
| 585 |
-
})
|
| 586 |
-
});
|
| 587 |
-
|
| 588 |
-
if (!res.ok) {
|
| 589 |
-
const errData = await res.json().catch(() => ({}));
|
| 590 |
-
throw new Error(errData.error?.message || `HTTP ${res.status}`);
|
| 591 |
}
|
| 592 |
-
|
| 593 |
-
|
| 594 |
-
|
| 595 |
-
|
| 596 |
-
|
| 597 |
-
|
| 598 |
-
|
| 599 |
-
|
| 600 |
-
|
| 601 |
-
|
| 602 |
-
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
|
| 608 |
-
|
| 609 |
-
|
| 610 |
-
|
| 611 |
-
|
| 612 |
-
|
| 613 |
-
|
| 614 |
-
|
| 615 |
-
|
| 616 |
-
|
| 617 |
-
|
| 618 |
-
|
| 619 |
-
|
| 620 |
-
|
| 621 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 622 |
}
|
| 623 |
-
}
|
| 624 |
-
}
|
| 625 |
-
|
| 626 |
-
if (fullAiText.trim()) {
|
| 627 |
-
currentChatMessages.push({ role: 'model', text: fullAiText.trim() });
|
| 628 |
-
await saveCurrentThreadToCloud();
|
| 629 |
}
|
| 630 |
-
} catch (err) {
|
| 631 |
-
if (err.name !== 'AbortError') {
|
| 632 |
-
thinking.classList.add('hidden');
|
| 633 |
-
appendMessage('model', `Voice processing error: ${err.message}`);
|
| 634 |
-
}
|
| 635 |
-
} finally {
|
| 636 |
-
thinking.classList.add('hidden');
|
| 637 |
-
setGeneratingState(false);
|
| 638 |
-
abortController = null;
|
| 639 |
}
|
| 640 |
}
|
| 641 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 642 |
// TEXT SEND
|
| 643 |
async function handleSend() {
|
| 644 |
if (isGenerating) {
|
|
|
|
| 534 |
}
|
| 535 |
|
| 536 |
async function handleAudioSend(audioBlob, mimeType) {
|
| 537 |
+
if (isGenerating) return;
|
| 538 |
+
|
| 539 |
+
const welcome = document.getElementById('welcome-screen');
|
| 540 |
+
if (welcome) welcome.remove();
|
| 541 |
+
|
| 542 |
+
// Create user bubble with loading state
|
| 543 |
+
const userBubble = createBubbleElement('user');
|
| 544 |
+
userBubble.textContent = 'ποΈ Transcribing...';
|
| 545 |
+
currentChatMessages.push({ role: 'user', text: '[Voice Note]' });
|
| 546 |
+
setGeneratingState(true);
|
| 547 |
+
|
| 548 |
+
thinking.classList.remove('hidden');
|
| 549 |
+
chat.scrollTop = chat.scrollHeight;
|
| 550 |
+
|
| 551 |
+
const base64Audio = await blobToBase64(audioBlob);
|
| 552 |
+
|
| 553 |
+
const scriptConstraint = currentScript === 'roman'
|
| 554 |
+
? "Reply ONLY in Romanized Bodo ending in 'i' not 'y'."
|
| 555 |
+
: "Reply ONLY in Devanagari Bodo.";
|
| 556 |
+
|
| 557 |
+
const apiHistory = currentChatMessages.slice(-6).map((msg) => ({
|
| 558 |
+
role: msg.role === 'user' ? 'user' : 'model',
|
| 559 |
+
parts: [{ text: msg.text }]
|
| 560 |
+
}));
|
| 561 |
+
|
| 562 |
+
// Force Gemini to split transcription and response
|
| 563 |
+
apiHistory[apiHistory.length - 1] = {
|
| 564 |
+
role: 'user',
|
| 565 |
+
parts: [
|
| 566 |
+
{
|
| 567 |
+
inlineData: {
|
| 568 |
+
mimeType: mimeType,
|
| 569 |
+
data: base64Audio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 570 |
}
|
| 571 |
+
},
|
| 572 |
+
{
|
| 573 |
+
text: `1. Transcribe what was said in the audio accurately.
|
| 574 |
+
2. Formulate your conversational reply as Orki.
|
| 575 |
+
Constraint: ${scriptConstraint}
|
| 576 |
+
|
| 577 |
+
Your output MUST strictly follow this exact format:
|
| 578 |
+
TRANSCRIPTION: <exact words the user spoke>
|
| 579 |
+
RESPONSE: <your witty reply>`
|
| 580 |
+
}
|
| 581 |
+
]
|
| 582 |
+
};
|
| 583 |
+
|
| 584 |
+
abortController = new AbortController();
|
| 585 |
+
let fullAiText = '';
|
| 586 |
+
|
| 587 |
+
try {
|
| 588 |
+
const res = await fetch(STREAM_API_URL, {
|
| 589 |
+
method: 'POST',
|
| 590 |
+
headers: { 'Content-Type': 'application/json' },
|
| 591 |
+
signal: abortController.signal,
|
| 592 |
+
body: JSON.stringify({
|
| 593 |
+
system_instruction: { parts: [{ text: getSystemInstruction() }] },
|
| 594 |
+
contents: apiHistory,
|
| 595 |
+
generationConfig: { temperature: 0.4, maxOutputTokens: 2000 }
|
| 596 |
+
})
|
| 597 |
+
});
|
| 598 |
+
|
| 599 |
+
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
| 600 |
+
|
| 601 |
+
thinking.classList.add('hidden');
|
| 602 |
+
const aiBubble = createBubbleElement('model');
|
| 603 |
+
|
| 604 |
+
const reader = res.body.getReader();
|
| 605 |
+
const decoder = new TextDecoder('utf-8');
|
| 606 |
+
let buffer = '';
|
| 607 |
+
|
| 608 |
+
while (true) {
|
| 609 |
+
const { done, value } = await reader.read();
|
| 610 |
+
if (done) break;
|
| 611 |
+
|
| 612 |
+
buffer += decoder.decode(value, { stream: true });
|
| 613 |
+
const lines = buffer.split('\n');
|
| 614 |
+
buffer = lines.pop();
|
| 615 |
+
|
| 616 |
+
for (const line of lines) {
|
| 617 |
+
const trimmed = line.trim();
|
| 618 |
+
if (trimmed.startsWith('data: ')) {
|
| 619 |
+
const jsonStr = trimmed.slice(6).trim();
|
| 620 |
+
if (!jsonStr) continue;
|
| 621 |
+
try {
|
| 622 |
+
const parsed = JSON.parse(jsonStr);
|
| 623 |
+
const chunk = parsed.candidates?.[0]?.content?.parts?.[0]?.text || '';
|
| 624 |
+
if (chunk) {
|
| 625 |
+
fullAiText += chunk;
|
| 626 |
+
|
| 627 |
+
// Parse transcript vs reply during stream
|
| 628 |
+
if (fullAiText.includes('RESPONSE:')) {
|
| 629 |
+
const parts = fullAiText.split('RESPONSE:');
|
| 630 |
+
const transcript = parts[0].replace('TRANSCRIPTION:', '').trim();
|
| 631 |
+
const reply = parts.slice(1).join('RESPONSE:').trimStart();
|
| 632 |
+
|
| 633 |
+
userBubble.textContent = transcript ? `ποΈ "${transcript}"` : 'ποΈ [Voice Note]';
|
| 634 |
+
aiBubble.innerHTML = renderMarkdown(reply);
|
| 635 |
+
} else if (fullAiText.includes('TRANSCRIPTION:')) {
|
| 636 |
+
const liveTranscript = fullAiText.replace('TRANSCRIPTION:', '').trim();
|
| 637 |
+
userBubble.textContent = liveTranscript ? `ποΈ "${liveTranscript}..."` : 'ποΈ Transcribing...';
|
| 638 |
+
} else {
|
| 639 |
+
aiBubble.innerHTML = renderMarkdown(fullAiText);
|
| 640 |
+
}
|
| 641 |
+
chat.scrollTop = chat.scrollHeight;
|
| 642 |
}
|
| 643 |
+
} catch (e) {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 644 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 645 |
}
|
| 646 |
}
|
| 647 |
|
| 648 |
+
// Save final cleanly parsed text to Firestore history
|
| 649 |
+
if (fullAiText.includes('RESPONSE:')) {
|
| 650 |
+
const parts = fullAiText.split('RESPONSE:');
|
| 651 |
+
const finalTranscript = parts[0].replace('TRANSCRIPTION:', '').trim();
|
| 652 |
+
const finalReply = parts.slice(1).join('RESPONSE:').trim();
|
| 653 |
+
|
| 654 |
+
currentChatMessages[currentChatMessages.length - 1].text = finalTranscript || '[Voice Note]';
|
| 655 |
+
currentChatMessages.push({ role: 'model', text: finalReply });
|
| 656 |
+
await saveCurrentThreadToCloud();
|
| 657 |
+
} else if (fullAiText.trim()) {
|
| 658 |
+
currentChatMessages.push({ role: 'model', text: fullAiText.trim() });
|
| 659 |
+
await saveCurrentThreadToCloud();
|
| 660 |
+
}
|
| 661 |
+
} catch (err) {
|
| 662 |
+
if (err.name !== 'AbortError') {
|
| 663 |
+
thinking.classList.add('hidden');
|
| 664 |
+
userBubble.textContent = 'ποΈ [Failed Voice Note]';
|
| 665 |
+
appendMessage('model', `Voice processing error: ${err.message}`);
|
| 666 |
+
}
|
| 667 |
+
} finally {
|
| 668 |
+
thinking.classList.add('hidden');
|
| 669 |
+
setGeneratingState(false);
|
| 670 |
+
abortController = null;
|
| 671 |
+
}
|
| 672 |
+
}
|
| 673 |
+
|
| 674 |
// TEXT SEND
|
| 675 |
async function handleSend() {
|
| 676 |
if (isGenerating) {
|