Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,150 +1,78 @@
|
|
| 1 |
-
import os
|
| 2 |
import base64
|
| 3 |
import torch
|
| 4 |
-
import numpy as np
|
| 5 |
-
import faiss
|
| 6 |
-
import json
|
| 7 |
|
| 8 |
from fastapi import FastAPI
|
| 9 |
from pydantic import BaseModel
|
| 10 |
-
from contextlib import asynccontextmanager
|
| 11 |
-
from huggingface_hub import snapshot_download
|
| 12 |
-
from sentence_transformers import SentenceTransformer
|
| 13 |
from PIL import Image
|
| 14 |
from io import BytesIO
|
| 15 |
|
| 16 |
-
from transformers import
|
| 17 |
|
| 18 |
# βββββββββββββββββββββββββββββ
|
| 19 |
-
#
|
| 20 |
# βββββββββββββββββββββββββββββ
|
| 21 |
-
MODEL_REPO = "Rady10/Plant-Disease-Qwen3VL-2B"
|
| 22 |
-
RAG_REPO = "Rady10/Agriculture-Rag-Data-Index"
|
| 23 |
|
| 24 |
-
|
| 25 |
|
| 26 |
-
os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
| 27 |
-
|
| 28 |
-
# βββββββββββββββββββββββββββββ
|
| 29 |
-
# GLOBALS
|
| 30 |
# βββββββββββββββββββββββββββββ
|
| 31 |
-
|
| 32 |
-
processor = None
|
| 33 |
-
|
| 34 |
-
faiss_index = None
|
| 35 |
-
rag_chunks = None
|
| 36 |
-
embedder = None
|
| 37 |
-
|
| 38 |
-
# βββββββββββββββββββββββββββββ
|
| 39 |
-
# FASTAPI APP
|
| 40 |
-
# βββββββββββββββββββββββββββββ
|
| 41 |
-
app = FastAPI(title="πΏ Plant Disease Vision API")
|
| 42 |
-
|
| 43 |
# βββββββββββββββββββββββββββββ
|
| 44 |
-
|
| 45 |
-
# βββββββββββββββββββββββββββββ
|
| 46 |
-
@asynccontextmanager
|
| 47 |
-
async def lifespan(app: FastAPI):
|
| 48 |
-
|
| 49 |
-
global model, processor, faiss_index, rag_chunks, embedder
|
| 50 |
-
|
| 51 |
-
print("Loading vision model...")
|
| 52 |
-
|
| 53 |
-
processor = AutoProcessor.from_pretrained(
|
| 54 |
-
MODEL_REPO,
|
| 55 |
-
trust_remote_code=True
|
| 56 |
-
)
|
| 57 |
-
|
| 58 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 59 |
-
MODEL_REPO,
|
| 60 |
-
torch_dtype=torch.float32,
|
| 61 |
-
device_map="cpu",
|
| 62 |
-
trust_remote_code=True
|
| 63 |
-
)
|
| 64 |
-
|
| 65 |
-
model.eval()
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
|
| 70 |
-
|
| 71 |
-
repo_id=RAG_REPO,
|
| 72 |
-
repo_type="dataset",
|
| 73 |
-
local_dir="./rag"
|
| 74 |
-
)
|
| 75 |
|
| 76 |
-
|
| 77 |
-
os.path.join(rag_dir, "agro.index")
|
| 78 |
-
)
|
| 79 |
-
|
| 80 |
-
with open(os.path.join(rag_dir, "chunks.json"), "r", encoding="utf-8") as f:
|
| 81 |
-
rag_chunks = json.load(f)
|
| 82 |
-
|
| 83 |
-
embedder = SentenceTransformer(
|
| 84 |
-
"sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2"
|
| 85 |
-
)
|
| 86 |
-
|
| 87 |
-
print("ALL LOADED")
|
| 88 |
-
|
| 89 |
-
yield
|
| 90 |
-
|
| 91 |
-
app = FastAPI(lifespan=lifespan)
|
| 92 |
|
| 93 |
# βββββββββββββββββββββββββββββ
|
| 94 |
-
# REQUEST
|
| 95 |
# βββββββββββββββββββββββββββββ
|
| 96 |
-
class
|
| 97 |
-
image: str
|
| 98 |
-
text: str = ""
|
| 99 |
|
| 100 |
# βββββββββββββββββββββββββββββ
|
| 101 |
-
#
|
| 102 |
# βββββββββββββββββββββββββββββ
|
| 103 |
-
def decode_image(
|
| 104 |
-
|
| 105 |
-
|
|
|
|
| 106 |
|
| 107 |
# βββββββββββββββββββββββββββββ
|
| 108 |
-
#
|
| 109 |
# βββββββββββββββββββββββββββββ
|
| 110 |
-
def
|
| 111 |
|
| 112 |
-
|
| 113 |
-
text = "What disease is shown in this plant image?"
|
| 114 |
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
images=image,
|
| 118 |
-
return_tensors="pt"
|
| 119 |
-
)
|
| 120 |
|
| 121 |
-
|
| 122 |
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
top_p=0.9
|
| 129 |
-
)
|
| 130 |
-
|
| 131 |
-
return processor.batch_decode(
|
| 132 |
-
output,
|
| 133 |
-
skip_special_tokens=True
|
| 134 |
-
)[0]
|
| 135 |
|
| 136 |
# βββββββββββββββββββββββββββββ
|
| 137 |
-
# API
|
| 138 |
# ββββββββοΏ½οΏ½οΏ½ββββββββββββββββββββ
|
| 139 |
-
@app.
|
| 140 |
-
def
|
| 141 |
-
return {"status": "vision api running"}
|
| 142 |
-
|
| 143 |
-
@app.post("/analyze")
|
| 144 |
-
def analyze(req: VisionRequest):
|
| 145 |
|
| 146 |
image = decode_image(req.image)
|
| 147 |
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import base64
|
| 2 |
import torch
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
from fastapi import FastAPI
|
| 5 |
from pydantic import BaseModel
|
|
|
|
|
|
|
|
|
|
| 6 |
from PIL import Image
|
| 7 |
from io import BytesIO
|
| 8 |
|
| 9 |
+
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
| 10 |
|
| 11 |
# βββββββββββββββββββββββββββββ
|
| 12 |
+
# MODEL (REPLACE WITH YOUR CLASSIFIER)
|
| 13 |
# βββββββββββββββββββββββββββββ
|
| 14 |
+
MODEL_REPO = "Rady10/Plant-Disease-Qwen3VL-2B" # or your plant model
|
|
|
|
| 15 |
|
| 16 |
+
app = FastAPI(title="πΏ Plant Disease Classifier")
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
# βββββββββββββββββββββββββββββ
|
| 19 |
+
# LOAD MODEL
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
# βββββββββββββββββββββββββββββ
|
| 21 |
+
print("Loading classifier...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
processor = AutoImageProcessor.from_pretrained(MODEL_REPO)
|
| 24 |
+
model = AutoModelForImageClassification.from_pretrained(MODEL_REPO)
|
| 25 |
|
| 26 |
+
model.eval()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
print("Model loaded")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
# βββββββββββββββββββββββββββββ
|
| 31 |
+
# REQUEST FORMAT
|
| 32 |
# βββββββββββββββββββββββββββββ
|
| 33 |
+
class Request(BaseModel):
|
| 34 |
+
image: str # base64 string
|
|
|
|
| 35 |
|
| 36 |
# βββββββββββββββββββββββββββββ
|
| 37 |
+
# DECODE IMAGE
|
| 38 |
# βββββββββββββββββββββββββββββ
|
| 39 |
+
def decode_image(b64):
|
| 40 |
+
return Image.open(
|
| 41 |
+
BytesIO(base64.b64decode(b64))
|
| 42 |
+
).convert("RGB")
|
| 43 |
|
| 44 |
# βββββββββββββββββββββββββββββ
|
| 45 |
+
# PREDICTION FUNCTION
|
| 46 |
# βββββββββββββββββββββββββββββ
|
| 47 |
+
def predict(image):
|
| 48 |
|
| 49 |
+
inputs = processor(images=image, return_tensors="pt")
|
|
|
|
| 50 |
|
| 51 |
+
with torch.no_grad():
|
| 52 |
+
outputs = model(**inputs)
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
logits = outputs.logits
|
| 55 |
|
| 56 |
+
pred_id = logits.argmax(-1).item()
|
| 57 |
+
|
| 58 |
+
label = model.config.id2label[pred_id]
|
| 59 |
+
|
| 60 |
+
return label
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
# βββββββββββββββββββββββββββββ
|
| 63 |
+
# API ENDPOINT
|
| 64 |
# ββββββββοΏ½οΏ½οΏ½ββββββββββββββββββββ
|
| 65 |
+
@app.post("/predict")
|
| 66 |
+
def predict_api(req: Request):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
image = decode_image(req.image)
|
| 69 |
|
| 70 |
+
label = predict(image)
|
| 71 |
+
|
| 72 |
+
return {
|
| 73 |
+
"prediction": label
|
| 74 |
+
}
|
| 75 |
|
| 76 |
+
@app.get("/")
|
| 77 |
+
def home():
|
| 78 |
+
return {"status": "classifier running"}
|