hasil diagnosis
This commit is contained in:
parent
6ad396bc27
commit
57cddbdc92
|
|
@ -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);
|
||||
}}
|
||||
}
|
||||
Binary file not shown.
|
|
@ -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)
|
||||
print("API MODEL SIAP DIGUNAKAN 🔥")
|
||||
app.run(debug=True)
|
||||
Binary file not shown.
|
|
@ -5,7 +5,9 @@
|
|||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Hasil Diagnosis - PawMedic</title>
|
||||
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@600;700;800&family=Inter:wght@300;400;500;600&display=swap" rel="stylesheet">
|
||||
|
||||
@php
|
||||
$diagnosis = session('diagnosis')?? [];
|
||||
@endphp
|
||||
<style>
|
||||
:root{
|
||||
--ff-heading: 'Poppins', system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial;
|
||||
|
|
@ -442,16 +444,13 @@
|
|||
<div class="diagnosis-result">
|
||||
<div class="diagnosis-label">Kemungkinan Penyakit</div>
|
||||
<div class="diagnosis-name">
|
||||
{{ session('hasil') ?? 'Tidak diketahui' }}
|
||||
{{ $diagnosis['nama'] ?? 'Tidak diketahui' }}
|
||||
</div>
|
||||
|
||||
<div class="diagnosis-category">
|
||||
Jenis: {{ session('jenis') ?? '-' }}
|
||||
Jenis: {{ $diagnosis['kategori'] ?? '-' }}
|
||||
</div>
|
||||
|
||||
<div style="margin-top:10px; font-weight:600; color:#4bb66f;">
|
||||
Confidence: {{ session('confidence') }}%
|
||||
</div>
|
||||
|
||||
<!-- Gejala yang Dipilih -->
|
||||
<div class="gejala-list-section">
|
||||
|
|
@ -471,7 +470,7 @@
|
|||
<span>💡 Rekomendasi Perawatan</span>
|
||||
</div>
|
||||
<ul class="recommendation-list">
|
||||
@foreach(session('pertolongan', []) as $item)
|
||||
@foreach($diagnosis['pertolongan'] ?? [] as $item)
|
||||
<li>{{ $item }}</li>
|
||||
@endforeach
|
||||
</ul>
|
||||
|
|
@ -482,7 +481,7 @@
|
|||
<span>🛡️ Pencegahan</span>
|
||||
</div>
|
||||
<ul class="recommendation-list">
|
||||
@foreach(session('pencegahan', []) as $item)
|
||||
@foreach($diagnosis['pencegahan'] ?? [] as $item)
|
||||
<li>{{ $item }}</li>
|
||||
@endforeach
|
||||
</ul>
|
||||
|
|
@ -528,7 +527,7 @@ function printDiagnosis() {
|
|||
|
||||
// Share function
|
||||
function shareDiagnosis() {
|
||||
const diagnosis = "{{ session('hasil') }}";
|
||||
const diagnosis = "{{ $diagnosis['nama'] ?? '-' }}";
|
||||
const gejala = @json(session('gejala', []));
|
||||
|
||||
const text = `Hasil Diagnosis PawMedic:\n\nPenyakit: ${diagnosis}\nGejala: ${gejala.join(', ')}\n\nDapatkan diagnosis di: ${window.location.origin}`;
|
||||
|
|
|
|||
|
|
@ -21,9 +21,7 @@
|
|||
|
||||
Route::post('/diagnosis/proses', [DiagnosisController::class, 'prosesDiagnosis'])->name('diagnosis.proses');
|
||||
|
||||
Route::get('/hasil-diagnosis', function () {
|
||||
return view('hasil-diagnosis');
|
||||
})->name('hasil-diagnosis');
|
||||
Route::get('/hasil-diagnosis', [DiagnosisController::class, 'hasil'])->name('hasil-diagnosis');
|
||||
|
||||
Route::get('/ulasan', function () {
|
||||
return view('ulasan');
|
||||
|
|
|
|||
Loading…
Reference in New Issue