Update app.py
Browse files
app.py
CHANGED
|
@@ -29,6 +29,7 @@ try:
|
|
| 29 |
SALESFORCE_AVAILABLE = True
|
| 30 |
except ImportError as e:
|
| 31 |
logging.error(f"Failed to import simple-salesforce: {str(e)}. Salesforce functionality will be disabled.")
|
|
|
|
| 32 |
SALESFORCE_AVAILABLE = False
|
| 33 |
|
| 34 |
# Salesforce configuration (loaded from environment variables for security)
|
|
@@ -40,16 +41,18 @@ SF_INSTANCE_URL = os.getenv("SF_INSTANCE_URL")
|
|
| 40 |
# Validate Salesforce environment variables
|
| 41 |
if not all([SF_USERNAME, SF_PASSWORD, SF_SECURITY_TOKEN, SF_INSTANCE_URL]):
|
| 42 |
logging.error("One or more Salesforce environment variables are missing. Salesforce functionality will be disabled.")
|
| 43 |
-
logging.error(f"SF_USERNAME: {SF_USERNAME}")
|
| 44 |
logging.error(f"SF_PASSWORD: {'Set' if SF_PASSWORD else 'Not Set'}")
|
| 45 |
logging.error(f"SF_SECURITY_TOKEN: {'Set' if SF_SECURITY_TOKEN else 'Not Set'}")
|
| 46 |
-
logging.error(f"SF_INSTANCE_URL: {SF_INSTANCE_URL}")
|
|
|
|
| 47 |
SALESFORCE_AVAILABLE = False
|
| 48 |
|
| 49 |
# Hugging Face configuration
|
| 50 |
HF_API_TOKEN = os.getenv("HF_API_TOKEN")
|
| 51 |
if not HF_API_TOKEN:
|
| 52 |
logging.error("Hugging Face API token (HF_API_TOKEN) not found. Hugging Face functionality will be disabled.")
|
|
|
|
| 53 |
HF_AVAILABLE = False
|
| 54 |
else:
|
| 55 |
HF_AVAILABLE = True
|
|
@@ -63,6 +66,9 @@ def init_salesforce():
|
|
| 63 |
return None, "Salesforce library not available"
|
| 64 |
|
| 65 |
try:
|
|
|
|
|
|
|
|
|
|
| 66 |
sf = Salesforce(
|
| 67 |
username=SF_USERNAME,
|
| 68 |
password=SF_PASSWORD,
|
|
@@ -70,12 +76,17 @@ def init_salesforce():
|
|
| 70 |
instance_url=SF_INSTANCE_URL
|
| 71 |
)
|
| 72 |
logging.info("Salesforce connected successfully")
|
|
|
|
|
|
|
|
|
|
| 73 |
return sf, "Salesforce connected successfully"
|
| 74 |
except SalesforceError as e:
|
| 75 |
logging.error(f"Salesforce authentication failed: {str(e)}")
|
|
|
|
| 76 |
raise
|
| 77 |
except Exception as e:
|
| 78 |
logging.error(f"Failed to initialize Salesforce connection: {str(e)}")
|
|
|
|
| 79 |
raise
|
| 80 |
|
| 81 |
# Summarize text using Hugging Face Inference API
|
|
@@ -147,12 +158,14 @@ def create_salesforce_record(score, checklist_summary, missing_summary, status,
|
|
| 147 |
"QA_Report__c": qa_report
|
| 148 |
}
|
| 149 |
|
| 150 |
-
logging.debug(f"Attempting to create Salesforce record with data: {record}")
|
| 151 |
result = sf.Project_Closure_Handover__c.create(record)
|
| 152 |
logging.info(f"Successfully created Salesforce record: {result}")
|
|
|
|
| 153 |
return "Record created successfully"
|
| 154 |
except SalesforceError as e:
|
| 155 |
logging.error(f"Salesforce error while creating Project_Closure_Handover__c record: {str(e)}")
|
|
|
|
| 156 |
return f"Salesforce error: {str(e)}"
|
| 157 |
except Exception as e:
|
| 158 |
logging.error(f"Failed to create Salesforce Project_Closure_Handover__c record: {str(e)}")
|
|
@@ -371,5 +384,4 @@ with gr.Blocks(css="""
|
|
| 371 |
demo.launch()
|
| 372 |
|
| 373 |
# Comment out demo.launch() for Hugging Face Spaces deployment
|
| 374 |
-
#
|
| 375 |
-
# demo.launch()
|
|
|
|
| 29 |
SALESFORCE_AVAILABLE = True
|
| 30 |
except ImportError as e:
|
| 31 |
logging.error(f"Failed to import simple-salesforce: {str(e)}. Salesforce functionality will be disabled.")
|
| 32 |
+
logging.error("Ensure 'simple-salesforce' is included in requirements.txt and installed in your Hugging Face Space.")
|
| 33 |
SALESFORCE_AVAILABLE = False
|
| 34 |
|
| 35 |
# Salesforce configuration (loaded from environment variables for security)
|
|
|
|
| 41 |
# Validate Salesforce environment variables
|
| 42 |
if not all([SF_USERNAME, SF_PASSWORD, SF_SECURITY_TOKEN, SF_INSTANCE_URL]):
|
| 43 |
logging.error("One or more Salesforce environment variables are missing. Salesforce functionality will be disabled.")
|
| 44 |
+
logging.error(f"SF_USERNAME: {SF_USERNAME if SF_USERNAME else 'Not Set'}")
|
| 45 |
logging.error(f"SF_PASSWORD: {'Set' if SF_PASSWORD else 'Not Set'}")
|
| 46 |
logging.error(f"SF_SECURITY_TOKEN: {'Set' if SF_SECURITY_TOKEN else 'Not Set'}")
|
| 47 |
+
logging.error(f"SF_INSTANCE_URL: {SF_INSTANCE_URL if SF_INSTANCE_URL else 'Not Set'}")
|
| 48 |
+
logging.error("Please set these variables in Hugging Face Space Settings > Secrets.")
|
| 49 |
SALESFORCE_AVAILABLE = False
|
| 50 |
|
| 51 |
# Hugging Face configuration
|
| 52 |
HF_API_TOKEN = os.getenv("HF_API_TOKEN")
|
| 53 |
if not HF_API_TOKEN:
|
| 54 |
logging.error("Hugging Face API token (HF_API_TOKEN) not found. Hugging Face functionality will be disabled.")
|
| 55 |
+
logging.error("Please set HF_API_TOKEN in Hugging Face Space Settings > Secrets.")
|
| 56 |
HF_AVAILABLE = False
|
| 57 |
else:
|
| 58 |
HF_AVAILABLE = True
|
|
|
|
| 66 |
return None, "Salesforce library not available"
|
| 67 |
|
| 68 |
try:
|
| 69 |
+
logging.info("Attempting to connect to Salesforce with the following credentials:")
|
| 70 |
+
logging.info(f"Username: {SF_USERNAME}")
|
| 71 |
+
logging.info(f"Instance URL: {SF_INSTANCE_URL}")
|
| 72 |
sf = Salesforce(
|
| 73 |
username=SF_USERNAME,
|
| 74 |
password=SF_PASSWORD,
|
|
|
|
| 76 |
instance_url=SF_INSTANCE_URL
|
| 77 |
)
|
| 78 |
logging.info("Salesforce connected successfully")
|
| 79 |
+
# Test a simple query to verify permissions
|
| 80 |
+
test_query = sf.query("SELECT Id FROM Project_Closure_Handover__c LIMIT 1")
|
| 81 |
+
logging.info(f"Test query result: {test_query}")
|
| 82 |
return sf, "Salesforce connected successfully"
|
| 83 |
except SalesforceError as e:
|
| 84 |
logging.error(f"Salesforce authentication failed: {str(e)}")
|
| 85 |
+
logging.error("Possible issues: Incorrect credentials, IP restrictions, or insufficient permissions.")
|
| 86 |
raise
|
| 87 |
except Exception as e:
|
| 88 |
logging.error(f"Failed to initialize Salesforce connection: {str(e)}")
|
| 89 |
+
logging.error("Check your Salesforce org settings, network restrictions, or API access.")
|
| 90 |
raise
|
| 91 |
|
| 92 |
# Summarize text using Hugging Face Inference API
|
|
|
|
| 158 |
"QA_Report__c": qa_report
|
| 159 |
}
|
| 160 |
|
| 161 |
+
logging.debug(f"Attempting to create Salesforce record in Project_Closure_Handover__c with data: {record}")
|
| 162 |
result = sf.Project_Closure_Handover__c.create(record)
|
| 163 |
logging.info(f"Successfully created Salesforce record: {result}")
|
| 164 |
+
logging.info(f"Record ID: {result.get('id')}")
|
| 165 |
return "Record created successfully"
|
| 166 |
except SalesforceError as e:
|
| 167 |
logging.error(f"Salesforce error while creating Project_Closure_Handover__c record: {str(e)}")
|
| 168 |
+
logging.error("Possible issues: Object permissions, field-level security, or validation rules.")
|
| 169 |
return f"Salesforce error: {str(e)}"
|
| 170 |
except Exception as e:
|
| 171 |
logging.error(f"Failed to create Salesforce Project_Closure_Handover__c record: {str(e)}")
|
|
|
|
| 384 |
demo.launch()
|
| 385 |
|
| 386 |
# Comment out demo.launch() for Hugging Face Spaces deployment
|
| 387 |
+
# demo.launch()
|
|
|