Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -3,6 +3,7 @@ from fastapi.responses import JSONResponse
|
|
| 3 |
import tensorflow as tf
|
| 4 |
import numpy as np
|
| 5 |
import os
|
|
|
|
| 6 |
from tensorflow.keras.models import load_model
|
| 7 |
from tensorflow.keras.preprocessing import image
|
| 8 |
from tensorflow.keras.layers import Layer, Conv2D, Softmax, Concatenate
|
|
@@ -114,7 +115,7 @@ async def predict_plant_disease(plant_name: str, file: UploadFile = File(...)):
|
|
| 114 |
file (UploadFile): The image file uploaded by the user.
|
| 115 |
|
| 116 |
Returns:
|
| 117 |
-
JSON response with the predicted class.
|
| 118 |
"""
|
| 119 |
# Ensure the plant name is valid
|
| 120 |
if plant_name not in loaded_models:
|
|
@@ -137,9 +138,20 @@ async def predict_plant_disease(plant_name: str, file: UploadFile = File(...)):
|
|
| 137 |
|
| 138 |
# Make prediction
|
| 139 |
prediction = model.predict(img_array)
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
finally:
|
| 144 |
# Clean up temporary file
|
| 145 |
os.remove(temp_path)
|
|
|
|
| 3 |
import tensorflow as tf
|
| 4 |
import numpy as np
|
| 5 |
import os
|
| 6 |
+
import requests
|
| 7 |
from tensorflow.keras.models import load_model
|
| 8 |
from tensorflow.keras.preprocessing import image
|
| 9 |
from tensorflow.keras.layers import Layer, Conv2D, Softmax, Concatenate
|
|
|
|
| 115 |
file (UploadFile): The image file uploaded by the user.
|
| 116 |
|
| 117 |
Returns:
|
| 118 |
+
JSON response with the predicted class and additional details from an external API.
|
| 119 |
"""
|
| 120 |
# Ensure the plant name is valid
|
| 121 |
if plant_name not in loaded_models:
|
|
|
|
| 138 |
|
| 139 |
# Make prediction
|
| 140 |
prediction = model.predict(img_array)
|
| 141 |
+
class_label = plant_disease_dict[plant_name][np.argmax(prediction)]
|
| 142 |
+
|
| 143 |
+
# Fetch additional data from external API
|
| 144 |
+
try:
|
| 145 |
+
response = requests.get(f"https://navpan2-sarva-ai-back.hf.space/kotlinback/{class_label}")
|
| 146 |
+
external_data = response.json() if response.status_code == 200 else {"error": "Failed to fetch external data"}
|
| 147 |
+
except Exception as e:
|
| 148 |
+
external_data = {"error": str(e)}
|
| 149 |
+
|
| 150 |
+
return JSONResponse(content={
|
| 151 |
+
"plant": plant_name,
|
| 152 |
+
"predicted_disease": class_label,
|
| 153 |
+
"external_data": external_data
|
| 154 |
+
})
|
| 155 |
finally:
|
| 156 |
# Clean up temporary file
|
| 157 |
os.remove(temp_path)
|