AdityaAdaki commited on
Commit
c5462e1
·
1 Parent(s): 90bcf34
Files changed (2) hide show
  1. .env +1 -4
  2. app.py +64 -180
.env CHANGED
@@ -1,4 +1 @@
1
- GEMINI_API_KEY=AIzaSyCtEVix5c3pv3wtt7JLUA0CWpmOCt2gBMw
2
- TELEGRAM_BOT_TOKEN=7868898974:AAH2p7sBkj0TH4sD__giOmUt-2ZXn7sg0_c
3
- TELEGRAM_CHAT_ID=5397241102
4
- # WEBHOOK_URL=https://your-domain.com # Replace with your actual domain
 
1
+ GEMINI_API_KEY=AIzaSyCtEVix5c3pv3wtt7JLUA0CWpmOCt2gBMw
 
 
 
app.py CHANGED
@@ -1,18 +1,9 @@
1
- from flask import Flask, request
2
  import google.generativeai as genai
3
  import base64
4
  import os
5
  from dotenv import load_dotenv
6
- import asyncio
7
- from datetime import datetime
8
- import re
9
- from aiogram import Bot, Dispatcher, types
10
- from aiogram.types import InputFile
11
- from aiogram.filters import Command, CommandStart
12
- from aiogram import F
13
-
14
- # Load environment variables
15
- load_dotenv()
16
 
17
  # Initialize Flask app
18
  UPLOAD_FOLDER = '/tmp/uploads'
@@ -23,190 +14,83 @@ app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
23
  genai.configure(api_key=os.getenv('GEMINI_API_KEY'))
24
  model = genai.GenerativeModel("gemini-1.5-flash")
25
 
26
- # Telegram Configuration
27
- TELEGRAM_BOT_TOKEN = os.getenv('TELEGRAM_BOT_TOKEN')
28
- WEBHOOK_URL = os.getenv('WEBHOOK_URL')
29
- bot = Bot(token=TELEGRAM_BOT_TOKEN)
30
- dp = Dispatcher()
31
-
32
- def extract_expiry_date(gemini_output):
33
- """Extract expiry date from Gemini's output."""
34
- date_pattern = r'(\d{1,2}[-/]\d{1,2}[-/]\d{2,4}|\d{1,2}\s+(?:January|February|March|April|May|June|July|August|September|October|November|December)\s+\d{2,4})'
35
- dates = re.findall(date_pattern, gemini_output, re.IGNORECASE)
36
-
37
- if dates:
38
- return dates[0]
39
- return None
40
-
41
- async def process_certificate(image_path, cert_name):
42
- """Process a single certificate."""
43
  with open(image_path, "rb") as image_file:
44
  encoded_image = base64.b64encode(image_file.read()).decode("utf-8")
45
 
