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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
if __name__ == "__main__":
|
| 26 |
-
|
| 27 |
-
app.run(host="0.0.0.0", port=5000)
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
import logging
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
from simple_salesforce import Salesforce
|
| 8 |
+
from flask import Flask, jsonify, request, render_template, redirect, url_for
|
| 9 |
|
| 10 |
+
# Configure logging
|
| 11 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 12 |
+
logger = logging.getLogger(__name__)
|
| 13 |
|
| 14 |
+
# Load environment variables
|
| 15 |
+
load_dotenv()
|
| 16 |
+
|
| 17 |
+
# Hugging Face API configuration
|
| 18 |
+
HUGGING_FACE_API_URL = os.getenv("HUGGING_FACE_API_URL", "https://api-inference.huggingface.co/models/distilgpt2")
|
| 19 |
+
HUGGING_FACE_API_TOKEN = os.getenv("HUGGING_FACE_API_TOKEN")
|
| 20 |
+
|
| 21 |
+
# Salesforce configuration
|
| 22 |
+
SALESFORCE_USERNAME = os.getenv("SALESFORCE_USERNAME")
|
| 23 |
+
SALESFORCE_PASSWORD = os.getenv("SALESFORCE_PASSWORD")
|
| 24 |
+
SALESFORCE_SECURITY_TOKEN = os.getenv("SALESFORCE_SECURITY_TOKEN")
|
| 25 |
+
SALESFORCE_DOMAIN = os.getenv("SALESFORCE_DOMAIN", "login")
|
| 26 |
+
|
| 27 |
+
# Validate environment variables
|
| 28 |
+
if not HUGGING_FACE_API_TOKEN:
|
| 29 |
+
logger.error("HUGGING_FACE_API_TOKEN is not set")
|
| 30 |
+
raise ValueError("HUGGING_FACE_API_TOKEN environment variable is not set")
|
| 31 |
+
if not HUGGING_FACE_API_URL.startswith("https://api-inference.huggingface.co/models/"):
|
| 32 |
+
logger.error("Invalid HUGGING_FACE_API_URL: %s", HUGGING_FACE_API_URL)
|
| 33 |
+
raise ValueError("HUGGING_FACE_API_URL must point to a valid Hugging Face model")
|
| 34 |
+
if not all([SALESFORCE_USERNAME, SALESFORCE_PASSWORD, SALESFORCE_SECURITY_TOKEN]):
|
| 35 |
+
logger.error("Salesforce credentials are incomplete")
|
| 36 |
+
raise ValueError("Salesforce credentials must be set")
|
| 37 |
+
|
| 38 |
+
# Initialize Flask app
|
| 39 |
app = Flask(__name__)
|
| 40 |
|
| 41 |
+
def generate_coaching_output(data):
|
| 42 |
+
"""
|
| 43 |
+
Generate daily checklist and tips using Hugging Face LLM.
|
| 44 |
+
"""
|
| 45 |
+
logger.info("Generating coaching output for supervisor %s", data['supervisor_id'])
|
| 46 |
+
milestones_json = json.dumps(data['milestones'], indent=2)
|
| 47 |
+
prompt = f"""
|
| 48 |
+
You are an AI Coach for construction site supervisors. Based on the following data, generate a daily checklist, three focus tips, and a motivational quote. Ensure outputs are concise, actionable, and tailored to the supervisor's role, project status, and reflection log.
|
| 49 |
+
|
| 50 |
+
Supervisor Role: {data['role']}
|
| 51 |
+
Project Milestones: {milestones_json}
|
| 52 |
+
Reflection Log: {data['reflection_log']}
|
| 53 |
+
Weather: {data['weather']}
|
| 54 |
+
|
| 55 |
+
Format the response as JSON:
|
| 56 |
+
{{
|
| 57 |
+
"checklist": ["item1", "item2", ...],
|
| 58 |
+
"tips": ["tip1", "tip2", "tip3"],
|
| 59 |
+
"quote": "motivational quote"
|
| 60 |
+
}}
|
| 61 |
+
"""
|
| 62 |
+
|
| 63 |
+
headers = {
|
| 64 |
+
"Authorization": f"Bearer {HUGGING_FACE_API_TOKEN}",
|
| 65 |
+
"Content-Type": "application/json"
|
| 66 |
+
}
|
| 67 |
+
payload = {
|
| 68 |
+
"inputs": prompt,
|
| 69 |
+
"parameters": {
|
| 70 |
+
"max_length": 200,
|
| 71 |
+
"temperature": 0.7,
|
| 72 |
+
"top_p": 0.9
|
| 73 |
+
}
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
try:
|
| 77 |
+
response = requests.post(HUGGING_FACE_API_URL, headers=headers, json=payload, timeout=5)
|
| 78 |
+
response.raise_for_status()
|
| 79 |
+
result = response.json()
|
| 80 |
+
generated_text = result[0]["generated_text"] if isinstance(result, list) else result["generated_text"]
|
| 81 |
+
|
| 82 |
+
start_idx = generated_text.find('{')
|
| 83 |
+
end_idx = generated_text.rfind('}') + 1
|
| 84 |
+
if start_idx == -1 or end_idx == 0:
|
| 85 |
+
logger.error("No valid JSON found in LLM output")
|
| 86 |
+
raise ValueError("No valid JSON found in LLM output")
|
| 87 |
+
|
| 88 |
+
json_str = generated_text[start_idx:end_idx]
|
| 89 |
+
output = json.loads(json_str)
|
| 90 |
+
logger.info("Successfully generated coaching output")
|
| 91 |
+
return output
|
| 92 |
+
|
| 93 |
+
except requests.exceptions.HTTPError as e:
|
| 94 |
+
logger.error("Hugging Face API HTTP error: %s", e)
|
| 95 |
+
return None
|
| 96 |
+
except (json.JSONDecodeError, ValueError) as e:
|
| 97 |
+
logger.error("Error parsing LLM output: %s", e)
|
| 98 |
+
return None
|
| 99 |
+
except Exception as e:
|
| 100 |
+
logger.error("Unexpected error in Hugging Face API call: %s", e)
|
| 101 |
+
return None
|
| 102 |
+
|
| 103 |
+
def save_to_salesforce(output, supervisor_id, project_id):
|
| 104 |
+
"""
|
| 105 |
+
Save coaching output to Salesforce Supervisor_AI_Coaching__c object.
|
| 106 |
+
"""
|
| 107 |
+
if not output:
|
| 108 |
+
logger.error("No coaching output to save")
|
| 109 |
+
return False
|
| 110 |
+
|
| 111 |
+
try:
|
| 112 |
+
sf = Salesforce(
|
| 113 |
+
username=SALESFORCE_USERNAME,
|
| 114 |
+
password=SALESFORCE_PASSWORD,
|
| 115 |
+
security_token=SALESFORCE_SECURITY_TOKEN,
|
| 116 |
+
domain=SALESFORCE_DOMAIN
|
| 117 |
+
)
|
| 118 |
+
logger.info("Connected to Salesforce")
|
| 119 |
+
|
| 120 |
+
coaching_record = {
|
| 121 |
+
"Supervisor_ID__c": supervisor_id,
|
| 122 |
+
"Project_ID__c": project_id,
|
| 123 |
+
"Daily_Checklist__c": "\n".join(output["checklist"]),
|
| 124 |
+
"Suggested_Tips__c": "\n".join(output["tips"]),
|
| 125 |
+
"Quote__c": output["quote"],
|
| 126 |
+
"Generated_Date__c": datetime.now().strftime("%Y-%m-%d")
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
sf.Supervisor_AI_Coaching__c.upsert(
|
| 130 |
+
f"Supervisor_ID__c/{supervisor_id}_{datetime.now().strftime('%Y-%m-%d')}",
|
| 131 |
+
coaching_record
|
| 132 |
+
)
|
| 133 |
+
logger.info("Successfully saved coaching record to Salesforce for supervisor %s", supervisor_id)
|
| 134 |
+
return True
|
| 135 |
+
|
| 136 |
+
except Exception as e:
|
| 137 |
+
logger.error("Salesforce error: %s", e)
|
| 138 |
+
return False
|
| 139 |
+
|
| 140 |
+
@app.route('/', methods=['GET'])
|
| 141 |
+
def redirect_to_ui():
|
| 142 |
+
"""
|
| 143 |
+
Redirect root URL to the UI.
|
| 144 |
+
"""
|
| 145 |
+
return redirect(url_for('ui'))
|
| 146 |
+
|
| 147 |
+
@app.route('/ui', methods=['GET'])
|
| 148 |
+
def ui():
|
| 149 |
+
"""
|
| 150 |
+
Serve the HTML user interface.
|
| 151 |
+
"""
|
| 152 |
+
return render_template('index.html')
|
| 153 |
+
|
| 154 |
+
@app.route('/generate', methods=['POST'])
|
| 155 |
+
def generate_endpoint():
|
| 156 |
+
"""
|
| 157 |
+
Endpoint to generate coaching output based on supervisor data.
|
| 158 |
+
"""
|
| 159 |
+
try:
|
| 160 |
+
data = request.get_json()
|
| 161 |
+
if not data or not all(key in data for key in ['supervisor_id', 'role', 'project_id', 'milestones', 'reflection_log', 'weather']):
|
| 162 |
+
return jsonify({"status": "error", "message": "Invalid or missing supervisor data"}), 400
|
| 163 |
|
| 164 |
+
coaching_output = generate_coaching_output(data)
|
| 165 |
+
if coaching_output:
|
| 166 |
+
success = save_to_salesforce(coaching_output, data["supervisor_id"], data["project_id"])
|
| 167 |
+
if success:
|
| 168 |
+
return jsonify({"status": "success", "output": coaching_output}), 200
|
| 169 |
+
else:
|
| 170 |
+
return jsonify({"status": "error", "message": "Failed to save to Salesforce"}), 500
|
| 171 |
+
else:
|
| 172 |
+
return jsonify({"status": "error", "message": "Failed to generate coaching output"}), 500
|
| 173 |
+
except Exception as e:
|
| 174 |
+
logger.error("Error in generate endpoint: %s", e)
|
| 175 |
+
return jsonify({"status": "error", "message": str(e)}), 500
|
| 176 |
|
| 177 |
+
@app.route('/health', methods=['GET'])
|
| 178 |
+
def health_check():
|
| 179 |
+
"""
|
| 180 |
+
Health check endpoint.
|
| 181 |
+
"""
|
| 182 |
+
return jsonify({"status": "healthy", "message": "Application is running"}), 200
|
| 183 |
|
| 184 |
if __name__ == "__main__":
|
| 185 |
+
app.run(host="0.0.0.0", port=int(os.getenv("PORT", 7860)))
|
|
|