Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, UploadFile, File
|
2 |
+
from fastapi.responses import JSONResponse
|
3 |
+
from fastapi.middleware.cors import CORSMiddleware
|
4 |
+
from huggingface_hub import hf_hub_download
|
5 |
+
|
6 |
+
import numpy as np
|
7 |
+
import tensorflow as tf
|
8 |
+
from tensorflow.keras.models import load_model
|
9 |
+
from tensorflow.keras.preprocessing.image import img_to_array
|
10 |
+
from tensorflow.keras.applications.efficientnet import preprocess_input
|
11 |
+
|
12 |
+
from PIL import Image
|
13 |
+
import json
|
14 |
+
import io
|
15 |
+
|
16 |
+
# ==== CONFIG ====
|
17 |
+
REPO_ID = "MAS-AI-0000/GameNet-1"
|
18 |
+
MODEL_FILENAME = "GameNetModel.h5"
|
19 |
+
LABELS_FILENAME = "label_to_index.json"
|
20 |
+
GENRE_FILENAME = "game_genre_map.json"
|
21 |
+
IMG_SIZE = (300, 300)
|
22 |
+
|
23 |
+
# ==== Load assets ====
|
24 |
+
model_path = hf_hub_download(repo_id=REPO_ID, filename=MODEL_FILENAME)
|
25 |
+
labels_path = hf_hub_download(repo_id=REPO_ID, filename=LABELS_FILENAME)
|
26 |
+
genre_path = hf_hub_download(repo_id=REPO_ID, filename=GENRE_FILENAME)
|
27 |
+
|
28 |
+
model = load_model(model_path)
|
29 |
+
|
30 |
+
with open(labels_path, "r") as f:
|
31 |
+
label_to_index = json.load(f)
|
32 |
+
index_to_label = {v: k for k, v in label_to_index.items()}
|
33 |
+
|
34 |
+
with open(genre_path, "r") as f:
|
35 |
+
genre_map = json.load(f)
|
36 |
+
|
37 |
+
# ==== FastAPI Setup ====
|
38 |
+
app = FastAPI()
|
39 |
+
|
40 |
+
# Optional: CORS if frontend is on different domain
|
41 |
+
app.add_middleware(
|
42 |
+
CORSMiddleware,
|
43 |
+
allow_origins=["*"], # change this in production
|
44 |
+
allow_credentials=True,
|
45 |
+
allow_methods=["*"],
|
46 |
+
allow_headers=["*"],
|
47 |
+
)
|
48 |
+
|
49 |
+
# Response schema
|
50 |
+
class Prediction(BaseModel):
|
51 |
+
game: str
|
52 |
+
genre: str
|
53 |
+
confidence: float
|
54 |
+
|
55 |
+
# Inference route
|
56 |
+
@app.post("/predict", response_model=Prediction)
|
57 |
+
async def predict(file: UploadFile = File(...)):
|
58 |
+
try:
|
59 |
+
image_bytes = await file.read()
|
60 |
+
img = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
61 |
+
img = img.resize(IMG_SIZE)
|
62 |
+
arr = img_to_array(img)
|
63 |
+
arr = preprocess_input(arr)
|
64 |
+
arr = np.expand_dims(arr, axis=0)
|
65 |
+
|
66 |
+
preds = model.predict(arr)
|
67 |
+
class_idx = int(np.argmax(preds))
|
68 |
+
confidence = float(np.max(preds))
|
69 |
+
|
70 |
+
game = index_to_label[class_idx]
|
71 |
+
genre = genre_map.get(game, "Unknown")
|
72 |
+
|
73 |
+
return Prediction(game=game, genre=genre, confidence=confidence)
|
74 |
+
|
75 |
+
except Exception as e:
|
76 |
+
return {"error": str(e)}
|