added google colab
Browse files- ProjectYFinal.ipynb +0 -0
- index.html +86 -0
- test_hf_api.py +36 -0
ProjectYFinal.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
index.html
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html>
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8" />
|
| 5 |
+
<title>ProjectY API Tester</title>
|
| 6 |
+
<style>
|
| 7 |
+
body { font-family: Arial, sans-serif; padding: 20px; max-width: 700px; }
|
| 8 |
+
input { width: 100%; padding: 10px; font-size: 14px; }
|
| 9 |
+
button { padding: 10px 14px; margin-top: 10px; margin-right: 8px; cursor: pointer; }
|
| 10 |
+
pre { background: #111; color: #0f0; padding: 12px; border-radius: 8px; overflow:auto; }
|
| 11 |
+
.row { margin: 12px 0; }
|
| 12 |
+
</style>
|
| 13 |
+
</head>
|
| 14 |
+
<body>
|
| 15 |
+
<h2>ProjectY Hugging Face API Tester</h2>
|
| 16 |
+
|
| 17 |
+
<div class="row">
|
| 18 |
+
<div><b>API Base URL</b></div>
|
| 19 |
+
<input id="baseUrl" value="https://Yashk0618-projecty-classifier-regressor.hf.space" />
|
| 20 |
+
<small>Leave this as-is unless your Space URL is different.</small>
|
| 21 |
+
</div>
|
| 22 |
+
|
| 23 |
+
<div class="row">
|
| 24 |
+
<div><b>Features (comma-separated numbers)</b></div>
|
| 25 |
+
<input id="features" value="1,2,3,4,5" />
|
| 26 |
+
<small>Important: the count must match what your model expects.</small>
|
| 27 |
+
</div>
|
| 28 |
+
|
| 29 |
+
<div class="row">
|
| 30 |
+
<button onclick="testHealth()">Test GET /</button>
|
| 31 |
+
<button onclick="predictWastage()">POST /predict/wastage</button>
|
| 32 |
+
<button onclick="predictStockout()">POST /predict/stockout</button>
|
| 33 |
+
</div>
|
| 34 |
+
|
| 35 |
+
<h3>Result</h3>
|
| 36 |
+
<pre id="out">Click a button to test…</pre>
|
| 37 |
+
|
| 38 |
+
<script>
|
| 39 |
+
function parseFeatures() {
|
| 40 |
+
const raw = document.getElementById("features").value.trim();
|
| 41 |
+
if (!raw) return [];
|
| 42 |
+
return raw.split(",").map(x => Number(x.trim()));
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
async function testHealth() {
|
| 46 |
+
const base = document.getElementById("baseUrl").value.trim();
|
| 47 |
+
const out = document.getElementById("out");
|
| 48 |
+
out.textContent = "Loading...";
|
| 49 |
+
try {
|
| 50 |
+
const res = await fetch(`${base}/`);
|
| 51 |
+
const text = await res.text();
|
| 52 |
+
out.textContent = `Status: ${res.status}\n\n${text}`;
|
| 53 |
+
} catch (e) {
|
| 54 |
+
out.textContent = "Error:\n" + e;
|
| 55 |
+
}
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
async function predictWastage() {
|
| 59 |
+
await postPredict("/predict/wastage");
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
async function predictStockout() {
|
| 63 |
+
await postPredict("/predict/stockout");
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
async function postPredict(path) {
|
| 67 |
+
const base = document.getElementById("baseUrl").value.trim();
|
| 68 |
+
const feats = parseFeatures();
|
| 69 |
+
const out = document.getElementById("out");
|
| 70 |
+
out.textContent = "Loading...";
|
| 71 |
+
try {
|
| 72 |
+
const res = await fetch(`${base}${path}`, {
|
| 73 |
+
method: "POST",
|
| 74 |
+
headers: { "Content-Type": "application/json" },
|
| 75 |
+
body: JSON.stringify({ features: feats })
|
| 76 |
+
});
|
| 77 |
+
|
| 78 |
+
const text = await res.text(); // handles JSON or error text
|
| 79 |
+
out.textContent = `POST ${path}\nStatus: ${res.status}\n\n${text}`;
|
| 80 |
+
} catch (e) {
|
| 81 |
+
out.textContent = "Error:\n" + e;
|
| 82 |
+
}
|
| 83 |
+
}
|
| 84 |
+
</script>
|
| 85 |
+
</body>
|
| 86 |
+
</html>
|
test_hf_api.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import random
|
| 3 |
+
|
| 4 |
+
BASE = "https://yashk0618-projecty-classifier-regressor.hf.space"
|
| 5 |
+
|
| 6 |
+
def predict_wastage(features):
|
| 7 |
+
url = f"{BASE}/predict/wastage"
|
| 8 |
+
payload = {"features": features}
|
| 9 |
+
|
| 10 |
+
r = requests.post(url, json=payload)
|
| 11 |
+
if not r.ok:
|
| 12 |
+
raise Exception(f"HF error {r.status_code}: {r.text}")
|
| 13 |
+
|
| 14 |
+
return r.json()
|
| 15 |
+
|
| 16 |
+
def predict_stockout(features):
|
| 17 |
+
url = f"{BASE}/predict/stockout"
|
| 18 |
+
payload = {"features": features}
|
| 19 |
+
|
| 20 |
+
r = requests.post(url, json=payload)
|
| 21 |
+
if not r.ok:
|
| 22 |
+
raise Exception(f"HF error {r.status_code}: {r.text}")
|
| 23 |
+
|
| 24 |
+
return r.json()
|
| 25 |
+
|
| 26 |
+
if __name__ == "__main__":
|
| 27 |
+
# 20 random feature values (match your JS test)
|
| 28 |
+
features = [round(random.random() * 10, 2) for _ in range(20)]
|
| 29 |
+
|
| 30 |
+
print("Features:", features)
|
| 31 |
+
|
| 32 |
+
wastage_result = predict_wastage(features)
|
| 33 |
+
stockout_result = predict_stockout(features)
|
| 34 |
+
|
| 35 |
+
print("Wastage result:", wastage_result)
|
| 36 |
+
print("Stockout result:", stockout_result)
|