Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import logging
|
|
| 2 |
import os
|
| 3 |
import uvicorn
|
| 4 |
from fastapi import FastAPI, HTTPException
|
|
|
|
| 5 |
from pydantic import BaseModel
|
| 6 |
from simple_salesforce import Salesforce
|
| 7 |
from contextlib import asynccontextmanager
|
|
@@ -97,6 +98,45 @@ def generate_coaching_data(project: dict, role: str) -> dict:
|
|
| 97 |
"engagementScore": engagement_score
|
| 98 |
}
|
| 99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
@app.post("/api/generate")
|
| 101 |
async def generate_coaching(request: ProjectRequest):
|
| 102 |
if sf is None:
|
|
@@ -104,7 +144,7 @@ async def generate_coaching(request: ProjectRequest):
|
|
| 104 |
raise HTTPException(status_code=500, detail="Salesforce connection not initialized")
|
| 105 |
|
| 106 |
try:
|
| 107 |
-
# Query Project__c by projectId
|
| 108 |
query = f"SELECT Id, Name, Project_Name__c, Milestones__c, Weather_Logs__c, Safety_Logs__c, Location__c, Supervisor_ID__c FROM Project__c WHERE Name = '{request.projectId}' LIMIT 1"
|
| 109 |
logger.info(f"Executing SOQL query: {query}")
|
| 110 |
result = sf.query(query)
|
|
@@ -136,6 +176,25 @@ async def generate_coaching(request: ProjectRequest):
|
|
| 136 |
"engagementScore": coaching_data['engagementScore']
|
| 137 |
}
|
| 138 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
logger.info(f"Generated coaching response: {response}")
|
| 140 |
return response
|
| 141 |
except Exception as e:
|
|
@@ -144,8 +203,13 @@ async def generate_coaching(request: ProjectRequest):
|
|
| 144 |
|
| 145 |
@app.get("/")
|
| 146 |
async def root():
|
| 147 |
-
logger.info("
|
| 148 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
# Start Uvicorn server for Hugging Face Spaces
|
| 151 |
logger.info("Starting Uvicorn server for Hugging Face Spaces")
|
|
|
|
| 2 |
import os
|
| 3 |
import uvicorn
|
| 4 |
from fastapi import FastAPI, HTTPException
|
| 5 |
+
from fastapi.responses import FileResponse
|
| 6 |
from pydantic import BaseModel
|
| 7 |
from simple_salesforce import Salesforce
|
| 8 |
from contextlib import asynccontextmanager
|
|
|
|
| 98 |
"engagementScore": engagement_score
|
| 99 |
}
|
| 100 |
|
| 101 |
+
@app.get("/api/latest-project")
|
| 102 |
+
async def get_latest_project():
|
| 103 |
+
"""Fetch the latest Project__c record based on CreatedDate."""
|
| 104 |
+
if sf is None:
|
| 105 |
+
logger.error("Salesforce connection not initialized")
|
| 106 |
+
raise HTTPException(status_code=500, detail="Salesforce connection not initialized")
|
| 107 |
+
|
| 108 |
+
try:
|
| 109 |
+
query = """
|
| 110 |
+
SELECT Id, Name, Project_Name__c, Milestones__c, Weather_Logs__c, Safety_Logs__c, Location__c, Supervisor_ID__c
|
| 111 |
+
FROM Project__c
|
| 112 |
+
ORDER BY CreatedDate DESC
|
| 113 |
+
LIMIT 1
|
| 114 |
+
"""
|
| 115 |
+
logger.info(f"Executing SOQL query for latest project: {query}")
|
| 116 |
+
result = sf.query(query)
|
| 117 |
+
|
| 118 |
+
if result['totalSize'] == 0:
|
| 119 |
+
logger.warning("No Project__c records found")
|
| 120 |
+
raise HTTPException(status_code=404, detail="No projects found")
|
| 121 |
+
|
| 122 |
+
project = result['records'][0]
|
| 123 |
+
logger.info(f"Fetched latest project: {project['Name']}")
|
| 124 |
+
|
| 125 |
+
# Prepare response with extracted fields
|
| 126 |
+
response = {
|
| 127 |
+
"projectId": project['Name'],
|
| 128 |
+
"projectName": project['Project_Name__c'],
|
| 129 |
+
"milestones": project.get('Milestones__c', ''),
|
| 130 |
+
"weatherLogs": project.get('Weather_Logs__c', ''),
|
| 131 |
+
"safetyLogs": project.get('Safety_Logs__c', ''),
|
| 132 |
+
"role": "Site Manager" # Default role; can be updated based on Supervisor_ID__c if needed
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
return response
|
| 136 |
+
except Exception as e:
|
| 137 |
+
logger.error(f"Error fetching latest project: {str(e)}")
|
| 138 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 139 |
+
|
| 140 |
@app.post("/api/generate")
|
| 141 |
async def generate_coaching(request: ProjectRequest):
|
| 142 |
if sf is None:
|
|
|
|
| 144 |
raise HTTPException(status_code=500, detail="Salesforce connection not initialized")
|
| 145 |
|
| 146 |
try:
|
| 147 |
+
# Query Project__c by projectId to get the full record
|
| 148 |
query = f"SELECT Id, Name, Project_Name__c, Milestones__c, Weather_Logs__c, Safety_Logs__c, Location__c, Supervisor_ID__c FROM Project__c WHERE Name = '{request.projectId}' LIMIT 1"
|
| 149 |
logger.info(f"Executing SOQL query: {query}")
|
| 150 |
result = sf.query(query)
|
|
|
|
| 176 |
"engagementScore": coaching_data['engagementScore']
|
| 177 |
}
|
| 178 |
|
| 179 |
+
# Insert into Supervisor_AI_Coaching__c
|
| 180 |
+
try:
|
| 181 |
+
coaching_record = {
|
| 182 |
+
'Project__c': project['Id'],
|
| 183 |
+
'Checklist__c': coaching_data['checklist'],
|
| 184 |
+
'Tips__c': coaching_data['tips'],
|
| 185 |
+
'Enguner_score__c': coaching_data['engagementScore'],
|
| 186 |
+
'Role__c': request.role,
|
| 187 |
+
'Project_Name__c': project['Project_Name__c'],
|
| 188 |
+
'Milestones__c': project.get('Milestones__c', ''),
|
| 189 |
+
'Weather_Logs__c': project.get('Weather_Logs__c', ''),
|
| 190 |
+
'Safety_Logs__c': project.get('Safety_Logs__c', '')
|
| 191 |
+
}
|
| 192 |
+
sf.Supervisor_AI_Coaching__c.create(coaching_record)
|
| 193 |
+
logger.info(f"Inserted coaching data into Supervisor_AI_Coaching__c for project {project['Name']}")
|
| 194 |
+
except Exception as e:
|
| 195 |
+
logger.error(f"Failed to insert into Supervisor_AI_Coaching__c: {str(e)}")
|
| 196 |
+
raise HTTPException(status_code=500, detail=f"Failed to save coaching data: {str(e)}")
|
| 197 |
+
|
| 198 |
logger.info(f"Generated coaching response: {response}")
|
| 199 |
return response
|
| 200 |
except Exception as e:
|
|
|
|
| 203 |
|
| 204 |
@app.get("/")
|
| 205 |
async def root():
|
| 206 |
+
logger.info("Serving index.html from root directory")
|
| 207 |
+
return FileResponse("index.html")
|
| 208 |
+
|
| 209 |
+
@app.get("/styles.css")
|
| 210 |
+
async def serve_styles():
|
| 211 |
+
logger.info("Serving styles.css from root directory")
|
| 212 |
+
return FileResponse("styles.css", media_type="text/css")
|
| 213 |
|
| 214 |
# Start Uvicorn server for Hugging Face Spaces
|
| 215 |
logger.info("Starting Uvicorn server for Hugging Face Spaces")
|