Spaces:
Sleeping
Sleeping
Commit ·
73709e3
1
Parent(s): 0609674
fix huggingface config
Browse files- app_fastapi.py +41 -42
app_fastapi.py
CHANGED
|
@@ -1,17 +1,3 @@
|
|
| 1 |
-
"""
|
| 2 |
-
AgriCare – Disease Detection API
|
| 3 |
-
--------------------------------
|
| 4 |
-
|
| 5 |
-
Production-grade AI backend for cassava disease detection.
|
| 6 |
-
|
| 7 |
-
Features:
|
| 8 |
-
✓ ONNX EfficientNet-B3 inference
|
| 9 |
-
✓ Proper softmax probabilities
|
| 10 |
-
✓ English-first medical guidance
|
| 11 |
-
✓ Multilingual translation via N-ATLaS
|
| 12 |
-
✓ Human-in-the-loop escalation
|
| 13 |
-
"""
|
| 14 |
-
|
| 15 |
import os
|
| 16 |
import io
|
| 17 |
import logging
|
|
@@ -41,25 +27,26 @@ logging.basicConfig(level=logging.INFO)
|
|
| 41 |
logger = logging.getLogger("agricare_api")
|
| 42 |
|
| 43 |
# -------------------------------------------------------
|
| 44 |
-
#
|
| 45 |
# -------------------------------------------------------
|
| 46 |
@app.get("/", response_class=HTMLResponse)
|
| 47 |
async def root():
|
| 48 |
return """
|
| 49 |
-
<!DOCTYPE html>
|
| 50 |
<html>
|
| 51 |
<head>
|
| 52 |
-
<meta http-equiv="refresh" content="0; url=/docs"
|
| 53 |
<title>AgriCare API</title>
|
| 54 |
</head>
|
| 55 |
-
<body
|
| 56 |
-
<
|
| 57 |
-
<p>Redirecting to API documentation…</p>
|
| 58 |
-
<p><a href="/docs">Open Swagger Docs</a></p>
|
| 59 |
</body>
|
| 60 |
</html>
|
| 61 |
"""
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
# -------------------------------------------------------
|
| 64 |
# Model Setup
|
| 65 |
# -------------------------------------------------------
|
|
@@ -81,7 +68,7 @@ IMG_SIZE = 300
|
|
| 81 |
LOW_CONF_THRESHOLD = 0.60
|
| 82 |
|
| 83 |
# -------------------------------------------------------
|
| 84 |
-
#
|
| 85 |
# -------------------------------------------------------
|
| 86 |
DISEASE_RECOMMENDATIONS = {
|
| 87 |
"Cassava Bacterial Blight":
|
|
@@ -99,16 +86,17 @@ DISEASE_RECOMMENDATIONS = {
|
|
| 99 |
"Lambda-cyhalothrin or Cypermethrin. Maintain field hygiene.",
|
| 100 |
|
| 101 |
"Cassava Mosaic Disease":
|
| 102 |
-
"Cassava Mosaic Disease was detected.
|
| 103 |
"Control whiteflies using Imidacloprid or Acetamiprid. "
|
| 104 |
-
"Uproot infected plants immediately.",
|
| 105 |
|
| 106 |
"Healthy Leaf":
|
| 107 |
-
"The cassava leaf is healthy. No treatment is required.
|
|
|
|
| 108 |
}
|
| 109 |
|
| 110 |
# -------------------------------------------------------
|
| 111 |
-
# Hugging Face N-ATLaS
|
| 112 |
# -------------------------------------------------------
|
| 113 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 114 |
NATLAS_URL = "https://router.huggingface.co/hf-inference/models/NCAIR1/N-ATLaS"
|
|
@@ -118,6 +106,12 @@ HEADERS = {
|
|
| 118 |
"Content-Type": "application/json"
|
| 119 |
}
|
| 120 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
# -------------------------------------------------------
|
| 122 |
# Utilities
|
| 123 |
# -------------------------------------------------------
|
|
@@ -133,29 +127,33 @@ def preprocess(image_bytes):
|
|
| 133 |
arr = np.transpose(arr, (2, 0, 1))
|
| 134 |
return arr[np.newaxis, :].astype(MODEL_DTYPE)
|
| 135 |
|
| 136 |
-
def translate_text(text: str, language: str):
|
| 137 |
if language.lower() == "english":
|
| 138 |
return text
|
| 139 |
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
-
|
| 143 |
-
r = requests.post(
|
| 144 |
-
NATLAS_URL,
|
| 145 |
-
headers=HEADERS,
|
| 146 |
-
json={"inputs": prompt},
|
| 147 |
-
timeout=20
|
| 148 |
-
)
|
| 149 |
-
if r.status_code == 200:
|
| 150 |
-
data = r.json()
|
| 151 |
-
return data[0].get("generated_text", text)
|
| 152 |
-
except Exception as e:
|
| 153 |
-
logger.error(e)
|
| 154 |
|
| 155 |
-
return
|
| 156 |
|
| 157 |
# -------------------------------------------------------
|
| 158 |
-
#
|
| 159 |
# -------------------------------------------------------
|
| 160 |
@app.post("/predict")
|
| 161 |
async def predict(
|
|
@@ -181,6 +179,7 @@ async def predict(
|
|
| 181 |
"confidence": round(confidence, 4),
|
| 182 |
"language": language,
|
| 183 |
"recommendation_text": final_text,
|
|
|
|
| 184 |
"probabilities": probs.tolist()
|
| 185 |
}
|
| 186 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import io
|
| 3 |
import logging
|
|
|
|
| 27 |
logger = logging.getLogger("agricare_api")
|
| 28 |
|
| 29 |
# -------------------------------------------------------
|
| 30 |
+
# Root → Docs
|
| 31 |
# -------------------------------------------------------
|
| 32 |
@app.get("/", response_class=HTMLResponse)
|
| 33 |
async def root():
|
| 34 |
return """
|
|
|
|
| 35 |
<html>
|
| 36 |
<head>
|
| 37 |
+
<meta http-equiv="refresh" content="0; url=/docs">
|
| 38 |
<title>AgriCare API</title>
|
| 39 |
</head>
|
| 40 |
+
<body>
|
| 41 |
+
<p>Redirecting to <a href="/docs">/docs</a>...</p>
|
|
|
|
|
|
|
| 42 |
</body>
|
| 43 |
</html>
|
| 44 |
"""
|
| 45 |
|
| 46 |
+
@app.get("/health")
|
| 47 |
+
def health():
|
| 48 |
+
return {"status": "ok"}
|
| 49 |
+
|
| 50 |
# -------------------------------------------------------
|
| 51 |
# Model Setup
|
| 52 |
# -------------------------------------------------------
|
|
|
|
| 68 |
LOW_CONF_THRESHOLD = 0.60
|
| 69 |
|
| 70 |
# -------------------------------------------------------
|
| 71 |
+
# English Source of Truth
|
| 72 |
# -------------------------------------------------------
|
| 73 |
DISEASE_RECOMMENDATIONS = {
|
| 74 |
"Cassava Bacterial Blight":
|
|
|
|
| 86 |
"Lambda-cyhalothrin or Cypermethrin. Maintain field hygiene.",
|
| 87 |
|
| 88 |
"Cassava Mosaic Disease":
|
| 89 |
+
"Cassava Mosaic Disease was detected. There is no direct chemical cure. "
|
| 90 |
"Control whiteflies using Imidacloprid or Acetamiprid. "
|
| 91 |
+
"Uproot and destroy infected plants immediately.",
|
| 92 |
|
| 93 |
"Healthy Leaf":
|
| 94 |
+
"The cassava leaf is healthy. No treatment is required. "
|
| 95 |
+
"Continue regular monitoring and good farm hygiene."
|
| 96 |
}
|
| 97 |
|
| 98 |
# -------------------------------------------------------
|
| 99 |
+
# Hugging Face – N-ATLaS
|
| 100 |
# -------------------------------------------------------
|
| 101 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 102 |
NATLAS_URL = "https://router.huggingface.co/hf-inference/models/NCAIR1/N-ATLaS"
|
|
|
|
| 106 |
"Content-Type": "application/json"
|
| 107 |
}
|
| 108 |
|
| 109 |
+
LANGUAGE_MAP = {
|
| 110 |
+
"yoruba": "Yoruba language",
|
| 111 |
+
"hausa": "Hausa language",
|
| 112 |
+
"igbo": "Igbo language"
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
# -------------------------------------------------------
|
| 116 |
# Utilities
|
| 117 |
# -------------------------------------------------------
|
|
|
|
| 127 |
arr = np.transpose(arr, (2, 0, 1))
|
| 128 |
return arr[np.newaxis, :].astype(MODEL_DTYPE)
|
| 129 |
|
| 130 |
+
def translate_text(text: str, language: str) -> str:
|
| 131 |
if language.lower() == "english":
|
| 132 |
return text
|
| 133 |
|
| 134 |
+
target_lang = LANGUAGE_MAP.get(language.lower(), language)
|
| 135 |
+
|
| 136 |
+
prompt = (
|
| 137 |
+
f"Translate the following agricultural advice into {target_lang}. "
|
| 138 |
+
f"Do NOT answer in English.\n\n{text}"
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
r = requests.post(
|
| 142 |
+
NATLAS_URL,
|
| 143 |
+
headers=HEADERS,
|
| 144 |
+
json={"inputs": prompt},
|
| 145 |
+
timeout=30
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
r.raise_for_status()
|
| 149 |
+
data = r.json()
|
| 150 |
|
| 151 |
+
translated = data[0].get("generated_text", text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
+
return translated.strip()
|
| 154 |
|
| 155 |
# -------------------------------------------------------
|
| 156 |
+
# Prediction Endpoint
|
| 157 |
# -------------------------------------------------------
|
| 158 |
@app.post("/predict")
|
| 159 |
async def predict(
|
|
|
|
| 179 |
"confidence": round(confidence, 4),
|
| 180 |
"language": language,
|
| 181 |
"recommendation_text": final_text,
|
| 182 |
+
"route_to_expert": confidence < LOW_CONF_THRESHOLD,
|
| 183 |
"probabilities": probs.tolist()
|
| 184 |
}
|
| 185 |
|