diff --git a/app/Http/Controllers/DiagnosisController.php b/app/Http/Controllers/DiagnosisController.php index b387b3a..97dfd29 100644 --- a/app/Http/Controllers/DiagnosisController.php +++ b/app/Http/Controllers/DiagnosisController.php @@ -8,46 +8,60 @@ class DiagnosisController extends Controller { public function prosesDiagnosis(Request $request) -{ - $input = $request->input('gejala', []); + { + $input = $request->input('gejala', []); - if (count($input) < 3) { - return redirect()->route('gejala') - ->with('error', 'Pilih minimal 3 gejala!'); + // validasi minimal 3 gejala + if (count($input) < 3) { + return redirect()->route('gejala') + ->with('error', 'Pilih minimal 3 gejala!'); + } + + $inputNama = $input; + + // ambil fitur dari Python + $response = Http::get('http://127.0.0.1:5000/gejala'); + + if (!$response->successful()) { + return redirect()->route('gejala') + ->with('error', 'Tidak bisa mengambil data gejala dari API'); + } + + $featureCols = $response->json(); + + // ubah ke format 1/0 + $fiturAssoc = []; + + foreach ($featureCols as $col) { + $fiturAssoc[$col] = in_array(trim($col), array_map('trim', $inputNama)) ? 1 : 0; + } + + // kirim ke Python API + $response = Http::post('http://127.0.0.1:5000/predict', $fiturAssoc); + + if (!$response->successful()) { + return redirect()->route('gejala') + ->with('error', 'Server AI tidak merespon!'); + } + + $data = $response->json(); + + // 🔥 ambil semua hasil dari Python + $diagnosis = [ + 'nama' => $data['penyakit'] ?? '-', + 'kategori' => $data['jenis'] ?? '-', + 'pertolongan' => $data['pertolongan'] ?? [], + 'pencegahan' => $data['pencegahan'] ?? [], + ]; + + return redirect()->route('hasil-diagnosis') + ->with('diagnosis', $diagnosis) + ->with('gejala', $inputNama); } - // 🔥 LANGSUNG PAKAI INPUT - $inputNama = $input; - - // ambil feature dari Python - $response = Http::get('http://127.0.0.1:5000/gejala'); - if (!$response->successful()) { - return redirect()->route('gejala') - ->with('error', 'Tidak bisa mengambil data gejala dari API'); + // 🔥 halaman hasil + public function hasil() + { + return view('hasil-diagnosis'); } - - $featureCols = $response->json(); - - // bikin vector - $fiturAssoc = []; - - foreach ($featureCols as $col) { - $fiturAssoc[$col] = in_array(trim($col), array_map('trim', $inputNama)) ? 1 : 0; - } - - // kirim ke Flask - $response = Http::post('http://127.0.0.1:5000/predict', $fiturAssoc); - - if (!$response->successful()) { - return redirect()->route('gejala') - ->with('error', 'Server AI tidak merespon!'); - } - - $data = $response->json(); - - $hasil = isset($data['hasil']) ? trim($data['hasil']) : 'Tidak diketahui'; - - return redirect()->route('hasil-diagnosis') - ->with('hasil', $hasil) - ->with('gejala', $inputNama); -}} \ No newline at end of file +} \ No newline at end of file diff --git a/public/data/Bissmilah lagi.xlsx b/public/data/Bissmilah lagi.xlsx new file mode 100644 index 0000000..13884dc Binary files /dev/null and b/public/data/Bissmilah lagi.xlsx differ diff --git a/python_api/app.py b/python_api/app.py index 9aac554..948f2a3 100644 --- a/python_api/app.py +++ b/python_api/app.py @@ -5,19 +5,52 @@ import pandas as pd app = Flask(__name__) -# load model -model = joblib.load("../python_artifacts/modell.joblib") +# ========================= +# LOAD MODEL +# ========================= +model = joblib.load("../python_artifacts/model.joblib") -# load fitur +# ========================= +# LOAD FEATURE +# ========================= with open("../python_artifacts/feature_cols.json") as f: feature_cols = json.load(f) -# endpoint gejala +# ========================= +# LOAD EXCEL +# ========================= +df_info = pd.read_excel("../public/data/Bissmilah lagi.xlsx") + +# bersihin data +df_info.columns = df_info.columns.str.strip() + +for col in df_info.columns: + df_info[col] = df_info[col].astype(str).str.strip() + +df_info['Penyakit'] = df_info['Penyakit'].str.lower() + +# buat dictionary +info_penyakit = {} + +for _, row in df_info.iterrows(): + info_penyakit[row['Penyakit']] = { + "jenis": row['Jenis'], + "pertolongan": [p.strip() for p in row['Pertolongan'].split(";")], + "pencegahan": [p.strip() for p in row['Pencegahan'].split(";")] + } + +print("DATA PENYAKIT:", info_penyakit.keys()) + +# ========================= +# ENDPOINT GEJALA +# ========================= @app.route('/gejala', methods=['GET']) def get_gejala(): return jsonify(feature_cols) -# endpoint predict +# ========================= +# ENDPOINT PREDICT +# ========================= @app.route('/predict', methods=['POST']) def predict(): data = request.json @@ -35,13 +68,31 @@ def predict(): input_df = pd.DataFrame([input_data], columns=feature_cols) hasil = model.predict(input_df)[0] + penyakit = str(hasil).lower().strip() + + print("HASIL MODEL:", hasil) + print("CARI DI EXCEL:", penyakit) + + info = info_penyakit.get(penyakit) + + if not info: + return jsonify({ + "penyakit": hasil, + "jenis": "-", + "pertolongan": [], + "pencegahan": [] + }) return jsonify({ - "hasil": hasil + "penyakit": hasil, + "jenis": info["jenis"], + "pertolongan": info["pertolongan"], + "pencegahan": info["pencegahan"] }) + +# ========================= +# RUN APP +# ========================= if __name__ == '__main__': - print("API MODEL SIAP DIGUNAKAN (DARI JUPYTER)") - app.run(debug=True) - print("FEATURE COLS:", feature_cols) -print("INPUT FROM LARAVEL:", data) -print("INPUT VECTOR:", input_data) \ No newline at end of file + print("API MODEL SIAP DIGUNAKAN 🔥") + app.run(debug=True) \ No newline at end of file diff --git a/python_artifacts/model.joblib b/python_artifacts/model.joblib index 8d37fc4..41e71e2 100644 Binary files a/python_artifacts/model.joblib and b/python_artifacts/model.joblib differ diff --git a/resources/views/hasil-diagnosis.blade.php b/resources/views/hasil-diagnosis.blade.php index 80defa7..cedc0b4 100644 --- a/resources/views/hasil-diagnosis.blade.php +++ b/resources/views/hasil-diagnosis.blade.php @@ -5,7 +5,9 @@ Hasil Diagnosis - PawMedic - +@php +$diagnosis = session('diagnosis')?? []; +@endphp