ghtmarco commited on
Commit
fe03bf4
·
verified ·
1 Parent(s): ba9ba46

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -101
app.py CHANGED
@@ -1,102 +1,102 @@
1
- import pandas as pd
2
- import numpy as np
3
- import gradio as gr
4
- import joblib
5
- from datetime import datetime
6
-
7
- # Load model, scaler, dan feature names
8
- model = joblib.load('random_forest_model.pkl')
9
- scaler = joblib.load('scaler.pkl')
10
- feature_names = joblib.load('feature_names.pkl')['feature_names']
11
-
12
- def predict_task_priority(task_name, duration, deadline_str):
13
- try:
14
- # Parse deadline string to calculate days
15
- start_date = datetime.now()
16
- try:
17
- deadline = datetime.strptime(deadline_str, '%Y-%m-%d')
18
- except:
19
- return "Error: Format tanggal harus YYYY-MM-DD (contoh: 2024-12-31)"
20
-
21
- deadline_days = (deadline - start_date).days
22
-
23
- if deadline_days < 0:
24
- return "Error: Deadline tidak boleh di masa lalu"
25
-
26
- # Buat DataFrame dengan feature names yang sesuai
27
- input_data = pd.DataFrame({
28
- 'duration_hours': [duration],
29
- 'deadline_days': [deadline_days]
30
- })
31
-
32
- # Transform menggunakan scaler
33
- input_scaled = scaler.transform(input_data)
34
-
35
- # Predict
36
- priority = model.predict(input_scaled)[0]
37
-
38
- priority_map = {
39
- 1: "Rendah",
40
- 2: "Sedang",
41
- 3: "Tinggi"
42
- }
43
-
44
- # Generate response
45
- response = f"Analisis Tugas: {task_name}\n"
46
- response += f"Durasi: {duration} jam\n"
47
- response += f"Deadline: {deadline_days} hari lagi\n"
48
- response += f"Prioritas: {priority_map[priority]}\n\n"
49
-
50
- # Add recommendations
51
- if priority == 3:
52
- response += "Rekomendasi: Kerjakan segera! Deadline dekat dan membutuhkan waktu lama."
53
- elif priority == 2:
54
- response += "Rekomendasi: Buatlah jadwal yang tepat dan mulai kerjakan secara bertahap."
55
- else:
56
- response += "Rekomendasi: Dapat dikerjakan dengan lebih santai, tapi tetap pantau progress."
57
-
58
- return response
59
-
60
- except Exception as e:
61
- return f"Error: {str(e)}"
62
-
63
- # Create Gradio interface
64
- iface = gr.Interface(
65
- fn=predict_task_priority,
66
- inputs=[
67
- gr.Dropdown(
68
- choices=[
69
- "Meeting",
70
- "Bekerja",
71
- "Belajar",
72
- "Tugas Kuliah",
73
- "Proyek"
74
- ],
75
- label="Nama Tugas"
76
- ),
77
- gr.Slider(
78
- minimum=1,
79
- maximum=10,
80
- value=5,
81
- step=0.5,
82
- label="Durasi Tugas (dalam jam)"
83
- ),
84
- gr.Textbox(
85
- label="Deadline (YYYY-MM-DD)",
86
- placeholder="Contoh: 2024-12-31",
87
- info="Masukkan tanggal dalam format YYYY-MM-DD"
88
- )
89
- ],
90
- outputs=gr.Textbox(label="Hasil Analisis", lines=6),
91
- title="Sistem Prioritas Tugas",
92
- description="""
93
- Sistem ini akan membantu Anda menentukan prioritas tugas berdasarkan:
94
- 1. Durasi pengerjaan tugas
95
- 2. Jarak waktu ke deadline
96
-
97
- Hasil analisis akan memberikan rekomendasi pengelolaan waktu yang sesuai.
98
- """
99
- )
100
-
101
- if __name__ == "__main__":
102
  iface.launch()
 
1
+ import pandas as pd
2
+ import numpy as np
3
+ import gradio as gr
4
+ import joblib
5
+ from datetime import datetime
6
+
7
+ # Load model, scaler, dan feature names
8
+ model = joblib.load('random_forest_model.pkl')
9
+ scaler = joblib.load('scaler.pkl')
10
+ feature_names = joblib.load('feature_names.pkl')['feature_names']
11
+
12
+ def predict_task_priority(task_name, duration, deadline_str):
13
+ try:
14
+ # Parse deadline string to calculate days
15
+ start_date = datetime.now()
16
+ try:
17
+ deadline = datetime.strptime(deadline_str, '%Y-%m-%d')
18
+ except:
19
+ return "Format tanggal harus YYYY-MM-DD"
20
+
21
+ deadline_days = (deadline - start_date).days
22
+
23
+ if deadline_days < 0:
24
+ return "Deadline tidak boleh di masa lalu"
25
+
26
+ # Buat DataFrame dengan feature names yang sesuai
27
+ input_data = pd.DataFrame({
28
+ 'duration_hours': [duration],
29
+ 'deadline_days': [deadline_days]
30
+ })
31
+
32
+ # Transform menggunakan scaler
33
+ input_scaled = scaler.transform(input_data)
34
+
35
+ # Predict
36
+ priority = model.predict(input_scaled)[0]
37
+
38
+ priority_map = {
39
+ 1: "Rendah",
40
+ 2: "Sedang",
41
+ 3: "Tinggi"
42
+ }
43
+
44
+ # Generate response
45
+ response = f"Analisis Tugas: {task_name}\n"
46
+ response += f"Durasi: {duration} jam\n"
47
+ response += f"Deadline: {deadline_days} hari lagi\n"
48
+ response += f"Prioritas: {priority_map[priority]}\n\n"
49
+
50
+ # Add recommendations
51
+ if priority == 3:
52
+ response += "Rekomendasi: Kerjakan segera! Deadline dekat dan membutuhkan waktu lama."
53
+ elif priority == 2:
54
+ response += "Rekomendasi: Buatlah jadwal yang tepat dan mulai kerjakan secara bertahap."
55
+ else:
56
+ response += "Rekomendasi: Dapat dikerjakan dengan lebih santai, tapi tetap pantau progress."
57
+
58
+ return response
59
+
60
+ except Exception as e:
61
+ return f"Error: {str(e)}"
62
+
63
+ # Create Gradio interface
64
+ iface = gr.Interface(
65
+ fn=predict_task_priority,
66
+ inputs=[
67
+ gr.Dropdown(
68
+ choices=[
69
+ "Meeting",
70
+ "Bekerja",
71
+ "Belajar",
72
+ "Tugas Kuliah",
73
+ "Proyek"
74
+ ],
75
+ label="Nama Tugas"
76
+ ),
77
+ gr.Slider(
78
+ minimum=1,
79
+ maximum=10,
80
+ value=5,
81
+ step=0.5,
82
+ label="Durasi Tugas (dalam jam)"
83
+ ),
84
+ gr.Textbox(
85
+ label="Deadline",
86
+ placeholder="Contoh: 2024-12-31",
87
+ # info=""
88
+ )
89
+ ],
90
+ outputs=gr.Textbox(label="Hasil Analisis", lines=6),
91
+ title="Sistem Prioritas Tugas",
92
+ description="""
93
+ Sistem ini akan membantu Anda menentukan prioritas tugas berdasarkan:
94
+ 1. Durasi pengerjaan tugas
95
+ 2. Jarak waktu ke deadline
96
+
97
+ Hasil analisis akan memberikan rekomendasi pengelolaan waktu yang sesuai.
98
+ """
99
+ )
100
+
101
+ if __name__ == "__main__":
102
  iface.launch()