File size: 7,720 Bytes
b2691e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import google.generativeai as genai
import os
from dotenv import load_dotenv
import re
import joblib
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler

class SimpleScheduler:
    def __init__(self):
        load_dotenv()
        api_key = os.getenv('GEMINI_API_KEY')
        genai.configure(api_key=api_key)
        self.model = genai.GenerativeModel('gemini-2.0-flash-exp')
        
        # Load ML model dan scaler
        try:
            self.rf_model = joblib.load('Models/random_forest_model.pkl')
            self.scaler = joblib.load('Models/scaler.pkl')
        except Exception as e:
            print(f"Warning: Could not load ML models: {str(e)}")
            self.rf_model = None
            self.scaler = None

    def generate_schedule(self, duration, goals, available_hours, considerations):
        """Generate schedule using Gemini with improved formatting"""
        duration_lower = duration.lower()
        
        # Select appropriate prompt based on duration
        if "year" in duration_lower:
            prompt = self._create_yearly_prompt(goals, available_hours, considerations)
        elif "month" in duration_lower:
            prompt = self._create_monthly_prompt(goals, available_hours, considerations)
        else:  # weekly
            prompt = self._create_weekly_prompt(goals, available_hours, considerations)

        try:
            response = self.model.generate_content(prompt)
            return self._format_output(response.text, duration, goals, available_hours, considerations)
        except Exception as e:
            raise Exception(f"Error generating schedule: {str(e)}")

    def _get_priority(self, hour):
        """Determine priority based on hour of day"""
        if hour < 12:
            return "HIGH"
        elif hour < 15:
            return "MEDIUM"
        else:
            return "LOW"

    def _format_time(self, time_str):
        """Format time string consistently"""
        # Remove any spaces and AM/PM
        time_str = time_str.replace(" ", "").upper()
        
        # Convert to 24-hour format if needed
        if "AM" in time_str or "PM" in time_str:
            hour = int(time_str.split(":")[0])
            if "PM" in time_str and hour != 12:
                hour += 12
            return f"{hour:02d}:00"
        
        return time_str

    def _format_output(self, schedule, duration, goals, available_hours, considerations):
        """Format the output with clean and consistent structure"""
        
        # Create header
        header = f"""SCHEDULE OVERVIEW

-------------------------------------------

Duration: {duration}

Goals: {goals}

Available Hours: {available_hours} hours/day

Considerations: {considerations}

-------------------------------------------\n"""

        # Process schedule content
        formatted_schedule = ""
        current_section = None
        
        for line in schedule.strip().split('\n'):
            line = line.strip()
            
            # Skip unnecessary lines
            if not line or "[Continue" in line or "rest" in line.lower():
                continue
                
            # Process QUARTER sections for yearly schedule
            if "QUARTER" in line.upper():
                formatted_schedule += f"\n{line}\n{'=' * len(line)}\n"
                continue
                
            # Process Week sections
            elif "WEEK" in line.upper() and "-" in line:
                week_info = line.replace('**', '').strip()
                formatted_schedule += f"\n{week_info}\n{'-' * len(week_info)}\n"
                continue
                
            # Process day headers for weekly schedule
            elif any(day in line.upper() for day in ['MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY']):
                day = line.split(':')[0].strip()
                formatted_schedule += f"\n{day}:\n"
                continue
            
            # Process time blocks
            elif ':' in line:
                try:
                    # Split into time and task
                    parts = line.split(':', 1)
                    if len(parts) < 2:
                        continue
                        
                    time_str = self._format_time(parts[0])
                    task = parts[1].strip()
                    
                    # Clean up task text
                    task = task.split('[')[0].strip()  # Remove existing priority if any
                    
                    # Get hour for priority
                    hour = int(time_str.split(':')[0])
                    priority = self._get_priority(hour)
                    
                    # Format schedule entry
                    formatted_schedule += f"  {time_str}: {task} [{priority}]\n"
                except Exception:
                    # If processing fails, add line as is
                    formatted_schedule += f"  {line}\n"
            
            # Add other lines that might be important
            elif line.strip() and not line.startswith('*'):
                formatted_schedule += f"{line}\n"

        return header + formatted_schedule

    def _create_weekly_prompt(self, goals, available_hours, considerations):
        return f"""

        Create a WEEKLY schedule for: {goals}

        Available Hours: {available_hours}/day

        Consider: {considerations}

        

        Format exactly as follows:

        

        MONDAY:

        09:00: [Morning activity]

        10:00: [Morning activity]

        11:00: [Morning activity]

        12:00: Lunch Break

        13:00: [Afternoon activity]

        14:00: [Afternoon activity]

        15:00: [Late activity]

        16:00: [Break/Gym if needed]

        

        TUESDAY:

        [Same format]

        

        Continue for each day including SATURDAY and SUNDAY.

        Use 24-hour format (09:00, 14:00).

        Keep descriptions brief and specific.

        Include breaks and considerations.

        """

    def _create_monthly_prompt(self, goals, available_hours, considerations):
        return f"""

        Create a MONTHLY schedule for: {goals}

        Available Hours: {available_hours}/day

        Consider: {considerations}

        

        Format as follows:

        

        WEEK 1:

        Monday-Friday:

        09:00: [Morning activity]

        10:00: [Morning activity]

        11:00: [Morning activity]

        12:00: Lunch Break

        13:00: [Afternoon activity]

        14:00: [Afternoon activity]

        15:00: [Late activity]

        16:00: [Break/Gym if needed]

        

        WEEK 2:

        [Same format]

        

        Continue for 4 weeks.

        Use 24-hour format.

        Keep descriptions brief and specific.

        Include breaks and considerations.

        """

    def _create_yearly_prompt(self, goals, available_hours, considerations):
        return f"""

        Create a YEARLY schedule for: {goals}

        Available Hours: {available_hours}/day

        Consider: {considerations}

        

        Format exactly as:

        

        QUARTER 1 (January-March)

        

        Week 1-4 - [Phase Name]

        09:00: [Primary morning activity]

        11:00: [Late morning activity]

        14:00: [Afternoon activity]

        

        Week 5-8 - [Next Phase]

        [Same format]

        

        Continue for all quarters.

        Use 24-hour format.

        Focus on major milestones.

        Keep descriptions brief and specific.

        """