TharaKavin commited on
Commit
fa5ebe0
·
verified ·
1 Parent(s): 289bd2d

Upload 5 files

Browse files
Files changed (5) hide show
  1. app.py +105 -0
  2. auth.py +26 -0
  3. certificate_ai.py +35 -0
  4. db.py +10 -0
  5. requirements.txt +6 -0
app.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import app as gr
2
+ from auth import login, signup
3
+ from certificate_ai import read_pdf, verify_certificate
4
+ from db import cert_collection
5
+
6
+ current_user = {"username":None,"role":None}
7
+
8
+
9
+ def user_login(username,password):
10
+
11
+ role = login(username,password)
12
+
13
+ if role == "user":
14
+ current_user["username"] = username
15
+ return "User Login Successful"
16
+
17
+ if role == "admin":
18
+ current_user["username"] = username
19
+ return "Admin Login Successful"
20
+
21
+ return "Invalid Login"
22
+
23
+
24
+ def upload_certificate(file):
25
+
26
+ text = read_pdf(file)
27
+
28
+ result = verify_certificate(text)
29
+
30
+ cert_collection.insert_one({
31
+ "username":current_user["username"],
32
+ "certificate_text":text,
33
+ "result":result
34
+ })
35
+
36
+ return result
37
+
38
+
39
+ def view_certificates():
40
+
41
+ data = cert_collection.find()
42
+
43
+ output = []
44
+
45
+ for d in data:
46
+ output.append([d["username"],d["result"]])
47
+
48
+ return output
49
+
50
+
51
+ with gr.Blocks() as demo:
52
+
53
+ gr.Markdown("# Internship Certificate Verification System")
54
+
55
+ with gr.Tab("Signup"):
56
+
57
+ username = gr.Textbox(label="Username")
58
+ password = gr.Textbox(label="Password",type="password")
59
+ role = gr.Dropdown(["user","admin"])
60
+
61
+ signup_btn = gr.Button("Signup")
62
+ signup_output = gr.Textbox()
63
+
64
+ signup_btn.click(signup,
65
+ inputs=[username,password,role],
66
+ outputs=signup_output)
67
+
68
+
69
+ with gr.Tab("Login"):
70
+
71
+ l_user = gr.Textbox(label="Username")
72
+ l_pass = gr.Textbox(label="Password",type="password")
73
+
74
+ login_btn = gr.Button("Login")
75
+ login_output = gr.Textbox()
76
+
77
+ login_btn.click(user_login,
78
+ inputs=[l_user,l_pass],
79
+ outputs=login_output)
80
+
81
+
82
+ with gr.Tab("Upload Certificate"):
83
+
84
+ file = gr.File(label="Upload PDF")
85
+
86
+ verify_btn = gr.Button("Verify")
87
+
88
+ result = gr.Textbox(label="Result")
89
+
90
+ verify_btn.click(upload_certificate,
91
+ inputs=file,
92
+ outputs=result)
93
+
94
+
95
+ with gr.Tab("Admin Dashboard"):
96
+
97
+ view_btn = gr.Button("View Certificates")
98
+
99
+ table = gr.Dataframe(headers=["Username","Result"])
100
+
101
+ view_btn.click(view_certificates,
102
+ outputs=table)
103
+
104
+
105
+ demo.launch()
auth.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import bcrypt
2
+ from db import users_collection
3
+
4
+ def signup(username, password, role):
5
+
6
+ hashed = bcrypt.hashpw(password.encode(), bcrypt.gensalt())
7
+
8
+ user = {
9
+ "username": username,
10
+ "password": hashed,
11
+ "role": role
12
+ }
13
+
14
+ users_collection.insert_one(user)
15
+
16
+ return "Signup Successful"
17
+
18
+
19
+ def login(username, password):
20
+
21
+ user = users_collection.find_one({"username": username})
22
+
23
+ if user and bcrypt.checkpw(password.encode(), user["password"]):
24
+ return user["role"]
25
+
26
+ return None
certificate_ai.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pdfplumber
2
+ from openai import OpenAI
3
+ import os
4
+ from dotenv import load_dotenv
5
+ load_dotenv()
6
+
7
+ client = OpenAI(base_url="https://openrouter.ai/api/v1", api_key=os.getenv("key"))
8
+
9
+ def read_pdf(pdf_file):
10
+
11
+ text = ""
12
+
13
+ with pdfplumber.open(pdf_file) as pdf:
14
+ for page in pdf.pages:
15
+ text += page.extract_text()
16
+
17
+ return text
18
+
19
+
20
+ def verify_certificate(text):
21
+
22
+ prompt = f"""
23
+ Analyze this internship certificate text and determine if it looks fake or genuine.
24
+ Return only: Fake or Genuine.
25
+
26
+ Certificate:
27
+ {text}
28
+ """
29
+
30
+ response = client.chat.completions.create(
31
+ model="z-ai/glm-4.5-air:free",
32
+ messages=[{"role":"user","content":prompt}]
33
+ )
34
+
35
+ return response.choices[0].message.content
db.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ from pymongo import MongoClient
2
+
3
+ MONGO_URI = "mongodb+srv://KavinTharanika:Tharakavin0114@cluster0.rqic8gp.mongodb.net/?retryWrites=true&w=majority"
4
+
5
+ client = MongoClient(MONGO_URI)
6
+
7
+ db = client["certificate_system"]
8
+
9
+ users_collection = db["users"]
10
+ cert_collection = db["certificates"]
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ gradio
2
+ pymongo
3
+ pdfplumber
4
+ openai
5
+ bcrypt
6
+ python-dotenv