Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import os
|
|
| 3 |
from fastapi import FastAPI, HTTPException
|
| 4 |
from pydantic import BaseModel
|
| 5 |
from simple_salesforce import Salesforce
|
|
|
|
| 6 |
import requests
|
| 7 |
from typing import Optional
|
| 8 |
from dotenv import load_dotenv
|
|
@@ -11,41 +12,52 @@ from dotenv import load_dotenv
|
|
| 11 |
logging.basicConfig(level=logging.INFO)
|
| 12 |
logger = logging.getLogger(__name__)
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
# Validate environment variables
|
| 24 |
-
required_vars = {
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
}
|
| 29 |
-
missing_vars = [var for var, value in required_vars.items() if not value]
|
| 30 |
-
if missing_vars:
|
| 31 |
-
|
| 32 |
-
|
| 33 |
|
| 34 |
-
# Initialize Salesforce connection
|
| 35 |
-
try:
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
except Exception as e:
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
app
|
|
|
|
| 49 |
|
| 50 |
# OpenWeatherMap API key
|
| 51 |
WEATHER_API_KEY = "60c00e1b8293d3c0482f8d6ca1a37003"
|
|
@@ -132,4 +144,11 @@ async def generate_coaching(request: ProjectRequest):
|
|
| 132 |
|
| 133 |
@app.get("/")
|
| 134 |
async def root():
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from fastapi import FastAPI, HTTPException
|
| 4 |
from pydantic import BaseModel
|
| 5 |
from simple_salesforce import Salesforce
|
| 6 |
+
from contextlib import asynccontextmanager
|
| 7 |
import requests
|
| 8 |
from typing import Optional
|
| 9 |
from dotenv import load_dotenv
|
|
|
|
| 12 |
logging.basicConfig(level=logging.INFO)
|
| 13 |
logger = logging.getLogger(__name__)
|
| 14 |
|
| 15 |
+
# Global Salesforce connection
|
| 16 |
+
sf = None
|
| 17 |
|
| 18 |
+
@asynccontextmanager
|
| 19 |
+
async def lifespan(app: FastAPI):
|
| 20 |
+
"""Manage application lifecycle."""
|
| 21 |
+
global sf
|
| 22 |
+
logger.info("Starting application initialization")
|
| 23 |
+
|
| 24 |
+
# Load environment variables
|
| 25 |
+
load_dotenv()
|
| 26 |
+
SF_USERNAME = os.getenv('SF_USERNAME')
|
| 27 |
+
SF_PASSWORD = os.getenv('SF_PASSWORD')
|
| 28 |
+
SF_SECURITY_TOKEN = os.getenv('SF_SECURITY_TOKEN')
|
| 29 |
|
| 30 |
+
# Validate environment variables
|
| 31 |
+
required_vars = {
|
| 32 |
+
'SF_USERNAME': SF_USERNAME,
|
| 33 |
+
'SF_PASSWORD': SF_PASSWORD,
|
| 34 |
+
'SF_SECURITY_TOKEN': SF_SECURITY_TOKEN
|
| 35 |
+
}
|
| 36 |
+
missing_vars = [var for var, value in required_vars.items() if not value]
|
| 37 |
+
if missing_vars:
|
| 38 |
+
logger.error(f"Missing required environment variables: {', '.join(missing_vars)}")
|
| 39 |
+
raise ValueError(f"Missing required environment variables: {', '.join(missing_vars)}")
|
| 40 |
|
| 41 |
+
# Initialize Salesforce connection
|
| 42 |
+
try:
|
| 43 |
+
sf = Salesforce(
|
| 44 |
+
username=SF_USERNAME,
|
| 45 |
+
password=SF_PASSWORD,
|
| 46 |
+
security_token=SF_SECURITY_TOKEN,
|
| 47 |
+
instance_url='https://aicoachforsitesupervisors-dev-ed.develop.my.salesforce.com',
|
| 48 |
+
version='63.0'
|
| 49 |
+
)
|
| 50 |
+
logger.info("Successfully connected to Salesforce")
|
| 51 |
+
except Exception as e:
|
| 52 |
+
logger.error(f"Failed to connect to Salesforce: {str(e)}")
|
| 53 |
+
sf = None
|
| 54 |
+
|
| 55 |
+
logger.info("Application initialized successfully")
|
| 56 |
+
yield
|
| 57 |
+
logger.info("Shutting down application")
|
| 58 |
|
| 59 |
+
# Initialize FastAPI app with lifespan
|
| 60 |
+
app = FastAPI(lifespan=lifespan)
|
| 61 |
|
| 62 |
# OpenWeatherMap API key
|
| 63 |
WEATHER_API_KEY = "60c00e1b8293d3c0482f8d6ca1a37003"
|
|
|
|
| 144 |
|
| 145 |
@app.get("/")
|
| 146 |
async def root():
|
| 147 |
+
logger.info("Root endpoint accessed")
|
| 148 |
+
return {"message": "Welcome to AI Coach API. Use POST /api/generate"}
|
| 149 |
+
|
| 150 |
+
# Prevent direct execution in production
|
| 151 |
+
if __name__ == "__main__":
|
| 152 |
+
import uvicorn
|
| 153 |
+
logger.info("Starting Uvicorn server for local development")
|
| 154 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|