Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,8 +3,48 @@ import pytesseract
|
|
| 3 |
from PIL import Image
|
| 4 |
import torch
|
| 5 |
import re
|
|
|
|
| 6 |
import os
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
from email.mime.text import MIMEText
|
| 10 |
from email.mime.multipart import MIMEMultipart
|
|
@@ -53,6 +93,8 @@ def normalize(box, width, height):
|
|
| 53 |
# EXTRACT DATA
|
| 54 |
# =====================================================
|
| 55 |
def extract_receipt(image):
|
|
|
|
|
|
|
| 56 |
|
| 57 |
data = pytesseract.image_to_data(
|
| 58 |
image,
|
|
@@ -66,7 +108,7 @@ def extract_receipt(image):
|
|
| 66 |
|
| 67 |
text = data["text"][i].strip()
|
| 68 |
|
| 69 |
-
if text != "":
|
| 70 |
x = data["left"][i]
|
| 71 |
y = data["top"][i]
|
| 72 |
w = data["width"][i]
|
|
@@ -88,7 +130,7 @@ def extract_receipt(image):
|
|
| 88 |
padding="max_length",
|
| 89 |
truncation=True,
|
| 90 |
is_split_into_words=True,
|
| 91 |
-
max_length=
|
| 92 |
)
|
| 93 |
|
| 94 |
encoding = {k: v.to(device) for k, v in encoding.items()}
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
import torch
|
| 5 |
import re
|
| 6 |
+
import requests
|
| 7 |
import os
|
| 8 |
+
|
| 9 |
+
RESEND_API_KEY = os.getenv("RESEND_API_KEY")
|
| 10 |
+
|
| 11 |
+
def send_claim_email(to_email, extracted):
|
| 12 |
+
|
| 13 |
+
if not RESEND_API_KEY:
|
| 14 |
+
return "❌ API key missing"
|
| 15 |
+
|
| 16 |
+
subject = "Insurance Claim Request"
|
| 17 |
+
|
| 18 |
+
html_body = f"""
|
| 19 |
+
<h2>Insurance Claim</h2>
|
| 20 |
+
<p><b>Provider:</b> {extracted['company']}</p>
|
| 21 |
+
<p><b>Date:</b> {extracted['date']}</p>
|
| 22 |
+
<p><b>Amount:</b> ₹{extracted['total']}</p>
|
| 23 |
+
"""
|
| 24 |
+
|
| 25 |
+
try:
|
| 26 |
+
response = requests.post(
|
| 27 |
+
"https://api.resend.com/emails",
|
| 28 |
+
headers={
|
| 29 |
+
"Authorization": f"Bearer {RESEND_API_KEY}",
|
| 30 |
+
"Content-Type": "application/json",
|
| 31 |
+
},
|
| 32 |
+
json={
|
| 33 |
+
"from": "onboarding@resend.dev",
|
| 34 |
+
"to": [to_email],
|
| 35 |
+
"subject": subject,
|
| 36 |
+
"html": html_body,
|
| 37 |
+
},
|
| 38 |
+
timeout=10
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
if response.status_code == 200:
|
| 42 |
+
return f"✅ Email sent to {to_email}"
|
| 43 |
+
else:
|
| 44 |
+
return f"❌ Failed: {response.text}"
|
| 45 |
+
|
| 46 |
+
except Exception as e:
|
| 47 |
+
return f"❌ Error: {str(e)}"
|
| 48 |
|
| 49 |
from email.mime.text import MIMEText
|
| 50 |
from email.mime.multipart import MIMEMultipart
|
|
|
|
| 93 |
# EXTRACT DATA
|
| 94 |
# =====================================================
|
| 95 |
def extract_receipt(image):
|
| 96 |
+
image = image.convert("RGB")
|
| 97 |
+
image.thumbnail((1200, 1200))
|
| 98 |
|
| 99 |
data = pytesseract.image_to_data(
|
| 100 |
image,
|
|
|
|
| 108 |
|
| 109 |
text = data["text"][i].strip()
|
| 110 |
|
| 111 |
+
if text.strip() != "" and len(text) > 2:
|
| 112 |
x = data["left"][i]
|
| 113 |
y = data["top"][i]
|
| 114 |
w = data["width"][i]
|
|
|
|
| 130 |
padding="max_length",
|
| 131 |
truncation=True,
|
| 132 |
is_split_into_words=True,
|
| 133 |
+
max_length=256
|
| 134 |
)
|
| 135 |
|
| 136 |
encoding = {k: v.to(device) for k, v in encoding.items()}
|