Askoscar commited on
Commit
8969268
Β·
verified Β·
1 Parent(s): b55a14c

Update script.js

Browse files
Files changed (1) hide show
  1. script.js +83 -59
script.js CHANGED
@@ -1,59 +1,83 @@
1
- document.addEventListener("DOMContentLoaded", () => {
2
- const aboutBtn = document.getElementById("aboutBtn");
3
- const donateBtn = document.getElementById("donateBtn");
4
- const aboutModal = document.getElementById("aboutModal");
5
- const donateModal = document.getElementById("donateModal");
6
- const closeAbout = document.getElementById("closeAbout");
7
- const closeDonate = document.getElementById("closeDonate");
8
- const sendBtn = document.getElementById("askButton");
9
- const chatBox = document.getElementById("chat-box");
10
- const input = document.getElementById("userInput");
11
-
12
- // open / close modals
13
- aboutBtn.onclick = () => aboutModal.style.display = "block";
14
- donateBtn.onclick = () => donateModal.style.display = "block";
15
- closeAbout.onclick = () => aboutModal.style.display = "none";
16
- closeDonate.onclick = () => donateModal.style.display = "none";
17
- window.onclick = e => {
18
- if (e.target === aboutModal) aboutModal.style.display = "none";
19
- if (e.target === donateModal) donateModal.style.display = "none";
20
- };
21
-
22
- // send message
23
- sendBtn.onclick = sendMessage;
24
- input.addEventListener("keydown", e => { if (e.key === "Enter") sendMessage(); });
25
-
26
- function appendMessage(who, text) {
27
- const div = document.createElement("div");
28
- div.classList.add("message", who);
29
- div.textContent = (who === "user" ? "🧍 " : "πŸͺ΄ ") + text;
30
- chatBox.appendChild(div);
31
- chatBox.scrollTop = chatBox.scrollHeight;
32
- }
33
-
34
- function sendMessage() {
35
- const q = input.value.trim();
36
- if (!q) return;
37
- appendMessage("user", q);
38
- input.value = "";
39
- appendMessage("bot", "🌿 Thinking...");
40
- setTimeout(() => {
41
- const a = simulateHerbalAnswer(q);
42
- replaceLastBotMessage(a);
43
- }, 1000);
44
- }
45
-
46
- function replaceLastBotMessage(text) {
47
- const bots = chatBox.querySelectorAll(".bot");
48
- if (bots.length === 0) return appendMessage("bot", text);
49
- bots[bots.length - 1].textContent = "πŸͺ΄ " + text;
50
- }
51
-
52
- function simulateHerbalAnswer(q) {
53
- const s = q.toLowerCase();
54
- if (s.includes("moringa")) return "Moringa oleifera β€” known locally as Nkruma. Rich in nutrients and supports **anti-inflammatory** and **antioxidant** activity.";
55
- if (s.includes("neem")) return "Azadirachta indica (Neem). Locally Nkwanta. Famous for **antimicrobial** and **skin-cleansing** effects.";
56
- if (s.includes("hello") || s.includes("hi")) return "Hello 🌿! Ask about any medicinal plant or natural remedy.";
57
- return "I’m trained for herbal knowledge. Try asking about a plant, its healing uses, or safe dosage.";
58
- }
59
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const API_URL = "https://api-inference.huggingface.co/models/google/gemma-2b-it";
2
+
3
+ async function askOscarAI(question) {
4
+ // Only allow herbal-related questions
5
+ const herbalWords = ["plant", "herb", "leaves", "roots", "bark", "healing", "natural", "disease", "dosage", "scientific name"];
6
+ const isHerbal = herbalWords.some(word => question.toLowerCase().includes(word));
7
+
8
+ if (!isHerbal) {
9
+ return "πŸƒ Sorry, I only answer questions about medicinal plants, natural healing, and herbal remedies.";
10
+ }
11
+
12
+ const prompt = `
13
+ You are AskOscar Herbal AI β€” an expert in natural and traditional medicine.
14
+ Respond like a warm, intelligent herbalist from Ghana, giving scientific names, local names, healing uses, dosage, and cultural facts.
15
+ User's question: ${question}
16
+ `;
17
+
18
+ const response = await fetch(API_URL, {
19
+ method: "POST",
20
+ headers: {
21
+ "Authorization": `Bearer ${process.env.HF_TOKEN}`,
22
+ "Content-Type": "application/json",
23
+ },
24
+ body: JSON.stringify({ inputs: prompt }),
25
+ });
26
+
27
+ const result = await response.json();
28
+ if (result.error) {
29
+ console.error(result.error);
30
+ return "🌿 The herbal spirits are resting right now... please try again soon.";
31
+ }
32
+
33
+ return result[0]?.generated_text || "No response received.";
34
+ }
35
+
36
+ document.addEventListener("DOMContentLoaded", () => {
37
+ const input = document.getElementById("userInput");
38
+ const button = document.getElementById("askButton");
39
+ const chatBox = document.getElementById("chat-box");
40
+
41
+ // About and Donate modals
42
+ const aboutModal = document.getElementById("aboutModal");
43
+ const aboutBtn = document.getElementById("aboutBtn");
44
+ const closeAbout = document.getElementById("closeAbout");
45
+
46
+ const donateModal = document.getElementById("donateModal");
47
+ const donateBtn = document.getElementById("donateBtn");
48
+ const closeDonate = document.getElementById("closeDonate");
49
+
50
+ aboutBtn.onclick = () => (aboutModal.style.display = "block");
51
+ closeAbout.onclick = () => (aboutModal.style.display = "none");
52
+ donateBtn.onclick = () => (donateModal.style.display = "block");
53
+ closeDonate.onclick = () => (donateModal.style.display = "none");
54
+
55
+ window.onclick = e => {
56
+ if (e.target === aboutModal) aboutModal.style.display = "none";
57
+ if (e.target === donateModal) donateModal.style.display = "none";
58
+ };
59
+
60
+ // Chat logic
61
+ button.addEventListener("click", async () => {
62
+ const question = input.value.trim();
63
+ if (!question) return;
64
+
65
+ const userMsg = document.createElement("div");
66
+ userMsg.classList.add("message", "user");
67
+ userMsg.textContent = "πŸ§β€β™‚οΈ " + question;
68
+ chatBox.appendChild(userMsg);
69
+
70
+ chatBox.scrollTop = chatBox.scrollHeight;
71
+ input.value = "";
72
+
73
+ const botMsg = document.createElement("div");
74
+ botMsg.classList.add("message", "bot");
75
+ botMsg.textContent = "🌿 Thinking...";
76
+ chatBox.appendChild(botMsg);
77
+ chatBox.scrollTop = chatBox.scrollHeight;
78
+
79
+ const answer = await askOscarAI(question);
80
+ botMsg.textContent = "πŸͺ΄ " + answer;
81
+ chatBox.scrollTop = chatBox.scrollHeight;
82
+ });
83
+ });