File size: 3,336 Bytes
c01a3fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
import joblib

# Title and Description of the App
st.title("Human vs LLM-Generated Text Differentiator")
st.write("This app predicts whether a given text is human-written or generated by a language model (LLM).")

# Step 1: Upload Dataset
st.header("Step 1: Upload the RoFT Dataset")
uploaded_file = st.file_uploader("Upload your roft.csv file", type="csv")

if uploaded_file is not None:
    # Load dataset
    data = pd.read_csv(uploaded_file)
    st.write("Dataset Loaded Successfully!")

    # Display the first few rows of the dataset
    st.subheader("Sample of the Dataset:")
    st.dataframe(data.head())

    # Preprocessing the data
    st.header("Step 2: Preprocess the Data")

    # Combine prompt_body and gen_body to form the complete text
    data['text'] = data['prompt_body'].fillna('') + ' ' + data['gen_body'].fillna('')
    data['label'] = data['true_boundary_index'].apply(lambda x: 1 if x == 9 else 0)  # 1 = Human, 0 = LLM

    st.write("Data Preprocessing Complete!")

    # Show distribution of labels
    st.subheader("Label Distribution:")
    st.bar_chart(data['label'].value_counts())

    # Feature Extraction
    st.header("Step 3: Train the Model")
    st.write("Extracting features using TF-IDF and training a Random Forest classifier.")

    # TF-IDF Vectorization
    vectorizer = TfidfVectorizer(max_features=5000)
    X = vectorizer.fit_transform(data['text']).toarray()
    y = data['label']

    # Train-Test Split
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

    # Train a Random Forest Classifier
    model = RandomForestClassifier(n_estimators=100, random_state=42)
    model.fit(X_train, y_train)

    # Evaluate the model
    y_pred = model.predict(X_test)
    accuracy = accuracy_score(y_test, y_pred)
    st.write(f"Model Accuracy: {accuracy * 100:.2f}%")

    # Save the model and vectorizer
    joblib.dump(model, 'text_classifier.pkl')
    joblib.dump(vectorizer, 'vectorizer.pkl')
    st.success("Model Trained and Saved Successfully!")

    # Step 4: User Input for Prediction
    st.header("Step 4: Predict Human vs LLM-Generated Text")

    # Load the trained model and vectorizer
    model = joblib.load('text_classifier.pkl')
    vectorizer = joblib.load('vectorizer.pkl')

    # Input text from the user
    user_input = st.text_area("Enter the text you want to classify:")

    if st.button("Predict"):
        if user_input.strip():
            # Vectorize the input text
            input_vector = vectorizer.transform([user_input]).toarray()

            # Predict and show the result
            prediction = model.predict(input_vector)
            confidence = model.predict_proba(input_vector).max() * 100

            if prediction[0] == 1:
                st.success(f"The text is likely **Human-Written** with a confidence of {confidence:.2f}%.")
            else:
                st.warning(f"The text is likely **LLM-Generated** with a confidence of {confidence:.2f}%.")
        else:
            st.error("Please enter some text for prediction.")