Spaces:
Running
Running
Update script.js
Browse files
script.js
CHANGED
|
@@ -1,59 +1,83 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
const
|
| 6 |
-
const
|
| 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 |
-
if (
|
| 57 |
-
|
| 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 |
+
});
|