Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,7 +14,6 @@ import logging
|
|
| 14 |
import textwrap
|
| 15 |
import tempfile
|
| 16 |
|
| 17 |
-
|
| 18 |
# Set up logging
|
| 19 |
logging.basicConfig(level=logging.INFO)
|
| 20 |
logger = logging.getLogger(__name__)
|
|
@@ -562,7 +561,8 @@ def save_to_salesforce(sf: Salesforce, data: Dict):
|
|
| 562 |
'Penalty_Details__c': data['penalty_details'][:131072],
|
| 563 |
'Penalty_Amounts__c': data['penalty_amounts'][:131072],
|
| 564 |
'Obligation_Details__c': data['obligation_details'][:131072],
|
| 565 |
-
'Delay_Details__c': data['delay_details'][:131072]
|
|
|
|
| 566 |
}
|
| 567 |
result = sf.Custom_Risk_Analysis__c.create(record)
|
| 568 |
logger.info(f"Successfully created Salesforce record: {result['id']}")
|
|
@@ -593,6 +593,11 @@ def format_clause_example(example: str, index: int) -> str:
|
|
| 593 |
|
| 594 |
def analyze_pdf(file_obj) -> List:
|
| 595 |
"""Main analysis function for Gradio interface"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 596 |
try:
|
| 597 |
if not file_obj:
|
| 598 |
raise Exception("No PDF file uploaded. Please upload a valid PDF file.")
|
|
@@ -682,7 +687,6 @@ def analyze_pdf(file_obj) -> List:
|
|
| 682 |
|
| 683 |
extracted_data = "\n".join([format_clause_example(sent, i+1) for i, sent in enumerate(penalty_sentences[:3])]) if penalty_sentences else "<div class='success-box'>✅ No penalty clauses found - Excellent contract terms!</div>"
|
| 684 |
|
| 685 |
-
record_id = str(uuid.uuid4())
|
| 686 |
project_title = os.path.splitext(os.path.basename(file_obj.name))[0]
|
| 687 |
|
| 688 |
# Generate PDF report
|
|
@@ -715,20 +719,12 @@ def analyze_pdf(file_obj) -> List:
|
|
| 715 |
logger.error(f"PDF report generation failed: {str(e)}")
|
| 716 |
pdf_report = None
|
| 717 |
|
| 718 |
-
#
|
| 719 |
-
pdf_path = None
|
| 720 |
if pdf_report:
|
| 721 |
-
|
| 722 |
-
|
| 723 |
-
|
| 724 |
-
|
| 725 |
-
|
| 726 |
-
# Upload PDF to Salesforce
|
| 727 |
-
pdf_content_id, pdf_url = None, None
|
| 728 |
-
if pdf_report:
|
| 729 |
-
pdf_content_id, pdf_url = upload_pdf_to_salesforce(pdf_report, project_title, record_id)
|
| 730 |
-
if not pdf_content_id:
|
| 731 |
-
logger.warning("Failed to upload PDF, but proceeding with record creation")
|
| 732 |
|
| 733 |
sf_data = {
|
| 734 |
'sentiment_score': sentiment_score,
|
|
@@ -740,7 +736,7 @@ def analyze_pdf(file_obj) -> List:
|
|
| 740 |
'penalty_amounts': "\n".join([f"${amt:,.2f}" for amt in penalty_values[:5]]) if penalty_values else "No specific penalty amounts found",
|
| 741 |
'obligation_details': "\n".join([f"{kw}: {count}" for kw, count in obligation_counts.items()]),
|
| 742 |
'delay_details': "\n".join([f"{kw}: {count}" for kw, count in delay_counts.items()]),
|
| 743 |
-
'pdf_url': pdf_url
|
| 744 |
}
|
| 745 |
|
| 746 |
try:
|
|
|
|
| 14 |
import textwrap
|
| 15 |
import tempfile
|
| 16 |
|
|
|
|
| 17 |
# Set up logging
|
| 18 |
logging.basicConfig(level=logging.INFO)
|
| 19 |
logger = logging.getLogger(__name__)
|
|
|
|
| 561 |
'Penalty_Details__c': data['penalty_details'][:131072],
|
| 562 |
'Penalty_Amounts__c': data['penalty_amounts'][:131072],
|
| 563 |
'Obligation_Details__c': data['obligation_details'][:131072],
|
| 564 |
+
'Delay_Details__c': data['delay_details'][:131072],
|
| 565 |
+
'PDF_URL__c': data['pdf_url']
|
| 566 |
}
|
| 567 |
result = sf.Custom_Risk_Analysis__c.create(record)
|
| 568 |
logger.info(f"Successfully created Salesforce record: {result['id']}")
|
|
|
|
| 593 |
|
| 594 |
def analyze_pdf(file_obj) -> List:
|
| 595 |
"""Main analysis function for Gradio interface"""
|
| 596 |
+
# Initialize variables that might be used in error handling
|
| 597 |
+
pdf_url = "Not uploaded"
|
| 598 |
+
project_title = "Unknown"
|
| 599 |
+
record_id = str(uuid.uuid4())
|
| 600 |
+
|
| 601 |
try:
|
| 602 |
if not file_obj:
|
| 603 |
raise Exception("No PDF file uploaded. Please upload a valid PDF file.")
|
|
|
|
| 687 |
|
| 688 |
extracted_data = "\n".join([format_clause_example(sent, i+1) for i, sent in enumerate(penalty_sentences[:3])]) if penalty_sentences else "<div class='success-box'>✅ No penalty clauses found - Excellent contract terms!</div>"
|
| 689 |
|
|
|
|
| 690 |
project_title = os.path.splitext(os.path.basename(file_obj.name))[0]
|
| 691 |
|
| 692 |
# Generate PDF report
|
|
|
|
| 719 |
logger.error(f"PDF report generation failed: {str(e)}")
|
| 720 |
pdf_report = None
|
| 721 |
|
| 722 |
+
# Upload PDF to Salesforce if generated
|
|
|
|
| 723 |
if pdf_report:
|
| 724 |
+
pdf_content_id, pdf_url = upload_pdf_to_salesforce(pdf_report, project_title, record_id)
|
| 725 |
+
if not pdf_content_id:
|
| 726 |
+
logger.warning("Failed to upload PDF to Salesforce")
|
| 727 |
+
pdf_url = "Not uploaded"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 728 |
|
| 729 |
sf_data = {
|
| 730 |
'sentiment_score': sentiment_score,
|
|
|
|
| 736 |
'penalty_amounts': "\n".join([f"${amt:,.2f}" for amt in penalty_values[:5]]) if penalty_values else "No specific penalty amounts found",
|
| 737 |
'obligation_details': "\n".join([f"{kw}: {count}" for kw, count in obligation_counts.items()]),
|
| 738 |
'delay_details': "\n".join([f"{kw}: {count}" for kw, count in delay_counts.items()]),
|
| 739 |
+
'pdf_url': pdf_url
|
| 740 |
}
|
| 741 |
|
| 742 |
try:
|