Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

secret Key #71

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
# Build and Deploy Your Own ChatGPT AI Application That Will Help You Code
![Open AI CodeGPT](https://i.ibb.co/LS4DRhb/image-257.png)

### [🌟 Become a top 1% Next.js 13 developer in only one course](https://jsmastery.pro/next13)
### [🚀 Land your dream programming job in 6 months](https://jsmastery.pro/masterclass)

### Launch your development career with project-based coaching - https://www.jsmastery.pro
# Mental Health Chat AI
![Mental Health Open AI CodeGPT](https://i.ibb.co/LS4DRhb/image-257.png)
2 changes: 1 addition & 1 deletion client/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ const handleSubmit = async (e) => {
// messageDiv.innerHTML = "..."
loader(messageDiv)

const response = await fetch('https://codex-im0y.onrender.com/', {
const response = await fetch('deployment link here', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand Down
120 changes: 120 additions & 0 deletions server/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import bot from './assets/bot.svg'
import user from './assets/user.svg'

const form = document.querySelector('form')
const chatContainer = document.querySelector('#chat_container')

let loadInterval

function loader(element) {
element.textContent = ''

loadInterval = setInterval(() => {
// Update the text content of the loading indicator
element.textContent += '.';

// If the loading indicator has reached three dots, reset it
if (element.textContent === '....') {
element.textContent = '';
}
}, 300);
}

function typeText(element, text) {
let index = 0

let interval = setInterval(() => {
if (index < text.length) {
element.innerHTML += text.charAt(index)
index++
} else {
clearInterval(interval)
}
}, 20)
}

// generate unique ID for each message div of bot
// necessary for typing text effect for that specific reply
// without unique ID, typing text will work on every element
function generateUniqueId() {
const timestamp = Date.now();
const randomNumber = Math.random();
const hexadecimalString = randomNumber.toString(16);

return `id-${timestamp}-${hexadecimalString}`;
}

function chatStripe(isAi, value, uniqueId) {
return (
`
<div class="wrapper ${isAi && 'ai'}">
<div class="chat">
<div class="profile">
<img
src=${isAi ? bot : user}
alt="${isAi ? 'bot' : 'user'}"
/>
</div>
<div class="message" id=${uniqueId}>${value}</div>
</div>
</div>
`
)
}

const handleSubmit = async (e) => {
e.preventDefault()

const data = new FormData(form)

// user's chatstripe
chatContainer.innerHTML += chatStripe(false, data.get('prompt'))

// to clear the textarea input
form.reset()

// bot's chatstripe
const uniqueId = generateUniqueId()
chatContainer.innerHTML += chatStripe(true, " ", uniqueId)

// to focus scroll to the bottom
chatContainer.scrollTop = chatContainer.scrollHeight;

// specific message div
const messageDiv = document.getElementById(uniqueId)

// messageDiv.innerHTML = "..."
loader(messageDiv)

const response = await fetch('https://project_openai_codex-1.onrender.com', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
prompt: data.get('prompt')
})
})

clearInterval(loadInterval)
messageDiv.innerHTML = " "

if (response.ok) {
const data = await response.json();
const parsedData = data.bot.trim() // trims any trailing spaces/'\n'

typeText(messageDiv, parsedData)
} else {
const err = await response.text()

messageDiv.innerHTML = "Something went wrong"
alert(err)
}
}

form.addEventListener('submit', handleSubmit)
form.addEventListener('keyup', (e) => {
if (e.keyCode === 13) {
handleSubmit(e)
}
})
48 changes: 0 additions & 48 deletions server/server.js

This file was deleted.