Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -33,6 +33,43 @@ if not all([SALESFORCE_USERNAME, SALESFORCE_PASSWORD, SALESFORCE_SECURITY_TOKEN]
|
|
| 33 |
# Initialize Flask app
|
| 34 |
app = Flask(__name__)
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
def generate_coaching_output(data):
|
| 37 |
"""
|
| 38 |
Generate daily checklist and tips using Hugging Face LLM.
|
|
@@ -95,9 +132,9 @@ Format the response as JSON:
|
|
| 95 |
logger.error("Unexpected error in Hugging Face API call: %s", e)
|
| 96 |
return None
|
| 97 |
|
| 98 |
-
def save_to_salesforce(
|
| 99 |
"""
|
| 100 |
-
Save coaching output to Salesforce Supervisor_AI_Coaching__c object.
|
| 101 |
"""
|
| 102 |
if not output:
|
| 103 |
logger.error("No coaching output to save")
|
|
@@ -113,8 +150,12 @@ def save_to_salesforce(output, supervisor_id, project_id):
|
|
| 113 |
logger.info("Connected to Salesforce")
|
| 114 |
|
| 115 |
coaching_record = {
|
| 116 |
-
"Supervisor_ID__c": supervisor_id,
|
| 117 |
-
"Project_ID__c": project_id,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
"Daily_Checklist__c": "\n".join(output["checklist"]),
|
| 119 |
"Suggested_Tips__c": "\n".join(output["tips"]),
|
| 120 |
"Quote__c": output["quote"],
|
|
@@ -122,10 +163,10 @@ def save_to_salesforce(output, supervisor_id, project_id):
|
|
| 122 |
}
|
| 123 |
|
| 124 |
sf.Supervisor_AI_Coaching__c.upsert(
|
| 125 |
-
f"Supervisor_ID__c/{supervisor_id}_{datetime.now().strftime('%Y-%m-%d')}",
|
| 126 |
coaching_record
|
| 127 |
)
|
| 128 |
-
logger.info("Successfully saved coaching record to Salesforce for supervisor %s", supervisor_id)
|
| 129 |
return True
|
| 130 |
|
| 131 |
except Exception as e:
|
|
@@ -139,35 +180,66 @@ def redirect_to_ui():
|
|
| 139 |
"""
|
| 140 |
return redirect(url_for('ui'))
|
| 141 |
|
| 142 |
-
@app.route('/ui', methods=['GET'])
|
| 143 |
def ui():
|
| 144 |
"""
|
| 145 |
-
Serve the HTML user interface.
|
| 146 |
"""
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
"""
|
| 152 |
-
Endpoint to generate coaching output based on supervisor data.
|
| 153 |
-
"""
|
| 154 |
-
try:
|
| 155 |
-
data = request.get_json()
|
| 156 |
-
if not data or not all(key in data for key in ['supervisor_id', 'role', 'project_id', 'milestones', 'reflection_log', 'weather']):
|
| 157 |
-
return jsonify({"status": "error", "message": "Invalid or missing supervisor data"}), 400
|
| 158 |
-
|
| 159 |
-
coaching_output = generate_coaching_output(data)
|
| 160 |
-
if coaching_output:
|
| 161 |
-
success = save_to_salesforce(coaching_output, data["supervisor_id"], data["project_id"])
|
| 162 |
-
if success:
|
| 163 |
-
return jsonify({"status": "success", "output": coaching_output}), 200
|
| 164 |
-
else:
|
| 165 |
-
return jsonify({"status": "error", "message": "Failed to save to Salesforce"}), 500
|
| 166 |
else:
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
|
| 172 |
@app.route('/health', methods=['GET'])
|
| 173 |
def health_check():
|
|
|
|
| 33 |
# Initialize Flask app
|
| 34 |
app = Flask(__name__)
|
| 35 |
|
| 36 |
+
def fetch_input_from_salesforce(supervisor_id, project_id):
|
| 37 |
+
"""
|
| 38 |
+
Fetch input data from Salesforce for a given supervisor and project.
|
| 39 |
+
"""
|
| 40 |
+
try:
|
| 41 |
+
sf = Salesforce(
|
| 42 |
+
username=SALESFORCE_USERNAME,
|
| 43 |
+
password=SALESFORCE_PASSWORD,
|
| 44 |
+
security_token=SALESFORCE_SECURITY_TOKEN,
|
| 45 |
+
domain=SALESFORCE_DOMAIN
|
| 46 |
+
)
|
| 47 |
+
logger.info("Connected to Salesforce for input fetch")
|
| 48 |
+
|
| 49 |
+
query = f"""
|
| 50 |
+
SELECT Role__c, Weather__c, Milestones__c, Reflection_Log__c
|
| 51 |
+
FROM Supervisor_AI_Coaching__c
|
| 52 |
+
WHERE Supervisor_ID__c = '{supervisor_id}' AND Project_ID__c = '{project_id}'
|
| 53 |
+
ORDER BY Generated_Date__c DESC
|
| 54 |
+
LIMIT 1
|
| 55 |
+
"""
|
| 56 |
+
result = sf.query(query)
|
| 57 |
+
if result['totalSize'] > 0:
|
| 58 |
+
record = result['records'][0]
|
| 59 |
+
return {
|
| 60 |
+
'role': record['Role__c'] or '',
|
| 61 |
+
'weather': record['Weather__c'] or '',
|
| 62 |
+
'milestones': record['Milestones__c'] or '',
|
| 63 |
+
'reflection': record['Reflection_Log__c'] or ''
|
| 64 |
+
}
|
| 65 |
+
else:
|
| 66 |
+
logger.info("No input data found for supervisor %s and project %s", supervisor_id, project_id)
|
| 67 |
+
return None
|
| 68 |
+
|
| 69 |
+
except Exception as e:
|
| 70 |
+
logger.error("Salesforce fetch error: %s", e)
|
| 71 |
+
return None
|
| 72 |
+
|
| 73 |
def generate_coaching_output(data):
|
| 74 |
"""
|
| 75 |
Generate daily checklist and tips using Hugging Face LLM.
|
|
|
|
| 132 |
logger.error("Unexpected error in Hugging Face API call: %s", e)
|
| 133 |
return None
|
| 134 |
|
| 135 |
+
def save_to_salesforce(data, output):
|
| 136 |
"""
|
| 137 |
+
Save input data and coaching output to Salesforce Supervisor_AI_Coaching__c object.
|
| 138 |
"""
|
| 139 |
if not output:
|
| 140 |
logger.error("No coaching output to save")
|
|
|
|
| 150 |
logger.info("Connected to Salesforce")
|
| 151 |
|
| 152 |
coaching_record = {
|
| 153 |
+
"Supervisor_ID__c": data['supervisor_id'],
|
| 154 |
+
"Project_ID__c": data['project_id'],
|
| 155 |
+
"Role__c": data['role'],
|
| 156 |
+
"Weather__c": data['weather'],
|
| 157 |
+
"Milestones__c": ", ".join(data['milestones']),
|
| 158 |
+
"Reflection_Log__c": data['reflection_log'],
|
| 159 |
"Daily_Checklist__c": "\n".join(output["checklist"]),
|
| 160 |
"Suggested_Tips__c": "\n".join(output["tips"]),
|
| 161 |
"Quote__c": output["quote"],
|
|
|
|
| 163 |
}
|
| 164 |
|
| 165 |
sf.Supervisor_AI_Coaching__c.upsert(
|
| 166 |
+
f"Supervisor_ID__c/{data['supervisor_id']}_{datetime.now().strftime('%Y-%m-%d')}",
|
| 167 |
coaching_record
|
| 168 |
)
|
| 169 |
+
logger.info("Successfully saved coaching record to Salesforce for supervisor %s", data['supervisor_id'])
|
| 170 |
return True
|
| 171 |
|
| 172 |
except Exception as e:
|
|
|
|
| 180 |
"""
|
| 181 |
return redirect(url_for('ui'))
|
| 182 |
|
| 183 |
+
@app.route('/ui', methods=['GET', 'POST'])
|
| 184 |
def ui():
|
| 185 |
"""
|
| 186 |
+
Serve the HTML user interface and handle form submissions.
|
| 187 |
"""
|
| 188 |
+
form_data = {}
|
| 189 |
+
output = {}
|
| 190 |
+
error = ""
|
| 191 |
+
|
| 192 |
+
if request.method == 'POST':
|
| 193 |
+
action = request.form.get('action')
|
| 194 |
+
form_data = {
|
| 195 |
+
'supervisor_id': request.form.get('supervisor_id', ''),
|
| 196 |
+
'project_id': request.form.get('project_id', ''),
|
| 197 |
+
'role': '',
|
| 198 |
+
'weather': '',
|
| 199 |
+
'milestones': '',
|
| 200 |
+
'reflection': ''
|
| 201 |
+
}
|
| 202 |
|
| 203 |
+
if not all([form_data['supervisor_id'], form_data['project_id']]):
|
| 204 |
+
error = "Error: Supervisor ID and Project ID are required."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
else:
|
| 206 |
+
if action == 'fetch':
|
| 207 |
+
# Fetch input data from Salesforce
|
| 208 |
+
input_data = fetch_input_from_salesforce(form_data['supervisor_id'], form_data['project_id'])
|
| 209 |
+
if input_data:
|
| 210 |
+
form_data.update(input_data)
|
| 211 |
+
else:
|
| 212 |
+
error = "No input data found in Salesforce for the given Supervisor ID and Project ID."
|
| 213 |
+
|
| 214 |
+
elif action == 'generate':
|
| 215 |
+
# Fetch input data from Salesforce
|
| 216 |
+
input_data = fetch_input_from_salesforce(form_data['supervisor_id'], form_data['project_id'])
|
| 217 |
+
if not input_data:
|
| 218 |
+
error = "No input data found in Salesforce for the given Supervisor ID and Project ID."
|
| 219 |
+
else:
|
| 220 |
+
form_data.update(input_data)
|
| 221 |
+
# Prepare data for generation
|
| 222 |
+
data = {
|
| 223 |
+
'supervisor_id': form_data['supervisor_id'],
|
| 224 |
+
'project_id': form_data['project_id'],
|
| 225 |
+
'role': form_data['role'],
|
| 226 |
+
'milestones': [m.strip() for m in form_data['milestones'].split(',') if m.strip()] if form_data['milestones'] else [],
|
| 227 |
+
'reflection_log': form_data['reflection'],
|
| 228 |
+
'weather': form_data['weather']
|
| 229 |
+
}
|
| 230 |
+
# Generate output
|
| 231 |
+
coaching_output = generate_coaching_output(data)
|
| 232 |
+
if coaching_output:
|
| 233 |
+
# Save both input and output to Salesforce
|
| 234 |
+
success = save_to_salesforce(data, coaching_output)
|
| 235 |
+
if success:
|
| 236 |
+
output = coaching_output
|
| 237 |
+
else:
|
| 238 |
+
error = "Error: Failed to save to Salesforce."
|
| 239 |
+
else:
|
| 240 |
+
error = "Error: Failed to generate coaching output."
|
| 241 |
+
|
| 242 |
+
return render_template('index.html', form_data=form_data, output=output, error=error)
|
| 243 |
|
| 244 |
@app.route('/health', methods=['GET'])
|
| 245 |
def health_check():
|