changcheng967 commited on
Commit
1c825c2
·
verified ·
1 Parent(s): 8a0fa9c

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. app.py +64 -5
  2. requirements.txt +1 -0
app.py CHANGED
@@ -1,8 +1,10 @@
1
  import gradio as gr
2
- from transformers import AutoModelForCausalLM, AutoTokenizer
3
  import torch
 
4
 
5
  MODEL_NAME = "changcheng967/Aegis-Qwen3-1.7B-SFT"
 
6
 
7
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True)
8
  model = AutoModelForCausalLM.from_pretrained(
@@ -12,6 +14,14 @@ model = AutoModelForCausalLM.from_pretrained(
12
  trust_remote_code=True,
13
  )
14
 
 
 
 
 
 
 
 
 
15
  SYSTEM_PROMPT = (
16
  "You are a student rewriting text in your own natural voice. "
17
  "Rewrite the following AI-generated text to sound like a real student wrote it. "
@@ -20,9 +30,38 @@ SYSTEM_PROMPT = (
20
  )
21
 
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  def humanize(ai_text, temperature, max_tokens):
24
  if not ai_text.strip():
25
- return "Please enter some AI-generated text."
26
 
27
  messages = [
28
  {"role": "system", "content": SYSTEM_PROMPT},
@@ -42,7 +81,24 @@ def humanize(ai_text, temperature, max_tokens):
42
  )
43
 
44
  response = tokenizer.decode(outputs[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True)
45
- return response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
 
48
  demo = gr.Interface(
@@ -52,9 +108,12 @@ demo = gr.Interface(
52
  gr.Slider(0.1, 1.5, value=0.7, step=0.1, label="Temperature"),
53
  gr.Slider(128, 1024, value=512, step=64, label="Max Tokens"),
54
  ],
55
- outputs=gr.Textbox(label="Humanized Text", lines=8),
 
 
 
56
  title="Aegis Humanizer",
57
- description="Rewrites AI-generated text to sound like a real student wrote it. Powered by Aegis Qwen3-1.7B SFT.",
58
  examples=[
59
  ["Artificial intelligence has significantly impacted the field of education. It provides personalized learning experiences and helps teachers identify areas where students need improvement.", 0.7, 512],
60
  ["Climate change is one of the most pressing issues facing the world today. Rising temperatures, melting ice caps, and extreme weather events are all consequences of human activity.", 0.7, 512],
 
1
  import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
3
  import torch
4
+ import numpy as np
5
 
6
  MODEL_NAME = "changcheng967/Aegis-Qwen3-1.7B-SFT"
7
+ DETECTOR_NAME = "desklib/ai-text-detector-v1.01"
8
 
9
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True)
10
  model = AutoModelForCausalLM.from_pretrained(
 
14
  trust_remote_code=True,
15
  )
16
 
17
+ detector = pipeline(
18
+ "text-classification",
19
+ model=DETECTOR_NAME,
20
+ device_map="auto",
21
+ truncation=True,
22
+ max_length=512,
23
+ )
24
+
25
  SYSTEM_PROMPT = (
26
  "You are a student rewriting text in your own natural voice. "
27
  "Rewrite the following AI-generated text to sound like a real student wrote it. "
 
30
  )
31
 
32
 
33
+ def compute_stats(text):
34
+ sentences = [s.strip() for s in text.replace("!", ".").replace("?", ".").split(".") if s.strip()]
35
+ if not sentences:
36
+ return {"burstiness": 0, "avg_sentence_len": 0, "sentence_len_std": 0, "vocab_diversity": 0}
37
+
38
+ lengths = [len(s.split()) for s in sentences]
39
+ words = text.lower().split()
40
+ unique = len(set(words))
41
+
42
+ return {
43
+ "burstiness": float(np.std(lengths) / max(np.mean(lengths), 1)),
44
+ "avg_sentence_len": round(np.mean(lengths), 1),
45
+ "sentence_len_std": round(np.std(lengths), 1),
46
+ "vocab_diversity": round(unique / max(len(words), 1), 3),
47
+ }
48
+
49
+
50
+ def score_text(text):
51
+ try:
52
+ result = detector(text)[0]
53
+ if result["label"] == "HUMAN":
54
+ human_score = 1 - result["score"]
55
+ else:
56
+ human_score = result["score"]
57
+ return round(human_score * 100, 1)
58
+ except Exception:
59
+ return -1
60
+
61
+
62
  def humanize(ai_text, temperature, max_tokens):
63
  if not ai_text.strip():
64
+ return "", "", ""
65
 
66
  messages = [
67
  {"role": "system", "content": SYSTEM_PROMPT},
 
81
  )
82
 
83
  response = tokenizer.decode(outputs[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True)
84
+
85
+ input_score = score_text(ai_text)
86
+ output_score = score_text(response)
87
+
88
+ input_stats = compute_stats(ai_text)
89
+ output_stats = compute_stats(response)
90
+
91
+ report = f"--- Detection Scores ---\n"
92
+ report += f"Input (AI): {input_score}% likely human\n"
93
+ report += f"Output: {output_score}% likely human\n"
94
+ report += f"\n--- Statistical Features ---\n"
95
+ report += f"{'Metric':<22} {'Input':>8} {'Output':>8}\n"
96
+ report += f"{'Burstiness':<22} {input_stats['burstiness']:>8.2f} {output_stats['burstiness']:>8.2f}\n"
97
+ report += f"{'Avg sentence length':<22} {input_stats['avg_sentence_len']:>8.1f} {output_stats['avg_sentence_len']:>8.1f}\n"
98
+ report += f"{'Sentence length std':<22} {input_stats['sentence_len_std']:>8.1f} {output_stats['sentence_len_std']:>8.1f}\n"
99
+ report += f"{'Vocab diversity':<22} {input_stats['vocab_diversity']:>8.3f} {output_stats['vocab_diversity']:>8.3f}\n"
100
+
101
+ return response, report
102
 
103
 
104
  demo = gr.Interface(
 
108
  gr.Slider(0.1, 1.5, value=0.7, step=0.1, label="Temperature"),
109
  gr.Slider(128, 1024, value=512, step=64, label="Max Tokens"),
110
  ],
111
+ outputs=[
112
+ gr.Textbox(label="Humanized Text", lines=8),
113
+ gr.Textbox(label="Detection Report", lines=10),
114
+ ],
115
  title="Aegis Humanizer",
116
+ description="Rewrites AI-generated text to sound like a real student. Shows detection scores and statistical features.",
117
  examples=[
118
  ["Artificial intelligence has significantly impacted the field of education. It provides personalized learning experiences and helps teachers identify areas where students need improvement.", 0.7, 512],
119
  ["Climate change is one of the most pressing issues facing the world today. Rising temperatures, melting ice caps, and extreme weather events are all consequences of human activity.", 0.7, 512],
requirements.txt CHANGED
@@ -1,3 +1,4 @@
1
  transformers>=4.40.0
2
  torch>=2.0.0
3
  accelerate>=0.20.0
 
 
1
  transformers>=4.40.0
2
  torch>=2.0.0
3
  accelerate>=0.20.0
4
+ numpy