yagnik12 commited on
Commit
95b8024
·
verified ·
1 Parent(s): 4123be0

Upload 2 files

Browse files
Files changed (2) hide show
  1. ai_text_detector_valid_final.py +59 -0
  2. app.py +18 -0
ai_text_detector_valid_final.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
3
+ import numpy as np
4
+
5
+ # Multiple AI text detection models
6
+ MODELS = {
7
+ "OpenAI Detector": "roberta-base-openai-detector", # HuggingFace OpenAI GPT-2 detector
8
+ "GPT Detector": "Kishanjaisoorya/AI-Text-Detector", # Community AI detector
9
+ "GPTZero-Style": "Hello-SimpleAI/chatgpt-detector-roberta" # Another HF model
10
+ }
11
+
12
+ def load_model(model_id):
13
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
14
+ model = AutoModelForSequenceClassification.from_pretrained(model_id)
15
+ return tokenizer, model
16
+
17
+ def predict(text, tokenizer, model):
18
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
19
+ with torch.no_grad():
20
+ outputs = model(**inputs)
21
+ probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
22
+ return probs[0].numpy() # [human_prob, ai_prob]
23
+
24
+ def detect_text(text):
25
+ results = {}
26
+ ai_scores = []
27
+
28
+ for name, model_id in MODELS.items():
29
+ try:
30
+ tokenizer, model = load_model(model_id)
31
+ probs = predict(text, tokenizer, model)
32
+ human_score, ai_score = probs
33
+ results[name] = {
34
+ "Human Probability": round(float(human_score) * 100, 2),
35
+ "AI Probability": round(float(ai_score) * 100, 2),
36
+ }
37
+ ai_scores.append(ai_score)
38
+ except Exception as e:
39
+ results[name] = {"error": str(e)}
40
+
41
+ # Final average score across models
42
+ if ai_scores:
43
+ avg_ai = np.mean(ai_scores) * 100
44
+ results["Final Score"] = {
45
+ "AI Probability (average)": round(avg_ai, 2),
46
+ "Human Probability (average)": round(100 - avg_ai, 2),
47
+ }
48
+
49
+ return results
50
+
51
+
52
+ if __name__ == "__main__":
53
+ text = input("Enter text to analyze:\n")
54
+ output = detect_text(text)
55
+ print("\n--- Detection Results ---")
56
+ for model, scores in output.items():
57
+ print(f"\n[{model}]")
58
+ for k, v in scores.items():
59
+ print(f"{k}: {v}%")
app.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import ai_text_detector_valid_final as detector
3
+
4
+ def run_detector(text):
5
+ try:
6
+ return detector.detect_text(text) # change if your function is named differently
7
+ except Exception as e:
8
+ return f"Error: {e}"
9
+
10
+ demo = gr.Interface(
11
+ fn=run_detector,
12
+ inputs=gr.Textbox(lines=10, placeholder="Paste your text here..."),
13
+ outputs="text",
14
+ title="AI Text Detector",
15
+ description="Check if text is AI-generated or human."
16
+ )
17
+
18
+ demo.launch()