import streamlit as st import tensorflow as tf import numpy as np import nltk import os from nltk.tokenize import sent_tokenize from transformers import DistilBertTokenizerFast, TFDistilBertForSequenceClassification # 🧠 Ensure sentence tokenizer works inside Hugging Face (use /tmp/) nltk_data_path = "/tmp/nltk_data" nltk.download("punkt", download_dir=nltk_data_path) nltk.data.path.append(nltk_data_path) # šŸ“¦ Load tokenizer and model tokenizer = DistilBertTokenizerFast.from_pretrained("distilbert-base-uncased") model = TFDistilBertForSequenceClassification.from_pretrained("sundaram07/distilbert-sentence-classifier") # 🧠 Predict probability for one sentence def predict_sentence_ai_probability(sentence): inputs = tokenizer(sentence, return_tensors="tf", truncation=True, padding=True) outputs = model(inputs) logits = outputs.logits prob_ai = tf.sigmoid(logits)[0][0].numpy() # Assuming binary classification (single neuron) return prob_ai # šŸ“Š Analyze full text def predict_ai_generated_percentage(text, threshold=0.75): text = text.strip() sentences = sent_tokenize(text) ai_sentence_count = 0 results = [] for sentence in sentences: prob = predict_sentence_ai_probability(sentence) is_ai = prob >= threshold results.append((sentence, prob, is_ai)) if is_ai: ai_sentence_count += 1 total_sentences = len(sentences) ai_percentage = (ai_sentence_count / total_sentences) * 100 if total_sentences > 0 else 0.0 return ai_percentage, results # šŸš€ Streamlit UI st.title("🧠 AI Content Detector") st.markdown("This tool detects the percentage of **AI-generated content** based on sentence-level analysis.") user_input = st.text_area("šŸ“‹ Paste your text here:", height=300) if st.button("šŸ” Analyze"): if user_input.strip() == "": st.warning("āš ļø Please enter some text to analyze.") else: ai_percentage, analysis_results = predict_ai_generated_percentage(user_input) st.subheader("šŸ”Ž Sentence-level Analysis") for i, (sentence, prob, is_ai) in enumerate(analysis_results, start=1): label = "🟢 Human" if not is_ai else "šŸ”“ AI" st.markdown(f"**{i}.** _{sentence}_\n\n→ **Probability AI:** `{prob:.2%}` → {label}") st.subheader("šŸ“Š Final Result") st.success(f"Estimated **AI-generated content**: **{ai_percentage:.2f}%**")