-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.html
More file actions
163 lines (142 loc) · 5.24 KB
/
index.html
File metadata and controls
163 lines (142 loc) · 5.24 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JARVIS - Virtual Assistant</title>
<link rel="shortcut icon" href="avatar.png" type="image/x-icon">
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
</head>
<body>
<section class="main">
<div class="image-container">
<div class="image">
<img src="giphy.gif" alt="image">
</div>
<h1>J A R V I S</h1>
<p>I'm a Virtual Assistant JARVIS, How may i help you?</p>
</div>
<div class="input">
<button class="talk"><i class="fas fa-microphone-alt"></i></button>
<h1 class="content"> Click here to speak</h1>
</div>
</section>
<script>
const btn = document.querySelector('.talk');
const content = document.querySelector('.content');
function speak(text){
const text_speak = new SpeechSynthesisUtterance(text);
text_speak.rate = 1;
text_speak.volume = 1;
text_speak.pitch = 1;
window.speechSynthesis.speak(text_speak);
}
function wishMe(){
var day = new Date();
var hour = day.getHours();
if(hour>=0 && hour<12){
speak("Good Morning Boss...");
} else if(hour>=12 && hour<17){
speak("Good Afternoon Master...");
} else{
speak("Good Evening Sir...");
}
}
window.addEventListener('load', ()=>{
speak("Initializing...");
wishMe();
});
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
const recognition = new SpeechRecognition();
recognition.onresult = (event)=>{
const currentIndex = event.resultIndex;
const transcript = event.results[currentIndex][0].transcript;
content.textContent = transcript;
takeCommand(transcript.toLowerCase());
}
btn.addEventListener('click', ()=>{
content.textContent = "Listening...";
recognition.start();
});
async function takeCommand(message){
if(message.includes('hey') || message.includes('hello')){
speak("Hello Sir, How May I Help You?");
}
else if(message.includes("open google")){
window.open("https://google.com", "_blank");
speak("Opening Google...")
}
else if(message.includes("open youtube")){
window.open("https://youtube.com", "_blank");
speak("Opening Youtube...")
}
else if(message.includes("open facebook")){
window.open("https://facebook.com", "_blank");
speak("Opening Facebook...")
}
else if(message.includes("open github")){
window.open("https://github.com", "_blank");
speak("Opening Github...")
}
else if(message.includes('search on google') || message.includes('search with google') || message.includes('search on internet')) {
window.open(`https://www.google.com/search?q=${message.replace(" ", "+")}`, "_blank");
const finalText = "This is what i found on internet regarding " + message;
speak(finalText);
}
else if(message.includes('time')) {
const time = new Date().toLocaleString(undefined, {hour: "numeric", minute: "numeric"})
const finalText = time;
speak(finalText);
}
else if(message.includes('date')) {
const date = new Date().toLocaleString(undefined, {month: "short", day: "numeric"})
const finalText = date;
speak(finalText);
}
else if(message.includes('calculator')) {
window.open('Calculator:///')
const finalText = "Opening Calculator";
speak(finalText);
}
else {
const customPrompt = "You are JARVIS AI assistant developed by BlackTechX. and my question to you is" + message ;
const response = await chat_with_gpt(customPrompt);
speak(response);
}
}
async function chat_with_gpt(prompt) {
try {
const api_key = await read_api_key(); // Read API key
const url = "https://api.openai.com/v1/chat/completions";
const headers = {
"Content-Type": "application/json",
"Authorization": `Bearer ${api_key}`,
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
};
const data = {
"model": "gpt-3.5-turbo",
"messages": [
{"role": "user", "content": prompt},
],
"max_tokens": Math.floor(Math.random() * (1050 - 850 + 1) + 850)
};
const response = await fetch(url, {
method: 'POST',
headers: headers,
body: JSON.stringify(data)
});
const responseData = await response.json();
return responseData.choices[0].message.content;
} catch (error) {
console.error("Error:", error);
return "I'm sorry, I couldn't process your request at the moment.";
}
}
async function read_api_key() {
return 'YOUR_API_KEY_HERE';
}
</script>
</body>
</html>