Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -5,9 +5,10 @@ from fastapi import FastAPI, File, UploadFile
|
|
5 |
from fastapi.responses import JSONResponse
|
6 |
from io import BytesIO
|
7 |
from PIL import Image
|
8 |
-
from tensorflow.keras.preprocessing.image import img_to_array
|
9 |
import tensorflow_addons as tfa
|
10 |
import uvicorn
|
|
|
11 |
|
12 |
# Initialize FastAPI app
|
13 |
app = FastAPI()
|
@@ -84,16 +85,19 @@ def predict_image(image_data):
|
|
84 |
class_idx = int(np.argmax(predictions, axis=1)[0]) # Convert to int for JSON serialization
|
85 |
confidence = float(predictions[0][class_idx]) # Convert to float for JSON serialization
|
86 |
class_label = class_labels.get(class_idx, "Unknown")
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
|
|
|
|
93 |
return {
|
94 |
"class_index": class_idx,
|
95 |
"class_label": class_label,
|
96 |
"confidence": confidence,
|
|
|
97 |
}
|
98 |
|
99 |
# Route for health check
|
@@ -113,4 +117,4 @@ async def api_predict_image(file: UploadFile = File(...)):
|
|
113 |
|
114 |
# Run the FastAPI app
|
115 |
if __name__ == "__main__":
|
116 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
5 |
from fastapi.responses import JSONResponse
|
6 |
from io import BytesIO
|
7 |
from PIL import Image
|
8 |
+
from tensorflow.keras.preprocessing.image import img_to_array
|
9 |
import tensorflow_addons as tfa
|
10 |
import uvicorn
|
11 |
+
import requests
|
12 |
|
13 |
# Initialize FastAPI app
|
14 |
app = FastAPI()
|
|
|
85 |
class_idx = int(np.argmax(predictions, axis=1)[0]) # Convert to int for JSON serialization
|
86 |
confidence = float(predictions[0][class_idx]) # Convert to float for JSON serialization
|
87 |
class_label = class_labels.get(class_idx, "Unknown")
|
88 |
+
|
89 |
+
# Fetch additional data from external API
|
90 |
+
try:
|
91 |
+
response = requests.get(f"https://navpan2-sarva-ai-back.hf.space/kotlinback/{class_label}")
|
92 |
+
external_data = response.json() if response.status_code == 200 else {"error": "Failed to fetch external data"}
|
93 |
+
except Exception as e:
|
94 |
+
external_data = {"error": str(e)}
|
95 |
+
|
96 |
return {
|
97 |
"class_index": class_idx,
|
98 |
"class_label": class_label,
|
99 |
"confidence": confidence,
|
100 |
+
"external_data": external_data
|
101 |
}
|
102 |
|
103 |
# Route for health check
|
|
|
117 |
|
118 |
# Run the FastAPI app
|
119 |
if __name__ == "__main__":
|
120 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|