Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import io
|
| 2 |
import sys
|
| 3 |
import time
|
|
@@ -12,6 +13,7 @@ from models.rice_leaf_validator import RiceLeafValidator, is_rice_leaf
|
|
| 12 |
from utils.image_processing import process_image
|
| 13 |
import requests
|
| 14 |
import json
|
|
|
|
| 15 |
from typing import Dict, Any
|
| 16 |
|
| 17 |
# Configure logging
|
|
@@ -247,8 +249,8 @@ async def validate_rice_leaf_image(file: UploadFile = File(...)):
|
|
| 247 |
raise HTTPException(status_code=500, detail=f"Failed to load models: {last_error}")
|
| 248 |
|
| 249 |
try:
|
| 250 |
-
# Save uploaded file temporarily
|
| 251 |
-
temp_file_path = f"temp_{file.filename}"
|
| 252 |
with open(temp_file_path, "wb") as buffer:
|
| 253 |
buffer.write(await file.read())
|
| 254 |
|
|
@@ -256,14 +258,12 @@ async def validate_rice_leaf_image(file: UploadFile = File(...)):
|
|
| 256 |
is_rice, confidence = is_rice_leaf(temp_file_path, leaf_validator_model, device)
|
| 257 |
|
| 258 |
# Remove temporary file
|
| 259 |
-
import os
|
| 260 |
os.remove(temp_file_path)
|
| 261 |
|
| 262 |
return {
|
| 263 |
"is_rice_leaf": is_rice,
|
| 264 |
"confidence": f"{confidence * 100:.2f}%"
|
| 265 |
}
|
| 266 |
-
|
| 267 |
except Exception as e:
|
| 268 |
logging.error(f"Error during leaf validation: {e}")
|
| 269 |
raise HTTPException(status_code=500, detail=f"Validation failed: {str(e)}")
|
|
@@ -285,8 +285,9 @@ async def predict_disease(file: UploadFile = File(...), use_ai_recommendation: b
|
|
| 285 |
# Read image file
|
| 286 |
start_time = time.time()
|
| 287 |
|
| 288 |
-
# Save uploaded file temporarily for validation
|
| 289 |
-
|
|
|
|
| 290 |
contents = await file.read()
|
| 291 |
with open(temp_file_path, "wb") as buffer:
|
| 292 |
buffer.write(contents)
|
|
|
|
| 1 |
+
|
| 2 |
import io
|
| 3 |
import sys
|
| 4 |
import time
|
|
|
|
| 13 |
from utils.image_processing import process_image
|
| 14 |
import requests
|
| 15 |
import json
|
| 16 |
+
import os
|
| 17 |
from typing import Dict, Any
|
| 18 |
|
| 19 |
# Configure logging
|
|
|
|
| 249 |
raise HTTPException(status_code=500, detail=f"Failed to load models: {last_error}")
|
| 250 |
|
| 251 |
try:
|
| 252 |
+
# Save uploaded file temporarily in /tmp (Hugging Face Spaces requirement)
|
| 253 |
+
temp_file_path = os.path.join("/tmp", f"temp_{file.filename}")
|
| 254 |
with open(temp_file_path, "wb") as buffer:
|
| 255 |
buffer.write(await file.read())
|
| 256 |
|
|
|
|
| 258 |
is_rice, confidence = is_rice_leaf(temp_file_path, leaf_validator_model, device)
|
| 259 |
|
| 260 |
# Remove temporary file
|
|
|
|
| 261 |
os.remove(temp_file_path)
|
| 262 |
|
| 263 |
return {
|
| 264 |
"is_rice_leaf": is_rice,
|
| 265 |
"confidence": f"{confidence * 100:.2f}%"
|
| 266 |
}
|
|
|
|
| 267 |
except Exception as e:
|
| 268 |
logging.error(f"Error during leaf validation: {e}")
|
| 269 |
raise HTTPException(status_code=500, detail=f"Validation failed: {str(e)}")
|
|
|
|
| 285 |
# Read image file
|
| 286 |
start_time = time.time()
|
| 287 |
|
| 288 |
+
# Save uploaded file temporarily for validation in /tmp (Hugging Face Spaces requirement)
|
| 289 |
+
import os
|
| 290 |
+
temp_file_path = os.path.join("/tmp", f"temp_{file.filename}")
|
| 291 |
contents = await file.read()
|
| 292 |
with open(temp_file_path, "wb") as buffer:
|
| 293 |
buffer.write(contents)
|