File size: 6,883 Bytes
be8d0f3
c579034
0aff634
ed2b9aa
0aff634
 
 
ed2b9aa
0aff634
73e907d
 
 
0aff634
 
ed2b9aa
 
 
 
 
 
fec7940
db7d9e9
fec7940
 
 
 
 
 
 
 
73e907d
c3efa14
73e907d
 
 
 
 
 
 
 
 
 
 
 
7f1c645
db7d9e9
7d12ca3
db7d9e9
c3efa14
 
 
fec7940
db7d9e9
73e907d
db7d9e9
73e907d
0aff634
73e907d
0aff634
73e907d
 
 
 
fec7940
 
 
1b45c09
73e907d
fec7940
0aff634
db7d9e9
fec7940
 
c3efa14
 
fec7940
e3ab5e9
73e907d
 
 
7f1c645
73e907d
 
 
 
 
0aff634
fec7940
 
db7d9e9
73e907d
 
fec7940
73e907d
fec7940
73e907d
7f1c645
db7d9e9
cf11161
882b3ea
 
7f1c645
f8843b8
c579034
ed2b9aa
c579034
 
083d065
 
c579034
 
ed2b9aa
c579034
083d065
 
7f1c645
c579034
 
 
ed2b9aa
7f1c645
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c579034
e5b1de1
fec7940
e5b1de1
 
fec7940
db7d9e9
fec7940
 
e5b1de1
 
fec7940
db7d9e9
fec7940
e5b1de1
fec7940
e5b1de1
7f1c645
 
 
 
 
0aff634
73e907d
7f1c645
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
175
176
177
178
179
180
181
import io
import os
import streamlit as st
import requests
from PIL import Image
from model import get_caption_model, generate_caption
from googletrans import Translator
import sqlite3

# Initialize Streamlit app
st.set_page_config(page_title="Image Caption Generator", layout="wide")

translator = Translator()

@st.cache_resource
def get_model():
    return get_caption_model()

caption_model = get_model()

# Constants
SIGNUP_SUCCESS_MSG = "Signup successful! You can now login."
SIGNUP_ERROR_EXISTING_USER = "Username already exists. Please choose a different username."
LOGIN_SUCCESS_MSG = "Login successful!"
LOGIN_ERROR_INVALID_CREDENTIALS = "Login failed. Invalid username or password."

# Define CSS styles
heading_style = "font-size: 24px; font-weight: bold; text-align: center;"
input_style = "margin-top: 10px; padding: 5px; width: 100%;"

# Function to create the SQLite table if it doesn't exist
@st.cache_resource
def create_table():
    with sqlite3.connect("login.db") as conn:
        cursor = conn.cursor()
        cursor.execute('''
            CREATE TABLE IF NOT EXISTS users (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                username TEXT NOT NULL UNIQUE,
                password TEXT NOT NULL,
                email TEXT NOT NULL,
                role TEXT NOT NULL
            )
        ''')

# Function for signup section
def signup_section():
    st.markdown(f"<p style='{heading_style}'>Signup</p>", unsafe_allow_html=True)
    new_username = st.text_input("New Username", key="new_username", help="Choose a unique username")
    new_password = st.text_input("New Password", type="password", key="new_password",  help="Password should be at least 8 characters long")
    new_email = st.text_input("Email", key="new_email", help="Enter a valid email address")

    if st.button("Signup"):
        if not new_username or not new_password or not new_email:
            st.error("All fields are required for signup.")
            return

        role = "user"

        try:
            with sqlite3.connect("login.db") as conn:
                cursor = conn.cursor()
                cursor.execute("INSERT INTO users (username, password, email, role) VALUES (?, ?, ?, ?)",
                               (new_username, new_password, new_email, role))
            st.success(SIGNUP_SUCCESS_MSG)
            st.balloons()
            
        except sqlite3.IntegrityError:
            st.error(SIGNUP_ERROR_EXISTING_USER)

