AdityaAdaki commited on
Commit
8c5881f
·
1 Parent(s): e0f186d
Files changed (2) hide show
  1. .env +6 -1
  2. app.py +53 -0
.env CHANGED
@@ -1 +1,6 @@
1
- GEMINI_API_KEY=AIzaSyCtEVix5c3pv3wtt7JLUA0CWpmOCt2gBMw
 
 
 
 
 
 
1
+ GEMINI_API_KEY=AIzaSyCtEVix5c3pv3wtt7JLUA0CWpmOCt2gBMw
2
+ SMTP_SERVER=smtp.gmail.com
3
+ SMTP_PORT=587
4
+ SMTP_USERNAME=your-email@gmail.com
5
+ SMTP_PASSWORD=beqk eefx fmir izxz
6
+ NOTIFICATION_EMAIL=adityaadaki21@gmail.com
app.py CHANGED
@@ -4,6 +4,13 @@ import base64
4
  import os
5
  from dotenv import load_dotenv
6
  from werkzeug.utils import secure_filename
 
 
 
 
 
 
 
7
 
8
  # Initialize Flask app
9
  UPLOAD_FOLDER = '/tmp/uploads'
@@ -14,6 +21,48 @@ app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
14
  genai.configure(api_key=os.getenv('GEMINI_API_KEY'))
15
  model = genai.GenerativeModel("gemini-1.5-flash")
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  def process_certificate(image_path, cert_name):
18
  """Process a single certificate and generate a response in structured HTML."""
19
  with open(image_path, "rb") as image_file:
@@ -74,6 +123,10 @@ def process_certificate(image_path, cert_name):
74
  response = model.generate_content([
75
  {'mime_type': 'image/jpeg', 'data': encoded_image}, prompt
76
  ])
 
 
 
 
77
 
78
  return response.text
79
 
 
4
  import os
5
  from dotenv import load_dotenv
6
  from werkzeug.utils import secure_filename
7
+ import smtplib
8
+ from email.mime.text import MIMEText
9
+ from email.mime.multipart import MIMEMultipart
10
+ import re
11
+
12
+ # Load environment variables
13
+ load_dotenv()
14
 
15
  # Initialize Flask app
16
  UPLOAD_FOLDER = '/tmp/uploads'
 
21
  genai.configure(api_key=os.getenv('GEMINI_API_KEY'))
22
  model = genai.GenerativeModel("gemini-1.5-flash")
23
 
24
+ # Email Configuration
25
+ SMTP_SERVER = os.getenv('SMTP_SERVER')
26
+ SMTP_PORT = int(os.getenv('SMTP_PORT'))
27
+ SMTP_USERNAME = os.getenv('SMTP_USERNAME')
28
+ SMTP_PASSWORD = os.getenv('SMTP_PASSWORD')
29
+ NOTIFICATION_EMAIL = os.getenv('NOTIFICATION_EMAIL')
30
+
31
+ def send_invalid_certificate_notification(cert_name, details):
32
+ """Send email notification for invalid certificate."""
33
+ try:
34
+ msg = MIMEMultipart()
35
+ msg['From'] = SMTP_USERNAME
36
+ msg['To'] = NOTIFICATION_EMAIL
37
+ msg['Subject'] = f'Invalid Certificate Detection - {cert_name}'
38
+
39
+ body = f"""
40
+ An invalid certificate has been detected:
41
+
42
+ Certificate Name: {cert_name}
43
+ Details: {details}
44
+
45
+ Please review this certificate as soon as possible.
46
+ """
47
+
48
+ msg.attach(MIMEText(body, 'plain'))
49
+
50
+ with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server:
51
+ server.starttls()
52
+ server.login(SMTP_USERNAME, SMTP_PASSWORD)
53
+ server.send_message(msg)
54
+
55
+ return True
56
+ except Exception as e:
57
+ print(f"Failed to send email notification: {str(e)}")
58
+ return False
59
+
60
+ def check_validity_from_response(response_text):
61
+ """Extract validity status from Gemini's response."""
62
+ # Look for validity indicators in the response
63
+ invalid_pattern = r'Invalid❌|invalid-badge'
64
+ return bool(re.search(invalid_pattern, response_text))
65
+
66
  def process_certificate(image_path, cert_name):
67
  """Process a single certificate and generate a response in structured HTML."""
68
  with open(image_path, "rb") as image_file:
 
123
  response = model.generate_content([
124
  {'mime_type': 'image/jpeg', 'data': encoded_image}, prompt
125
  ])
126
+
127
+ # Check if certificate is invalid
128
+ if check_validity_from_response(response.text):
129
+ send_invalid_certificate_notification(cert_name, response.text)
130
 
131
  return response.text
132