Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,8 +16,8 @@ import io
|
|
| 16 |
|
| 17 |
# ==== CONFIG ====
|
| 18 |
REPO_ID = "MAS-AI-0000/GameNet-1"
|
| 19 |
-
|
| 20 |
-
MODEL_FILENAME = "GameNetModel.keras"
|
| 21 |
LABELS_FILENAME = "label_to_index.json"
|
| 22 |
GENRE_FILENAME = "game_genre_map.json"
|
| 23 |
IMG_SIZE = (300, 300)
|
|
@@ -58,21 +58,28 @@ class Prediction(BaseModel):
|
|
| 58 |
@app.post("/predict", response_model=Prediction)
|
| 59 |
async def predict(file: UploadFile = File(...)):
|
| 60 |
try:
|
|
|
|
| 61 |
image_bytes = await file.read()
|
| 62 |
img = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
arr = img_to_array(img)
|
| 65 |
-
arr = preprocess_input(arr)
|
| 66 |
arr = np.expand_dims(arr, axis=0)
|
| 67 |
|
|
|
|
| 68 |
preds = model.predict(arr)
|
| 69 |
class_idx = int(np.argmax(preds))
|
| 70 |
confidence = float(np.max(preds))
|
| 71 |
|
| 72 |
-
|
|
|
|
| 73 |
genre = genre_map.get(game, "Unknown")
|
| 74 |
|
| 75 |
return Prediction(game=game, genre=genre, confidence=confidence)
|
| 76 |
|
| 77 |
except Exception as e:
|
| 78 |
-
return {"error": str(e)}
|
|
|
|
| 16 |
|
| 17 |
# ==== CONFIG ====
|
| 18 |
REPO_ID = "MAS-AI-0000/GameNet-1"
|
| 19 |
+
MODEL_FILENAME = "GameNetModel.h5"
|
| 20 |
+
#MODEL_FILENAME = "GameNetModel.keras"
|
| 21 |
LABELS_FILENAME = "label_to_index.json"
|
| 22 |
GENRE_FILENAME = "game_genre_map.json"
|
| 23 |
IMG_SIZE = (300, 300)
|
|
|
|
| 58 |
@app.post("/predict", response_model=Prediction)
|
| 59 |
async def predict(file: UploadFile = File(...)):
|
| 60 |
try:
|
| 61 |
+
# Step 1: Load image
|
| 62 |
image_bytes = await file.read()
|
| 63 |
img = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
| 64 |
+
|
| 65 |
+
# Step 2: Resize for EfficientNetB3 (300x300)
|
| 66 |
+
img = img.resize(IMG_SIZE, Image.Resampling.BICUBIC)
|
| 67 |
+
|
| 68 |
+
# Step 3: Convert to array and preprocess
|
| 69 |
arr = img_to_array(img)
|
| 70 |
+
arr = preprocess_input(arr) # normalize like in Colab
|
| 71 |
arr = np.expand_dims(arr, axis=0)
|
| 72 |
|
| 73 |
+
# Step 4: Inference
|
| 74 |
preds = model.predict(arr)
|
| 75 |
class_idx = int(np.argmax(preds))
|
| 76 |
confidence = float(np.max(preds))
|
| 77 |
|
| 78 |
+
# Step 5: Get label and genre
|
| 79 |
+
game = index_to_label.get(class_idx, "Unknown")
|
| 80 |
genre = genre_map.get(game, "Unknown")
|
| 81 |
|
| 82 |
return Prediction(game=game, genre=genre, confidence=confidence)
|
| 83 |
|
| 84 |
except Exception as e:
|
| 85 |
+
return JSONResponse(content={"error": str(e)}, status_code=500)
|