Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -19,7 +19,7 @@ KPI_FLAG_DEFAULT = os.getenv('KPI_FLAG', 'True') == 'True'
|
|
19 |
ENGAGEMENT_SCORE_DEFAULT = float(os.getenv('ENGAGEMENT_SCORE', '85.0'))
|
20 |
|
21 |
# Load model and tokenizer
|
22 |
-
model_name = "distilgpt2"
|
23 |
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True)
|
24 |
model = AutoModelForCausalLM.from_pretrained(model_name, low_cpu_mem_usage=True)
|
25 |
|
@@ -145,7 +145,7 @@ def save_to_salesforce(role, project_id, milestones, reflection, checklist, sugg
|
|
145 |
except Exception as e:
|
146 |
print(f"β Error saving to Salesforce: {e}")
|
147 |
|
148 |
-
# Generate Function
|
149 |
def generate_outputs(role, supervisor_name, project_id, milestones, reflection):
|
150 |
if not all([role, supervisor_name, project_id, milestones, reflection]):
|
151 |
return "β Please fill all fields.", "", ""
|
@@ -162,35 +162,48 @@ def generate_outputs(role, supervisor_name, project_id, milestones, reflection):
|
|
162 |
with torch.no_grad():
|
163 |
outputs = model.generate(
|
164 |
inputs['input_ids'],
|
165 |
-
max_new_tokens=
|
166 |
no_repeat_ngram_size=2,
|
167 |
do_sample=True,
|
168 |
top_p=0.9,
|
169 |
-
temperature=0.
|
170 |
pad_token_id=tokenizer.pad_token_id
|
171 |
)
|
172 |
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
173 |
except Exception as e:
|
174 |
print(f"β οΈ Generation error: {e}")
|
175 |
-
|
|
|
|
|
|
|
|
|
|
|
176 |
|
177 |
def extract_between(text, start, end):
|
178 |
s = text.find(start)
|
179 |
e = text.find(end, s) if end else len(text)
|
180 |
-
return text[s + len(start):e].strip() if s != -1 else "
|
181 |
|
182 |
checklist = extract_between(result, "Checklist:\n", "Suggestions:")
|
183 |
suggestions = extract_between(result, "Suggestions:\n", "Quote:")
|
184 |
quote = extract_between(result, "Quote:\n", None)
|
185 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
186 |
save_to_salesforce(role, project_id, milestones, reflection, checklist, suggestions, quote, supervisor_name)
|
187 |
return checklist, suggestions, quote
|
188 |
|
189 |
# Dashboard button function
|
190 |
def open_dashboard():
|
191 |
-
# Replace this with your actual Salesforce dashboard URL
|
192 |
salesforce_dashboard_url = "https://your-salesforce-instance.lightning.force.com/lightning/page/home"
|
193 |
-
# Return HTML link that opens in a new tab
|
194 |
return f'<a href="{salesforce_dashboard_url}" target="_blank" rel="noopener noreferrer" style="font-size:16px;">Open Salesforce Dashboard</a>'
|
195 |
|
196 |
# Interface
|
@@ -211,12 +224,12 @@ def create_interface():
|
|
211 |
generate = gr.Button("Generate")
|
212 |
clear = gr.Button("Clear")
|
213 |
refresh = gr.Button("π Refresh Roles")
|
214 |
-
dashboard_btn = gr.Button("Dashboard")
|
215 |
|
216 |
checklist_output = gr.Textbox(label="β
Daily Checklist")
|
217 |
suggestions_output = gr.Textbox(label="π‘ Focus Suggestions")
|
218 |
quote_output = gr.Textbox(label="β¨ Motivational Quote")
|
219 |
-
dashboard_link = gr.HTML("")
|
220 |
|
221 |
role.change(fn=lambda r: gr.update(choices=get_supervisor_name_by_role(r)), inputs=role, outputs=supervisor_name)
|
222 |
supervisor_name.change(fn=get_projects_for_supervisor, inputs=supervisor_name, outputs=project_id)
|
@@ -236,4 +249,4 @@ def create_interface():
|
|
236 |
|
237 |
if __name__ == "__main__":
|
238 |
app = create_interface()
|
239 |
-
app.launch()
|
|
|
19 |
ENGAGEMENT_SCORE_DEFAULT = float(os.getenv('ENGAGEMENT_SCORE', '85.0'))
|
20 |
|
21 |
# Load model and tokenizer
|
22 |
+
model_name = "distilgpt2"
|
23 |
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True)
|
24 |
model = AutoModelForCausalLM.from_pretrained(model_name, low_cpu_mem_usage=True)
|
25 |
|
|
|
145 |
except Exception as e:
|
146 |
print(f"β Error saving to Salesforce: {e}")
|
147 |
|
148 |
+
# Generate Function (Updated)
|
149 |
def generate_outputs(role, supervisor_name, project_id, milestones, reflection):
|
150 |
if not all([role, supervisor_name, project_id, milestones, reflection]):
|
151 |
return "β Please fill all fields.", "", ""
|
|
|
162 |
with torch.no_grad():
|
163 |
outputs = model.generate(
|
164 |
inputs['input_ids'],
|
165 |
+
max_new_tokens=100, # Reduced for faster generation
|
166 |
no_repeat_ngram_size=2,
|
167 |
do_sample=True,
|
168 |
top_p=0.9,
|
169 |
+
temperature=0.7, # Slightly lowered for more focused output
|
170 |
pad_token_id=tokenizer.pad_token_id
|
171 |
)
|
172 |
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
173 |
except Exception as e:
|
174 |
print(f"β οΈ Generation error: {e}")
|
175 |
+
# Fallback values if generation fails
|
176 |
+
checklist = "- Conduct safety briefing\n- Inspect equipment functionality"
|
177 |
+
suggestions = "- Monitor team coordination\n- Review safety protocols"
|
178 |
+
quote = "Success is the sum of small efforts, repeated day in and day out."
|
179 |
+
save_to_salesforce(role, project_id, milestones, reflection, checklist, suggestions, quote, supervisor_name)
|
180 |
+
return checklist, suggestions, quote
|
181 |
|
182 |
def extract_between(text, start, end):
|
183 |
s = text.find(start)
|
184 |
e = text.find(end, s) if end else len(text)
|
185 |
+
return text[s + len(start):e].strip() if s != -1 else ""
|
186 |
|
187 |
checklist = extract_between(result, "Checklist:\n", "Suggestions:")
|
188 |
suggestions = extract_between(result, "Suggestions:\n", "Quote:")
|
189 |
quote = extract_between(result, "Quote:\n", None)
|
190 |
|
191 |
+
# Ensure checklist and suggestions are never blank
|
192 |
+
if not checklist.strip():
|
193 |
+
checklist = "- Conduct safety briefing based on role\n- Verify milestone progress"
|
194 |
+
if not suggestions.strip():
|
195 |
+
suggestions = "- Address concerns from reflection log\n- Focus on team communication"
|
196 |
+
|
197 |
+
# Fallback quote if empty
|
198 |
+
if not quote.strip():
|
199 |
+
quote = "Success is the sum of small efforts, repeated day in and day out."
|
200 |
+
|
201 |
save_to_salesforce(role, project_id, milestones, reflection, checklist, suggestions, quote, supervisor_name)
|
202 |
return checklist, suggestions, quote
|
203 |
|
204 |
# Dashboard button function
|
205 |
def open_dashboard():
|
|
|
206 |
salesforce_dashboard_url = "https://your-salesforce-instance.lightning.force.com/lightning/page/home"
|
|
|
207 |
return f'<a href="{salesforce_dashboard_url}" target="_blank" rel="noopener noreferrer" style="font-size:16px;">Open Salesforce Dashboard</a>'
|
208 |
|
209 |
# Interface
|
|
|
224 |
generate = gr.Button("Generate")
|
225 |
clear = gr.Button("Clear")
|
226 |
refresh = gr.Button("π Refresh Roles")
|
227 |
+
dashboard_btn = gr.Button("Dashboard")
|
228 |
|
229 |
checklist_output = gr.Textbox(label="β
Daily Checklist")
|
230 |
suggestions_output = gr.Textbox(label="π‘ Focus Suggestions")
|
231 |
quote_output = gr.Textbox(label="β¨ Motivational Quote")
|
232 |
+
dashboard_link = gr.HTML("")
|
233 |
|
234 |
role.change(fn=lambda r: gr.update(choices=get_supervisor_name_by_role(r)), inputs=role, outputs=supervisor_name)
|
235 |
supervisor_name.change(fn=get_projects_for_supervisor, inputs=supervisor_name, outputs=project_id)
|
|
|
249 |
|
250 |
if __name__ == "__main__":
|
251 |
app = create_interface()
|
252 |
+
app.launch()
|