Spaces:
Runtime error
Runtime error
abeerrai01 commited on
Commit ·
df15b48
0
Parent(s):
my commit
Browse files- .gitattributes +1 -0
- .gitignore +1 -0
- .idea/.gitignore +5 -0
- .idea/Pest_disease.iml +10 -0
- .idea/inspectionProfiles/Project_Default.xml +12 -0
- .idea/inspectionProfiles/profiles_settings.xml +6 -0
- .idea/misc.xml +7 -0
- .idea/modules.xml +8 -0
- .idea/vcs.xml +6 -0
- Dockerfile +18 -0
- __pycache__/main.cpython-311.pyc +0 -0
- main.py +119 -0
- requirements.txt +5 -0
.gitattributes
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
.venv
|
.idea/.gitignore
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Default ignored files
|
| 2 |
+
/shelf/
|
| 3 |
+
/workspace.xml
|
| 4 |
+
# Editor-based HTTP Client requests
|
| 5 |
+
/httpRequests/
|
.idea/Pest_disease.iml
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<module type="PYTHON_MODULE" version="4">
|
| 3 |
+
<component name="NewModuleRootManager">
|
| 4 |
+
<content url="file://$MODULE_DIR$">
|
| 5 |
+
<excludeFolder url="file://$MODULE_DIR$/.venv" />
|
| 6 |
+
</content>
|
| 7 |
+
<orderEntry type="jdk" jdkName="Python 3.11 (Pest_disease)" jdkType="Python SDK" />
|
| 8 |
+
<orderEntry type="sourceFolder" forTests="false" />
|
| 9 |
+
</component>
|
| 10 |
+
</module>
|
.idea/inspectionProfiles/Project_Default.xml
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<component name="InspectionProjectProfileManager">
|
| 2 |
+
<profile version="1.0">
|
| 3 |
+
<option name="myName" value="Project Default" />
|
| 4 |
+
<inspection_tool class="PyPackageRequirementsInspection" enabled="true" level="WARNING" enabled_by_default="true">
|
| 5 |
+
<option name="ignoredPackages">
|
| 6 |
+
<list>
|
| 7 |
+
<option value="huggingface_hub" />
|
| 8 |
+
</list>
|
| 9 |
+
</option>
|
| 10 |
+
</inspection_tool>
|
| 11 |
+
</profile>
|
| 12 |
+
</component>
|
.idea/inspectionProfiles/profiles_settings.xml
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<component name="InspectionProjectProfileManager">
|
| 2 |
+
<settings>
|
| 3 |
+
<option name="USE_PROJECT_PROFILE" value="false" />
|
| 4 |
+
<version value="1.0" />
|
| 5 |
+
</settings>
|
| 6 |
+
</component>
|
.idea/misc.xml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<project version="4">
|
| 3 |
+
<component name="Black">
|
| 4 |
+
<option name="sdkName" value="Python 3.11 (Pest_disease)" />
|
| 5 |
+
</component>
|
| 6 |
+
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11 (Pest_disease)" project-jdk-type="Python SDK" />
|
| 7 |
+
</project>
|
.idea/modules.xml
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<project version="4">
|
| 3 |
+
<component name="ProjectModuleManager">
|
| 4 |
+
<modules>
|
| 5 |
+
<module fileurl="file://$PROJECT_DIR$/.idea/Pest_disease.iml" filepath="$PROJECT_DIR$/.idea/Pest_disease.iml" />
|
| 6 |
+
</modules>
|
| 7 |
+
</component>
|
| 8 |
+
</project>
|
.idea/vcs.xml
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<project version="4">
|
| 3 |
+
<component name="VcsDirectoryMappings">
|
| 4 |
+
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
| 5 |
+
</component>
|
| 6 |
+
</project>
|
Dockerfile
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ✅ Base image with Python
|
| 2 |
+
FROM python:3.10-slim
|
| 3 |
+
|
| 4 |
+
# Set working directory
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# Copy files
|
| 8 |
+
COPY requirements.txt .
|
| 9 |
+
COPY main.py .
|
| 10 |
+
|
| 11 |
+
# Install dependencies
|
| 12 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 13 |
+
|
| 14 |
+
# Expose the port
|
| 15 |
+
EXPOSE 7860
|
| 16 |
+
|
| 17 |
+
# Run FastAPI server
|
| 18 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
__pycache__/main.cpython-311.pyc
ADDED
|
Binary file (6.45 kB). View file
|
|
|
main.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import torch
|
| 6 |
+
import io
|
| 7 |
+
|
| 8 |
+
# Initialize FastAPI app
|
| 9 |
+
app = FastAPI(title="🌱 Plant Disease Detection API (MobileNetV2)")
|
| 10 |
+
|
| 11 |
+
# Load model + processor from Hugging Face
|
| 12 |
+
MODEL_NAME = "linkanjarad/mobilenet_v2_1.0_224-plant-disease-identification"
|
| 13 |
+
processor = AutoImageProcessor.from_pretrained(MODEL_NAME)
|
| 14 |
+
model = AutoModelForImageClassification.from_pretrained(MODEL_NAME)
|
| 15 |
+
|
| 16 |
+
# Device setup
|
| 17 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 18 |
+
model.to(device)
|
| 19 |
+
model.eval()
|
| 20 |
+
|
| 21 |
+
@app.get("/")
|
| 22 |
+
def home():
|
| 23 |
+
return {"message": "✅ Plant Disease Detection API is running!"}
|
| 24 |
+
|
| 25 |
+
@app.post("/predict/")
|
| 26 |
+
async def predict(file: UploadFile = File(...)):
|
| 27 |
+
try:
|
| 28 |
+
# Read image
|
| 29 |
+
image = Image.open(io.BytesIO(await file.read())).convert("RGB")
|
| 30 |
+
|
| 31 |
+
# Preprocess
|
| 32 |
+
inputs = processor(images=image, return_tensors="pt").to(device)
|
| 33 |
+
|
| 34 |
+
# Run inference
|
| 35 |
+
with torch.no_grad():
|
| 36 |
+
outputs = model(**inputs)
|
| 37 |
+
logits = outputs.logits
|
| 38 |
+
predicted_class_idx = logits.argmax(-1).item()
|
| 39 |
+
|
| 40 |
+
# Decode prediction
|
| 41 |
+
label = model.config.id2label[predicted_class_idx]
|
| 42 |
+
confidence = torch.nn.functional.softmax(logits, dim=-1)[0][predicted_class_idx].item()
|
| 43 |
+
|
| 44 |
+
# Response
|
| 45 |
+
result = {
|
| 46 |
+
"filename": file.filename,
|
| 47 |
+
"prediction": label,
|
| 48 |
+
"confidence": round(confidence, 3),
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
return JSONResponse(content=result)
|
| 52 |
+
|
| 53 |
+
except Exception as e:
|
| 54 |
+
return JSONResponse(content={"error": str(e)})
|
| 55 |
+
from fastapi import FastAPI, File, UploadFile
|
| 56 |
+
from fastapi.responses import JSONResponse
|
| 57 |
+
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
| 58 |
+
from PIL import Image
|
| 59 |
+
import torch
|
| 60 |
+
import io
|
| 61 |
+
import uvicorn
|
| 62 |
+
|
| 63 |
+
# 🌿 Initialize FastAPI app
|
| 64 |
+
app = FastAPI(title="🌱 Plant Disease Detection API (MobileNetV2)")
|
| 65 |
+
|
| 66 |
+
# 🧠 Load model + processor from your Hugging Face repo
|
| 67 |
+
MODEL_NAME = "theabeerrai/mobilenet_plant_disease"
|
| 68 |
+
processor = AutoImageProcessor.from_pretrained(MODEL_NAME)
|
| 69 |
+
model = AutoModelForImageClassification.from_pretrained(MODEL_NAME)
|
| 70 |
+
|
| 71 |
+
# ⚙️ Set device (GPU if available)
|
| 72 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 73 |
+
model.to(device)
|
| 74 |
+
model.eval()
|
| 75 |
+
|
| 76 |
+
@app.get("/")
|
| 77 |
+
def root():
|
| 78 |
+
return {
|
| 79 |
+
"status": "✅ API is live!",
|
| 80 |
+
"model": MODEL_NAME,
|
| 81 |
+
"device": str(device),
|
| 82 |
+
"usage": "POST an image to /predict/ to identify plant disease",
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
@app.post("/predict/")
|
| 86 |
+
async def predict(file: UploadFile = File(...)):
|
| 87 |
+
try:
|
| 88 |
+
# Read and convert uploaded image
|
| 89 |
+
image_bytes = await file.read()
|
| 90 |
+
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
| 91 |
+
|
| 92 |
+
# Preprocess image
|
| 93 |
+
inputs = processor(images=image, return_tensors="pt").to(device)
|
| 94 |
+
|
| 95 |
+
# Run inference
|
| 96 |
+
with torch.no_grad():
|
| 97 |
+
outputs = model(**inputs)
|
| 98 |
+
logits = outputs.logits
|
| 99 |
+
predicted_class_idx = logits.argmax(-1).item()
|
| 100 |
+
|
| 101 |
+
# Get prediction label and confidence
|
| 102 |
+
label = model.config.id2label[predicted_class_idx]
|
| 103 |
+
confidence = torch.nn.functional.softmax(logits, dim=-1)[0][predicted_class_idx].item()
|
| 104 |
+
|
| 105 |
+
# Return result
|
| 106 |
+
return JSONResponse(
|
| 107 |
+
content={
|
| 108 |
+
"filename": file.filename,
|
| 109 |
+
"prediction": label,
|
| 110 |
+
"confidence": round(confidence, 3),
|
| 111 |
+
}
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
except Exception as e:
|
| 115 |
+
return JSONResponse(status_code=500, content={"error": str(e)})
|
| 116 |
+
|
| 117 |
+
# ✅ Local run (for testing Docker or dev mode)
|
| 118 |
+
if __name__ == "__main__":
|
| 119 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
torch
|
| 4 |
+
transformers
|
| 5 |
+
pillow
|