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 = """ """ 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("

Welcome to AI Content Detector

", unsafe_allow_html=True) st.markdown("") columns = st.columns((2, 1, 2)) # Use custom CSS to style the button button_pressed = """ """ 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( """ """, 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("

Detect AI Content

", unsafe_allow_html=True) # Add some space st.markdown("
", 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("
", unsafe_allow_html=True) # Use CSS to style the button button_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("
", unsafe_allow_html=True) st.markdown("

Result:

", 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"

Error: Please enter text..💀

" elif result[0]['label'] == 'AI': result_html = f"

Predicted Label: {result[0]['label']} 😢

" else: result_html = f"

Predicted Label: {result[0]['label']} 🎉

" 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"

This text is likely to be written by {result[0]['label']} is {confidence_percentage}%

" st.markdown(score_html, unsafe_allow_html=True) progress_bar_css = f""" """ st.markdown(progress_bar_css, unsafe_allow_html=True) st.progress(result[0]['score']) st.markdown( """
Site is not optimize for mobile view.
""", unsafe_allow_html=True ) if __name__ == "__main__": main()