File size: 5,793 Bytes
40f9579
 
 
 
 
 
 
 
 
 
 
ddc8227
41712a7
a5c43b8
40f9579
 
 
 
a5c43b8
a5015e9
40f9579
a5c43b8
577935d
 
40f9579
 
 
 
 
 
 
 
577935d
40f9579
 
 
 
a9732bf
40f9579
 
 
 
 
 
a5c43b8
40f9579
 
 
 
 
 
a5c43b8
40f9579
 
 
 
 
a5c43b8
40f9579
 
a2737e5
40f9579
a2737e5
40f9579
 
a2737e5
40f9579
 
 
 
a2737e5
40f9579
a5c43b8
40f9579
 
 
a2737e5
 
 
ddc8227
a5c43b8
40f9579
a2737e5
cac13e1
 
a5c43b8
 
 
 
 
 
 
 
 
 
 
 
 
 
a9732bf
 
 
 
 
 
cac13e1
 
 
a5c43b8
 
 
a9732bf
a5c43b8
 
a9732bf
a5c43b8
 
a9732bf
a5c43b8
a2737e5
 
a5c43b8
 
 
 
 
 
 
cac13e1
a5c43b8
a9732bf
a5c43b8
 
 
 
 
cac13e1
a9732bf
a5c43b8
a2737e5
 
 
a5c43b8
a2737e5
 
a5c43b8
 
 
 
cac13e1
a2737e5
 
a5c43b8
a2737e5
 
 
cac13e1
a2737e5
a5c43b8
a2737e5
 
 
a5c43b8
a2737e5
 
 
 
 
cac13e1
a2737e5
 
40f9579
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import gradio as gr
import sqlite3
import re
import bcrypt
import numpy as np
import cv2
from PIL import Image
import tensorflow as tf
import os
import warnings

from pages import about, community, user_guide

# Suppress TensorFlow and warning logs
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
warnings.filterwarnings("ignore")
np.seterr(all='ignore')

# Load TensorFlow model
deepfake_model = tf.keras.models.load_model("model_15_64.h5")

# Setup SQLite database
db_path = os.path.abspath("users.db")
conn = sqlite3.connect(db_path, check_same_thread=False)
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS user_details (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    NAME TEXT,
    PHONE TEXT,
    EMAIL TEXT UNIQUE,
    GENDER TEXT,
    PASSWORD BLOB
)
''')
conn.commit()

# Validators
def is_valid_email(email):
    return re.match(r"[^@]+@[^@]+\.[^@]+", email)

def is_valid_phone(phone):
    return re.match(r"^[0-9]{10}$", phone)

# Image preprocessing
def preprocess_image(image):
    image = np.array(image)
    image = cv2.resize(image, (128, 128))
    image = image.astype(np.float32) / 255.0
    return np.expand_dims(image, axis=0)

# Prediction
def predict_image(image):
    preprocessed = preprocess_image(image)
    prediction = deepfake_model.predict(preprocessed)[0][0]
    return "βœ… Real Image" if prediction >= 0.5 else "⚠️ Fake Image"

# Register logic
def register_user(name, phone, email, password):
    if not is_valid_email(email):
        return "❌ Invalid email"
    if not is_valid_phone(phone):
        return "❌ Phone must be 10 digits"
    cursor.execute("SELECT * FROM user_details WHERE EMAIL = ?", (email,))
    if cursor.fetchone():
        return "⚠️ Email already registered"
    hashed_pw = bcrypt.hashpw(password.encode(), bcrypt.gensalt())
    cursor.execute("INSERT INTO user_details (NAME, PHONE, EMAIL, GENDER, PASSWORD) VALUES (?, ?, ?, ?, ?)",
                   (name, phone, email, "U", hashed_pw))
    conn.commit()
    return "βœ… Registration successful! Please log in."

# Login logic
def login_user(email, password):
    cursor.execute("SELECT PASSWORD FROM user_details WHERE EMAIL = ?", (email,))
    result = cursor.fetchone()
    if result and bcrypt.checkpw(password.encode(), result[0] if isinstance(result[0], bytes) else result[0].encode()):
        return True
    return False

# Gradio UI
with gr.Blocks() as demo:
    is_logged_in = gr.State(False)
    active_tab = gr.State(0)  # To manage which tab is active

    with gr.Tabs():
        # Login Tab (Visible by default)
        with gr.Tab("πŸ” Login") as login_tab:
            gr.Markdown("### Login or Sign Up")
            name = gr.Textbox(label="Name (Sign Up Only)")
            phone = gr.Textbox(label="Phone (Sign Up Only)")
            email = gr.Textbox(label="Email")
            password = gr.Textbox(label="Password", type="password")
            login_btn = gr.Button("Login")
            signup_btn = gr.Button("Sign Up")
            message_output = gr.Markdown("", visible=False)

        # Detect Deepfake Tab (Initially Hidden)
        with gr.Tab("πŸ§ͺ Detect Deepfake", visible=False) as detect_tab:
            gr.Markdown("### Upload an Image to Detect Deepfake")
            image_input = gr.Image(type="pil")
            result = gr.Textbox(label="Prediction Result")
            predict_btn = gr.Button("Predict")
            predict_btn.click(fn=predict_image, inputs=image_input, outputs=result)

            # Logout button inside the Detect tab
            logout_btn = gr.Button("Logout", visible=False)
        
        # About, Community, User Guide (Always visible)
        with gr.Tab("ℹ️ About"):
            about.layout()

        with gr.Tab("🌐 Community"):
            community.layout()

        with gr.Tab("πŸ“˜ User Guide"):
            user_guide.layout()

    # Handlers
    def handle_login(email, password):
        success = login_user(email, password)
        if success:
            return (
                "βœ… Login successful!",  # Success message
                True,  # Update logged-in state
                gr.update(visible=False),  # Hide Login tab
                gr.update(visible=True),  # Show Detect tab
                gr.update(visible=True),  # Show Logout button
                gr.update(visible=True),  # Show Detect Deepfake tab
            )
        return (
            "❌ Invalid credentials",  # Error message
            False,  # Keep logged-in state False
            gr.update(visible=True),  # Keep Login tab visible
            gr.update(visible=False),  # Hide Detect tab
            gr.update(visible=False),  # Hide Logout button
            gr.update(visible=False),  # Hide Detect Deepfake tab
        )
    
    def handle_signup(name, phone, email, password):
        msg = register_user(name, phone, email, password)
        return gr.update(value=msg, visible=True)
    
    def handle_logout():
        return (
            False,  # User logs out
            gr.update(visible=True),  # Show Login tab again
            gr.update(visible=False),  # Hide Detect tab
            gr.update(visible=False),  # Hide Logout button
            gr.update(visible=False),  # Hide Detect Deepfake tab
        )

    # Button clicks
    login_btn.click(
        fn=handle_login,
        inputs=[email, password],
        outputs=[message_output, is_logged_in, login_tab, detect_tab, logout_btn, detect_tab]
    )
    
    signup_btn.click(
        fn=handle_signup,
        inputs=[name, phone, email, password],
        outputs=[message_output]
    )

    logout_btn.click(
        fn=handle_logout,
        inputs=[],
        outputs=[is_logged_in, login_tab, detect_tab, logout_btn, detect_tab]
    )

if __name__ == "__main__":
    demo.launch()