Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,17 +3,25 @@ import torch
|
|
3 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
4 |
from joblib import Memory
|
5 |
import datetime
|
|
|
|
|
|
|
|
|
6 |
|
7 |
# Initialize cache
|
8 |
cache_dir = "./cache"
|
9 |
memory = Memory(cache_dir, verbose=0)
|
10 |
|
11 |
-
# Load pre-trained model and tokenizer
|
12 |
model_name = "distilgpt2"
|
13 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
14 |
-
model = AutoModelForCausalLM.from_pretrained(model_name)
|
|
|
|
|
|
|
|
|
15 |
|
16 |
-
# Define a more explicit prompt template
|
17 |
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:
|
18 |
|
19 |
Checklist:
|
@@ -25,13 +33,47 @@ Suggestions:
|
|
25 |
Quote:
|
26 |
- Your motivational quote here
|
27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
Inputs:
|
29 |
Role: {role}
|
30 |
Project: {project_id}
|
31 |
Milestones: {milestones}
|
32 |
Reflection: {reflection}
|
33 |
|
34 |
-
|
35 |
|
36 |
# Cache reset check
|
37 |
last_reset = datetime.date.today()
|
@@ -43,7 +85,7 @@ def reset_cache_if_new_day():
|
|
43 |
memory.clear()
|
44 |
last_reset = today
|
45 |
|
46 |
-
# Cached generation function with improved parsing
|
47 |
@memory.cache
|
48 |
def generate_outputs(role, project_id, milestones, reflection):
|
49 |
reset_cache_if_new_day()
|
@@ -60,16 +102,27 @@ def generate_outputs(role, project_id, milestones, reflection):
|
|
60 |
reflection=reflection
|
61 |
)
|
62 |
|
63 |
-
# Tokenize
|
64 |
-
inputs = tokenizer(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
outputs = model.generate(
|
66 |
inputs["input_ids"],
|
67 |
-
|
|
|
68 |
num_return_sequences=1,
|
69 |
no_repeat_ngram_size=2,
|
70 |
do_sample=True,
|
71 |
top_p=0.9,
|
72 |
-
temperature=0.8
|
|
|
73 |
)
|
74 |
|
75 |
# Decode generated text
|
@@ -105,17 +158,30 @@ def generate_outputs(role, project_id, milestones, reflection):
|
|
105 |
if not quote:
|
106 |
quote = "No quote generated."
|
107 |
|
108 |
-
#
|
109 |
if checklist == "No checklist generated.":
|
110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
if suggestions == "No suggestions generated.":
|
112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
if quote == "No quote generated.":
|
114 |
-
quote = "- Keep
|
115 |
|
116 |
return checklist, suggestions, quote
|
117 |
|
118 |
-
# Gradio interface
|
119 |
def create_interface():
|
120 |
with gr.Blocks() as demo:
|
121 |
gr.Markdown("# Construction Supervisor AI Coach")
|
|
|
3 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
4 |
from joblib import Memory
|
5 |
import datetime
|
6 |
+
import os
|
7 |
+
|
8 |
+
# Enable offline mode for Hugging Face transformers
|
9 |
+
os.environ["HF_HUB_OFFLINE"] = "1"
|
10 |
|
11 |
# Initialize cache
|
12 |
cache_dir = "./cache"
|
13 |
memory = Memory(cache_dir, verbose=0)
|
14 |
|
15 |
+
# Load pre-trained model and tokenizer with local_files_only=True
|
16 |
model_name = "distilgpt2"
|
17 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, local_files_only=True)
|
18 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, local_files_only=True)
|
19 |
+
|
20 |
+
# Set pad_token_id to eos_token_id to avoid warnings
|
21 |
+
tokenizer.pad_token = tokenizer.eos_token
|
22 |
+
model.config.pad_token_id = tokenizer.eos_token_id
|
23 |
|
24 |
+
# Define a more explicit prompt template with few-shot examples
|
25 |
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:
|
26 |
|
27 |
Checklist:
|
|
|
33 |
Quote:
|
34 |
- Your motivational quote here
|
35 |
|
36 |
+
Here are examples of expected outputs:
|
37 |
+
|
38 |
+
Example 1:
|
39 |
+
Inputs:
|
40 |
+
Role: Supervisor
|
41 |
+
Project: PROJ-01
|
42 |
+
Milestones: Lay foundation, Check materials
|
43 |
+
Reflection: Team is on track, but weather might delay work.
|
44 |
+
Checklist:
|
45 |
+
- Confirm foundation laying schedule by 8 AM.
|
46 |
+
- Inspect materials for quality at 10 AM.
|
47 |
+
Suggestions:
|
48 |
+
- Monitor weather updates and plan contingencies.
|
49 |
+
- Ensure team has protective gear for rain.
|
50 |
+
Quote:
|
51 |
+
- Stay steady—every foundation leads to greater heights!
|
52 |
+
|
53 |
+
Example 2:
|
54 |
+
Inputs:
|
55 |
+
Role: Foreman
|
56 |
+
Project: PROJ-02
|
57 |
+
Milestones: Install beams, Safety check
|
58 |
+
Reflection: Minor delay due to late delivery.
|
59 |
+
Checklist:
|
60 |
+
- Oversee beam installation starting at 9 AM.
|
61 |
+
- Conduct safety check at 1 PM.
|
62 |
+
Suggestions:
|
63 |
+
- Follow up with supplier to prevent future delays.
|
64 |
+
- Review safety protocols with the team.
|
65 |
+
Quote:
|
66 |
+
- Safety first, progress always—keep the momentum!
|
67 |
+
|
68 |
+
Now, generate the checklist, suggestions, and quote for the following inputs:
|
69 |
+
|
70 |
Inputs:
|
71 |
Role: {role}
|
72 |
Project: {project_id}
|
73 |
Milestones: {milestones}
|
74 |
Reflection: {reflection}
|
75 |
|
76 |
+
Generate the response in the same format as the examples above."""
|
77 |
|
78 |
# Cache reset check
|
79 |
last_reset = datetime.date.today()
|
|
|
85 |
memory.clear()
|
86 |
last_reset = today
|
87 |
|
88 |
+
# Cached generation function with improved parsing and context-aware fallbacks
|
89 |
@memory.cache
|
90 |
def generate_outputs(role, project_id, milestones, reflection):
|
91 |
reset_cache_if_new_day()
|
|
|
102 |
reflection=reflection
|
103 |
)
|
104 |
|
105 |
+
# Tokenize with attention_mask
|
106 |
+
inputs = tokenizer(
|
107 |
+
prompt,
|
108 |
+
return_tensors="pt",
|
109 |
+
max_length=512,
|
110 |
+
truncation=True,
|
111 |
+
padding=True,
|
112 |
+
return_attention_mask=True
|
113 |
+
)
|
114 |
+
|
115 |
+
# Generate with attention_mask
|
116 |
outputs = model.generate(
|
117 |
inputs["input_ids"],
|
118 |
+
attention_mask=inputs["attention_mask"],
|
119 |
+
max_length=1500,
|
120 |
num_return_sequences=1,
|
121 |
no_repeat_ngram_size=2,
|
122 |
do_sample=True,
|
123 |
top_p=0.9,
|
124 |
+
temperature=0.8,
|
125 |
+
pad_token_id=tokenizer.eos_token_id
|
126 |
)
|
127 |
|
128 |
# Decode generated text
|
|
|
158 |
if not quote:
|
159 |
quote = "No quote generated."
|
160 |
|
161 |
+
# Context-aware fallbacks based on inputs
|
162 |
if checklist == "No checklist generated.":
|
163 |
+
checklist_items = []
|
164 |
+
milestone_list = [m.strip() for m in milestones.split(",")]
|
165 |
+
for i, milestone in enumerate(milestone_list, 1):
|
166 |
+
checklist_items.append(f"- {milestone} by {8 + i*2} AM.")
|
167 |
+
checklist_items.append("- Check equipment status before end of day.")
|
168 |
+
checklist = "\n".join(checklist_items)
|
169 |
+
|
170 |
if suggestions == "No suggestions generated.":
|
171 |
+
suggestions_items = []
|
172 |
+
if "equipment issues" in reflection.lower():
|
173 |
+
suggestions_items.append("- Schedule equipment maintenance to avoid future delays.")
|
174 |
+
if "suppliers" in reflection.lower():
|
175 |
+
suggestions_items.append("- Set up a morning call with suppliers to confirm timelines.")
|
176 |
+
suggestions_items.append("- Brief the team on tomorrow’s goals during the daily huddle.")
|
177 |
+
suggestions = "\n".join(suggestions_items if suggestions_items else ["- Coordinate with the team.", "- Plan for contingencies."])
|
178 |
+
|
179 |
if quote == "No quote generated.":
|
180 |
+
quote = "- Keep building—every step forward counts!"
|
181 |
|
182 |
return checklist, suggestions, quote
|
183 |
|
184 |
+
# Gradio interface
|
185 |
def create_interface():
|
186 |
with gr.Blocks() as demo:
|
187 |
gr.Markdown("# Construction Supervisor AI Coach")
|