Skip to content

Commit

Permalink
added llm
Browse files Browse the repository at this point in the history
  • Loading branch information
hasanahmed1810 committed Sep 13, 2023
1 parent dee7be4 commit 3cf5b4a
Show file tree
Hide file tree
Showing 6 changed files with 300 additions and 8 deletions.
19 changes: 19 additions & 0 deletions assets/images/loading2.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions assets/images/mic/mic.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions assets/images/mic/muteMic.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
60 changes: 60 additions & 0 deletions css/home.css
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;
}
30 changes: 22 additions & 8 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@

<!-- main-LTR -->
<link rel="stylesheet" href="css/main-LTR.css">

<link rel="stylesheet" href="css/home.css">
</head>
<body class=" dark-theme landing-page-demo">
<!--Start Page Header-->
Expand Down Expand Up @@ -119,15 +121,25 @@ <h1 class="hero-title ">Where AI meets <span class="featured-text">Cooking <svg
<div class="hero-img-wraper"><img class="img-fluid " src="assets/images/hero/chef-hero.png" alt="" draggable="false"></div>
</div>
<br>
<div class="mx-auto ">
<div class="box service-box wow fadeInUp reveal-start" data-wow-offset="0" data-wow-delay=".1s">
<div class="service-content">
<h3>How Can I Help You?</h3>
<p class="service-text">
Ask Me Any Cooking Related Question
</p>
</div><a class="read-more" href="#0">Get Started<i class="bi bi-arrow-right icon "> </i></a>
<div class="col-10">
<h4 class="clr-main">
Ask me anything
</h4>
</div>

<div class="askContainer">
<div class="askflex-container">
<input type="text" id="question" placeholder="Enter your question..." />
<button id="ask">
Ask
</button>
<div id="mic-container">
<img id="mic" src="assets/images/mic/mic.svg" alt="">
<img id="muteMic" src="assets/images/mic/muteMic.svg" alt="">
</div>
</div>
<div id="loading-image"></div>
<div id="response"></div>
</div>
</div>
</div>
Expand Down Expand Up @@ -879,5 +891,7 @@ <h2 class=" footer-col-title">contact information</h2>
<script src="js/vendors/jquery.fancybox.min.js"></script>
<!-- main -->
<script src="js/main.js"></script>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script src="js/askMe.js"></script>
</body>
</html>
165 changes: 165 additions & 0 deletions js/askMe.js
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)

0 comments on commit 3cf5b4a

Please sign in to comment.