SahilSingh0 commited on
Commit
869dd64
Β·
verified Β·
1 Parent(s): bc9aab1

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +20 -56
  2. requirements.txt +1 -0
app.py CHANGED
@@ -1,77 +1,41 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
- # πŸ”Ή Load model once and cache it
5
- # This runs only at startup, not on every request
6
- print("πŸ”„ Loading AI detection model...")
7
- pipe = pipeline("text-classification", model="roberta-base-openai-detector")
8
- print("βœ… Model loaded successfully!")
9
 
10
- def detect_text(text: str):
11
- """Detect whether the text is AI-generated or Human-written."""
12
- if not text.strip():
13
  return {}, "❌ Please enter some text."
14
 
15
  try:
16
- results = pipe(text)
17
-
18
- # Convert results into a probability dictionary
19
- probs = {r["label"]: float(r["score"]) for r in results}
20
-
21
- # Get label with highest probability
22
- verdict = str(max(probs, key=probs.get))
23
-
24
- # Make verdict more user-friendly
25
- if verdict.upper() in ["LABEL_0", "FAKE", "AI"]:
26
- verdict_message = "πŸ€– This looks AI-generated"
27
- elif verdict.upper() in ["LABEL_1", "REAL", "HUMAN"]:
28
- verdict_message = "πŸ“ This looks Human-written"
29
- else:
30
- verdict_message = f"⚠️ Unknown verdict: {verdict}"
31
-
32
- return probs, verdict_message
33
-
34
  except Exception as e:
35
  return {}, f"❌ Error: {str(e)}"
36
 
37
-
38
  with gr.Blocks() as demo:
39
- gr.Markdown(
40
- """
41
- # πŸ•΅οΈ AI Content Detector
42
- Paste some text below and check if it's **AI-generated or Human-written**.
43
- """
44
- )
45
 
46
  with gr.Row():
47
- text_input = gr.Textbox(
48
- label="✍️ Input Text",
49
- lines=10,
50
- placeholder="Paste text here..."
51
- )
52
 
53
  with gr.Row():
54
- output_probs = gr.Label(label="πŸ“Š Probabilities")
55
- output_verdict = gr.Textbox(label="βœ… Verdict", interactive=False)
56
 
57
  with gr.Row():
58
- submit_btn = gr.Button("πŸ”Ž Submit", variant="primary")
59
- clear_btn = gr.Button("🧹 Clear")
60
-
61
- # Submit action
62
- submit_btn.click(
63
- fn=detect_text,
64
- inputs=text_input,
65
- outputs=[output_probs, output_verdict],
66
- )
67
 
68
- # Clear action
69
- clear_btn.click(
70
- fn=lambda: ("", {}, ""),
71
- inputs=[],
72
- outputs=[text_input, output_probs, output_verdict],
73
- )
74
 
75
- # πŸ”Ή Launch app
76
  if __name__ == "__main__":
77
  demo.launch()
 
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
+ # Load state-of-the-art AI text detector
5
+ text_pipe = pipeline("text-classification", model="wangkevin02/AI_Detect_Model")
 
 
 
6
 
7
+ def detect_text(input_text: str):
8
+ if not input_text.strip():
 
9
  return {}, "❌ Please enter some text."
10
 
11
  try:
12
+ outputs = text_pipe(input_text)
13
+ # Outputs look like: [{'label':'0'/'1','score':...}]
14
+ label = outputs[0]["label"]
15
+ score = float(outputs[0]["score"])
16
+
17
+ prob = {"Human-written": 1 - score, "AI-generated": score}
18
+ verdict = "πŸ€– This looks AI-generated" if label == "1" else "πŸ“ This looks Human-written"
19
+ return prob, verdict
 
 
 
 
 
 
 
 
 
 
20
  except Exception as e:
21
  return {}, f"❌ Error: {str(e)}"
22
 
 
23
  with gr.Blocks() as demo:
24
+ gr.Markdown("# AI Content Detector (Upgraded)\nFaster and more reliable detection using an advanced classifier.")
 
 
 
 
 
25
 
26
  with gr.Row():
27
+ text_input = gr.Textbox(lines=10, placeholder="Paste text here...", label="Enter Text to Analyze")
 
 
 
 
28
 
29
  with gr.Row():
30
+ output_probs = gr.Label(label="Probabilities")
31
+ output_verdict = gr.Textbox(label="Verdict", interactive=False)
32
 
33
  with gr.Row():
34
+ submit_btn = gr.Button("Check Text", variant="primary")
35
+ clear_btn = gr.Button("Clear")
 
 
 
 
 
 
 
36
 
37
+ submit_btn.click(detect_text, inputs=text_input, outputs=[output_probs, output_verdict])
38
+ clear_btn.click(lambda: ("", {}, ""), inputs=[], outputs=[text_input, output_probs, output_verdict])
 
 
 
 
39
 
 
40
  if __name__ == "__main__":
41
  demo.launch()
requirements.txt CHANGED
@@ -3,3 +3,4 @@ transformers==4.43.3
3
  torch>=2.0.0
4
  accelerate>=0.33.0
5
  safetensors>=0.4.2
 
 
3
  torch>=2.0.0
4
  accelerate>=0.33.0
5
  safetensors>=0.4.2
6
+ pillow>=10.0.0