-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dee7be4
commit 3cf5b4a
Showing
6 changed files
with
300 additions
and
8 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
.clr-main { | ||
color: black; | ||
} | ||
|
||
.askContainer { | ||
max-width: 28rem; | ||
padding-bottom: 1.5rem; | ||
} | ||
|
||
.askflex-container { | ||
display: flex; | ||
justify-content: space-between; | ||
align-items: center; | ||
} | ||
|
||
#question { | ||
width: 100%; | ||
padding: 0.75rem 1rem; | ||
margin-right: 0.5rem; | ||
border: 1px solid #e5e7eb; | ||
border-radius: 0.375rem; | ||
outline: none; | ||
} | ||
|
||
#ask { | ||
padding: 0.8rem 1.5rem; | ||
border: none; | ||
border-radius: 0.375rem; | ||
outline: none; | ||
background: linear-gradient(90deg, #030005 0%, #01163d 100%); | ||
color: white; | ||
cursor: pointer; | ||
} | ||
|
||
#loading-img { | ||
width: 100%; | ||
} | ||
|
||
#response { | ||
margin-top: 1.5rem; | ||
} | ||
|
||
#title { | ||
margin-top: 0; | ||
} | ||
|
||
#ask-social { | ||
margin-top: -40px; | ||
} | ||
|
||
#mic-container { | ||
text-align: center; | ||
} | ||
|
||
#mic, #muteMic { | ||
width: 50px; | ||
} | ||
#mic { | ||
display: none; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
|
||
//........ LLM ChatGpt functionality........... | ||
const questionInput = document.getElementById("question"); | ||
const askButton = document.getElementById("ask"); | ||
const responseDiv = document.getElementById("response"); | ||
|
||
document.addEventListener("DOMContentLoaded", () => { | ||
const handleAsk = async () => { | ||
const question = questionInput.value; | ||
questionInput.innerHTML = " " | ||
if (question) { | ||
askButton.innerHTML = '<img id="loading-img" src="assets/images/loading2.svg" width="110" height="20" alt="Loading" />'; | ||
setResponse('Please wait! Response is coming....') | ||
setIsLoading(true) | ||
try { | ||
const url = 'https://open-ai21.p.rapidapi.com/conversationgpt'; | ||
const options = { | ||
method: 'POST', | ||
headers: { | ||
'content-type': 'application/json', | ||
'X-RapidAPI-Key': '12865b262bmsh1df342a7a8209adp11946djsned15b0207e45', | ||
'X-RapidAPI-Host': 'open-ai21.p.rapidapi.com' | ||
}, | ||
body: JSON.stringify({ | ||
messages: [ | ||
{ | ||
role: 'user', | ||
content: question | ||
} | ||
] | ||
}) | ||
}; | ||
axios | ||
.request({ | ||
url, | ||
method: options.method, | ||
headers: options.headers, | ||
data: options.body | ||
}) | ||
.then((response) => { | ||
const regex = /\\n/g; | ||
const ans = response.data.GPT.replace(regex, '<br>'); | ||
setResponse(ans); | ||
setIsLoading(false) | ||
}) | ||
.catch((error) => { | ||
console.error(error); | ||
setIsLoading(false) | ||
setResponse('Something went wrong. Please try again') | ||
}); | ||
} catch (error) { | ||
console.error(error); | ||
setIsLoading(false) | ||
setResponse('Something went wrong. Please try again') | ||
} | ||
} | ||
}; | ||
|
||
const setResponse = (text) => { | ||
responseDiv.innerHTML = text; | ||
}; | ||
|
||
const setIsLoading = (isLoading) => { | ||
console.log("i'm in setIsLoading", isLoading) | ||
askButton.disabled = isLoading; | ||
if (isLoading) { | ||
askButton.style.cursor = 'not-allowed' | ||
} | ||
else { | ||
askButton.innerHTML = 'Ask'; | ||
askButton.style.cursor = 'pointer'; | ||
} | ||
}; | ||
|
||
askButton.onclick = handleAsk | ||
}); | ||
|
||
|
||
// ...........mic Related js (Speech Recognition)............ | ||
const mic = document.querySelector('#mic') | ||
const mutedMic = document.querySelector('#muteMic') | ||
let socket; | ||
let mediaRecorder; | ||
let lastUserActivityTime; | ||
let activityTimer; | ||
|
||
const startUsingMicrophone = () => { | ||
openMic(); | ||
lastUserActivityTime = Date.now() | ||
navigator.mediaDevices.getUserMedia({ audio: true }).then((stream) => { | ||
if (!MediaRecorder.isTypeSupported('audio/webm')) | ||
{ | ||
muteMic(); | ||
return alert('Browser not supported') | ||
} | ||
console.log('In navigator'); | ||
mediaRecorder = new MediaRecorder(stream, { | ||
mimeType: 'audio/webm', | ||
}) | ||
socket = new WebSocket('wss://api.deepgram.com/v1/listen', [ | ||
'token', | ||
'd39b4e9d55390e5e8065a9b69966ff361a3db22d', | ||
]) | ||
socket.onopen = () => { | ||
mediaRecorder.addEventListener('dataavailable', async (event) => { | ||
if (event.data.size > 0 && socket.readyState == 1) { | ||
socket.send(event.data) | ||
} | ||
}) | ||
mediaRecorder.start(1000) | ||
} | ||
|
||
socket.onmessage = (message) => { | ||
const received = JSON.parse(message.data) | ||
const transcript = received.channel.alternatives[0].transcript | ||
if (transcript && received.is_final) { | ||
lastUserActivityTime = Date.now() | ||
questionInput.value += | ||
transcript + ' ' | ||
} | ||
} | ||
|
||
socket.onclose = () => { | ||
console.log({ event: 'onclose' }) | ||
} | ||
|
||
socket.onerror = (error) => { | ||
questionInput.value = 'Somethign went worng' | ||
muteMic() | ||
console.log({ event: 'onerror', error }) | ||
} | ||
|
||
startActivityTimer() | ||
}) | ||
} | ||
|
||
const openMic = () => { | ||
mic.style.display = 'inline-block'; | ||
mutedMic.style.display = 'none' | ||
} | ||
|
||
const muteMic = () => { | ||
mic.style.display = 'none'; | ||
mutedMic.style.display = 'inline-block' | ||
} | ||
|
||
const closeSocket = () => { | ||
muteMic(); | ||
socket.close(); | ||
mediaRecorder.stop(); | ||
clearInterval(activityTimer); | ||
}; | ||
|
||
const startActivityTimer = () => { | ||
activityTimer = setInterval(() => { | ||
const currentTime = Date.now(); | ||
const inactivityTime = currentTime - lastUserActivityTime; | ||
if (inactivityTime > 10000) { | ||
closeSocket(); | ||
} | ||
}, 1000); | ||
}; | ||
|
||
mic.addEventListener('click', closeSocket) | ||
mutedMic.addEventListener('click', startUsingMicrophone) |