46
  prompt = (
47
- f"""Note: The current date is 12/12/2024 (12 December 2024). Process the following certificate:
48
  - {cert_name}: (image attached)
49
-
50
- Instructions as provided...
51
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  )
53
 
54
  response = model.generate_content([
55
  {'mime_type': 'image/jpeg', 'data': encoded_image}, prompt
56
  ])
57
 
58
- expiry_date = extract_expiry_date(response.text)
59
- return response.text, expiry_date
60
-
61
- @dp.message(CommandStart())
62
- async def send_welcome(message: types.Message):
63
- """Handle the /start command."""
64
- welcome_text = (
65
- "👋 Welcome to the Certificate Validation Bot!\n\n"
66
- "Send me a photo or document of your certificate, and I'll:\n"
67
- "✅ Process the certificate\n"
68
- "📅 Extract the expiry date\n"
69
- "⚠️ Alert you if it's expiring soon\n\n"
70
- "Just send me your certificate as a photo or document!"
71
- )
72
- await message.reply(welcome_text)
73
 
74
- @dp.message(Command('help'))
75
- async def send_help(message: types.Message):
76
- """Handle the /help command."""
77
- help_text = (
78
- "🤖 Certificate Validation Bot Help\n\n"
79
- "Commands:\n"
80
- "/start - Start the bot\n"
81
- "/help - Show this help message\n\n"
82
- "To validate a certificate:\n"
83
- "1. Send the certificate as a photo or document\n"
84
- "2. Wait for the processing (usually takes a few seconds)\n"
85
- "3. Receive the validation results and expiry alerts\n\n"
86
- "The bot will automatically alert you if your certificate is expiring within 30 days."
87
- )
88
- await message.reply(help_text)
89
-
90
- @dp.message(F.content_type.in_({'photo', 'document'}))
91
- async def handle_file(message: types.Message):
92
- """Handle certificate upload via Telegram."""
93
- try:
94
- # Send processing message
95
- processing_msg = await message.reply("🔄 Processing your certificate...")
96
 
97
- # Save the file
98
- upload_dir = app.config['UPLOAD_FOLDER']
99
- os.makedirs(upload_dir, exist_ok=True)
100
 
101
- if message.photo:
102
- # Handle photo
103
- photo = message.photo[-1] # Get the largest photo size
104
- file_path = os.path.join(upload_dir, f"{photo.file_id}.jpg")
105
- await photo.download(destination_file=file_path)
106
- cert_name = "Photo Certificate"
107
- else:
108
- # Handle document
109
- document = message.document
110
- file_path = os.path.join(upload_dir, document.file_name)
111
- await document.download(destination_file=file_path)
112
- cert_name = document.file_name
113
-
114
- # Process the certificate
115
- try:
116
- result_text, expiry_date = await process_certificate(file_path, cert_name)
117
 
118
- # Format the response message
119
- response_message = "📄 *Certificate Analysis:*\n\n"
120
- response_message += result_text
121
-
122
- # Send the analysis result
123
- await bot.send_message(
124
- chat_id=message.chat.id,
125
- text=response_message,
126
- parse_mode='Markdown'
127
- )
128
-
129
- # Handle expiry date if found
130
- if expiry_date:
131
- try:
132
- # Try different date formats
133
- for date_format in ['%d/%m/%Y', '%d-%m-%Y', '%d %B %Y']:
134
- try:
135
- expiry_date_obj = datetime.strptime(expiry_date, date_format)
136
- break
137
- except ValueError:
138
- continue
139
-
140
- current_date = datetime.now()
141
- days_until_expiry = (expiry_date_obj - current_date).days
142
-
143
- # Prepare alert message based on expiry status
144
- if days_until_expiry <= 30:
145
- alert_message = "⚠️ *Certificate Expiry Alert*\n\n"
146
- alert_message += f"*Certificate:* {cert_name}\n"
147
-
148
- if days_until_expiry < 0:
149
- alert_message += f"*Status:* 🚫 EXPIRED ({abs(days_until_expiry)} days ago)"
150
- else:
151
- alert_message += f"*Status:* ⚠️ Expiring in {days_until_expiry} days"
152
-
153
- # Add recommendation
154
- if days_until_expiry < 0:
155
- alert_message += "\n\n*Recommendation:* Immediate renewal required!"
156
- else:
157
- alert_message += "\n\n*Recommendation:* Please plan for renewal soon."
158
-
159
- await bot.send_message(
160
- chat_id=message.chat.id,
161
- text=alert_message,
162
- parse_mode='Markdown'
163
- )
164
- except Exception as date_error:
165
- await bot.send_message(
166
- chat_id=message.chat.id,
167
- text=f"⚠️ Could not process expiry date: {str(date_error)}"
168
- )
169
-
170
- except Exception as proc_error:
171
- await bot.send_message(
172
- chat_id=message.chat.id,
173
- text=f"❌ Error processing certificate: {str(proc_error)}"
174
- )
175
-
176
- # Clean up - delete the file after processing
177
- if os.path.exists(file_path):
178
- os.remove(file_path)
179
-
180
- except Exception as e:
181
- await bot.send_message(
182
- chat_id=message.chat.id,
183
- text=f"❌ Error handling file: {str(e)}"
184
- )
185
- finally:
186
- # Delete the processing message
187
  try:
188
- await processing_msg.delete()
189
- except:
190
- pass
191
-
192
- # Add Flask route for Telegram webhook
193
- @app.route(f'/{TELEGRAM_BOT_TOKEN}', methods=['POST'])
194
- async def webhook():
195
- """Handle incoming updates from Telegram webhook."""
196
- if request.headers.get('content-type') == 'application/json':
197
- update = types.Update(**request.json)
198
- await dp.process_update(update)
199
- return '', 200
200
- return '', 403
201
-
202
- # Initialize webhook on startup
203
- @app.before_first_request
204
- def setup_webhook():
205
- """Set up the Telegram webhook when the Flask app starts."""
206
- asyncio.run(bot.set_webhook(f"{WEBHOOK_URL}/{TELEGRAM_BOT_TOKEN}"))
 
207
 
208
  if __name__ == "__main__":
209
- # Create upload directory if it doesn't exist
210
- os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
211
- # Run Flask app
212
- app.run(host='0.0.0.0', port=7860)
 
1
+ from flask import Flask, request, render_template
2
  import google.generativeai as genai
3
  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
  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:
20
  encoded_image = base64.b64encode(image_file.read()).decode("utf-8")
21
 
22
  prompt = (
23
+ """Note: The current date is 12/12/2024 (12 December 2024). Process the following certificate:
24
  - {cert_name}: (image attached)
25
+
26
+ For the certificate:
27
+ 1. Check the expiry date on the certificate. Use the current date (12 December 2024) as the reference point:
28
+ - If the expiry date is earlier than the current date (past date), mark the certificate as Invalid❌.
29
+ - If the expiry date is later than the current date (future date), mark the certificate as Valid✅.
30
+ - If no expiry date is found but there are signs of validity (e.g., government seals, authorized signatures, or proper right marks), mark the certificate as Valid✅.
31
+
32
+ 2. If neither the expiry date nor other validity signs are present, use semantic analysis of the certificate's content to determine its status:
33
+ - Mark the certificate as Valid✅ if the content supports its authenticity (e.g., trustworthy terms, verified names, or credible organizations).
34
+ - Mark the certificate as Invalid❌ if the content lacks any evidence of validity.
35
+
36
+ 3. Structure the results in a clear and properly formatted table with the following columns:
37
+ - Field: Field extracted from the certificate.
38
+ - Value: The corresponding value.
39
+
40
+ 4. Add a section indicating the certificate's overall validity status (Valid✅ or Invalid❌).
41
+
42
+ Return the entire result strictly within the '''html opening and '''html closing tags.
43
+ Ensure compatibility with rendering on a webpage.
44
+ """.format(cert_name=cert_name)
45
  )
46
 
47
  response = model.generate_content([
48
  {'mime_type': 'image/jpeg', 'data': encoded_image}, prompt
49
  ])
50
 
51
+ return response.text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
+ @app.route("/", methods=["GET", "POST"])
54
+ def home():
55
+ if request.method == "POST":
56
+ cert_name = "Affiliation Certificate"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
+ # Check if the post request has the file part
59
+ if 'certificate' not in request.files:
60
+ return render_template("index.html", error="No file part in the request")
61
 
62
+ file = request.files['certificate']
63
+
64
+ # If user does not select file, browser also submits an empty part without filename
65
+ if file.filename == '':
66
+ return render_template("index.html", error="No file selected")
 
 
 
 
 
 
 
 
 
 
 
67
 
68
+ # Check if the file type is allowed (you may want to add more types)
69
+ allowed_extensions = {'png', 'jpg', 'jpeg', 'pdf'}
70
+ if not file.filename.lower().endswith(tuple(allowed_extensions)):
71
+ return render_template("index.html", error="Invalid file type. Please upload an image or PDF file.")
72
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  try:
74
+ # Create uploads directory if it doesn't exist
75
+ upload_dir = app.config['UPLOAD_FOLDER']
76
+ os.makedirs(upload_dir, exist_ok=True)
77
+
78
+ # Secure the filename and save the file
79
+ filename = secure_filename(file.filename)
80
+ image_path = os.path.join(upload_dir, filename)
81
+ file.save(image_path)
82
+
83
+ # Process the certificate
84
+ try:
85
+ gemini_output = process_certificate(image_path, cert_name)
86
+ return render_template("index.html", gemini_output=gemini_output)
87
+ except Exception as e:
88
+ return render_template("index.html", error=f"Error processing certificate: {str(e)}")
89
+
90
+ except Exception as e:
91
+ return render_template("index.html", error=f"Error saving file: {str(e)}")
92
+
93
+ return render_template("index.html")
94
 
95
  if __name__ == "__main__":
96
+ app.run(host='0.0.0.0', port=7860)