aarthigoud commited on
Commit
fcd4d46
·
verified ·
1 Parent(s): e50cce3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +131 -0
app.py ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from unittest import result
2
+ from pdfplumber import pdf
3
+ import streamlit as st
4
+ import requests
5
+ from crypto import encrypt_pdf, decrypt_pdf
6
+ import os
7
+ import time
8
+ import json, asyncio
9
+ from transformers import pipeline
10
+
11
+ api_keys = {}
12
+ with open('api_keys.json', 'r') as file:
13
+ api_keys = json.load(file)
14
+
15
+ PINATA_API_KEY = api_keys['PINATA_API_KEY']
16
+ PINATA_SECRET_API_KEY = api_keys['PINATA_SECRET_API_KEY']
17
+ PINATA_API_URL = "https://api.pinata.cloud/pinning/pinFileToIPFS"
18
+ IPFS_GATEWAY = "https://gateway.pinata.cloud/ipfs/"
19
+
20
+ with open("university_keys.json", "r") as f:
21
+ UNIVERSITY_KEYS = json.load(f)
22
+
23
+ @st.cache_resource
24
+ def load_ai_checker():
25
+ return pipeline("text-generation", model="distilgpt2")
26
+
27
+ ai_checker = load_ai_checker()
28
+
29
+ def run_ai_check(pdf_text):
30
+ prompt = "Does this certificate look fake? Answer with 'Yes' or 'No': " + pdf_text[:500]
31
+ result = ai_checker(prompt, max_length=512, do_sample=False)[0]['generated_text']
32
+ print(result, "AI Check \n")
33
+ return "No" in result
34
+
35
+ def upload_to_ipfs(file_bytes, filename):
36
+ headers = {
37
+ "pinata_api_key": PINATA_API_KEY,
38
+ "pinata_secret_api_key": PINATA_SECRET_API_KEY
39
+ }
40
+ files = {"file": (filename, file_bytes, "application/pdf")}
41
+ response = requests.post(PINATA_API_URL, headers=headers, files=files)
42
+ if response.status_code == 200:
43
+ return response.json().get("IpfsHash")
44
+ return None
45
+
46
+ if __name__ == "__main__":
47
+ st.title("🎓 Certificate Verification System")
48
+ tabs = ["Sign Certificate", "Encrypt Certificate", "Verify Certificate", "Decrypt Certificate"]
49
+ choice = st.sidebar.radio("Select Action", tabs)
50
+
51
+ if choice == "Sign Certificate":
52
+ st.header("Sign & Encrypt Certificate")
53
+ university = st.selectbox("Select University", list(UNIVERSITY_KEYS.keys()))
54
+ pdf_file = st.file_uploader("Upload Certificate PDF", type=["pdf"])
55
+ sign_button = st.button("Sign and Encrypt Certificate")
56
+
57
+ if pdf_file and university and sign_button:
58
+ pdf_bytes = pdf_file.read()
59
+ # AI Check
60
+ ai_check_passed = run_ai_check(pdf_bytes.decode(errors='ignore'))
61
+ if not ai_check_passed:
62
+ st.error("❌ AI Check Failed: Fake or Invalid Certificate Detected!")
63
+ else:
64
+ encrypted_pdf = encrypt_pdf(pdf_bytes, UNIVERSITY_KEYS[university].encode())
65
+ filename = f"{university.replace(' ', '_')}_{time.time()}_signed_and_encrypted.pdf"
66
+ ipfs_hash = upload_to_ipfs(encrypted_pdf, filename)
67
+ if ipfs_hash:
68
+ st.success(f"✅ Certificate Signed & Stored on IPFS! URL: {IPFS_GATEWAY}{ipfs_hash}")
69
+ st.download_button("Download Signed Certificate", encrypted_pdf, filename)
70
+ else:
71
+ st.error("Failed to upload encrypted certificate to IPFS.")
72
+ elif choice == "Encrypt Certificate":
73
+ st.header("Encrypt Certificate")
74
+ university = st.selectbox("Select University", list(UNIVERSITY_KEYS.keys()))
75
+ pdf_file = st.file_uploader("Upload Certificate PDF", type=["pdf"])
76
+ encrypt_button = st.button("Encrypt Certificate")
77
+
78
+ if pdf_file and university and encrypt_button:
79
+ pdf_bytes = pdf_file.read()
80
+ encrypted_pdf = encrypt_pdf(pdf_bytes, UNIVERSITY_KEYS[university].encode())
81
+ filename = f"{university.replace(' ', '_')}_encrypted.pdf"
82
+ st.success("✅ Certificate Encrypted Successfully!")
83
+ st.download_button("Download Encrypted Certificate", encrypted_pdf, filename)
84
+
85
+ elif choice == "Verify Certificate":
86
+ st.header("Verify Certificate")
87
+ university = st.selectbox("Select University", list(UNIVERSITY_KEYS.keys()))
88
+ ipfs_hash = st.text_input("Enter IPFS Hash of the Signed Certificate")
89
+ user_pdf_file = st.file_uploader("Upload Signed and Encrypted Certificate PDF", type=["pdf"])
90
+ _verify_button = st.button("Verify Certificate")
91
+
92
+ if ipfs_hash and university and _verify_button and user_pdf_file:
93
+ response = requests.get(f"{IPFS_GATEWAY}{ipfs_hash}")
94
+ if response.status_code == 200:
95
+ encrypted_pdf = response.content
96
+ try:
97
+ user_pdf_file = user_pdf_file.read()
98
+ print("I am working")
99
+ if encrypted_pdf != user_pdf_file:
100
+ st.error("❌ Verification Failed! Encrypted PDF does not match the IPFS Hash.")
101
+ st.stop()
102
+ decrypted_pdf = decrypt_pdf(encrypted_pdf, UNIVERSITY_KEYS[university].encode())
103
+ st.success("Certificate Verified Successfully and Valid ✅")
104
+ st.download_button("Download Decrypted Certificate", decrypted_pdf, "decrypted_certificate.pdf")
105
+ except:
106
+ st.error("❌ Verification Failed! Wrong University Selected.")
107
+ else:
108
+ st.error("Invalid IPFS Hash or file not found.")
109
+ elif choice == "Decrypt Certificate":
110
+ st.header("Decrypt Certificate")
111
+ university = st.selectbox("Select University", list(UNIVERSITY_KEYS.keys()))
112
+ pdf_file = st.file_uploader("Upload Encrypted Certificate PDF", type=["pdf"])
113
+ decrypt_button = st.button("Decrypt Certificate")
114
+
115
+ if pdf_file and university and decrypt_button:
116
+ pdf_bytes = pdf_file.read()
117
+ try:
118
+ decrypted_pdf = decrypt_pdf(pdf_bytes, UNIVERSITY_KEYS[university].encode())
119
+ st.success("✅ Certificate Decrypted Successfully!")
120
+ st.download_button("Download Decrypted Certificate", decrypted_pdf, "decrypted_certificate.pdf")
121
+ except:
122
+ st.error("❌ Decryption Failed! Wrong University Selected.")
123
+
124
+ # Footer
125
+ st.markdown("---")
126
+ st.markdown("📌 **Built by:**")
127
+ st.text(f"Led By: Dr. M. Asha Kiran")
128
+ st.text(f"Developer: Gajja Aarthi Goud")
129
+ st.text(f"Roll Number: 23MC201A34")
130
+ st.text(f"Department: MCA, IT Department, Anurag University")
131
+ # st.run()