Spaces:
Runtime error
Runtime error
Create script.js
Browse files
script.js
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
document.addEventListener("DOMContentLoaded", () => {
|
| 2 |
+
const recordBtn = document.getElementById("start-recording");
|
| 3 |
+
const sendBtn = document.getElementById("send-btn");
|
| 4 |
+
const userInput = document.getElementById("question");
|
| 5 |
+
const chatLog = document.getElementById("chat-log");
|
| 6 |
+
|
| 7 |
+
let recognition;
|
| 8 |
+
let isRecording = false;
|
| 9 |
+
|
| 10 |
+
// Funzione per aggiungere messaggi al log
|
| 11 |
+
function addMessage(text, sender) {
|
| 12 |
+
const message = document.createElement("div");
|
| 13 |
+
message.classList.add("message", sender);
|
| 14 |
+
message.textContent = text;
|
| 15 |
+
chatLog.appendChild(message);
|
| 16 |
+
chatLog.scrollTop = chatLog.scrollHeight; // Auto-scroll
|
| 17 |
+
// Scroll con animazione
|
| 18 |
+
chatLog.scrollTo({
|
| 19 |
+
top: chatLog.scrollHeight,
|
| 20 |
+
behavior: "smooth"
|
| 21 |
+
});
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
// Evento: invio input testuale
|
| 25 |
+
sendBtn.addEventListener("click", () => {
|
| 26 |
+
const text = userInput.value.trim();
|
| 27 |
+
if (text) {
|
| 28 |
+
addMessage(text, "user");
|
| 29 |
+
userInput.value = ""; // Pulisce l'input
|
| 30 |
+
fetchResponse(text);
|
| 31 |
+
}
|
| 32 |
+
});
|
| 33 |
+
|
| 34 |
+
// Evento: start/stop registrazione vocale
|
| 35 |
+
recordBtn.addEventListener("click", () => {
|
| 36 |
+
if (!('webkitSpeechRecognition' in window)) {
|
| 37 |
+
addMessage("Riconoscimento vocale non supportato nel browser.", "bot");
|
| 38 |
+
return;
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
if (!isRecording) {
|
| 42 |
+
recognition = new webkitSpeechRecognition();
|
| 43 |
+
recognition.lang = "en-US";
|
| 44 |
+
recognition.continuous = false;
|
| 45 |
+
recognition.interimResults = false;
|
| 46 |
+
|
| 47 |
+
// Aggiungo classe animazione al bottone
|
| 48 |
+
recordBtn.classList.add("recording");
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
recognition.onstart = () => {
|
| 52 |
+
isRecording = true;
|
| 53 |
+
recordBtn.textContent = "Stop Recording";
|
| 54 |
+
addMessage("Recording question...", "bot");
|
| 55 |
+
};
|
| 56 |
+
|
| 57 |
+
recognition.onresult = (event) => {
|
| 58 |
+
const transcript = event.results[0][0].transcript;
|
| 59 |
+
addMessage(transcript, "user");
|
| 60 |
+
fetchResponse(transcript);
|
| 61 |
+
};
|
| 62 |
+
|
| 63 |
+
recognition.onerror = (event) => {
|
| 64 |
+
addMessage("Errore nel riconoscimento vocale: " + event.error, "bot");
|
| 65 |
+
};
|
| 66 |
+
|
| 67 |
+
recognition.onend = () => {
|
| 68 |
+
isRecording = false;
|
| 69 |
+
recordBtn.textContent = "Start Recording";
|
| 70 |
+
addMessage("Recording stopped.", "bot");
|
| 71 |
+
// Rimuovo animazione quando finisce la registrazione
|
| 72 |
+
recordBtn.classList.remove("recording");
|
| 73 |
+
};
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
recognition.start();
|
| 77 |
+
|
| 78 |
+
} else {
|
| 79 |
+
// Stop registrazione se è già in corso
|
| 80 |
+
recognition.stop();
|
| 81 |
+
}
|
| 82 |
+
});
|
| 83 |
+
|
| 84 |
+
// Funzione per inviare la domanda al backend
|
| 85 |
+
// Funzione per inviare la domanda al backend e leggere la risposta
|
| 86 |
+
async function fetchResponse(question) {
|
| 87 |
+
addMessage("I'm thinking, be patient...", "bot");
|
| 88 |
+
|
| 89 |
+
const context = "Fornisci il contesto dinamico o fisso qui";
|
| 90 |
+
const longAnswer = "Fornisci la risposta lunga dinamica o fissa qui";
|
| 91 |
+
|
| 92 |
+
try {
|
| 93 |
+
const response = await fetch("https://frammartina-pubmedqa-chatbot-2.hf.space/chat", {
|
| 94 |
+
method: "POST",
|
| 95 |
+
headers: { "Content-Type": "application/json" },
|
| 96 |
+
body: JSON.stringify({
|
| 97 |
+
question: question,
|
| 98 |
+
context: context,
|
| 99 |
+
long_answer: long_answer
|
| 100 |
+
})
|
| 101 |
+
})
|
| 102 |
+
});
|
| 103 |
+
|
| 104 |
+
if (response.ok) {
|
| 105 |
+
const data = await response.json();
|
| 106 |
+
addMessage(data.answer, "bot");
|
| 107 |
+
// --- Sintesi vocale della risposta ---
|
| 108 |
+
speakText(data.answer);
|
| 109 |
+
} else {
|
| 110 |
+
addMessage("Spiacente, non sono riuscito a processare la tua domanda.", "bot");
|
| 111 |
+
}
|
| 112 |
+
} catch (error) {
|
| 113 |
+
addMessage("Si è verificato un errore durante la connessione al server.", "bot");
|
| 114 |
+
console.error(error);
|
| 115 |
+
}
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
// Funzione per leggere il testo con sintesi vocale
|
| 119 |
+
function speakText(text) {
|
| 120 |
+
if ('speechSynthesis' in window) {
|
| 121 |
+
const utterance = new SpeechSynthesisUtterance(text);
|
| 122 |
+
utterance.lang = "en-US"; // Cambia lingua se necessario
|
| 123 |
+
window.speechSynthesis.cancel(); // Ferma eventuali letture precedenti
|
| 124 |
+
window.speechSynthesis.speak(utterance);
|
| 125 |
+
} else {
|
| 126 |
+
alert("La sintesi vocale non è supportata da questo browser.");
|
| 127 |
+
}
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
});
|