Spaces:
Build error
Build error
Upload 4 files
Browse files- app.py +40 -0
- c46cfe98-a47f-4596-a93b-ea93bb8806f3.jpg +0 -0
- index.html +277 -0
- requirements.txt +16 -0
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, send_from_directory
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 3 |
+
import torch
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
app = Flask(__name__)
|
| 7 |
+
|
| 8 |
+
# --- Load the model ---
|
| 9 |
+
model_name = "microsoft/phi-2"
|
| 10 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 11 |
+
|
| 12 |
+
print("Loading model... this may take a few minutes.")
|
| 13 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 14 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto")
|
| 15 |
+
generator = pipeline("text-generation", model=model, tokenizer=tokenizer, device=0 if torch.cuda.is_available() else -1)
|
| 16 |
+
print("Model loaded.")
|
| 17 |
+
|
| 18 |
+
# --- Serve your custom HTML interface ---
|
| 19 |
+
@app.route("/")
|
| 20 |
+
def home():
|
| 21 |
+
return send_from_directory(os.path.dirname(__file__), "index.html")
|
| 22 |
+
|
| 23 |
+
# --- API endpoint for AI ---
|
| 24 |
+
@app.route("/ask", methods=["POST"])
|
| 25 |
+
def ask():
|
| 26 |
+
data = request.get_json()
|
| 27 |
+
question = data.get("question", "")
|
| 28 |
+
if not question:
|
| 29 |
+
return jsonify({"answer": "Please ask a valid question."})
|
| 30 |
+
|
| 31 |
+
try:
|
| 32 |
+
response = generator(question, max_length=200, do_sample=True, temperature=0.7)
|
| 33 |
+
answer = response[0]["generated_text"]
|
| 34 |
+
return jsonify({"answer": answer})
|
| 35 |
+
except Exception as e:
|
| 36 |
+
print(e)
|
| 37 |
+
return jsonify({"answer": "⚠️ There was an error generating the answer."})
|
| 38 |
+
|
| 39 |
+
if __name__ == "__main__":
|
| 40 |
+
app.run(host="0.0.0.0", port=7860, debug=True)
|
c46cfe98-a47f-4596-a93b-ea93bb8806f3.jpg
ADDED
|
index.html
ADDED
|
@@ -0,0 +1,277 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
| 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>AskOscar Herbal AI 🌿</title>
|
| 7 |
+
<style>
|
| 8 |
+
body {
|
| 9 |
+
font-family: "Garamond", serif;
|
| 10 |
+
background-image: url("c46cfe98-a47f-4596-a93b-ea93bb8806f3.jpg");
|
| 11 |
+
background-size: cover;
|
| 12 |
+
background-position: center;
|
| 13 |
+
color: #fff;
|
| 14 |
+
text-align: center;
|
| 15 |
+
height: 100vh;
|
| 16 |
+
margin: 0;
|
| 17 |
+
display: flex;
|
| 18 |
+
flex-direction: column;
|
| 19 |
+
justify-content: center;
|
| 20 |
+
backdrop-filter: brightness(0.4);
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
h1 {
|
| 24 |
+
font-size: 2.5em;
|
| 25 |
+
margin-bottom: 10px;
|
| 26 |
+
text-shadow: 2px 2px 5px #000;
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
p {
|
| 30 |
+
margin-top: 0;
|
| 31 |
+
font-size: 1.2em;
|
| 32 |
+
text-shadow: 1px 1px 3px #000;
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
.container {
|
| 36 |
+
background-color: rgba(0, 0, 0, 0.6);
|
| 37 |
+
border-radius: 20px;
|
| 38 |
+
padding: 30px;
|
| 39 |
+
max-width: 650px;
|
| 40 |
+
margin: 0 auto;
|
| 41 |
+
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.5);
|
| 42 |
+
display: flex;
|
| 43 |
+
flex-direction: column;
|
| 44 |
+
align-items: center;
|
| 45 |
+
height: 80vh;
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
#chat-box {
|
| 49 |
+
background-color: rgba(255, 255, 255, 0.1);
|
| 50 |
+
border-radius: 10px;
|
| 51 |
+
padding: 15px;
|
| 52 |
+
width: 100%;
|
| 53 |
+
height: 65%;
|
| 54 |
+
overflow-y: auto;
|
| 55 |
+
text-align: left;
|
| 56 |
+
margin-bottom: 15px;
|
| 57 |
+
box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.5);
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
.message {
|
| 61 |
+
margin-bottom: 15px;
|
| 62 |
+
line-height: 1.4em;
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
.user {
|
| 66 |
+
text-align: right;
|
| 67 |
+
color: #aef7c7;
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
.bot {
|
| 71 |
+
text-align: left;
|
| 72 |
+
color: #fffbe6;
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
.input-container {
|
| 76 |
+
width: 100%;
|
| 77 |
+
display: flex;
|
| 78 |
+
justify-content: center;
|
| 79 |
+
align-items: center;
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
input {
|
| 83 |
+
width: 70%;
|
| 84 |
+
padding: 12px;
|
| 85 |
+
border: none;
|
| 86 |
+
border-radius: 10px;
|
| 87 |
+
font-size: 1em;
|
| 88 |
+
margin-top: 10px;
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
button {
|
| 92 |
+
background-color: #00b894;
|
| 93 |
+
border: none;
|
| 94 |
+
border-radius: 10px;
|
| 95 |
+
padding: 12px 20px;
|
| 96 |
+
font-size: 1em;
|
| 97 |
+
color: white;
|
| 98 |
+
cursor: pointer;
|
| 99 |
+
margin-left: 10px;
|
| 100 |
+
transition: background 0.3s ease;
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
button:hover {
|
| 104 |
+
background-color: #019870;
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
.top-buttons {
|
| 108 |
+
position: absolute;
|
| 109 |
+
top: 15px;
|
| 110 |
+
right: 15px;
|
| 111 |
+
display: flex;
|
| 112 |
+
gap: 10px;
|
| 113 |
+
z-index: 1000;
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
.top-buttons button {
|
| 117 |
+
background-color: #019870;
|
| 118 |
+
border-radius: 8px;
|
| 119 |
+
padding: 8px 14px;
|
| 120 |
+
font-size: 0.9em;
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
footer {
|
| 124 |
+
position: absolute;
|
| 125 |
+
bottom: 10px;
|
| 126 |
+
width: 100%;
|
| 127 |
+
text-align: center;
|
| 128 |
+
font-size: 0.9em;
|
| 129 |
+
color: #ccc;
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
.modal {
|
| 133 |
+
display: none;
|
| 134 |
+
position: fixed;
|
| 135 |
+
z-index: 999;
|
| 136 |
+
left: 0;
|
| 137 |
+
top: 0;
|
| 138 |
+
width: 100%;
|
| 139 |
+
height: 100%;
|
| 140 |
+
overflow: auto;
|
| 141 |
+
background-color: rgba(0, 0, 0, 0.8);
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
.modal-content {
|
| 145 |
+
background-color: #222;
|
| 146 |
+
margin: 15% auto;
|
| 147 |
+
padding: 20px;
|
| 148 |
+
border-radius: 10px;
|
| 149 |
+
width: 80%;
|
| 150 |
+
max-width: 500px;
|
| 151 |
+
text-align: left;
|
| 152 |
+
color: #eee;
|
| 153 |
+
box-shadow: 0 0 15px rgba(0, 0, 0, 0.6);
|
| 154 |
+
}
|
| 155 |
+
|
| 156 |
+
.close {
|
| 157 |
+
color: #aaa;
|
| 158 |
+
float: right;
|
| 159 |
+
font-size: 22px;
|
| 160 |
+
cursor: pointer;
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
.close:hover {
|
| 164 |
+
color: white;
|
| 165 |
+
}
|
| 166 |
+
</style>
|
| 167 |
+
</head>
|
| 168 |
+
<body>
|
| 169 |
+
<div class="top-buttons">
|
| 170 |
+
<button id="aboutBtn">About</button>
|
| 171 |
+
<button id="donateBtn">Donate</button>
|
| 172 |
+
</div>
|
| 173 |
+
|
| 174 |
+
<div class="container">
|
| 175 |
+
<h1>🌿 AskOscar Herbal AI 🌿</h1>
|
| 176 |
+
<p>Ask me anything about medicinal plants, healing herbs, or natural remedies worldwide.</p>
|
| 177 |
+
|
| 178 |
+
<div id="chat-box"></div>
|
| 179 |
+
|
| 180 |
+
<div class="input-container">
|
| 181 |
+
<input id="userInput" placeholder="Ask about any medicinal plant..." />
|
| 182 |
+
<button id="askButton">Ask</button>
|
| 183 |
+
</div>
|
| 184 |
+
</div>
|
| 185 |
+
|
| 186 |
+
<footer>Powered by Hugging Face • Herbal Wisdom by Oscar</footer>
|
| 187 |
+
|
| 188 |
+
<!-- About Modal -->
|
| 189 |
+
<div id="aboutModal" class="modal">
|
| 190 |
+
<div class="modal-content">
|
| 191 |
+
<span class="close" id="closeAbout">×</span>
|
| 192 |
+
<h2>About</h2>
|
| 193 |
+
<p>
|
| 194 |
+
Oscar Teye, visionary founder of <strong>AskOscar.ai</strong>, connecting humanity with nature's healing power using intelligence and wisdom.
|
| 195 |
+
</p>
|
| 196 |
+
</div>
|
| 197 |
+
</div>
|
| 198 |
+
|
| 199 |
+
<!-- Donate Modal -->
|
| 200 |
+
<div id="donateModal" class="modal">
|
| 201 |
+
<div class="modal-content">
|
| 202 |
+
<span class="close" id="closeDonate">×</span>
|
| 203 |
+
<h2>Support AskOscar.ai</h2>
|
| 204 |
+
<p>
|
| 205 |
+
🌿 Every seed of support helps this herbal AI grow stronger!
|
| 206 |
+
You can contribute via Mobile Money: <strong>0530327720</strong> or share the website with friends 🌍
|
| 207 |
+
</p>
|
| 208 |
+
</div>
|
| 209 |
+
</div>
|
| 210 |
+
|
| 211 |
+
<script>
|
| 212 |
+
document.addEventListener("DOMContentLoaded", () => {
|
| 213 |
+
|
| 214 |
+
// --- Corrected askOscarAI function ---
|
| 215 |
+
async function askOscarAI(question) {
|
| 216 |
+
try {
|
| 217 |
+
const response = await fetch("/ask", { // ← call local server
|
| 218 |
+
method: "POST",
|
| 219 |
+
headers: { "Content-Type": "application/json" },
|
| 220 |
+
body: JSON.stringify({ question })
|
| 221 |
+
});
|
| 222 |
+
const data = await response.json();
|
| 223 |
+
return data.answer;
|
| 224 |
+
} catch (err) {
|
| 225 |
+
console.error(err);
|
| 226 |
+
return "⚠️ Unable to connect to the herbal network.";
|
| 227 |
+
}
|
| 228 |
+
}
|
| 229 |
+
|
| 230 |
+
const chatBox = document.getElementById("chat-box");
|
| 231 |
+
const input = document.getElementById("userInput");
|
| 232 |
+
const sendBtn = document.getElementById("askButton");
|
| 233 |
+
|
| 234 |
+
async function sendQuestion() {
|
| 235 |
+
const question = input.value.trim();
|
| 236 |
+
if (!question) return;
|
| 237 |
+
|
| 238 |
+
const userMsg = document.createElement("div");
|
| 239 |
+
userMsg.classList.add("message", "user");
|
| 240 |
+
userMsg.textContent = "🧍 " + question;
|
| 241 |
+
chatBox.appendChild(userMsg);
|
| 242 |
+
|
| 243 |
+
const botMsg = document.createElement("div");
|
| 244 |
+
botMsg.classList.add("message", "bot");
|
| 245 |
+
botMsg.textContent = "🌿 Thinking...";
|
| 246 |
+
chatBox.appendChild(botMsg);
|
| 247 |
+
chatBox.scrollTop = chatBox.scrollHeight;
|
| 248 |
+
|
| 249 |
+
input.value = "";
|
| 250 |
+
|
| 251 |
+
const answer = await askOscarAI(question);
|
| 252 |
+
botMsg.textContent = "🪴 " + answer;
|
| 253 |
+
chatBox.scrollTop = chatBox.scrollHeight;
|
| 254 |
+
}
|
| 255 |
+
|
| 256 |
+
sendBtn.addEventListener("click", sendQuestion);
|
| 257 |
+
input.addEventListener("keypress", (e) => { if (e.key === "Enter") sendQuestion(); });
|
| 258 |
+
|
| 259 |
+
const aboutModal = document.getElementById("aboutModal");
|
| 260 |
+
const donateModal = document.getElementById("donateModal");
|
| 261 |
+
const aboutBtn = document.getElementById("aboutBtn");
|
| 262 |
+
const donateBtn = document.getElementById("donateBtn");
|
| 263 |
+
const closeAbout = document.getElementById("closeAbout");
|
| 264 |
+
const closeDonate = document.getElementById("closeDonate");
|
| 265 |
+
|
| 266 |
+
aboutBtn.onclick = () => (aboutModal.style.display = "block");
|
| 267 |
+
donateBtn.onclick = () => (donateModal.style.display = "block");
|
| 268 |
+
closeAbout.onclick = () => (aboutModal.style.display = "none");
|
| 269 |
+
closeDonate.onclick = () => (donateModal.style.display = "none");
|
| 270 |
+
window.onclick = (event) => {
|
| 271 |
+
if (event.target === aboutModal) aboutModal.style.display = "none";
|
| 272 |
+
if (event.target === donateModal) donateModal.style.display = "none";
|
| 273 |
+
};
|
| 274 |
+
});
|
| 275 |
+
</script>
|
| 276 |
+
</body>
|
| 277 |
+
</html>
|
requirements.txt
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==5.9.1
|
| 2 |
+
transformers==4.57.1
|
| 3 |
+
torch==2.9.0
|
| 4 |
+
huggingface_hub==0.36.0
|
| 5 |
+
accelerate==1.11.0
|
| 6 |
+
requests==2.32.5
|
| 7 |
+
numpy==2.3.4
|
| 8 |
+
pandas==2.1.1
|
| 9 |
+
ffmpy==0.3.0
|
| 10 |
+
aiofiles==23.1.0
|
| 11 |
+
jinja2==3.1.6
|
| 12 |
+
pillow==10.1.1
|
| 13 |
+
pyyaml==6.0.3
|
| 14 |
+
typing-extensions==4.15.0
|
| 15 |
+
orjson==3.9.2
|
| 16 |
+
semantic-version==2.10.0
|