Spaces:
Sleeping
Sleeping
menambahkan keras load model from jso
Browse files- Model_Load.py +2 -0
- app.py +59 -50
Model_Load.py
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
|
|
| 1 |
from keras.models import model_from_json
|
|
|
|
| 2 |
|
| 3 |
def load_model_from_files(json_path, weights_path):
|
| 4 |
with open(json_path, "r") as json_file:
|
|
|
|
| 1 |
+
import keras
|
| 2 |
from keras.models import model_from_json
|
| 3 |
+
print("keras versio:", keras.__version__)
|
| 4 |
|
| 5 |
def load_model_from_files(json_path, weights_path):
|
| 6 |
with open(json_path, "r") as json_file:
|
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
import gradio as gr
|
| 6 |
import numpy as np
|
| 7 |
from keras.preprocessing import image
|
|
@@ -12,12 +12,21 @@ from fastapi import FastAPI, File, UploadFile
|
|
| 12 |
from fastapi.responses import JSONResponse
|
| 13 |
from io import BytesIO
|
| 14 |
from PIL import Image
|
| 15 |
-
import
|
| 16 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 17 |
import tensorflow as tf
|
| 18 |
-
tf.config.set_visible_devices([], 'GPU')
|
| 19 |
import logging
|
|
|
|
|
|
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
# Inisialisasi logger
|
| 23 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -66,48 +75,48 @@ def classify_image(img):
|
|
| 66 |
|
| 67 |
return label_output, deskripsi, lokasi, akurasi
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
app = FastAPI()
|
| 72 |
-
|
| 73 |
-
app.add_middleware(
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
)
|
| 80 |
-
|
| 81 |
-
@app.post("/api/predict")
|
| 82 |
-
async def predict(file: UploadFile = File(...)):
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
)
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
# Jalankan app
|
| 112 |
if __name__ == "__main__":
|
| 113 |
-
uvicorn
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
|
| 3 |
+
import keras
|
| 4 |
+
print("keras versio:", keras.__version__)
|
| 5 |
import gradio as gr
|
| 6 |
import numpy as np
|
| 7 |
from keras.preprocessing import image
|
|
|
|
| 12 |
from fastapi.responses import JSONResponse
|
| 13 |
from io import BytesIO
|
| 14 |
from PIL import Image
|
| 15 |
+
from tensorflow.keras.models import model_from_json
|
|
|
|
| 16 |
import tensorflow as tf
|
|
|
|
| 17 |
import logging
|
| 18 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 19 |
+
from keras.models import model_from_json
|
| 20 |
|
| 21 |
+
def load_model_from_files(json_path, weights_path):
|
| 22 |
+
with open(json_path, "r") as json_file:
|
| 23 |
+
loaded_model_json = json_file.read()
|
| 24 |
+
model = model_from_json(loaded_model_json)
|
| 25 |
+
model.load_weights(weights_path)
|
| 26 |
+
return model
|
| 27 |
+
|
| 28 |
+
# Nonaktifkan GPU (jika tidak digunakan)
|
| 29 |
+
tf.config.set_visible_devices([], 'GPU')
|
| 30 |
|
| 31 |
# Inisialisasi logger
|
| 32 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 75 |
|
| 76 |
return label_output, deskripsi, lokasi, akurasi
|
| 77 |
|
| 78 |
+
# Fungsi untuk membuat FastAPI app
|
| 79 |
+
def create_app():
|
| 80 |
+
app = FastAPI()
|
| 81 |
+
|
| 82 |
+
app.add_middleware(
|
| 83 |
+
CORSMiddleware,
|
| 84 |
+
allow_origins=["http://localhost:9000"], # atau sesuaikan dengan asal frontend
|
| 85 |
+
allow_credentials=True,
|
| 86 |
+
allow_methods=["*"],
|
| 87 |
+
allow_headers=["*"],
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
@app.post("/api/predict")
|
| 91 |
+
async def predict(file: UploadFile = File(...)):
|
| 92 |
+
contents = await file.read()
|
| 93 |
+
img = Image.open(BytesIO(contents)).convert("RGB")
|
| 94 |
+
label_output, deskripsi, lokasi, akurasi = classify_image(img)
|
| 95 |
+
return JSONResponse(content={
|
| 96 |
+
"label_output": label_output,
|
| 97 |
+
"deskripsi": deskripsi,
|
| 98 |
+
"lokasi": lokasi,
|
| 99 |
+
"confidence": akurasi
|
| 100 |
+
})
|
| 101 |
+
|
| 102 |
+
gradio_app = gr.Interface(
|
| 103 |
+
fn=classify_image,
|
| 104 |
+
inputs=gr.Image(type="pil", label="Upload Gambar"),
|
| 105 |
+
outputs=[
|
| 106 |
+
gr.Textbox(label="Output Klasifikasi"),
|
| 107 |
+
gr.Textbox(label="Deskripsi Lengkap", lines=20, max_lines=50),
|
| 108 |
+
gr.HTML(label="Link Lokasi"),
|
| 109 |
+
],
|
| 110 |
+
flagging_mode="never",
|
| 111 |
+
title="Klasifikasi Gambar",
|
| 112 |
+
description="Upload gambar, sistem akan mengklasifikasikan dan memberikan deskripsi mengenai gambar tersebut."
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
app = gr.mount_gradio_app(app, gradio_app, path="/gradio")
|
| 116 |
+
return app
|
| 117 |
+
|
| 118 |
+
# Hanya jalan jika dijalankan langsung, bukan import
|
|
|
|
|
|
|
| 119 |
if __name__ == "__main__":
|
| 120 |
+
import uvicorn
|
| 121 |
+
app = create_app()
|
| 122 |
+
uvicorn.run(app, host="127.0.0.1", port=8000)
|