Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, render_template
|
| 2 |
+
from google.cloud import vision
|
| 3 |
+
import io
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import requests
|
| 6 |
+
from salesforce_api import Salesforce
|
| 7 |
+
from datasets import load_dataset
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
app = Flask(__name__)
|
| 11 |
+
|
| 12 |
+
# Initialize Google Cloud Vision client
|
| 13 |
+
vision_client = vision.ImageAnnotatorClient()
|
| 14 |
+
|
| 15 |
+
# Initialize Salesforce client
|
| 16 |
+
sf = Salesforce(
|
| 17 |
+
username='your_salesforce_username',
|
| 18 |
+
password='your_salesforce_password',
|
| 19 |
+
security_token='your_salesforce_security_token'
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# Load dataset from Hugging Face
|
| 23 |
+
dataset = load_dataset('Ajay98/setajsondataset')
|
| 24 |
+
json_data = pd.DataFrame(dataset['train'])
|
| 25 |
+
|
| 26 |
+
@app.route('/')
|
| 27 |
+
def index():
|
| 28 |
+
return render_template('index.html')
|
| 29 |
+
|
| 30 |
+
@app.route('/upload', methods=['POST'])
|
| 31 |
+
def upload_image():
|
| 32 |
+
if 'file' not in request.files:
|
| 33 |
+
return "No file part"
|
| 34 |
+
|
| 35 |
+
file = request.files['file']
|
| 36 |
+
|
| 37 |
+
if file.filename == '':
|
| 38 |
+
return "No selected file"
|
| 39 |
+
|
| 40 |
+
if file:
|
| 41 |
+
# Extract text from the image
|
| 42 |
+
extracted_text = extract_text_from_image(file)
|
| 43 |
+
|
| 44 |
+
# Compare extracted text with JSON dataset
|
| 45 |
+
matched_data = compare_data(extracted_text, json_data)
|
| 46 |
+
|
| 47 |
+
# Update Salesforce with structured data
|
| 48 |
+
update_salesforce(matched_data)
|
| 49 |
+
|
| 50 |
+
return jsonify({"extracted_text": extracted_text, "matched_data": matched_data.to_dict(orient='records')})
|
| 51 |
+
|
| 52 |
+
def extract_text_from_image(image_file):
|
| 53 |
+
content = image_file.read()
|
| 54 |
+
image = vision.Image(content=content)
|
| 55 |
+
response = vision_client.text_detection(image=image)
|
| 56 |
+
texts = response.text_annotations
|
| 57 |
+
extracted_text = texts[0].description if texts else ''
|
| 58 |
+
return extracted_text
|
| 59 |
+
|
| 60 |
+
def compare_data(extracted_text, json_data):
|
| 61 |
+
matches = json_data[json_data['column_name'].str.contains(extracted_text, case=False, na=False)]
|
| 62 |
+
return matches
|
| 63 |
+
|
| 64 |
+
def update_salesforce(data):
|
| 65 |
+
for _, row in data.iterrows():
|
| 66 |
+
sf.Contact.create({
|
| 67 |
+
'LastName': row['LastName'],
|
| 68 |
+
'FirstName': row['FirstName'],
|
| 69 |
+
'Email': row['Email'],
|
| 70 |
+
'Phone': row['Phone']
|
| 71 |
+
})
|
| 72 |
+
|
| 73 |
+
if __name__ == '__main__':
|
| 74 |
+
app.run(debug=True)
|