Spaces:
Build error
Build error
| import pandas as pd | |
| import numpy as np | |
| import gradio as gr | |
| import joblib | |
| from datetime import datetime | |
| # Load model, scaler, dan feature names | |
| model = joblib.load('random_forest_model.pkl') | |
| scaler = joblib.load('scaler.pkl') | |
| feature_names = joblib.load('feature_names.pkl')['feature_names'] | |
| def predict_task_priority(task_name, duration, deadline_str): | |
| try: | |
| # Validasi input task name | |
| if not task_name.strip(): | |
| return "Error: Nama tugas tidak boleh kosong" | |
| # Parse deadline string to calculate days | |
| start_date = datetime.now() | |
| try: | |
| deadline = datetime.strptime(deadline_str, '%Y-%m-%d') | |
| except: | |
| return "Error: Format tanggal harus YYYY-MM-DD (contoh: 2024-12-31)" | |
| deadline_days = (deadline - start_date).days | |
| if deadline_days < 0: | |
| return "Error: Deadline tidak boleh di masa lalu" | |
| # Buat DataFrame dengan feature names yang sesuai | |
| input_data = pd.DataFrame({ | |
| 'duration_hours': [duration], | |
| 'deadline_days': [deadline_days] | |
| }) | |
| # Transform menggunakan scaler | |
| input_scaled = scaler.transform(input_data) | |
| # Predict | |
| priority = model.predict(input_scaled)[0] | |
| priority_map = { | |
| 1: "Tinggi", | |
| 2: "Sedang", | |
| 3: "Rendah" | |
| } | |
| # Generate response | |
| response = f"Analisis Tugas: {task_name}\n" | |
| response += f"Durasi: {duration} jam\n" | |
| response += f"Deadline: {deadline_str} ({deadline_days} hari lagi)\n" | |
| response += f"Prioritas: {priority_map[priority]}\n\n" | |
| # Add recommendations based on priority | |
| if priority == 1: | |
| response += "Rekomendasi: Kerjakan segera! Deadline dekat dan membutuhkan waktu lama." | |
| response += "\nTindakan yang disarankan:" | |
| response += "\n• Fokus utama pada tugas ini" | |
| response += "\n• Buat jadwal spesifik untuk pengerjaannya" | |
| response += "\n• Siapkan semua resource yang dibutuhkan" | |
| elif priority == 2: | |
| response += "Rekomendasi: Buatlah jadwal yang tepat dan mulai kerjakan secara bertahap." | |
| response += "\nTindakan yang disarankan:" | |
| response += "\n• Bagi tugas menjadi beberapa milestone" | |
| response += "\n• Alokasikan waktu setiap hari untuk progress" | |
| response += "\n• Pantau perkembangan secara reguler" | |
| else: | |
| response += "Rekomendasi: Dapat dikerjakan dengan lebih santai, tapi tetap pantau progress." | |
| response += "\nTindakan yang disarankan:" | |
| response += "\n• Buat timeline longgar untuk pengerjaan" | |
| response += "\n• Sesuaikan dengan jadwal tugas prioritas lain" | |
| response += "\n• Review progress mingguan" | |
| return response | |
| except Exception as e: | |
| return f"Error: {str(e)}" | |
| # Create Gradio interface | |
| iface = gr.Interface( | |
| fn=predict_task_priority, | |
| inputs=[ | |
| gr.Textbox( | |
| label="Nama Tugas", | |
| placeholder="Masukkan nama tugas Anda", | |
| info="Contoh: Laporan Akhir, Presentasi Project, dll." | |
| ), | |
| gr.Slider( | |
| minimum=1, | |
| maximum=10, | |
| value=5, | |
| step=0.5, | |
| label="Durasi Tugas (dalam jam)", | |
| info="Perkiraan waktu yang dibutuhkan untuk menyelesaikan tugas" | |
| ), | |
| gr.Textbox( | |
| label="Deadline (YYYY-MM-DD)", | |
| placeholder="Contoh: 2024-12-31", | |
| info="Masukkan tanggal dalam format YYYY-MM-DD" | |
| ) | |
| ], | |
| outputs=gr.Textbox(label="Hasil Analisis", lines=10), | |
| title="Sistem Prioritas Tugas", | |
| description=""" | |
| Sistem ini akan membantu Anda menentukan prioritas tugas berdasarkan: | |
| 1. Durasi pengerjaan tugas | |
| 2. Jarak waktu ke deadline | |
| Hasil analisis akan memberikan rekomendasi pengelolaan waktu yang sesuai untuk membantu | |
| Anda mengelola tugas dengan lebih efektif. | |
| """, | |
| examples=[ | |
| ["Laporan Akhir Semester", 8, "2024-11-30"], | |
| ["Presentasi Project", 3, "2024-12-15"], | |
| ["Revisi Proposal", 5, "2024-11-25"] | |
| ] | |
| ) | |
| if __name__ == "__main__": | |
| iface.launch() |