Advertisement
KanjiCoder

GPT_VOICE_ACTiVATED_ASSISTANT

Apr 23rd, 2023
968
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.   <meta charset="utf-8">
  5.   <meta name="viewport" content="width=device-width, initial-scale=1">
  6.   <title>Voice-Activated Chatbot with ChatGPT</title>
  7.   <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
  8.   <style>
  9.     body {
  10.       font-family: Arial, sans-serif;
  11.       text-align: center;
  12.     }
  13.     h1 {
  14.       margin-top: 50px;
  15.     }
  16.     button {
  17.       background-color: #4CAF50;
  18.       color: white;
  19.       border: none;
  20.       padding: 15px 20px;
  21.       text-align: center;
  22.       text-decoration: none;
  23.       display: inline-block;
  24.       font-size: 16px;
  25.       margin: 10px;
  26.       cursor: pointer;
  27.       border-radius: 10px;
  28.     }
  29.     #microphone-button {
  30.       background-color: #2196F3;
  31.     }
  32.     #response {
  33.       margin-top: 50px;
  34.       font-size: 24px;
  35.     }
  36.   </style>
  37. </head>
  38. <body>
  39.   <h1>Voice-Activated Chatbot with ChatGPT</h1>
  40.   <button id="microphone-button"><i class="fa fa-microphone"></i></button>
  41.   <div id="response"></div>
  42.   <script>
  43.     const recognition = new window.webkitSpeechRecognition();
  44.     recognition.continuous = true;
  45.     recognition.interimResults = true;
  46.  
  47.     let finalTranscript = '';
  48.     recognition.onresult = event => {
  49.       let interimTranscript = '';
  50.       for (let i = event.resultIndex; i < event.results.length; i++) {
  51.         const transcript = event.results[i][0].transcript;
  52.         if (event.results[i].isFinal) {
  53.           finalTranscript += transcript;
  54.         } else {
  55.           interimTranscript += transcript;
  56.         }
  57.       }
  58.  
  59.       if (finalTranscript !== '') {
  60.         sendRequest(finalTranscript);
  61.       }
  62.     };
  63.  
  64.     recognition.onerror = event => {
  65.       console.error(event.error);
  66.     };
  67.  
  68.     document.querySelector('#microphone-button').addEventListener('click', () => {
  69.       recognition.start();
  70.     });
  71.  
  72.     const API_ENDPOINT = 'https://api.openai.com/v1/engines/davinci-codex/completions';
  73.  
  74.     const sendRequest = async input => {
  75.       const response = await fetch(API_ENDPOINT, {
  76.         method: 'POST',
  77.         headers: {
  78.           'Content-Type': 'application/json',
  79.           'Authorization': `Bearer ${YOUR_API_KEY}`,
  80.         },
  81.         body: JSON.stringify({
  82.           prompt: input,
  83.           max_tokens: 60,
  84.           n: 1,
  85.           stop: null,
  86.           temperature: 0.5,
  87.         }),
  88.       });
  89.  
  90.       if (!response.ok) {
  91.         throw new Error(`HTTP error! status: ${response.status}`);
  92.       }
  93.  
  94.       const data = await response.json();
  95.       const message = data.choices[0].text.trim();
  96.       speak(message);
  97.     };
  98.  
  99.     const synth = window.speechSynthesis;
  100.  
  101.     const speak = message => {
  102.       const utterance = new SpeechSynthesisUtterance(message);
  103.       utterance.voice = synth.getVoices()[0];
  104.       synth.speak(utterance);
  105.       document.querySelector('#response').textContent = message;
  106.     };
  107.   </script>
  108. </body>
  109. </html>
  110.  
Tags: Gpt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement