Spaces:
Running
Running
Menyesuaikan app.py dan menambah lokasi di location.py
Browse files- app.py +59 -10
- location.py +12 -0
app.py
CHANGED
@@ -3,6 +3,13 @@ import numpy as np
|
|
3 |
from keras.preprocessing import image
|
4 |
from Model_Load import load_model_from_files
|
5 |
from description import description
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
|
8 |
# Load model dan label
|
@@ -25,30 +32,72 @@ def classify_image(img):
|
|
25 |
confidence = np.max(pred)
|
26 |
predicted_label = labels[np.argmax(pred)]
|
27 |
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
else:
|
31 |
-
deskripsi = description.get(predicted_label, "Deskripsi belum tersedia.")
|
32 |
label_output = f"{predicted_label} (Confidence: {confidence * 100:.2f}%)"
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
34 |
|
|
|
35 |
|
36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
fn=classify_image,
|
41 |
inputs=gr.Image(type="pil", label="Upload Gambar"),
|
42 |
outputs=[
|
43 |
gr.Textbox(label="Output Klasifikasi"),
|
44 |
-
gr.Textbox(label="Deskripsi Lengkap", lines=20, max_lines=50)
|
|
|
45 |
],
|
46 |
allow_flagging="never",
|
47 |
title="Klasifikasi Gambar",
|
48 |
-
description="Upload gambar
|
49 |
)
|
50 |
|
|
|
|
|
51 |
|
52 |
-
#
|
53 |
if __name__ == "__main__":
|
54 |
-
|
|
|
|
3 |
from keras.preprocessing import image
|
4 |
from Model_Load import load_model_from_files
|
5 |
from description import description
|
6 |
+
from location import location
|
7 |
+
from fastapi import FastAPI, File, UploadFile
|
8 |
+
from fastapi.responses import JSONResponse
|
9 |
+
from io import BytesIO
|
10 |
+
from PIL import Image
|
11 |
+
import uvicorn
|
12 |
+
from fastapi.middleware.cors import CORSMiddleware
|
13 |
|
14 |
|
15 |
# Load model dan label
|
|
|
32 |
confidence = np.max(pred)
|
33 |
predicted_label = labels[np.argmax(pred)]
|
34 |
|
35 |
+
akurasi = float(confidence)
|
36 |
+
if confidence < 0.8:
|
37 |
+
label_output = "Tidak dapat dikenali (Confidence: {:.2f}%)".format(confidence * 100)
|
38 |
+
deskripsi = (
|
39 |
+
"Tolong arahkan ke objek yang jelas agar bisa diidentifikasikan. "
|
40 |
+
"Pastikan anda berada di salah satu tempat seperti:\n"
|
41 |
+
"- Benteng Vredeburg\n- Candi Borobudur\n- Candi Prambanan\n"
|
42 |
+
"- Gedung Agung Istana Kepresidenan Yogyakarta\n- Masjid Gedhe Kauman\n"
|
43 |
+
"- Monumen Serangan 1 Maret\n- Museum Gunungapi Merapi\n- Situs Ratu Boko\n"
|
44 |
+
"- Taman Sari\n- Tugu Yogyakarta"
|
45 |
+
)
|
46 |
+
lokasi = "-"
|
47 |
else:
|
|
|
48 |
label_output = f"{predicted_label} (Confidence: {confidence * 100:.2f}%)"
|
49 |
+
deskripsi = description.get(predicted_label, "Deskripsi belum tersedia.")
|
50 |
+
lokasi = location.get(predicted_label, None)
|
51 |
+
if lokasi:
|
52 |
+
lokasi = f'<a href="{lokasi}" target="_blank">Lihat Lokasi di Google Maps</a>'
|
53 |
+
else:
|
54 |
+
lokasi = "Lokasi tidak ditemukan"
|
55 |
|
56 |
+
return label_output, deskripsi, lokasi, akurasi
|
57 |
|
58 |
|
59 |
+
# FastAPI instance
|
60 |
+
app = FastAPI()
|
61 |
+
|
62 |
+
app.add_middleware(
|
63 |
+
CORSMiddleware,
|
64 |
+
allow_origins=["http://localhost:9000"], # atau sesuaikan dengan asal frontend
|
65 |
+
allow_credentials=True,
|
66 |
+
allow_methods=["*"],
|
67 |
+
allow_headers=["*"],
|
68 |
+
)
|
69 |
|
70 |
+
@app.post("/api/predict")
|
71 |
+
async def predict(file: UploadFile = File(...)):
|
72 |
+
contents = await file.read()
|
73 |
+
img = Image.open(BytesIO(contents)).convert("RGB")
|
74 |
+
label_output, deskripsi, lokasi, akurasi = classify_image(img)
|
75 |
+
return JSONResponse(content={
|
76 |
+
"label_output": label_output,
|
77 |
+
"deskripsi": deskripsi,
|
78 |
+
"lokasi" : lokasi,
|
79 |
+
"confidence": akurasi
|
80 |
+
})
|
81 |
+
|
82 |
+
|
83 |
+
# Gradio antarmuka (opsional tetap ditampilkan)
|
84 |
+
gradio_app = gr.Interface(
|
85 |
fn=classify_image,
|
86 |
inputs=gr.Image(type="pil", label="Upload Gambar"),
|
87 |
outputs=[
|
88 |
gr.Textbox(label="Output Klasifikasi"),
|
89 |
+
gr.Textbox(label="Deskripsi Lengkap", lines=20, max_lines=50),
|
90 |
+
gr.HTML(label="Link Lokasi"),
|
91 |
],
|
92 |
allow_flagging="never",
|
93 |
title="Klasifikasi Gambar",
|
94 |
+
description="Upload gambar, sistem akan mengklasifikasikan dan memberikan deskripsi mengenai gambar tersebut."
|
95 |
)
|
96 |
|
97 |
+
# Mount Gradio ke FastAPI
|
98 |
+
app = gr.mount_gradio_app(app, gradio_app, path="")
|
99 |
|
100 |
+
# Jalankan app
|
101 |
if __name__ == "__main__":
|
102 |
+
uvicorn.run(app, host="127.0.0.1", port=8000)
|
103 |
+
|
location.py
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
location = {
|
2 |
+
"Benteng Vredeburg": "https://surli.cc/bffath",
|
3 |
+
"Candi Borobudur": "https://surl.lu/rqoiya",
|
4 |
+
"Candi Prambanan": "https://surl.lu/evjzzc",
|
5 |
+
"Gedung Agung Istana Kepresidenan": "https://surl.li/czgmti",
|
6 |
+
"Masjid Gedhe Kauman": "https://surl.li/wpatgm",
|
7 |
+
"Monumen Serangan 1 Maret": "https://surl.lu/axmfwy",
|
8 |
+
"Museum Gunungapi Merapi": "https://surl.lu/wnkqof",
|
9 |
+
"Situs Ratu Boko": "https://surl.li/abdxrb",
|
10 |
+
"Taman Sari": "https://surl.li/rwpdyu",
|
11 |
+
"Tugu Yogyakarta": "https://surl.li/tnehjk"
|
12 |
+
}
|