File size: 3,944 Bytes
d7d28b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e626789
 
 
 
 
 
 
 
d7d28b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
document.addEventListener("DOMContentLoaded", () => {
  const recordBtn = document.getElementById("start-recording");
  const sendBtn = document.getElementById("send-btn");
  const userInput = document.getElementById("question");
  const chatLog = document.getElementById("chat-log");

  let recognition;
  let isRecording = false;

  // Funzione per aggiungere messaggi al log
  function addMessage(text, sender) {
    const message = document.createElement("div");
    message.classList.add("message", sender);
    message.textContent = text;
    chatLog.appendChild(message);
    chatLog.scrollTop = chatLog.scrollHeight; // Auto-scroll
    // Scroll con animazione
  chatLog.scrollTo({
    top: chatLog.scrollHeight,
    behavior: "smooth"
  });
  }

  // Evento: invio input testuale
  sendBtn.addEventListener("click", () => {
    const text = userInput.value.trim();
    if (text) {
      addMessage(text, "user");
      userInput.value = ""; // Pulisce l'input
      fetchResponse(text);
    }
  });

  // Evento: start/stop registrazione vocale
  recordBtn.addEventListener("click", () => {
    if (!('webkitSpeechRecognition' in window)) {
      addMessage("Riconoscimento vocale non supportato nel browser.", "bot");
      return;
    }

    if (!isRecording) {
      recognition = new webkitSpeechRecognition();
      recognition.lang = "en-US";
      recognition.continuous = false;
      recognition.interimResults = false;

      // Aggiungo classe animazione al bottone
      recordBtn.classList.add("recording");

  
      recognition.onstart = () => {
        isRecording = true;
        recordBtn.textContent = "Stop Recording";
        addMessage("Recording question...", "bot");
      };

      recognition.onresult = (event) => {
        const transcript = event.results[0][0].transcript;
        addMessage(transcript, "user");
        fetchResponse(transcript);
      };

      recognition.onerror = (event) => {
        addMessage("Errore nel riconoscimento vocale: " + event.error, "bot");
      };

      recognition.onend = () => {
        isRecording = false;
        recordBtn.textContent = "Start Recording";
        addMessage("Recording stopped.", "bot");
         // Rimuovo animazione quando finisce la registrazione
        recordBtn.classList.remove("recording");
      };
     

      recognition.start();

    } else {
      // Stop registrazione se è già in corso
      recognition.stop();
    }
  });

  // Funzione per inviare la domanda al backend
 // Funzione per inviare la domanda al backend e leggere la risposta
async function fetchResponse(question) {
  addMessage("I'm thinking, be patient...", "bot");

  const context = "Fornisci il contesto dinamico o fisso qui";
  const longAnswer = "Fornisci la risposta lunga dinamica o fissa qui";

  try {
   const response = await fetch("https://frammartina-pubmedqa-chatbot.hf.space/chat", { 
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({
    question: question,
    context: context,
    long_answer: long_answer
  })
    });

    if (response.ok) {
      const data = await response.json();
      addMessage(data.answer, "bot");
      // --- Sintesi vocale della risposta ---
      speakText(data.answer);
    } else {
      addMessage("Spiacente, non sono riuscito a processare la tua domanda.", "bot");
    }
  } catch (error) {
    addMessage("Si è verificato un errore durante la connessione al server.", "bot");
    console.error(error);
  }
}

// Funzione per leggere il testo con sintesi vocale
function speakText(text) {
  if ('speechSynthesis' in window) {
    const utterance = new SpeechSynthesisUtterance(text);
    utterance.lang = "en-US"; // Cambia lingua se necessario
    window.speechSynthesis.cancel(); // Ferma eventuali letture precedenti
    window.speechSynthesis.speak(utterance);
  } else {
    alert("La sintesi vocale non è supportata da questo browser.");
  }
}
  
});