Update app.py
Browse files
app.py
CHANGED
@@ -3,35 +3,41 @@ import torch
|
|
3 |
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
4 |
import requests
|
5 |
import re
|
|
|
6 |
from bs4 import BeautifulSoup
|
|
|
7 |
|
8 |
-
#
|
9 |
MODEL_NAME = "roberta-base-openai-detector"
|
10 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
11 |
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
12 |
|
13 |
-
# AI
|
14 |
def detect_ai_content(text):
|
15 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
16 |
outputs = model(**inputs)
|
17 |
scores = torch.nn.functional.softmax(outputs.logits, dim=1)
|
18 |
-
return scores[0][1].item()
|
19 |
|
20 |
-
# Writing
|
21 |
def stylometry_analysis(text):
|
22 |
words = text.split()
|
23 |
avg_word_length = sum(len(word) for word in words) / len(words) if words else 0
|
24 |
complex_words_ratio = len([word for word in words if len(word) > 6]) / len(words) if words else 0
|
25 |
passive_voice_count = len(re.findall(r'\b(is|was|were|has been|have been|had been)\b \w+ed', text))
|
26 |
-
return {
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
# Semantic
|
29 |
def semantic_analysis(text):
|
30 |
keywords = ["AI", "generated", "neural network", "LLM", "GPT", "transformer"]
|
31 |
ai_patterns = sum([text.lower().count(keyword) for keyword in keywords])
|
32 |
return ai_patterns / len(text.split()) if text.split() else 0
|
33 |
|
34 |
-
# Web
|
35 |
def check_plagiarism(text):
|
36 |
search_url = f"https://www.google.com/search?q={'+'.join(text.split()[:10])}"
|
37 |
headers = {"User-Agent": "Mozilla/5.0"}
|
@@ -42,24 +48,53 @@ def check_plagiarism(text):
|
|
42 |
return any("wikipedia" in link or "researchgate" in link or "arxiv" in link for link in links)
|
43 |
return False
|
44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
# Streamlit UI
|
46 |
-
st.title("
|
47 |
-
st.write("
|
|
|
|
|
48 |
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
56 |
|
57 |
-
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
3 |
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
4 |
import requests
|
5 |
import re
|
6 |
+
import numpy as np
|
7 |
from bs4 import BeautifulSoup
|
8 |
+
from datasets import load_dataset
|
9 |
|
10 |
+
# Load AI detection model
|
11 |
MODEL_NAME = "roberta-base-openai-detector"
|
12 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
13 |
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
14 |
|
15 |
+
# AI content detection function
|
16 |
def detect_ai_content(text):
|
17 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
18 |
outputs = model(**inputs)
|
19 |
scores = torch.nn.functional.softmax(outputs.logits, dim=1)
|
20 |
+
return scores[0][1].item() # AI probability
|
21 |
|
22 |
+
# Writing style analysis
|
23 |
def stylometry_analysis(text):
|
24 |
words = text.split()
|
25 |
avg_word_length = sum(len(word) for word in words) / len(words) if words else 0
|
26 |
complex_words_ratio = len([word for word in words if len(word) > 6]) / len(words) if words else 0
|
27 |
passive_voice_count = len(re.findall(r'\b(is|was|were|has been|have been|had been)\b \w+ed', text))
|
28 |
+
return {
|
29 |
+
"avg_word_length": avg_word_length,
|
30 |
+
"complex_words_ratio": complex_words_ratio,
|
31 |
+
"passive_voice_count": passive_voice_count
|
32 |
+
}
|
33 |
|
34 |
+
# Semantic similarity analysis
|
35 |
def semantic_analysis(text):
|
36 |
keywords = ["AI", "generated", "neural network", "LLM", "GPT", "transformer"]
|
37 |
ai_patterns = sum([text.lower().count(keyword) for keyword in keywords])
|
38 |
return ai_patterns / len(text.split()) if text.split() else 0
|
39 |
|
40 |
+
# Web crawling for plagiarism detection
|
41 |
def check_plagiarism(text):
|
42 |
search_url = f"https://www.google.com/search?q={'+'.join(text.split()[:10])}"
|
43 |
headers = {"User-Agent": "Mozilla/5.0"}
|
|
|
48 |
return any("wikipedia" in link or "researchgate" in link or "arxiv" in link for link in links)
|
49 |
return False
|
50 |
|
51 |
+
# Self-learning system (Internet AI Content Collection)
|
52 |
+
def collect_ai_generated_text():
|
53 |
+
dataset = load_dataset("openai/webgpt_comparisons", split="train")
|
54 |
+
ai_text_samples = [item["answer_1"] for item in dataset.select(range(50))]
|
55 |
+
return ai_text_samples
|
56 |
+
|
57 |
+
# Auto-learn from new AI models
|
58 |
+
def update_model_with_new_patterns():
|
59 |
+
new_ai_texts = collect_ai_generated_text()
|
60 |
+
for text in new_ai_texts:
|
61 |
+
ai_score = detect_ai_content(text)
|
62 |
+
if ai_score < 0.5:
|
63 |
+
# Fine-tune model logic (this will need cloud-based model retraining)
|
64 |
+
pass
|
65 |
+
|
66 |
# Streamlit UI
|
67 |
+
st.title("Self-Learning AI Content Detector")
|
68 |
+
st.write("Detect AI-generated text and continuously learn from new AI models.")
|
69 |
+
|
70 |
+
text_input = st.text_area("Enter text to analyze:")
|
71 |
|
72 |
+
if st.button("Analyze"):
|
73 |
+
if text_input.strip():
|
74 |
+
ai_probability = detect_ai_content(text_input)
|
75 |
+
writing_features = stylometry_analysis(text_input)
|
76 |
+
semantic_score = semantic_analysis(text_input)
|
77 |
+
is_plagiarized = check_plagiarism(text_input)
|
78 |
|
79 |
+
# Multi-layer AI detection logic
|
80 |
+
is_ai_generated = (
|
81 |
+
ai_probability > 0.5 or
|
82 |
+
writing_features["complex_words_ratio"] > 0.4 or
|
83 |
+
semantic_score > 0.2 or
|
84 |
+
is_plagiarized
|
85 |
+
)
|
86 |
|
87 |
+
result = "AI-Generated" if is_ai_generated else "Human-Written"
|
88 |
|
89 |
+
st.subheader("Detection Results")
|
90 |
+
st.write(f"**AI Probability:** {ai_probability:.2f}")
|
91 |
+
st.write(f"**Complex Words Ratio:** {writing_features['complex_words_ratio']:.2f}")
|
92 |
+
st.write(f"**Passive Voice Count:** {writing_features['passive_voice_count']}")
|
93 |
+
st.write(f"**Semantic Score:** {semantic_score:.2f}")
|
94 |
+
st.write(f"**Plagiarism Detected:** {'Yes' if is_plagiarized else 'No'}")
|
95 |
+
st.subheader(f"Final Verdict: {result}")
|
96 |
+
|
97 |
+
# Auto-learn from new AI patterns
|
98 |
+
update_model_with_new_patterns()
|
99 |
+
else:
|
100 |
+
st.error("Please enter some text for analysis.")
|