# Function for login section
def login_section():
    st.markdown(f"<p style='{heading_style}'>Login</p>", unsafe_allow_html=True)
    username = st.text_input("Username", key="login_username", help="Enter your username")
    password = st.text_input("Password", type="password", key="login_password",help="Enter your password")

    if st.button("Login"):
        if not username or not password:
            st.error("Username and password are required for login.")
            return

        try:
            with sqlite3.connect("login.db") as conn:
                cursor = conn.cursor()
                cursor.execute("SELECT * FROM users WHERE username = ?", (username,))
                user = cursor.fetchone()

            if user and user[2] == password:
                st.success(LOGIN_SUCCESS_MSG)
                st.write(f"You are logged in as: {user[1]}")
                st.session_state.username = username
                st.session_state.selected_tab = "Generate Caption"
                st.balloons()
            else:
                st.error(LOGIN_ERROR_INVALID_CREDENTIALS)
        except sqlite3.OperationalError as e:
            st.error(f"An error occurred while trying to log in: {e}")

def translate_caption(caption, target_language):
    translated = translator.translate(caption, dest=target_language)
    return translated.text

def predict(cap_col):
    captions = []
    pred_caption = generate_caption('tmp.jpg', caption_model)

    cap_col.markdown('#### Predicted Captions:')
    translated_caption = translate_caption(pred_caption, target_language)
    captions.append(translated_caption)

    for _ in range(4):
        pred_caption = generate_caption('tmp.jpg', caption_model, add_noise=True)
        if pred_caption not in captions:
            translated_caption = translate_caption(pred_caption, target_language)
            captions.append(translated_caption)

    cap_col.markdown('<div class="caption-container">', unsafe_allow_html=True)
    for c in captions:
        cap_col.markdown(f'<div class="cap-line" style="color: black; background-color: light grey; padding: 5px; margin-bottom: 5px; font-family: \'Palatino Linotype\', \'Book Antiqua\', Palatino, serif;">{c}</div>', unsafe_allow_html=True)
    cap_col.markdown('</div>', unsafe_allow_html=True)

def generate_caption_section():
    st.markdown('<h1 style="text-align:center; font-family:Arial; width:fit-content; font-size:3em; color:black; text-shadow: 2px 2px 4px #000000;">IMAGE CAPTION GENERATOR</h1>', unsafe_allow_html=True)
    col1, col2 = st.columns(2)

    # Image URL input
    img_url = st.text_input(label='Enter Image URL')

    # Image upload input
    img_upload = st.file_uploader(label='Upload Image', type=['jpg', 'png', 'jpeg'])

    # Language selection dropdown
    target_language = st.selectbox('Select Target Language', ['en', 'ta', 'hi', 'es', 'fr', 'zh-cn'], index=0)

    # Process image and generate captions
    if img_url:
        img = Image.open(requests.get(img_url, stream=True).raw)
        img = img.convert('RGB')
        col1.image(img, caption="Input Image", use_column_width=True)
        img.save('tmp.jpg')
        predict(col2)

        st.markdown('<center style="opacity: 70%">OR</center>', unsafe_allow_html=True)

    elif img_upload:
        img = img_upload.read()
        img = Image.open(io.BytesIO(img))
        img = img.convert('RGB')
        col1.image(img, caption="Input Image", use_column_width=True)
        img.save('tmp.jpg')
        predict(col2)

    # Remove temporary image file
    if img_url or img_upload:
        os.remove('tmp.jpg')

def main():
    # Create the database table if it doesn't exist
    create_table()

    # Define the navigation tabs
    tabs = ["Signup", "Login", "Generate Caption"]

    # Select the active tab based on user input
    selected_tab = st.sidebar.selectbox("Navigation", tabs)

    # Route to the appropriate section based on the selected tab
    if selected_tab == "Signup":
        signup_section()
    elif selected_tab == "Login":
        login_section()
    elif selected_tab == "Generate Caption":
        # Check if the user is logged in before allowing access to the image caption generation
        if 'username' in st.session_state:
            generate_caption_section()
        else:
            st.write("Please login to access this feature.")

if __name__ == "__main__":
    main()