File size: 7,601 Bytes
85910a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import streamlit as st
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.ensemble import ExtraTreesClassifier

# Load and preprocess the data
data = pd.read_csv('dataset.csv')
vectorizer = TfidfVectorizer()
X = vectorizer.fit_transform(data['text'])
y = data['label']

# Train the model
classifier = ExtraTreesClassifier(n_estimators=50, random_state=2)
classifier.fit(X, y)

# Define the prediction function
def predict(text):
    if not text:
        return [{'label': 'Error', 'score': 0}]
    text_vectorized = vectorizer.transform([text])
    prediction = classifier.predict(text_vectorized)[0]
    if prediction == 'AI':
        score = classifier.predict_proba(text_vectorized)[0][0]
    else:
        score = 1 - classifier.predict_proba(text_vectorized)[0][1]
    response = [
        {
            'label': prediction,
            'score': round(float(score), 4)
        }
    ]
    return response

# Create a Streamlit app
def main():
    st.set_page_config(
        page_title="AI Detector",
        page_icon="πŸ€–",
        layout="wide",
    )
    page_bg_img = """
    <style>
    [data-testid="stAppViewContainer"] {
        background-image: url("https://images.unsplash.com/photo-1698945746290-a9d1cc575e77?auto=format&fit=crop&q=80&w=1946&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D");
        background-size: cover;
        background-repeat: no-repeat;
        background-position: center;
    }
    [data-testid="stHeader"] {
        background-color: transparent;
    }

    data-testid="stToolbar"{
    right: 2rem;
    }
    </style>
    """
    st.markdown(page_bg_img, unsafe_allow_html=True)

    # Initialize session state
    if 'started' not in st.session_state:
        st.session_state.started = False

    # Check if the "Get Started" button is clicked
    if not st.session_state.started:
        st.markdown("<h1 style='display: flex; text-align:center;justify-content: center; align-items: center; font-size: 50px ; height: 55vh;'>Welcome to AI Content Detector</h1>", unsafe_allow_html=True)
        st.markdown("")
        columns = st.columns((2, 1, 2))

        # Use custom CSS to style the button

        button_pressed = """
            <style>
                div.stButton > button:first-child {
                background-color: #000;
                border: 1px solid #000;
                border-radius: 20px;
                color: #fff;
                cursor: pointer;
                display: inline-block;
                font-family: ITCAvantGardeStd-Bk, Arial, sans-serif;
                font-size: 20px;
                font-weight: 50;
                line-height: 40px;
                padding: 12px 40px;
                text-align: center;
                text-transform: none;
                user-select: none;
                -webkit-user-select: none;
                width: max-content;
                }
                div.stButton > button:first-child:hover {
                    background-color: #ffffff;
                    border: 1px solid #000;
                    color: #000000;
                    font-family: monospace;
                }
            </style>
        """
        st.markdown(button_pressed, unsafe_allow_html=True)

        button_pressed = columns[1].button("Get Started", key="get_started_button", on_click=lambda: st.session_state.__setitem__('started', True))
        if button_pressed:
            st.session_state.started = True
            st.markdown(
            """
            <script>
                var button = document.querySelector('.stButton button.button-50');
                button.style.backgroundColor = "#000"; // Change background color to black
            </script>
            """,
            unsafe_allow_html=True,
        )

    # If the app has been started
    if st.session_state.started:
        st.markdown("")

        # Use HTML to create a heading in the main app
        st.markdown("<h1 style='text-align:center;font-size: 50px;height: 15vh;'>Detect AI Content</h1>", unsafe_allow_html=True)

        # Add some space
        st.markdown("<br>", unsafe_allow_html=True)

        # Create a text input with a label
        text = st.text_area(" ", placeholder="Type your text here", height=200)

        # Add space between input and button
        st.markdown("<br>", unsafe_allow_html=True)

        # Use CSS to style the button
        button_style = """
            <style>
                div.stButton > button:first-child {
                    background-color: #000;
                    color: #fff;
                    width: 150px;
                    Height: 50px;
                    display: flex;
                    float: right;
                    color: white;
                    font-size: 18px;
                    border: none;
                    cursor: pointer;
                    margin-right: 16cm;
                }
                div.stButton > button:first-child:hover {
                    background-color: #fff;
                    color: #000000;
                    border: 1px solid #000;
                }
            </style>
        """
        st.markdown(button_style, unsafe_allow_html=True)

        # Create a button to trigger the analysis
        if st.button("Detect Text"):
            result = predict(text)
            st.markdown("<hr>", unsafe_allow_html=True)
            st.markdown("<h2>Result:</h2>", unsafe_allow_html=True)

            if result[0]['label'] == 'Error':
                label_color = "color: #FF2400;"
                progress_color = "#FF2400"
            elif result[0]['label'] == 'AI':
                label_color = "color: #FF2400;"
                progress_color = "#FF2400"
            else:
                label_color = "color: #00B140;"
                progress_color = "#00B140"

            # Use HTML to style the prediction result
            if result[0]['label'] == 'Error':
                result_html = f"<p style='font-size: 30px; font-weight: bold; {label_color}'>Error: Please enter text..πŸ’€</p>"
            elif result[0]['label'] == 'AI':
                result_html = f"<p style='font-size: 30px; font-weight: bold; {label_color}'>Predicted Label: {result[0]['label']} 😒</p>"
            else:
                result_html = f"<p style='font-size: 30px; font-weight: bold; {label_color}'>Predicted Label: {result[0]['label']} πŸŽ‰</p>"
            st.markdown(result_html, unsafe_allow_html=True)

            if result[0]['label'] != 'Error':

                # Add a status bar for the Confidence Score with percentages
                confidence_percentage = result[0]['score'] * 100

                score_html = f"<p style='font-size: 25px;{label_color}'>This text is likely to be written by {result[0]['label']} is {confidence_percentage}%</p>"
                st.markdown(score_html, unsafe_allow_html=True)
                progress_bar_css = f"""
                <style>
                    .stProgress > div > div > div > div {{
                        background-color: {progress_color};
                    }}
                </style>
                """
                st.markdown(progress_bar_css, unsafe_allow_html=True)
                st.progress(result[0]['score'])




    st.markdown(
            """
            <div style="position: absolute; bottom: -5.8cm;">
                <b color: #000000;">Site is not optimize for mobile view.</b>
            </div>
            """,
            unsafe_allow_html=True
        )

if __name__ == "__main__":
    main()