pavansuresh commited on
Commit
26632cd
·
verified ·
1 Parent(s): f5acbb5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -16
app.py CHANGED
@@ -6,15 +6,20 @@ import json
6
  import requests
7
  from simple_salesforce import Salesforce
8
  import urllib.parse
 
 
9
 
10
  app = Flask(__name__)
11
 
12
- # Salesforce Connected App credentials
13
- SALESFORCE_CONSUMER_KEY = 'your_consumer_key'
14
- SALESFORCE_CONSUMER_SECRET = 'your_consumer_secret'
15
- SALESFORCE_REDIRECT_URI = 'http://localhost:5000/oauth/callback'
16
- SALESFORCE_AUTH_URL = 'https://login.salesforce.com/services/oauth2/authorize'
17
- SALESFORCE_TOKEN_URL = 'https://login.salesforce.com/services/oauth2/token'
 
 
 
18
 
19
  # Database connection
20
  conn = psycopg2.connect(
@@ -37,12 +42,12 @@ instance_url = None
37
  @app.route('/login')
38
  def login():
39
  params = {
40
- 'client_id': SALESFORCE_CONSUMER_KEY,
41
  'redirect_uri': SALESFORCE_REDIRECT_URI,
42
  'response_type': 'code',
43
  'scope': 'api refresh_token'
44
  }
45
- auth_url = f"{SALESFORCE_AUTH_URL}?{urllib.parse.urlencode(params)}"
46
  return redirect(auth_url)
47
 
48
  @app.route('/oauth/callback')
@@ -54,14 +59,14 @@ def oauth_callback():
54
 
55
  data = {
56
  'grant_type': 'authorization_code',
57
- 'client_id': SALESFORCE_CONSUMER_KEY,
58
- 'client_secret': SALESFORCE_CONSUMER_SECRET,
59
  'redirect_uri': SALESFORCE_REDIRECT_URI,
60
  'code': code
61
  }
62
- response = requests.post(SALESFORCE_TOKEN_URL, data=data)
63
  if response.status_code != 200:
64
- return jsonify({"error": "Failed to obtain access token"}), 500
65
 
66
  token_data = response.json()
67
  access_token = token_data['access_token']
@@ -93,8 +98,8 @@ def upload_contract():
93
 
94
  file = request.files['contract']
95
  text = extract_text_from_pdf(file)
96
- if not text:
97
- return jsonify({"error": "Failed to extract text from PDF"}), 500
98
 
99
  # Extract entities
100
  entities = ner_pipeline(text)
@@ -121,12 +126,12 @@ def upload_contract():
121
  conn.commit()
122
  except Exception as e:
123
  conn.rollback()
124
- return jsonify({"error": str(e)}), 500
125
 
126
  # Push to Salesforce
127
  try:
128
  sf = Salesforce(instance_url=instance_url, session_id=access_token)
129
- sf.Contract__c.create({
130
  'Content__c': text[:1000], # Truncate for Salesforce field limit
131
  'Entities__c': json.dumps(extracted_data),
132
  'Compliance__c': json.dumps(compliance_results)
@@ -136,6 +141,7 @@ def upload_contract():
136
 
137
  return jsonify({
138
  "contract_id": contract_id,
 
139
  "entities": extracted_data,
140
  "compliance": compliance_results
141
  })
 
6
  import requests
7
  from simple_salesforce import Salesforce
8
  import urllib.parse
9
+ from dotenv import load_dotenv
10
+ import os
11
 
12
  app = Flask(__name__)
13
 
14
+ # Load environment variables
15
+ load_dotenv()
16
+ SALESFORCE_OAUTH_URL = os.getenv('SALESFORCE_OAUTH_URL')
17
+ SF_CLIENT_ID = os.getenv('SF_CLIENT_ID')
18
+ SF_CLIENT_SECRET = os.getenv('SF_CLIENT_SECRET')
19
+ SALESFORCE_REDIRECT_URI = os.getenv('SALESFORCE_REDIRECT_URI')
20
+ SF_USERNAME = os.getenv('SF_USERNAME')
21
+ SF_PASSWORD = os.getenv('SF_PASSWORD')
22
+ SF_DOMAIN = os.getenv('SF_DOMAIN')
23
 
24
  # Database connection
25
  conn = psycopg2.connect(
 
42
  @app.route('/login')
43
  def login():
44
  params = {
45
+ 'client_id': SF_CLIENT_ID,
46
  'redirect_uri': SALESFORCE_REDIRECT_URI,
47
  'response_type': 'code',
48
  'scope': 'api refresh_token'
49
  }
50
+ auth_url = f"https://login.salesforce.com/services/oauth2/authorize?{urllib.parse.urlencode(params)}"
51
  return redirect(auth_url)
52
 
53
  @app.route('/oauth/callback')
 
59
 
60
  data = {
61
  'grant_type': 'authorization_code',
62
+ 'client_id': SF_CLIENT_ID,
63
+ 'client_secret': SF_CLIENT_SECRET,
64
  'redirect_uri': SALESFORCE_REDIRECT_URI,
65
  'code': code
66
  }
67
+ response = requests.post(SALESFORCE_OAUTH_URL, data=data)
68
  if response.status_code != 200:
69
+ return jsonify({"error": "Failed to obtain access token", "details": response.text}), 500
70
 
71
  token_data = response.json()
72
  access_token = token_data['access_token']
 
98
 
99
  file = request.files['contract']
100
  text = extract_text_from_pdf(file)
101
+ if not text or isinstance(text, str) and text.startswith("Error"):
102
+ return jsonify({"error": "Failed to extract text from PDF", "details": text}), 500
103
 
104
  # Extract entities
105
  entities = ner_pipeline(text)
 
126
  conn.commit()
127
  except Exception as e:
128
  conn.rollback()
129
+ return jsonify({"error": f"Database error: {str(e)}"}), 500
130
 
131
  # Push to Salesforce
132
  try:
133
  sf = Salesforce(instance_url=instance_url, session_id=access_token)
134
+ sf_response = sf.Contract__c.create({
135
  'Content__c': text[:1000], # Truncate for Salesforce field limit
136
  'Entities__c': json.dumps(extracted_data),
137
  'Compliance__c': json.dumps(compliance_results)
 
141
 
142
  return jsonify({
143
  "contract_id": contract_id,
144
+ "salesforce_id": sf_response['id'],
145
  "entities": extracted_data,
146
  "compliance": compliance_results
147
  })