pavansuresh commited on
Commit
b749800
·
verified ·
1 Parent(s): 548a348

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -3
app.py CHANGED
@@ -1,12 +1,21 @@
1
- from flask import Flask, request, jsonify
2
  import PyPDF2
3
  from transformers import pipeline
4
  import psycopg2
5
  import json
6
- import torch
 
 
7
 
8
  app = Flask(__name__)
9
 
 
 
 
 
 
 
 
10
  # Database connection
11
  conn = psycopg2.connect(
12
  dbname="contracts_db",
@@ -21,6 +30,48 @@ cursor = conn.cursor()
21
  ner_pipeline = pipeline("ner", model="dslim/bert-base-NER", aggregation_strategy="simple")
22
  compliance_pipeline = pipeline("text-classification", model="facebook/bart-large-mnli")
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  def extract_text_from_pdf(file):
25
  try:
26
  pdf_reader = PyPDF2.PdfReader(file)
@@ -33,6 +84,10 @@ def extract_text_from_pdf(file):
33
 
34
  @app.route('/upload_contract', methods=['POST'])
35
  def upload_contract():
 
 
 
 
36
  if 'contract' not in request.files:
37
  return jsonify({"error": "No file uploaded"}), 400
38
 
@@ -56,7 +111,7 @@ def upload_contract():
56
  "score": result['scores'][0]
57
  })
58
 
59
- # Store in database
60
  try:
61
  cursor.execute(
62
  "INSERT INTO contracts (content, entities, compliance) VALUES (%s, %s, %s) RETURNING id",
@@ -68,6 +123,17 @@ def upload_contract():
68
  conn.rollback()
69
  return jsonify({"error": str(e)}), 500
70
 
 
 
 
 
 
 
 
 
 
 
 
71
  return jsonify({
72
  "contract_id": contract_id,
73
  "entities": extracted_data,
 
1
+ from flask import Flask, request, jsonify, redirect, url_for
2
  import PyPDF2
3
  from transformers import pipeline
4
  import psycopg2
5
  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(
21
  dbname="contracts_db",
 
30
  ner_pipeline = pipeline("ner", model="dslim/bert-base-NER", aggregation_strategy="simple")
31
  compliance_pipeline = pipeline("text-classification", model="facebook/bart-large-mnli")
32
 
33
+ # Store OAuth tokens
34
+ access_token = None
35
+ instance_url = None
36
+
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')
49
+ def oauth_callback():
50
+ global access_token, instance_url
51
+ code = request.args.get('code')
52
+ if not code:
53
+ return jsonify({"error": "Authorization code not found"}), 400
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']
68
+ instance_url = token_data['instance_url']
69
+ return redirect(url_for('index'))
70
+
71
+ @app.route('/')
72
+ def index():
73
+ return jsonify({"message": "Welcome to Contract Analyzer. Please log in to Salesforce."})
74
+
75
  def extract_text_from_pdf(file):
76
  try:
77
  pdf_reader = PyPDF2.PdfReader(file)
 
84
 
85
  @app.route('/upload_contract', methods=['POST'])
86
  def upload_contract():
87
+ global access_token, instance_url
88
+ if not access_token or not instance_url:
89
+ return jsonify({"error": "Please authenticate with Salesforce first"}), 401
90
+
91
  if 'contract' not in request.files:
92
  return jsonify({"error": "No file uploaded"}), 400
93
 
 
111
  "score": result['scores'][0]
112
  })
113
 
114
+ # Store in PostgreSQL
115
  try:
116
  cursor.execute(
117
  "INSERT INTO contracts (content, entities, compliance) VALUES (%s, %s, %s) RETURNING id",
 
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)
133
+ })
134
+ except Exception as e:
135
+ return jsonify({"error": f"Failed to save to Salesforce: {str(e)}"}), 500
136
+
137
  return jsonify({
138
  "contract_id": contract_id,
139
  "entities": extracted_data,