Spaces:
No application file
No application file
Upload 8 files
Browse files- api.py +16 -0
- content.py +29 -0
- index.html +799 -0
- knowledge_base.py +44 -0
- prompts.py +270 -0
- query_ai.py +50 -0
- requirements.txt +46 -0
- speech_to_text.py +0 -0
api.py
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify
|
2 |
+
from query_ai import get_response # Ensure this file is uploaded
|
3 |
+
|
4 |
+
app = Flask(__name__)
|
5 |
+
|
6 |
+
@app.route('/chat', methods=['POST'])
|
7 |
+
def chat():
|
8 |
+
user_message = request.json.get("message")
|
9 |
+
if not user_message:
|
10 |
+
return jsonify({"error": "No message provided"}), 400
|
11 |
+
|
12 |
+
bot_response = get_response(user_message)
|
13 |
+
return jsonify({"response": bot_response})
|
14 |
+
|
15 |
+
if __name__ == '__main__':
|
16 |
+
app.run(host='0.0.0.0', port=7860) # Use port 7860 for Hugging Face
|
content.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# content.py
|
2 |
+
|
3 |
+
def get_response_format(query_type):
|
4 |
+
"""
|
5 |
+
Defines AI response formatting using HTML for better visual appeal.
|
6 |
+
"""
|
7 |
+
|
8 |
+
response_templates = {
|
9 |
+
"default": "<p>{response}</p>", # Standard paragraph format
|
10 |
+
"paragraph": "<div style='font-size: 16px; line-height: 1.6;'><strong>📝 Detailed Explanation:</strong><br><br>{response}</div>",
|
11 |
+
"bullet_points": (
|
12 |
+
"<div style='font-size: 16px; line-height: 1.6;'>"
|
13 |
+
"<strong>🔹 Key Points:</strong><br><ul>"
|
14 |
+
+ "".join([f"<li>{point.strip()}</li>" for point in "{response}".split("\n") if point.strip()])
|
15 |
+
+ "</ul></div>"
|
16 |
+
).replace("{response}", "{response}"), # Fix incorrect replacement
|
17 |
+
"points_with_paragraph": (
|
18 |
+
"<div style='font-size: 16px; line-height: 1.6;'>"
|
19 |
+
"<strong>📝 Overview:</strong><br><br>{paragraph}<br><br>"
|
20 |
+
"<strong>🔹 Key Takeaways:</strong><br><ul>"
|
21 |
+
+ "".join([f"<li>{point.strip()}</li>" for point in "{points}".split("\n") if point.strip()])
|
22 |
+
+ "</ul></div>"
|
23 |
+
).replace("{paragraph}", "{response}").replace("{points}", "{response}"),
|
24 |
+
"bold": "<strong>{response}</strong>",
|
25 |
+
"italic": "<em>{response}</em>",
|
26 |
+
"underline": "<u>{response}</u>",
|
27 |
+
}
|
28 |
+
|
29 |
+
return response_templates.get(query_type, response_templates["default"])
|
index.html
ADDED
@@ -0,0 +1,799 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html lang="en">
|
3 |
+
<head>
|
4 |
+
<meta charset="UTF-8">
|
5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
+
<title>Responsive Chatbot Widget</title>
|
7 |
+
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&display=swap" rel="stylesheet">
|
8 |
+
<style>
|
9 |
+
/* Reset and Base Styles */
|
10 |
+
* {
|
11 |
+
box-sizing: border-box;
|
12 |
+
margin: 0;
|
13 |
+
padding: 0;
|
14 |
+
}
|
15 |
+
|
16 |
+
body {
|
17 |
+
font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
|
18 |
+
line-height: 1.5;
|
19 |
+
color: #333;
|
20 |
+
background-color: #f5f5f5;
|
21 |
+
}
|
22 |
+
|
23 |
+
/* Floating Chatbot Icon */
|
24 |
+
#chatbotIcon {
|
25 |
+
position: fixed;
|
26 |
+
bottom: 20px;
|
27 |
+
right: 20px;
|
28 |
+
width: 60px;
|
29 |
+
height: 60px;
|
30 |
+
/* background: linear-gradient(135deg, #6366F1, #A855F7); */
|
31 |
+
/* color: white; */
|
32 |
+
border-radius: 50%;
|
33 |
+
display: flex;
|
34 |
+
justify-content: center;
|
35 |
+
align-items: center;
|
36 |
+
font-size: 30px;
|
37 |
+
cursor: pointer;
|
38 |
+
/* transition: transform 0.3s, box-shadow 0.3s; */
|
39 |
+
/* box-shadow: 0 4px 15px rgba(99, 102, 241, 0.4); */
|
40 |
+
|
41 |
+
z-index: 1000;
|
42 |
+
user-select: none;
|
43 |
+
}
|
44 |
+
|
45 |
+
/* #chatbotIcon:hover {
|
46 |
+
transform: scale(1.05);
|
47 |
+
box-shadow: 0 6px 20px rgba(99, 102, 241, 0.5);
|
48 |
+
} */
|
49 |
+
|
50 |
+
#chatimg{
|
51 |
+
position: fixed;
|
52 |
+
bottom: 20px;
|
53 |
+
right: 20px;
|
54 |
+
width: 80px;
|
55 |
+
height: 80px;
|
56 |
+
border-radius: 50%;
|
57 |
+
box-shadow: 0 4px 15px rgba(99, 102, 241, 0.4);
|
58 |
+
transition: transform 0.3s, box-shadow 0.3s;
|
59 |
+
z-index: 1000;
|
60 |
+
user-select: none;
|
61 |
+
border-radius: 50%;
|
62 |
+
display: flex;
|
63 |
+
justify-content: center;
|
64 |
+
align-items: center;
|
65 |
+
font-size: 30px;
|
66 |
+
cursor: pointer;
|
67 |
+
}
|
68 |
+
#chatimg:hover {
|
69 |
+
transform: scale(1.05);
|
70 |
+
box-shadow: 0 6px 20px rgba(99, 102, 241, 0.5);
|
71 |
+
}
|
72 |
+
|
73 |
+
/* Chatbot Window */
|
74 |
+
#chatbotContainer {
|
75 |
+
position: fixed;
|
76 |
+
bottom: 90px;
|
77 |
+
right: 20px;
|
78 |
+
width: 350px;
|
79 |
+
max-width: 90vw;
|
80 |
+
background: white;
|
81 |
+
border-radius: 16px;
|
82 |
+
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.15);
|
83 |
+
display: none;
|
84 |
+
flex-direction: column;
|
85 |
+
overflow: hidden;
|
86 |
+
z-index: 999;
|
87 |
+
height: 500px;
|
88 |
+
max-height: 80vh;
|
89 |
+
}
|
90 |
+
|
91 |
+
/* Chatbot Header */
|
92 |
+
#chatbotHeader {
|
93 |
+
background: linear-gradient(135deg, #6366F1, #A855F7);
|
94 |
+
color: white;
|
95 |
+
padding: 15px;
|
96 |
+
font-weight: 600;
|
97 |
+
position: relative;
|
98 |
+
font-size: 16px;
|
99 |
+
}
|
100 |
+
|
101 |
+
#closeChat {
|
102 |
+
position: absolute;
|
103 |
+
right: 12px;
|
104 |
+
top: 50%;
|
105 |
+
transform: translateY(-50%);
|
106 |
+
cursor: pointer;
|
107 |
+
font-size: 18px;
|
108 |
+
opacity: 0.8;
|
109 |
+
transition: opacity 0.2s;
|
110 |
+
height: 24px;
|
111 |
+
width: 24px;
|
112 |
+
display: flex;
|
113 |
+
align-items: center;
|
114 |
+
justify-content: center;
|
115 |
+
border-radius: 50%;
|
116 |
+
}
|
117 |
+
|
118 |
+
#closeChat:hover {
|
119 |
+
opacity: 1;
|
120 |
+
background-color: rgba(255, 255, 255, 0.2);
|
121 |
+
}
|
122 |
+
|
123 |
+
/* Chat Messages */
|
124 |
+
#chatMessages {
|
125 |
+
flex: 1;
|
126 |
+
overflow-y: auto;
|
127 |
+
padding: 15px;
|
128 |
+
background: #f5f7fa;
|
129 |
+
display: flex;
|
130 |
+
flex-direction: column;
|
131 |
+
}
|
132 |
+
|
133 |
+
/* Chat Bubbles */
|
134 |
+
.message {
|
135 |
+
padding: 12px 16px;
|
136 |
+
margin: 5px 0;
|
137 |
+
border-radius: 14px;
|
138 |
+
max-width: 80%;
|
139 |
+
word-wrap: break-word;
|
140 |
+
position: relative;
|
141 |
+
animation: fadeIn 0.3s ease;
|
142 |
+
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
|
143 |
+
line-height: 1.4;
|
144 |
+
}
|
145 |
+
|
146 |
+
@keyframes fadeIn {
|
147 |
+
from { opacity: 0; transform: translateY(10px); }
|
148 |
+
to { opacity: 1; transform: translateY(0); }
|
149 |
+
}
|
150 |
+
|
151 |
+
/* User Message */
|
152 |
+
.user {
|
153 |
+
background: linear-gradient(135deg, #6366F1, #A855F7);
|
154 |
+
color: white;
|
155 |
+
align-self: flex-end;
|
156 |
+
border-bottom-right-radius: 2px;
|
157 |
+
}
|
158 |
+
|
159 |
+
/* Bot Message */
|
160 |
+
.bot {
|
161 |
+
background: white;
|
162 |
+
color: #333;
|
163 |
+
align-self: flex-start;
|
164 |
+
border-bottom-left-radius: 2px;
|
165 |
+
}
|
166 |
+
|
167 |
+
/* User Input */
|
168 |
+
#chatInput {
|
169 |
+
display: flex;
|
170 |
+
padding: 12px;
|
171 |
+
border-top: 1px solid #eaeaea;
|
172 |
+
background: white;
|
173 |
+
align-items: center;
|
174 |
+
}
|
175 |
+
|
176 |
+
#userMessage {
|
177 |
+
flex: 1;
|
178 |
+
padding: 12px 16px;
|
179 |
+
border: none;
|
180 |
+
border-radius: 24px;
|
181 |
+
background-color: #f0f0f0;
|
182 |
+
outline: none;
|
183 |
+
font-size: 14px;
|
184 |
+
font-family: inherit;
|
185 |
+
}
|
186 |
+
|
187 |
+
#sendMessage, #micButton {
|
188 |
+
background: linear-gradient(135deg, #6366F1, #A855F7);
|
189 |
+
color: white;
|
190 |
+
border: none;
|
191 |
+
border-radius: 50%;
|
192 |
+
width: 40px;
|
193 |
+
height: 40px;
|
194 |
+
margin-left: 10px;
|
195 |
+
cursor: pointer;
|
196 |
+
display: flex;
|
197 |
+
align-items: center;
|
198 |
+
justify-content: center;
|
199 |
+
transition: transform 0.2s;
|
200 |
+
flex-shrink: 0;
|
201 |
+
}
|
202 |
+
|
203 |
+
#sendMessage:hover, #micButton:hover {
|
204 |
+
transform: scale(1.05);
|
205 |
+
}
|
206 |
+
|
207 |
+
/* Typing indicator */
|
208 |
+
.typing-indicator {
|
209 |
+
display: flex;
|
210 |
+
padding: 12px 16px;
|
211 |
+
background-color: white;
|
212 |
+
border-radius: 14px;
|
213 |
+
width: fit-content;
|
214 |
+
margin: 5px 0;
|
215 |
+
align-self: flex-start;
|
216 |
+
border-bottom-left-radius: 2px;
|
217 |
+
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
|
218 |
+
}
|
219 |
+
|
220 |
+
.typing-indicator span {
|
221 |
+
height: 8px;
|
222 |
+
width: 8px;
|
223 |
+
background-color: #ccc;
|
224 |
+
border-radius: 50%;
|
225 |
+
display: inline-block;
|
226 |
+
margin-right: 5px;
|
227 |
+
animation: bounce 1.3s linear infinite;
|
228 |
+
}
|
229 |
+
|
230 |
+
.typing-indicator span:nth-child(2) {
|
231 |
+
animation-delay: 0.15s;
|
232 |
+
}
|
233 |
+
|
234 |
+
.typing-indicator span:nth-child(3) {
|
235 |
+
animation-delay: 0.3s;
|
236 |
+
margin-right: 0;
|
237 |
+
}
|
238 |
+
|
239 |
+
@keyframes bounce {
|
240 |
+
0%, 60%, 100% { transform: translateY(0); }
|
241 |
+
30% { transform: translateY(-4px); }
|
242 |
+
}
|
243 |
+
|
244 |
+
/* Welcome Screen */
|
245 |
+
#welcomeScreen {
|
246 |
+
display: flex;
|
247 |
+
flex-direction: column;
|
248 |
+
align-items: center;
|
249 |
+
justify-content: center;
|
250 |
+
background: linear-gradient(135deg, #26288E, #5C4A6C);
|
251 |
+
height: 100%;
|
252 |
+
padding: 20px;
|
253 |
+
text-align: center;
|
254 |
+
}
|
255 |
+
|
256 |
+
#robotAnimationContainer {
|
257 |
+
position: relative;
|
258 |
+
width: 240px;
|
259 |
+
height: 200px;
|
260 |
+
margin-bottom: 20px;
|
261 |
+
|
262 |
+
overflow: hidden;
|
263 |
+
|
264 |
+
}
|
265 |
+
|
266 |
+
#robotAnimation {
|
267 |
+
width: 100%;
|
268 |
+
height: 100%;
|
269 |
+
object-fit: cover;
|
270 |
+
}
|
271 |
+
|
272 |
+
#botTitle {
|
273 |
+
color: white;
|
274 |
+
font-weight: 600;
|
275 |
+
margin-bottom: 5px;
|
276 |
+
font-size: 18px;
|
277 |
+
}
|
278 |
+
|
279 |
+
#botSubtitle {
|
280 |
+
color: rgba(255, 255, 255, 0.7);
|
281 |
+
font-size: 13px;
|
282 |
+
margin-bottom: 20px;
|
283 |
+
}
|
284 |
+
|
285 |
+
#getStartedBtn {
|
286 |
+
background: linear-gradient(135deg, #6366F1, #A855F7);
|
287 |
+
color: white;
|
288 |
+
border: none;
|
289 |
+
padding: 10px 20px;
|
290 |
+
border-radius: 30px;
|
291 |
+
font-weight: 600;
|
292 |
+
cursor: pointer;
|
293 |
+
transition: all 0.2s;
|
294 |
+
margin-top: 10px;
|
295 |
+
}
|
296 |
+
|
297 |
+
#getStartedBtn:hover {
|
298 |
+
transform: scale(1.05);
|
299 |
+
}
|
300 |
+
|
301 |
+
#versionTag {
|
302 |
+
color: rgba(255, 255, 255, 0.5);
|
303 |
+
font-size: 12px;
|
304 |
+
margin-top: 15px;
|
305 |
+
}
|
306 |
+
|
307 |
+
/* Name Input Screen */
|
308 |
+
#nameInputScreen {
|
309 |
+
display: none;
|
310 |
+
flex-direction: column;
|
311 |
+
align-items: center;
|
312 |
+
justify-content: center;
|
313 |
+
background: linear-gradient(135deg, #26288E, #5C4A6C);
|
314 |
+
height: 100%;
|
315 |
+
padding: 20px;
|
316 |
+
text-align: center;
|
317 |
+
}
|
318 |
+
|
319 |
+
#namePrompt {
|
320 |
+
color: white;
|
321 |
+
font-weight: 600;
|
322 |
+
margin-bottom: 15px;
|
323 |
+
font-size: 16px;
|
324 |
+
}
|
325 |
+
|
326 |
+
#nameInput {
|
327 |
+
padding: 10px 15px;
|
328 |
+
border: none;
|
329 |
+
border-radius: 20px;
|
330 |
+
width: 80%;
|
331 |
+
max-width: 250px;
|
332 |
+
font-size: 14px;
|
333 |
+
outline: none;
|
334 |
+
margin-bottom: 15px;
|
335 |
+
}
|
336 |
+
|
337 |
+
#submitNameBtn {
|
338 |
+
background: linear-gradient(135deg, #6366F1, #A855F7);
|
339 |
+
color: white;
|
340 |
+
border: none;
|
341 |
+
padding: 10px 20px;
|
342 |
+
border-radius: 30px;
|
343 |
+
font-weight: 600;
|
344 |
+
cursor: pointer;
|
345 |
+
transition: all 0.2s;
|
346 |
+
}
|
347 |
+
|
348 |
+
#submitNameBtn:hover {
|
349 |
+
transform: scale(1.05);
|
350 |
+
}
|
351 |
+
|
352 |
+
/* Mobile Styles (Small devices) */
|
353 |
+
@media (max-width: 480px) {
|
354 |
+
#chatbotContainer {
|
355 |
+
bottom: 80px;
|
356 |
+
right: 10px;
|
357 |
+
width: calc(100vw - 20px);
|
358 |
+
max-width: none;
|
359 |
+
height: calc(100vh - 100px);
|
360 |
+
max-height: none;
|
361 |
+
}
|
362 |
+
|
363 |
+
#chatbotIcon {
|
364 |
+
width: 50px;
|
365 |
+
height: 50px;
|
366 |
+
font-size: 24px;
|
367 |
+
bottom: 15px;
|
368 |
+
right: 15px;
|
369 |
+
}
|
370 |
+
|
371 |
+
#userMessage {
|
372 |
+
padding: 10px 12px;
|
373 |
+
}
|
374 |
+
|
375 |
+
#sendMessage, #micButton {
|
376 |
+
width: 36px;
|
377 |
+
height: 36px;
|
378 |
+
}
|
379 |
+
|
380 |
+
.message {
|
381 |
+
max-width: 85%;
|
382 |
+
}
|
383 |
+
}
|
384 |
+
|
385 |
+
/* Tablet Styles (Medium devices) */
|
386 |
+
@media (min-width: 481px) and (max-width: 768px) {
|
387 |
+
#chatbotContainer {
|
388 |
+
width: 340px;
|
389 |
+
height: 450px;
|
390 |
+
}
|
391 |
+
}
|
392 |
+
|
393 |
+
/* Large Screen Styles */
|
394 |
+
@media (min-width: 1200px) {
|
395 |
+
#chatbotContainer {
|
396 |
+
width: 380px;
|
397 |
+
height: 550px;
|
398 |
+
}
|
399 |
+
}
|
400 |
+
|
401 |
+
/* Landscape Mode on Mobile */
|
402 |
+
@media (max-height: 500px) {
|
403 |
+
#chatbotContainer {
|
404 |
+
height: calc(100vh - 80px);
|
405 |
+
bottom: 70px;
|
406 |
+
}
|
407 |
+
|
408 |
+
#chatbotHeader {
|
409 |
+
padding: 10px 15px;
|
410 |
+
}
|
411 |
+
|
412 |
+
#chatInput {
|
413 |
+
padding: 8px;
|
414 |
+
}
|
415 |
+
|
416 |
+
#welcomeScreen, #nameInputScreen {
|
417 |
+
padding: 10px;
|
418 |
+
}
|
419 |
+
|
420 |
+
#robotAnimationContainer {
|
421 |
+
width: 150px;
|
422 |
+
height: 100px;
|
423 |
+
margin-bottom: 10px;
|
424 |
+
}
|
425 |
+
}
|
426 |
+
|
427 |
+
/* Dark Mode Support */
|
428 |
+
@media (prefers-color-scheme: dark) {
|
429 |
+
body {
|
430 |
+
background-color: #121212;
|
431 |
+
color: #f0f0f0;
|
432 |
+
}
|
433 |
+
|
434 |
+
#chatbotContainer {
|
435 |
+
background: #1e1e1e;
|
436 |
+
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.3);
|
437 |
+
}
|
438 |
+
|
439 |
+
#chatMessages {
|
440 |
+
background: #2d2d2d;
|
441 |
+
}
|
442 |
+
|
443 |
+
.bot {
|
444 |
+
background: #3d3d3d;
|
445 |
+
color: #f0f0f0;
|
446 |
+
}
|
447 |
+
|
448 |
+
#userMessage {
|
449 |
+
background-color: #3d3d3d;
|
450 |
+
color: #f0f0f0;
|
451 |
+
}
|
452 |
+
|
453 |
+
#chatInput {
|
454 |
+
background: #1e1e1e;
|
455 |
+
border-top: 1px solid #333;
|
456 |
+
}
|
457 |
+
|
458 |
+
.typing-indicator {
|
459 |
+
background-color: #3d3d3d;
|
460 |
+
}
|
461 |
+
|
462 |
+
.typing-indicator span {
|
463 |
+
background-color: #888;
|
464 |
+
}
|
465 |
+
|
466 |
+
#nameInput {
|
467 |
+
background-color: #3d3d3d;
|
468 |
+
color: #f0f0f0;
|
469 |
+
}
|
470 |
+
}
|
471 |
+
</style>
|
472 |
+
</head>
|
473 |
+
<body>
|
474 |
+
|
475 |
+
|
476 |
+
<div id="chatbotIcon"><img id="chatimg" src="/Static\digibot.png"></div>
|
477 |
+
|
478 |
+
|
479 |
+
<div id="chatbotContainer">
|
480 |
+
<div id="chatbotHeader">
|
481 |
+
DigiBot
|
482 |
+
<span id="closeChat">✖</span>
|
483 |
+
</div>
|
484 |
+
|
485 |
+
<!-- Welcome Screen -->
|
486 |
+
<div id="welcomeScreen">
|
487 |
+
<div id="robotAnimationContainer">
|
488 |
+
<img id="robotAnimation" src="/Static/vid2.gif" alt="Smart AI Chatbot">
|
489 |
+
</div>
|
490 |
+
|
491 |
+
<div id="botTitle">Smart AI Chatbot Assistance</div>
|
492 |
+
<div id="botSubtitle">Your digital assistant for all your questions</div>
|
493 |
+
<div id="versionTag">v2.0</div>
|
494 |
+
<button id="getStartedBtn">Get Started</button>
|
495 |
+
</div>
|
496 |
+
|
497 |
+
<!-- Name Input Screen with Robot -->
|
498 |
+
<div id="nameInputScreen">
|
499 |
+
<div id="robotAnimationContainer">
|
500 |
+
<img id="robotAnimation" src="/Static/vid2.gif" alt="Smart AI Chatbot">
|
501 |
+
</div>
|
502 |
+
<div id="namePrompt">Hello! I'm your AI assistant. What's your name?</div>
|
503 |
+
<input type="text" id="nameInput" placeholder="Enter your name..." />
|
504 |
+
<button id="submitNameBtn">Continue</button>
|
505 |
+
</div>
|
506 |
+
|
507 |
+
<div id="chatMessages" style="display: none;"></div>
|
508 |
+
<div id="chatInput" style="display: none;">
|
509 |
+
<input type="text" id="userMessage" placeholder="Type a message..." />
|
510 |
+
|
511 |
+
<button id="micButton">
|
512 |
+
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-mic-fill" viewBox="0 0 16 16">
|
513 |
+
<path d="M5 3a3 3 0 0 1 6 0v5a3 3 0 0 1-6 0z"/>
|
514 |
+
<path d="M3.5 6.5A.5.5 0 0 1 4 7v1a4 4 0 0 0 8 0V7a.5.5 0 0 1 1 0v1a5 5 0 0 1-4.5 4.975V15h3a.5.5 0 0 1 0 1h-7a.5.5 0 0 1 0-1h3v-2.025A5 5 0 0 1 3 8V7a.5.5 0 0 1 .5-.5"/>
|
515 |
+
</svg>
|
516 |
+
</button>
|
517 |
+
<button id="sendMessage">
|
518 |
+
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
519 |
+
<path d="M22 2L11 13"></path>
|
520 |
+
<path d="M22 2L15 22L11 13L2 9L22 2Z"></path>
|
521 |
+
</svg>
|
522 |
+
</button>
|
523 |
+
</div>
|
524 |
+
</div>
|
525 |
+
|
526 |
+
<script>
|
527 |
+
// API Endpoint - Using a mockup function for demonstration
|
528 |
+
// Replace this with your actual API endpoint when you connect to a backend
|
529 |
+
const API_URL = "https://chatbot-5dsh.onrender.com/chat";
|
530 |
+
|
531 |
+
// Mockup function for demo purposes
|
532 |
+
function mockApiCall(message) {
|
533 |
+
return new Promise((resolve) => {
|
534 |
+
// Simulate network delay
|
535 |
+
setTimeout(() => {
|
536 |
+
// Simple responses based on message content
|
537 |
+
let response;
|
538 |
+
const lowerMsg = message.toLowerCase();
|
539 |
+
|
540 |
+
if (lowerMsg.includes("hello") || lowerMsg.includes("hi")) {
|
541 |
+
response = "Hello there! How can I help you today?";
|
542 |
+
} else if (lowerMsg.includes("help") || lowerMsg.includes("support")) {
|
543 |
+
response = "I'm here to help! What do you need assistance with?";
|
544 |
+
} else if (lowerMsg.includes("time") || lowerMsg.includes("date")) {
|
545 |
+
response = "It's currently " + new Date().toLocaleString();
|
546 |
+
} else if (lowerMsg.includes("thank")) {
|
547 |
+
response = "You're welcome! Is there anything else I can help with?";
|
548 |
+
} else if (lowerMsg.includes("weather")) {
|
549 |
+
response = "I'm sorry, I don't have access to real-time weather data. You might want to check a weather service for that information.";
|
550 |
+
} else if (lowerMsg.includes("name")) {
|
551 |
+
response = "I'm your AI assistant. How can I assist you today?";
|
552 |
+
} else {
|
553 |
+
response = "I understand you're asking about '" + message + "'. Could you provide more details so I can help you better?";
|
554 |
+
}
|
555 |
+
|
556 |
+
resolve({ response: response });
|
557 |
+
}, 1000);
|
558 |
+
});
|
559 |
+
}
|
560 |
+
|
561 |
+
// Get Elements
|
562 |
+
const chatbotIcon = document.getElementById("chatbotIcon");
|
563 |
+
const chatbotContainer = document.getElementById("chatbotContainer");
|
564 |
+
const closeChat = document.getElementById("closeChat");
|
565 |
+
const chatMessages = document.getElementById("chatMessages");
|
566 |
+
const userMessageInput = document.getElementById("userMessage");
|
567 |
+
const sendMessageButton = document.getElementById("sendMessage");
|
568 |
+
const welcomeScreen = document.getElementById("welcomeScreen");
|
569 |
+
const getStartedBtn = document.getElementById("getStartedBtn");
|
570 |
+
const chatInput = document.getElementById("chatInput");
|
571 |
+
const nameInputScreen = document.getElementById("nameInputScreen");
|
572 |
+
const nameInput = document.getElementById("nameInput");
|
573 |
+
const submitNameBtn = document.getElementById("submitNameBtn");
|
574 |
+
|
575 |
+
// Store the username
|
576 |
+
let userName = "";
|
577 |
+
|
578 |
+
// Flag to track if chat has been initialized
|
579 |
+
let chatInitialized = false;
|
580 |
+
|
581 |
+
// Toggle Chatbot Window
|
582 |
+
// chatbotIcon.addEventListener("click", () => {
|
583 |
+
// if (chatbotContainer.style.display === "none" || !chatbotContainer.style.display) {
|
584 |
+
// chatbotContainer.style.display = "flex";
|
585 |
+
|
586 |
+
// // If chat hasn't been initialized yet, show welcome screen
|
587 |
+
// if (!chatInitialized) {
|
588 |
+
// welcomeScreen.style.display = "flex";
|
589 |
+
// nameInputScreen.style.display = "none";
|
590 |
+
// chatMessages.style.display = "none";
|
591 |
+
// chatInput.style.display = "none";
|
592 |
+
// } else {
|
593 |
+
// // If chat was initialized but closed, just show the chat interface
|
594 |
+
// welcomeScreen.style.display = "none";
|
595 |
+
// nameInputScreen.style.display = "none";
|
596 |
+
// chatMessages.style.display = "flex";
|
597 |
+
// chatInput.style.display = "flex";
|
598 |
+
// }
|
599 |
+
// } else {
|
600 |
+
// chatbotContainer.style.display = "none";
|
601 |
+
// }
|
602 |
+
// });
|
603 |
+
|
604 |
+
// Modify the existing chatbotIcon click event listener
|
605 |
+
chatbotIcon.addEventListener("click", () => {
|
606 |
+
if (chatbotContainer.style.display === "none" || !chatbotContainer.style.display) {
|
607 |
+
chatbotContainer.style.display = "flex";
|
608 |
+
|
609 |
+
// If chat has been fully initialized before, reset to initial state
|
610 |
+
if (chatInitialized) {
|
611 |
+
// Clear previous chat messages
|
612 |
+
chatMessages.innerHTML = '';
|
613 |
+
|
614 |
+
// Show welcome screen again
|
615 |
+
welcomeScreen.style.display = "flex";
|
616 |
+
nameInputScreen.style.display = "none";
|
617 |
+
chatMessages.style.display = "none";
|
618 |
+
chatInput.style.display = "none";
|
619 |
+
|
620 |
+
// Reset chat initialization flag
|
621 |
+
chatInitialized = false;
|
622 |
+
|
623 |
+
// Clear stored username
|
624 |
+
userName = "";
|
625 |
+
|
626 |
+
// Clear name input
|
627 |
+
nameInput.value = "";
|
628 |
+
} else {
|
629 |
+
// If chat hasn't been initialized, show welcome screen as before
|
630 |
+
welcomeScreen.style.display = "flex";
|
631 |
+
nameInputScreen.style.display = "none";
|
632 |
+
chatMessages.style.display = "none";
|
633 |
+
chatInput.style.display = "none";
|
634 |
+
}
|
635 |
+
} else {
|
636 |
+
chatbotContainer.style.display = "none";
|
637 |
+
}
|
638 |
+
});
|
639 |
+
|
640 |
+
// Get Started Button
|
641 |
+
getStartedBtn.addEventListener("click", () => {
|
642 |
+
welcomeScreen.style.display = "none";
|
643 |
+
nameInputScreen.style.display = "flex";
|
644 |
+
chatMessages.style.display = "none";
|
645 |
+
chatInput.style.display = "none";
|
646 |
+
});
|
647 |
+
|
648 |
+
// Submit Name Button
|
649 |
+
submitNameBtn.addEventListener("click", () => {
|
650 |
+
submitUserName();
|
651 |
+
});
|
652 |
+
|
653 |
+
// Handle name input enter key press
|
654 |
+
nameInput.addEventListener("keypress", function(event) {
|
655 |
+
if (event.key === "Enter") {
|
656 |
+
submitUserName();
|
657 |
+
}
|
658 |
+
});
|
659 |
+
|
660 |
+
function submitUserName() {
|
661 |
+
userName = nameInput.value.trim();
|
662 |
+
if (!userName) {
|
663 |
+
// userName = "Friend"; // Default name if user doesn't enter anything
|
664 |
+
alert("Please enter your name");
|
665 |
+
return
|
666 |
+
}
|
667 |
+
|
668 |
+
// Hide name input, show chat interface
|
669 |
+
nameInputScreen.style.display = "none";
|
670 |
+
chatMessages.style.display = "flex";
|
671 |
+
chatInput.style.display = "flex";
|
672 |
+
|
673 |
+
// Set chat as initialized
|
674 |
+
chatInitialized = true;
|
675 |
+
|
676 |
+
// Add personalized welcome message
|
677 |
+
displayMessage(`Hello ${userName}! How can I help you today?`, "bot");
|
678 |
+
userMessageInput.focus();
|
679 |
+
}
|
680 |
+
|
681 |
+
// Close Chatbot Window
|
682 |
+
closeChat.addEventListener("click", (e) => {
|
683 |
+
e.stopPropagation();
|
684 |
+
chatbotContainer.style.display = "none";
|
685 |
+
});
|
686 |
+
|
687 |
+
// Send User Message
|
688 |
+
sendMessageButton.addEventListener("click", sendMessage);
|
689 |
+
userMessageInput.addEventListener("keypress", function(event) {
|
690 |
+
if (event.key === "Enter") {
|
691 |
+
sendMessage();
|
692 |
+
}
|
693 |
+
});
|
694 |
+
|
695 |
+
async function sendMessage() {
|
696 |
+
let userMessage = userMessageInput.value.trim();
|
697 |
+
if (!userMessage) return;
|
698 |
+
|
699 |
+
// Display User Message
|
700 |
+
displayMessage(userMessage, "user");
|
701 |
+
userMessageInput.value = "";
|
702 |
+
|
703 |
+
// Show typing indicator
|
704 |
+
const typingIndicator = document.createElement("div");
|
705 |
+
typingIndicator.className = "typing-indicator";
|
706 |
+
typingIndicator.innerHTML = "<span></span><span></span><span></span>";
|
707 |
+
chatMessages.appendChild(typingIndicator);
|
708 |
+
chatMessages.scrollTop = chatMessages.scrollHeight;
|
709 |
+
|
710 |
+
// Call Flask API
|
711 |
+
try {
|
712 |
+
let response = await fetch(API_URL, {
|
713 |
+
method: "POST",
|
714 |
+
headers: { "Content-Type": "application/json" },
|
715 |
+
body: JSON.stringify({
|
716 |
+
message: userMessage,
|
717 |
+
userName: userName // Send username with the message
|
718 |
+
})
|
719 |
+
});
|
720 |
+
|
721 |
+
// Remove typing indicator
|
722 |
+
chatMessages.removeChild(typingIndicator);
|
723 |
+
|
724 |
+
let data = await response.json();
|
725 |
+
displayMessage(data.response, "bot");
|
726 |
+
} catch (error) {
|
727 |
+
// Remove typing indicator
|
728 |
+
chatMessages.removeChild(typingIndicator);
|
729 |
+
displayMessage("❌ Error: Unable to connect to chatbot.", "bot");
|
730 |
+
}
|
731 |
+
}
|
732 |
+
|
733 |
+
function displayMessage(text, sender) {
|
734 |
+
let messageDiv = document.createElement("div");
|
735 |
+
messageDiv.textContent = text;
|
736 |
+
messageDiv.classList.add(sender, "message");
|
737 |
+
text = text
|
738 |
+
.replace(/## (.*?)(\n|$)/g, "<h2>$1</h2>") // Convert ## Heading to <h2>
|
739 |
+
.replace(/### (.*?)(\n|$)/g, "<h3>$1</h3>") // Convert ### Heading to <h3>
|
740 |
+
.replace(/\*\*(.*?)\*\*/g, "<strong>$1</strong>") // Bold (**text** → <strong>text</strong>)
|
741 |
+
.replace(/\*(.*?)\*/g, "<em>$1</em>") // Italic (*text* → <em>text</em>)
|
742 |
+
.replace(/__(.*?)__/g, "<u>$1</u>") // Underline (__text__ → <u>text</u>)
|
743 |
+
.replace(/- (.*?)(\n|$)/g, "<li>$1</li>") // Bullet points (- text → <li>text</li>)
|
744 |
+
.replace(/\n/g, "<br>"); // Preserve line breaks
|
745 |
+
|
746 |
+
// Wrap bullet points in <ul> if they exist
|
747 |
+
if (text.includes("<li>")) {
|
748 |
+
text = text.replace(/(<li>.*?<\/li>)/gs, "<ul>$1</ul>");
|
749 |
+
}
|
750 |
+
|
751 |
+
messageDiv.innerHTML= text;
|
752 |
+
|
753 |
+
chatMessages.appendChild(messageDiv);
|
754 |
+
chatMessages.scrollTop = chatMessages.scrollHeight; // Keep the latest message in view
|
755 |
+
let words = text.split(" "); // Split text into words
|
756 |
+
messageDiv.innerHTML = ""; // Clear initial content
|
757 |
+
let index = 0;
|
758 |
+
|
759 |
+
function typeWords() {
|
760 |
+
if (index < words.length) {
|
761 |
+
messageDiv.innerHTML += words[index] + " "; // Append word
|
762 |
+
index++;
|
763 |
+
chatMessages.scrollTop = chatMessages.scrollHeight; // Auto-scroll
|
764 |
+
setTimeout(typeWords, 60); // Adjust speed (Lower = Faster)
|
765 |
+
}
|
766 |
+
}
|
767 |
+
|
768 |
+
typeWords();
|
769 |
+
}
|
770 |
+
|
771 |
+
// Prevent clicks inside chatbot container from closing it
|
772 |
+
chatbotContainer.addEventListener("click", (e) => {
|
773 |
+
e.stopPropagation();
|
774 |
+
});
|
775 |
+
|
776 |
+
document.getElementById("micButton").addEventListener("click", function() {
|
777 |
+
fetch("/speech") // Calls Flask speech-to-text API
|
778 |
+
.then(response => response.json())
|
779 |
+
.then(data => {
|
780 |
+
let recognizedText = data.recognized_text;
|
781 |
+
if (recognizedText) {
|
782 |
+
document.getElementById("userMessage").value = recognizedText; // Autofill input
|
783 |
+
document.getElementById("sendMessage").click(); // Send automatically
|
784 |
+
} else {
|
785 |
+
alert("Couldn't recognize speech. Try again.");
|
786 |
+
}
|
787 |
+
})
|
788 |
+
.catch(error => console.error("Error:", error));
|
789 |
+
});
|
790 |
+
// Handle window resize
|
791 |
+
window.addEventListener("resize", () => {
|
792 |
+
// If chat is open, adjust scroll
|
793 |
+
if (chatbotContainer.style.display === "flex" && chatMessages.style.display === "flex") {
|
794 |
+
chatMessages.scrollTop = chatMessages.scrollHeight;
|
795 |
+
}
|
796 |
+
});
|
797 |
+
</script>
|
798 |
+
</body>
|
799 |
+
</html>
|
knowledge_base.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import logging
|
3 |
+
|
4 |
+
# Configure logging
|
5 |
+
logging.basicConfig(level=logging.INFO)
|
6 |
+
|
7 |
+
# Load the knowledge base from data.json
|
8 |
+
def load_knowledge_base():
|
9 |
+
"""Loads knowledge base from data.json file."""
|
10 |
+
try:
|
11 |
+
with open("data.json", "r", encoding="utf-8") as file:
|
12 |
+
data = json.load(file)
|
13 |
+
logging.info("Knowledge base loaded successfully.")
|
14 |
+
return data
|
15 |
+
except Exception as e:
|
16 |
+
logging.error(f"Failed to load knowledge base: {e}")
|
17 |
+
return {"paragraphs": [], "headings": {}, "links": {}}
|
18 |
+
|
19 |
+
knowledge_base = load_knowledge_base()
|
20 |
+
|
21 |
+
def search_knowledge_base(query):
|
22 |
+
"""Search knowledge base for relevant information."""
|
23 |
+
try:
|
24 |
+
for para in knowledge_base.get("paragraphs", []):
|
25 |
+
if query.lower() in para.lower():
|
26 |
+
return para
|
27 |
+
for heading_list in knowledge_base.get("headings", {}).values():
|
28 |
+
for heading in heading_list:
|
29 |
+
if query.lower() in heading.lower():
|
30 |
+
return heading
|
31 |
+
return None
|
32 |
+
except Exception as e:
|
33 |
+
logging.error(f"Error searching knowledge base: {e}")
|
34 |
+
return None
|
35 |
+
|
36 |
+
def save_to_knowledge_base(question, answer):
|
37 |
+
"""Save new Gemini AI responses to the knowledge base."""
|
38 |
+
try:
|
39 |
+
knowledge_base["paragraphs"].append(answer)
|
40 |
+
with open("data.json", "w", encoding="utf-8") as file:
|
41 |
+
json.dump(knowledge_base, file, indent=4, ensure_ascii=False)
|
42 |
+
logging.info("New data saved to knowledge base.")
|
43 |
+
except Exception as e:
|
44 |
+
logging.error(f"Failed to save data to knowledge base: {e}")
|
prompts.py
AD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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# prompts.py
|
2 |
+
|
3 |
+
PROMPTS = {
|
4 |
+
# 🔹 General Advice
|
5 |
+
"self_improvement": """You are a life coach helping users improve their productivity, confidence, and habits.
|
6 |
+
- Share **goal-setting techniques**.
|
7 |
+
- Offer **time management strategies**.
|
8 |
+
- Provide **ways to build confidence and self-discipline**.
|
9 |
+
""",
|
10 |
+
|
11 |
+
"motivation": """You are a motivational speaker.
|
12 |
+
- Provide **daily motivational quotes**.
|
13 |
+
- Share **real-life success stories** for inspiration.
|
14 |
+
- Offer **positive affirmations & productivity tips**.
|
15 |
+
""",
|
16 |
+
|
17 |
+
"relationship_advice": """You are a relationship coach.
|
18 |
+
- Provide **communication tips for healthy relationships**.
|
19 |
+
- Offer **advice on resolving conflicts**.
|
20 |
+
- Share insights on **building trust and emotional connection**.
|
21 |
+
""",
|
22 |
+
|
23 |
+
# 🔹 Advanced AI/ML & Data Science
|
24 |
+
"deep_learning": """You are an AI expert specializing in Deep Learning.
|
25 |
+
- Explain **CNNs, RNNs, Transformers, and GANs**.
|
26 |
+
- Provide **TensorFlow & PyTorch code examples**.
|
27 |
+
- Guide on **training models efficiently**.
|
28 |
+
""",
|
29 |
+
|
30 |
+
"nlp": """You are an expert in Natural Language Processing (NLP).
|
31 |
+
- Explain **Tokenization, Named Entity Recognition, and Transformers**.
|
32 |
+
- Provide **code examples using Hugging Face & spaCy**.
|
33 |
+
- Share **best practices for text preprocessing**.
|
34 |
+
""",
|
35 |
+
|
36 |
+
"big_data": """You are a Data Engineer.
|
37 |
+
- Guide on **handling large-scale data processing**.
|
38 |
+
- Explain **Hadoop, Spark, and distributed computing**.
|
39 |
+
- Offer **best practices for database optimization**.
|
40 |
+
""",
|
41 |
+
|
42 |
+
# 🔹 Business & Productivity
|
43 |
+
"business_strategy": """You are a business strategist.
|
44 |
+
- Share insights on **market trends & business growth strategies**.
|
45 |
+
- Explain **how to create an effective business plan**.
|
46 |
+
- Provide **case studies on successful companies**.
|
47 |
+
""",
|
48 |
+
|
49 |
+
"startup_funding": """You are a startup advisor.
|
50 |
+
- Guide on **how to secure funding from investors**.
|
51 |
+
- Explain **Venture Capital, Angel Investors, and Bootstrapping**.
|
52 |
+
- Provide **pitch deck tips for fundraising**.
|
53 |
+
""",
|
54 |
+
|
55 |
+
"marketing_tips": """You are a digital marketing expert.
|
56 |
+
- Explain **SEO, social media marketing, and PPC advertising**.
|
57 |
+
- Share strategies for **email marketing and content creation**.
|
58 |
+
- Provide **case studies on successful marketing campaigns**.
|
59 |
+
""",
|
60 |
+
|
61 |
+
# 🔹 Science, Technology & Space
|
62 |
+
"space_exploration": """You are an expert in space exploration.
|
63 |
+
- Provide updates on **NASA, SpaceX, and Mars missions**.
|
64 |
+
- Explain **black holes, exoplanets, and dark matter**.
|
65 |
+
- Share **exciting upcoming space missions**.
|
66 |
+
""",
|
67 |
+
|
68 |
+
"quantum_computing": """You are an expert in Quantum Computing.
|
69 |
+
- Explain **Qubits, Superposition, and Entanglement**.
|
70 |
+
- Share insights on **Quantum Algorithms like Shor’s and Grover’s**.
|
71 |
+
- Provide updates on **IBM, Google, and Microsoft’s quantum advancements**.
|
72 |
+
""",
|
73 |
+
|
74 |
+
# 🔹 Education & Study Assistance
|
75 |
+
"math_tutor": """You are a math tutor.
|
76 |
+
- Explain **calculus, algebra, and trigonometry concepts**.
|
77 |
+
- Provide **step-by-step solutions to math problems**.
|
78 |
+
- Offer **real-world applications of mathematical theories**.
|
79 |
+
""",
|
80 |
+
|
81 |
+
"language_learning": """You are a language learning coach.
|
82 |
+
- Help users learn **new vocabulary and grammar rules**.
|
83 |
+
- Offer **language exercises for daily practice**.
|
84 |
+
- Provide **common phrases and pronunciation tips**.
|
85 |
+
""",
|
86 |
+
|
87 |
+
# 🔹 Travel & Lifestyle
|
88 |
+
"budget_travel": """You are a budget travel expert.
|
89 |
+
- Share **affordable travel destinations**.
|
90 |
+
- Provide **money-saving travel tips**.
|
91 |
+
- Recommend **budget-friendly accommodations and transport options**.
|
92 |
+
""",
|
93 |
+
|
94 |
+
"luxury_travel": """You are a luxury travel consultant.
|
95 |
+
- Recommend **high-end destinations and 5-star resorts**.
|
96 |
+
- Provide **insider tips on luxury travel experiences**.
|
97 |
+
- Share insights on **VIP travel services and hidden gems**.
|
98 |
+
""",
|
99 |
+
|
100 |
+
# 🔹 Specialized Topics
|
101 |
+
"legal_advice": """You are a legal expert (but not a lawyer).
|
102 |
+
- Provide **general information on contracts, business law, and rights**.
|
103 |
+
- Explain **basic legal terms in simple language**.
|
104 |
+
- Advise users to consult a real lawyer for **legal representation**.
|
105 |
+
""",
|
106 |
+
|
107 |
+
"philosophy": """You are a philosophy teacher.
|
108 |
+
- Explain **major philosophical concepts from different schools of thought**.
|
109 |
+
- Discuss **theories from Aristotle, Kant, Nietzsche, and more**.
|
110 |
+
- Offer insights into **ethics, logic, and existentialism**.
|
111 |
+
""",
|
112 |
+
|
113 |
+
"psychology": """You are a psychology expert.
|
114 |
+
- Explain **cognitive biases, behavioral psychology, and mental health theories**.
|
115 |
+
- Provide **scientific insights into human behavior**.
|
116 |
+
- Share **psychology-backed productivity and well-being tips**.
|
117 |
+
""",
|
118 |
+
|
119 |
+
"astrology": """You are an astrology guide.
|
120 |
+
- Explain **zodiac signs and their personality traits**.
|
121 |
+
- Share **daily, weekly, and monthly horoscopes**.
|
122 |
+
- Provide insights into **planetary movements and their effects**.
|
123 |
+
""",
|
124 |
+
|
125 |
+
"anime_recommendations": """You are an anime expert.
|
126 |
+
- Recommend **anime series based on different genres**.
|
127 |
+
- Provide **short reviews and summaries**.
|
128 |
+
- Suggest **must-watch anime classics and new releases**.
|
129 |
+
""",
|
130 |
+
|
131 |
+
# 🔹 Emergency & Safety Tips
|
132 |
+
"disaster_preparedness": """You are a disaster preparedness expert.
|
133 |
+
- Provide **emergency preparedness tips for natural disasters**.
|
134 |
+
- Explain **what to do in case of an earthquake, flood, or hurricane**.
|
135 |
+
- Offer **first-aid basics and survival strategies**.
|
136 |
+
""",
|
137 |
+
|
138 |
+
"cyber_security": """You are a cybersecurity expert.
|
139 |
+
- Explain **how to protect personal data online**.
|
140 |
+
- Share **best practices for strong passwords and two-factor authentication**.
|
141 |
+
- Provide **guidance on avoiding phishing scams and malware**.
|
142 |
+
""",
|
143 |
+
|
144 |
+
# General Prompt (Default)
|
145 |
+
"general": """You are a professional AI assistant. Provide structured, concise, and engaging answers.
|
146 |
+
Use clear headings, bullet points, and avoid unnecessary disclaimers. Keep responses user-friendly and to the point.""",
|
147 |
+
|
148 |
+
# Medical Advice (Not a doctor, but can provide general health insights)
|
149 |
+
"medical_advice": """You are a virtual assistant providing general health guidance.
|
150 |
+
- **Summarize possible causes** concisely.
|
151 |
+
- **Offer actionable steps** for relief.
|
152 |
+
- **Highlight when to see a doctor**.
|
153 |
+
- Keep responses **structured, clear, and empathetic**.
|
154 |
+
⚠️ Do not claim to be a medical professional.""",
|
155 |
+
|
156 |
+
# Programming Help
|
157 |
+
"technical_help": """You are an AI assistant specializing in coding and troubleshooting.
|
158 |
+
- Use **step-by-step explanations**.
|
159 |
+
- Provide **code snippets** for clarity.
|
160 |
+
- Offer **real-world examples** where applicable.
|
161 |
+
- Keep solutions **concise and optimized**.
|
162 |
+
""",
|
163 |
+
|
164 |
+
# AI/ML Guidance
|
165 |
+
"ai_ml": """You are an expert in Artificial Intelligence and Machine Learning.
|
166 |
+
- Provide insights on **models, training techniques, and datasets**.
|
167 |
+
- Offer **Python code examples** using Scikit-learn, TensorFlow, or PyTorch.
|
168 |
+
- Explain concepts **in an easy-to-understand way** with analogies.
|
169 |
+
""",
|
170 |
+
|
171 |
+
# Cloud Computing & DevOps
|
172 |
+
"cloud_devops": """You are an expert in Cloud Computing & DevOps.
|
173 |
+
- Explain **AWS, Azure, and GCP concepts**.
|
174 |
+
- Guide on **CI/CD, Kubernetes, and Docker**.
|
175 |
+
- Provide **step-by-step DevOps best practices**.
|
176 |
+
""",
|
177 |
+
|
178 |
+
# Cybersecurity & Ethical Hacking
|
179 |
+
"cybersecurity": """You are an expert in cybersecurity and ethical hacking.
|
180 |
+
- Explain **common security threats & vulnerabilities**.
|
181 |
+
- Offer **best security practices** for organizations.
|
182 |
+
- Provide **penetration testing techniques** (without promoting illegal activities).
|
183 |
+
- Educate users on **password security, encryption, and safe browsing**.
|
184 |
+
""",
|
185 |
+
|
186 |
+
# Data Science & Analytics
|
187 |
+
"data_science": """You are an AI specializing in Data Science.
|
188 |
+
- Guide on **data preprocessing, feature engineering, and model evaluation**.
|
189 |
+
- Explain **EDA (Exploratory Data Analysis)** with Python examples.
|
190 |
+
- Provide **visualization techniques using Matplotlib & Seaborn**.
|
191 |
+
""",
|
192 |
+
|
193 |
+
# Fitness & Health
|
194 |
+
"fitness_tips": """You are a fitness expert providing practical fitness and nutrition advice.
|
195 |
+
- Share **warm-up, workout, and recovery tips**.
|
196 |
+
- Provide **dietary recommendations** based on different fitness goals.
|
197 |
+
- Motivate users to **stay consistent with their routine**.
|
198 |
+
""",
|
199 |
+
|
200 |
+
# Financial Advice
|
201 |
+
"financial_advice": """You are an AI finance assistant.
|
202 |
+
- Provide insights on **saving, investing, and budgeting**.
|
203 |
+
- Explain **stock market trends, cryptocurrency, and personal finance**.
|
204 |
+
- Help users **manage expenses and financial planning** effectively.
|
205 |
+
""",
|
206 |
+
|
207 |
+
# Travel Recommendations
|
208 |
+
"travel_recommendations": """You are a travel assistant helping users with personalized travel recommendations.
|
209 |
+
- Suggest **best destinations** based on the user's interests.
|
210 |
+
- Recommend **local food, activities, and must-visit places**.
|
211 |
+
- Provide **travel safety tips and budget planning**.
|
212 |
+
""",
|
213 |
+
|
214 |
+
# Career Guidance & Resume Review
|
215 |
+
"career_guidance": """You are a career consultant helping users with job search strategies.
|
216 |
+
- Provide **resume-building tips & best practices**.
|
217 |
+
- Offer advice on **interview preparation & soft skills**.
|
218 |
+
- Guide users on **job trends and upskilling opportunities**.
|
219 |
+
""",
|
220 |
+
|
221 |
+
# Mental Health & Motivation
|
222 |
+
"mental_health": """You are an AI providing motivation and mental wellness guidance.
|
223 |
+
- Share **stress management techniques**.
|
224 |
+
- Provide **positive affirmations & self-care tips**.
|
225 |
+
- Guide users on **work-life balance & mindfulness exercises**.
|
226 |
+
⚠️ Do not replace professional therapy or mental health counseling.
|
227 |
+
""",
|
228 |
+
|
229 |
+
# Educational Guidance
|
230 |
+
"study_tips": """You are an academic mentor helping students improve their study habits.
|
231 |
+
- Share **effective learning techniques** (Pomodoro, active recall, spaced repetition).
|
232 |
+
- Offer **time management strategies for exams**.
|
233 |
+
- Provide **recommendations on online courses & certifications**.
|
234 |
+
""",
|
235 |
+
|
236 |
+
# Business & Startup Advice
|
237 |
+
"business_startup": """You are a startup mentor guiding entrepreneurs.
|
238 |
+
- Offer insights on **business models, funding, and scaling a startup**.
|
239 |
+
- Provide **marketing & growth strategies**.
|
240 |
+
- Guide on **common mistakes to avoid in entrepreneurship**.
|
241 |
+
""",
|
242 |
+
|
243 |
+
# Cooking & Recipes
|
244 |
+
"cooking_recipes": """You are a chef providing easy-to-follow recipes.
|
245 |
+
- Suggest **quick meals based on available ingredients**.
|
246 |
+
- Share **healthy meal ideas for different diets** (vegan, keto, etc.).
|
247 |
+
- Provide **cooking techniques for beginners & experts**.
|
248 |
+
""",
|
249 |
+
|
250 |
+
# Entertainment & Book Recommendations
|
251 |
+
"book_movies": """You are an entertainment and book recommendation assistant.
|
252 |
+
- Suggest **must-read books across different genres**.
|
253 |
+
- Recommend **movies & TV shows based on user preferences**.
|
254 |
+
- Provide **short reviews & ratings**.
|
255 |
+
""",
|
256 |
+
|
257 |
+
# Science & Space Exploration
|
258 |
+
"science_space": """You are an AI expert in Science and Space Exploration.
|
259 |
+
- Explain **scientific phenomena in an engaging way**.
|
260 |
+
- Share updates on **NASA, SpaceX, and latest space discoveries**.
|
261 |
+
- Provide **facts on black holes, galaxies, and the universe**.
|
262 |
+
"""
|
263 |
+
}
|
264 |
+
|
265 |
+
def get_prompt(user_input):
|
266 |
+
"""Returns a predefined prompt if available, otherwise a fallback response."""
|
267 |
+
for key, prompt in PROMPTS.items():
|
268 |
+
if key in user_input:
|
269 |
+
return prompt
|
270 |
+
return PROMPTS.get("general", "I'm not sure how to respond to that.")
|
query_ai.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import google.generativeai as genai
|
2 |
+
import logging
|
3 |
+
from content import get_response_format
|
4 |
+
from prompts import get_prompt
|
5 |
+
import os
|
6 |
+
# Configure logging
|
7 |
+
logging.basicConfig(level=logging.INFO)
|
8 |
+
|
9 |
+
# Set up Gemini API Key (Replace with your API Key)
|
10 |
+
# GEMINI_API_KEY = "your-api-key"
|
11 |
+
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
12 |
+
|
13 |
+
def query_gemini(prompt, query_type="default"):
|
14 |
+
"""
|
15 |
+
Queries Gemini AI and returns a formatted response.
|
16 |
+
"""
|
17 |
+
try:
|
18 |
+
model = genai.GenerativeModel(model_name="gemini-2.0-flash-lite")
|
19 |
+
|
20 |
+
# Add structured prompt formatting
|
21 |
+
structured_prompt = get_prompt(prompt.lower())
|
22 |
+
full_prompt = f"{structured_prompt}\n\nFormat your response clearly.\n\nUser Query: {prompt}"
|
23 |
+
|
24 |
+
# Generate response
|
25 |
+
response = model.generate_content(full_prompt)
|
26 |
+
response_text = response.text.strip() if response.text else "I couldn't generate a response."
|
27 |
+
|
28 |
+
return format_response(response_text, query_type)
|
29 |
+
|
30 |
+
except Exception as e:
|
31 |
+
logging.error(f"Gemini API error: {e}")
|
32 |
+
return "Sorry, an error occurred while generating a response."
|
33 |
+
|
34 |
+
def format_response(response_text, query_type="default"):
|
35 |
+
"""
|
36 |
+
Formats AI responses based on the query type using content.py rules.
|
37 |
+
"""
|
38 |
+
response_template = get_response_format(query_type)
|
39 |
+
|
40 |
+
if not isinstance(response_template, str):
|
41 |
+
logging.error("Invalid response format: response_templates should be a dictionary string.")
|
42 |
+
return response_text # Return unformatted response as fallback
|
43 |
+
|
44 |
+
formatted_response = response_template.replace("{response}", response_text)
|
45 |
+
|
46 |
+
# Preserve new lines for bullet points
|
47 |
+
if query_type in ["bullet_points", "points_with_paragraph"]:
|
48 |
+
formatted_response = formatted_response.replace("\\n", "\n")
|
49 |
+
|
50 |
+
return formatted_response
|
requirements.txt
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
annotated-types==0.7.0
|
2 |
+
blinker==1.9.0
|
3 |
+
cachetools==5.5.2
|
4 |
+
certifi==2025.1.31
|
5 |
+
cffi==1.17.1
|
6 |
+
charset-normalizer==3.4.1
|
7 |
+
click==8.1.8
|
8 |
+
colorama==0.4.6
|
9 |
+
Flask==3.1.0
|
10 |
+
flask-cors==5.0.1
|
11 |
+
google-ai-generativelanguage==0.6.15
|
12 |
+
google-api-core==2.24.1
|
13 |
+
google-api-python-client==2.162.0
|
14 |
+
google-auth==2.38.0
|
15 |
+
google-auth-httplib2==0.2.0
|
16 |
+
google-cloud-speech==2.31.0
|
17 |
+
google-generativeai==0.8.4
|
18 |
+
googleapis-common-protos==1.68.0
|
19 |
+
grpcio==1.71.0rc2
|
20 |
+
grpcio-status==1.71.0rc2
|
21 |
+
gunicorn==23.0.0
|
22 |
+
httplib2==0.22.0
|
23 |
+
idna==3.10
|
24 |
+
itsdangerous==2.2.0
|
25 |
+
Jinja2==3.1.5
|
26 |
+
MarkupSafe==3.0.2
|
27 |
+
numpy==2.2.3
|
28 |
+
packaging==24.2
|
29 |
+
proto-plus==1.26.0
|
30 |
+
protobuf==5.29.3
|
31 |
+
pyasn1==0.6.1
|
32 |
+
pyasn1_modules==0.4.1
|
33 |
+
pycparser==2.22
|
34 |
+
pydantic==2.10.6
|
35 |
+
pydantic_core==2.27.2
|
36 |
+
pydub==0.25.1
|
37 |
+
pyparsing==3.2.1
|
38 |
+
requests==2.32.3
|
39 |
+
rsa==4.9
|
40 |
+
SpeechRecognition==3.14.1
|
41 |
+
tqdm==4.67.1
|
42 |
+
typing_extensions==4.12.2
|
43 |
+
uritemplate==4.1.1
|
44 |
+
urllib3==2.3.0
|
45 |
+
waitress==3.0.2
|
46 |
+
Werkzeug==3.1.3
|
speech_to_text.py
ADDED
File without changes
|