File size: 7,516 Bytes
9a1eb1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7041536
 
 
 
 
 
 
 
 
 
 
 
9a1eb1e
 
 
 
7041536
9a1eb1e
 
 
 
7041536
9a1eb1e
 
7041536
9a1eb1e
 
 
 
 
 
7041536
9a1eb1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7041536
9a1eb1e
 
7041536
9a1eb1e
7041536
9a1eb1e
7041536
9a1eb1e
 
 
 
7041536
 
 
 
9a1eb1e
7041536
9a1eb1e
7041536
 
 
 
 
9a1eb1e
 
 
 
7041536
 
9a1eb1e
7041536
9a1eb1e
7041536
9a1eb1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from joblib import Memory
import datetime

# Initialize cache
cache_dir = "./cache"
memory = Memory(cache_dir, verbose=0)

# Load pre-trained model and tokenizer
model_name = "distilgpt2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)

# Set pad_token_id to eos_token_id to avoid warnings
tokenizer.pad_token = tokenizer.eos_token
model.config.pad_token_id = tokenizer.eos_token_id

# Define prompt template
PROMPT_TEMPLATE = """You are an AI coach for construction supervisors. Based on the following inputs, generate a daily checklist, focus suggestions, and a motivational quote. Format your response with clear labels as follows:

Checklist:
- Item 1
- Item 2
Suggestions:
- Suggestion 1
- Suggestion 2
Quote:
- Your motivational quote here

Inputs:
Role: {role}
Project: {project_id}
Milestones: {milestones}
Reflection: {reflection}
"""

# Cache reset check
last_reset = datetime.date.today()

def reset_cache_if_new_day():
    global last_reset
    today = datetime.date.today()
    if today > last_reset:
        memory.clear()
        last_reset = today

# Cached generation function
@memory.cache
def generate_outputs(role, project_id, milestones, reflection):
    reset_cache_if_new_day()
    
    # Validate inputs
    if not all([role, project_id, milestones, reflection]):
        return "Error: All fields are required.", "", ""
    
    # Create prompt
    prompt = PROMPT_TEMPLATE.format(
        role=role,
        project_id=project_id,
        milestones=milestones,
        reflection=reflection
    )
    
    # Tokenize with attention_mask
    inputs = tokenizer(
        prompt,
        return_tensors="pt",
        max_length=512,
        truncation=True,
        padding=True,
        return_attention_mask=True
    )
    
    # Generate with attention_mask
    with torch.no_grad():
        outputs = model.generate(
            inputs["input_ids"],
            attention_mask=inputs["attention_mask"],
            max_length=1000,
            num_return_sequences=1,
            no_repeat_ngram_size=2,
            do_sample=True,
            top_p=0.9,
            temperature=0.8,
            pad_token_id=tokenizer.eos_token_id
        )
    
    # Decode generated text
    generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
    
    # Parse the output
    checklist = "No checklist generated."
    suggestions = "No suggestions generated."
    quote = "No quote generated."
    
    # Extract sections using labels
    if "Checklist:" in generated_text:
        checklist_start = generated_text.find("Checklist:") + len("Checklist:")
        suggestions_start = generated_text.find("Suggestions:") if "Suggestions:" in generated_text else len(generated_text)
        checklist = generated_text[checklist_start:suggestions_start].strip()
        if not checklist:
            checklist = "No checklist generated."
    
    if "Suggestions:" in generated_text:
        suggestions_start = generated_text.find("Suggestions:") + len("Suggestions:")
        quote_start = generated_text.find("Quote:") if "Quote:" in generated_text else len(generated_text)
        suggestions = generated_text[suggestions_start:quote_start].strip()
        if not suggestions:
            suggestions = "No suggestions generated."
    
    if "Quote:" in generated_text:
        quote_start = generated_text.find("Quote:") + len("Quote:")
        quote = generated_text[quote_start:].strip()
        if not quote:
            quote = "No quote generated."
    
    # Context-aware fallbacks
    if checklist == "No checklist generated.":
        checklist_items = []
        milestone_list = [m.strip() for m in milestones.split(",")]
        for i, milestone in enumerate(milestone_list, 1):
            time = 8 + (i-1)*2 if i <= 3 else 4  # Follow sample timing (8 AM, 10 AM, 12 PM, 4 PM)
            period = "AM" if i <= 3 else "PM"
            checklist_items.append(f"- {milestone} by {time} {period}.")
        # Add context-specific task
        if "safety" in reflection.lower():
            checklist_items.append("- Review safety compliance by 4 PM.")
        else:
            checklist_items.append("- Check equipment status by 4 PM.")
        checklist = "\n".join(checklist_items)
    
    if suggestions == "No suggestions generated.":
        suggestions_items = []
        reflection_lower = reflection.lower()
        if "safety" in reflection_lower:
            suggestions_items.append("- Address safety concerns with a team briefing on proper procedures.")
        if "delay" in reflection_lower or "late" in reflection_lower:
            suggestions_items.append("- Follow up with suppliers to prevent future delays.")
        if "weather" in reflection_lower:
            suggestions_items.append("- Monitor weather updates and plan contingencies.")
        if "equipment" in reflection_lower:
            suggestions_items.append("- Schedule equipment maintenance to avoid future issues.")
        if "suppliers" in reflection_lower:
            suggestions_items.append("- Set up a morning call with suppliers to confirm timelines.")
        # Add generic suggestion
        suggestions_items.append("- Brief the team on tomorrow’s goals during the daily huddle.")
        suggestions = "\n".join(suggestions_items if suggestions_items else ["- Coordinate with the team.", "- Plan for contingencies."])
    
    if quote == "No quote generated.":
        reflection_lower = reflection.lower()
        if "safety" in reflection_lower:
            quote = "- Build with care—safety today ensures success tomorrow!"
        elif "delay" in reflection_lower or "late" in reflection_lower:
            quote = "- Keep moving forward—every challenge is a step toward success!"
        elif "weather" in reflection_lower:
            quote = "- Steady progress leads to great achievements, no matter the weather!"
        else:
            quote = "- Success is built one solid step at a time!"

    return checklist, suggestions, quote

# Gradio interface
def create_interface():
    with gr.Blocks() as demo:
        gr.Markdown("# Construction Supervisor AI Coach")
        gr.Markdown("Enter details to generate a daily checklist, focus suggestions, and a motivational quote.")
        
        with gr.Row():
            role = gr.Dropdown(choices=["Supervisor", "Foreman", "Project Manager"], label="Role")
            project_id = gr.Textbox(label="Project ID")
        
        milestones = gr.Textbox(label="Milestones (comma-separated KPIs)")
        reflection = gr.Textbox(label="Reflection Log", lines=5)
        
        with gr.Row():
            submit = gr.Button("Generate")
            clear = gr.Button("Clear")
        
        checklist_output = gr.Textbox(label="Daily Checklist")
        suggestions_output = gr.Textbox(label="Focus Suggestions")
        quote_output = gr.Textbox(label="Motivational Quote")
        
        submit.click(
            fn=generate_outputs,
            inputs=[role, project_id, milestones, reflection],
            outputs=[checklist_output, suggestions_output, quote_output]
        )
        clear.click(
            fn=lambda: ("", "", "", ""),
            inputs=None,
            outputs=[role, project_id, milestones, reflection]
        )
    
    return demo

if __name__ == "__main__":
    demo = create_interface()
    demo.launch()