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