Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
import logging
|
|
|
|
| 3 |
from datetime import datetime
|
| 4 |
from flask import Flask, render_template, request, redirect, url_for, session, jsonify, send_file
|
| 5 |
from simple_salesforce import Salesforce
|
|
@@ -22,18 +23,80 @@ app = Flask(__name__)
|
|
| 22 |
app.secret_key = os.getenv('FLASK_SECRET_KEY', 'default-secret-key')
|
| 23 |
|
| 24 |
# Salesforce connection
|
|
|
|
| 25 |
try:
|
| 26 |
sf = Salesforce(
|
| 27 |
username=os.getenv('SALESFORCE_USERNAME'),
|
| 28 |
password=os.getenv('SALESFORCE_PASSWORD'),
|
| 29 |
security_token=os.getenv('SALESFORCE_SECURITY_TOKEN'),
|
| 30 |
-
domain=os.getenv('SALESFORCE_DOMAIN', '
|
| 31 |
)
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
except Exception as e:
|
| 34 |
logging.error(f"Failed to connect to Salesforce: {str(e)}")
|
| 35 |
sf = None
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
@app.route('/')
|
| 38 |
def home():
|
| 39 |
if 'supervisor_name' not in session:
|
|
@@ -56,7 +119,7 @@ def login():
|
|
| 56 |
|
| 57 |
if not sf:
|
| 58 |
logging.error("Salesforce connection not available during login")
|
| 59 |
-
return
|
| 60 |
|
| 61 |
try:
|
| 62 |
query = f"SELECT Id, Name, Password__c FROM Supervisor_AI_Coaching__c WHERE Name = '{username}'"
|
|
@@ -93,7 +156,7 @@ def signup():
|
|
| 93 |
|
| 94 |
if not sf:
|
| 95 |
logging.error("Salesforce connection not available during signup")
|
| 96 |
-
return
|
| 97 |
|
| 98 |
try:
|
| 99 |
existing_user = sf.query(f"SELECT Id FROM Supervisor_AI_Coaching__c WHERE Name = '{username}'")
|
|
@@ -222,19 +285,9 @@ def generate_coaching():
|
|
| 222 |
return jsonify({'status': 'error', 'message': 'Salesforce connection unavailable'}), 500
|
| 223 |
|
| 224 |
try:
|
| 225 |
-
#
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
'Review project milestones',
|
| 229 |
-
'Conduct team check-in',
|
| 230 |
-
'Update resource allocation'
|
| 231 |
-
],
|
| 232 |
-
'tips': [
|
| 233 |
-
'Set clear deadlines',
|
| 234 |
-
'Encourage team feedback',
|
| 235 |
-
'Monitor progress daily'
|
| 236 |
-
]
|
| 237 |
-
}
|
| 238 |
|
| 239 |
if supervisor_name != 'GUEST':
|
| 240 |
query = f"SELECT Id FROM Supervisor_AI_Coaching__c WHERE Name = '{supervisor_name}'"
|
|
@@ -325,7 +378,6 @@ def download_pdf():
|
|
| 325 |
buffer.seek(0)
|
| 326 |
|
| 327 |
if supervisor_name != 'GUEST':
|
| 328 |
-
# Update the download link (placeholder for now)
|
| 329 |
record_id = result['records'][0]['Id']
|
| 330 |
sf.Supervisor_AI_Coaching__c.update(record_id, {
|
| 331 |
'Download_Link__c': 'https://example.com/report.pdf'
|
|
|
|
| 1 |
import os
|
| 2 |
import logging
|
| 3 |
+
import requests
|
| 4 |
from datetime import datetime
|
| 5 |
from flask import Flask, render_template, request, redirect, url_for, session, jsonify, send_file
|
| 6 |
from simple_salesforce import Salesforce
|
|
|
|
| 23 |
app.secret_key = os.getenv('FLASK_SECRET_KEY', 'default-secret-key')
|
| 24 |
|
| 25 |
# Salesforce connection
|
| 26 |
+
sf = None
|
| 27 |
try:
|
| 28 |
sf = Salesforce(
|
| 29 |
username=os.getenv('SALESFORCE_USERNAME'),
|
| 30 |
password=os.getenv('SALESFORCE_PASSWORD'),
|
| 31 |
security_token=os.getenv('SALESFORCE_SECURITY_TOKEN'),
|
| 32 |
+
domain=os.getenv('SALESFORCE_DOMAIN', 'login')
|
| 33 |
)
|
| 34 |
+
# Verify connection by querying Salesforce metadata
|
| 35 |
+
user_info = sf.query("SELECT Id, Name FROM User WHERE Username = '{}'".format(os.getenv('SALESFORCE_USERNAME')))
|
| 36 |
+
if user_info['totalSize'] > 0:
|
| 37 |
+
logging.info(f"Successfully connected to Salesforce org. User: {user_info['records'][0]['Name']}")
|
| 38 |
+
else:
|
| 39 |
+
logging.error("Salesforce connection established, but user not found in org")
|
| 40 |
except Exception as e:
|
| 41 |
logging.error(f"Failed to connect to Salesforce: {str(e)}")
|
| 42 |
sf = None
|
| 43 |
|
| 44 |
+
# Hugging Face API configuration
|
| 45 |
+
HUGGING_FACE_API_TOKEN = os.getenv('HUGGING_FACE_API_TOKEN')
|
| 46 |
+
HUGGING_FACE_API_URL = os.getenv('HUGGING_FACE_API_URL')
|
| 47 |
+
HUGGING_FACE_HEADERS = {
|
| 48 |
+
'Authorization': f'Bearer {HUGGING_FACE_API_TOKEN}',
|
| 49 |
+
'Content-Type': 'application/json'
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
def generate_coaching_output_with_hf(data):
|
| 53 |
+
try:
|
| 54 |
+
prompt = (
|
| 55 |
+
f"Generate a daily checklist and focus tips for a supervisor with the following details:\n"
|
| 56 |
+
f"Project ID: {data.get('project_id', 'N/A')}\n"
|
| 57 |
+
f"Engagement Score: {data.get('engagement_score', 0)}%\n"
|
| 58 |
+
f"KPI Flag: {'Active' if data.get('kpi_flag', False) else 'Inactive'}\n"
|
| 59 |
+
f"Reflection Log: {data.get('reflection_log', 'No reflections yet.')}\n\n"
|
| 60 |
+
"Provide a checklist (3 items) and focus tips (3 items) in a concise format."
|
| 61 |
+
)
|
| 62 |
+
payload = {
|
| 63 |
+
"inputs": prompt,
|
| 64 |
+
"parameters": {"max_length": 150, "num_return_sequences": 1}
|
| 65 |
+
}
|
| 66 |
+
response = requests.post(HUGGING_FACE_API_URL, headers=HUGGING_FACE_HEADERS, json=payload)
|
| 67 |
+
response.raise_for_status()
|
| 68 |
+
result = response.json()
|
| 69 |
+
generated_text = result[0]['generated_text'].split('\n')
|
| 70 |
+
|
| 71 |
+
checklist = []
|
| 72 |
+
tips = []
|
| 73 |
+
is_checklist = True
|
| 74 |
+
for line in generated_text:
|
| 75 |
+
line = line.strip()
|
| 76 |
+
if not line:
|
| 77 |
+
continue
|
| 78 |
+
if "focus tips" in line.lower():
|
| 79 |
+
is_checklist = False
|
| 80 |
+
continue
|
| 81 |
+
if is_checklist and len(checklist) < 3:
|
| 82 |
+
checklist.append(line.replace('- ', '').strip())
|
| 83 |
+
elif not is_checklist and len(tips) < 3:
|
| 84 |
+
tips.append(line.replace('- ', '').strip())
|
| 85 |
+
|
| 86 |
+
# Fallback if model output is incomplete
|
| 87 |
+
while len(checklist) < 3:
|
| 88 |
+
checklist.append(f"Placeholder Task {len(checklist) + 1}")
|
| 89 |
+
while len(tips) < 3:
|
| 90 |
+
tips.append(f"Placeholder Tip {len(tips) + 1}")
|
| 91 |
+
|
| 92 |
+
return {'checklist': checklist, 'tips': tips}
|
| 93 |
+
except Exception as e:
|
| 94 |
+
logging.error(f"Error generating coaching output with Hugging Face API: {str(e)}")
|
| 95 |
+
return {
|
| 96 |
+
'checklist': ['Review project milestones', 'Conduct team check-in', 'Update resource allocation'],
|
| 97 |
+
'tips': ['Set clear deadlines', 'Encourage team feedback', 'Monitor progress daily']
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
@app.route('/')
|
| 101 |
def home():
|
| 102 |
if 'supervisor_name' not in session:
|
|
|
|
| 119 |
|
| 120 |
if not sf:
|
| 121 |
logging.error("Salesforce connection not available during login")
|
| 122 |
+
return render_template('login.html', error="Salesforce connection unavailable")
|
| 123 |
|
| 124 |
try:
|
| 125 |
query = f"SELECT Id, Name, Password__c FROM Supervisor_AI_Coaching__c WHERE Name = '{username}'"
|
|
|
|
| 156 |
|
| 157 |
if not sf:
|
| 158 |
logging.error("Salesforce connection not available during signup")
|
| 159 |
+
return render_template('signup.html', error="Salesforce connection unavailable")
|
| 160 |
|
| 161 |
try:
|
| 162 |
existing_user = sf.query(f"SELECT Id FROM Supervisor_AI_Coaching__c WHERE Name = '{username}'")
|
|
|
|
| 285 |
return jsonify({'status': 'error', 'message': 'Salesforce connection unavailable'}), 500
|
| 286 |
|
| 287 |
try:
|
| 288 |
+
# Fetch supervisor data to pass to Hugging Face API
|
| 289 |
+
data = request.json
|
| 290 |
+
output = generate_coaching_output_with_hf(data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 291 |
|
| 292 |
if supervisor_name != 'GUEST':
|
| 293 |
query = f"SELECT Id FROM Supervisor_AI_Coaching__c WHERE Name = '{supervisor_name}'"
|
|
|
|
| 378 |
buffer.seek(0)
|
| 379 |
|
| 380 |
if supervisor_name != 'GUEST':
|
|
|
|
| 381 |
record_id = result['records'][0]['Id']
|
| 382 |
sf.Supervisor_AI_Coaching__c.update(record_id, {
|
| 383 |
'Download_Link__c': 'https://example.com/report.pdf'
|