KavinduHansaka commited on
Commit
bbf0867
·
verified ·
1 Parent(s): 611c100

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -54
app.py CHANGED
@@ -4,27 +4,25 @@ import os
4
  from huggingface_hub import login
5
  import torch
6
 
7
- # Authenticate with Hugging Face
8
  HUGGINGFACE_TOKEN = os.getenv("HF_TOKEN")
9
  login(token=HUGGINGFACE_TOKEN)
10
 
11
- # Load Phi-4 Mini
12
- phi_model_id = "microsoft/phi-4-mini-instruct"
13
- phi_tokenizer = AutoTokenizer.from_pretrained(phi_model_id, token=HUGGINGFACE_TOKEN)
14
- phi_model = AutoModelForCausalLM.from_pretrained(
15
- phi_model_id, torch_dtype="auto", device_map="auto", token=HUGGINGFACE_TOKEN
16
- )
17
  phi_pipe = pipeline("text-generation", model=phi_model, tokenizer=phi_tokenizer)
18
 
19
- # Load T5 for paraphrasing
20
  t5_pipe = pipeline("text2text-generation", model="google-t5/t5-base")
21
 
22
- # Load AI detector
23
- ai_model_id = "openai-community/roberta-base-openai-detector"
24
- ai_tokenizer = AutoTokenizer.from_pretrained(ai_model_id)
25
- ai_model = AutoModelForSequenceClassification.from_pretrained(ai_model_id)
26
 
27
- # Chunking helper
28
  def chunk_text(text, max_tokens=300):
29
  paragraphs = text.split("\n\n")
30
  chunks, current = [], ""
@@ -38,7 +36,7 @@ def chunk_text(text, max_tokens=300):
38
  chunks.append(current.strip())
39
  return chunks
40
 
41
- # Phi prompt-based generation
42
  def generate_phi_prompt(text, instruction):
43
  chunks = chunk_text(text)
44
  outputs = []
@@ -51,36 +49,7 @@ def generate_phi_prompt(text, instruction):
51
  outputs.append(result.strip())
52
  return "\n\n".join(outputs)
53
 
54
- # AI detection logic
55
- def detect_ai_text(text):
56
- inputs = ai_tokenizer(text, return_tensors="pt", truncation=True, padding=True)
57
- with torch.no_grad():
58
- logits = ai_model(**inputs).logits
59
- probs = torch.softmax(logits, dim=1).squeeze()
60
- return probs[0].item(), probs[1].item() # human, ai
61
-
62
- # Auto-rewrite if AI > 50%
63
- def auto_rewrite_for_human(text):
64
- rewritten = generate_phi_prompt(
65
- text,
66
- "Rewrite the following text so that it is indistinguishable from human writing and avoids AI detection. Be natural and fluent:"
67
- )
68
- human_score, ai_score = detect_ai_text(rewritten)
69
- if ai_score < 0.05:
70
- return rewritten, f"✅ Rewritten to pass detection. AI Likelihood: {round(ai_score * 100, 2)}%"
71
- else:
72
- return rewritten, f"⚠️ Rewritten, but AI Likelihood is still high: {round(ai_score * 100, 2)}%"
73
-
74
- # Smart detection & rewrite combo
75
- def check_and_rewrite(text):
76
- human_score, ai_score = detect_ai_text(text)
77
- if ai_score > 0.5:
78
- rewritten, message = auto_rewrite_for_human(text)
79
- return rewritten, {"Likely Human": round(human_score, 2), "Likely AI-Generated": round(ai_score, 2)}, message
80
- else:
81
- return text, {"Likely Human": round(human_score, 2), "Likely AI-Generated": round(ai_score, 2)}, "✅ Text is human-like. No rewrite needed."
82
-
83
- # Tool functions
84
  def fix_grammar(text):
85
  return generate_phi_prompt(text, "Correct all grammar and punctuation errors in the following text. Provide only the corrected version:")
86
 
@@ -98,7 +67,22 @@ def paraphrase(text):
98
  outputs.append(output)
99
  return "\n\n".join(outputs)
100
 
101
- # File utilities
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
  def load_file(file_obj):
103
  if file_obj is None:
104
  return ""
@@ -110,15 +94,15 @@ def save_file(text):
110
  f.write(text)
111
  return path
112
 
113
- # Gradio UI
114
  with gr.Blocks() as demo:
115
- gr.Markdown("# ✍️ AI Writing Assistant + AI Detector & Auto-Rewriter")
116
- gr.Markdown("Fix grammar, improve tone, paraphrase, detect AI content, and automatically rewrite if needed.")
117
 
118
  with gr.Row():
119
  file_input = gr.File(label="📂 Upload .txt File", file_types=[".txt"])
120
  load_btn = gr.Button("���� Load Text")
121
- input_text = gr.Textbox(lines=12, label="Or Paste Text")
122
 
123
  load_btn.click(fn=load_file, inputs=file_input, outputs=input_text)
124
 
@@ -127,20 +111,26 @@ with gr.Blocks() as demo:
127
  btn_tone = gr.Button("🎯 Improve Tone")
128
  btn_fluency = gr.Button("🔄 Improve Fluency")
129
  btn_paraphrase = gr.Button("🌀 Paraphrase")
130
- btn_detect = gr.Button("🕵️ Detect + Rewrite if AI")
131
 
132
  output_text = gr.Textbox(lines=12, label="Output")
133
- ai_output = gr.Label(label="AI Detection Result")
134
- rewrite_status = gr.Textbox(label="Status Message", interactive=False)
135
 
136
  btn_grammar.click(fn=fix_grammar, inputs=input_text, outputs=output_text)
137
  btn_tone.click(fn=improve_tone, inputs=input_text, outputs=output_text)
138
  btn_fluency.click(fn=improve_fluency, inputs=input_text, outputs=output_text)
139
  btn_paraphrase.click(fn=paraphrase, inputs=input_text, outputs=output_text)
140
- btn_detect.click(fn=check_and_rewrite, inputs=input_text, outputs=[output_text, ai_output, rewrite_status])
 
 
 
 
 
 
 
 
 
141
 
142
  gr.Markdown("## 📤 Download Output")
143
- download_btn = gr.Button("💾 Download as .txt")
144
  download_file = gr.File(label="Click to download", interactive=True)
145
  download_btn.click(fn=save_file, inputs=output_text, outputs=download_file)
146
 
 
4
  from huggingface_hub import login
5
  import torch
6
 
7
+ # Authenticate with Hugging Face token
8
  HUGGINGFACE_TOKEN = os.getenv("HF_TOKEN")
9
  login(token=HUGGINGFACE_TOKEN)
10
 
11
+ # Load Phi-4 Mini for grammar/tone/fluency
12
+ phi_id = "microsoft/phi-4-mini-instruct"
13
+ phi_tokenizer = AutoTokenizer.from_pretrained(phi_id, token=HUGGINGFACE_TOKEN)
14
+ phi_model = AutoModelForCausalLM.from_pretrained(phi_id, torch_dtype="auto", device_map="auto", token=HUGGINGFACE_TOKEN)
 
 
15
  phi_pipe = pipeline("text-generation", model=phi_model, tokenizer=phi_tokenizer)
16
 
17
+ # Load updated T5 model for paraphrasing
18
  t5_pipe = pipeline("text2text-generation", model="google-t5/t5-base")
19
 
20
+ # Load updated AI detector
21
+ detector_id = "openai-community/roberta-base-openai-detector"
22
+ detector_tokenizer = AutoTokenizer.from_pretrained(detector_id)
23
+ detector_model = AutoModelForSequenceClassification.from_pretrained(detector_id)
24
 
25
+ # Text chunking
26
  def chunk_text(text, max_tokens=300):
27
  paragraphs = text.split("\n\n")
28
  chunks, current = [], ""
 
36
  chunks.append(current.strip())
37
  return chunks
38
 
39
+ # Prompt handling
40
  def generate_phi_prompt(text, instruction):
41
  chunks = chunk_text(text)
42
  outputs = []
 
49
  outputs.append(result.strip())
50
  return "\n\n".join(outputs)
51
 
52
+ # Writing tools
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  def fix_grammar(text):
54
  return generate_phi_prompt(text, "Correct all grammar and punctuation errors in the following text. Provide only the corrected version:")
55
 
 
67
  outputs.append(output)
68
  return "\n\n".join(outputs)
69
 
70
+ # AI Detection
71
+ def detect_ai_percent(text):
72
+ inputs = detector_tokenizer(text, return_tensors="pt", truncation=True, padding=True)
73
+ with torch.no_grad():
74
+ logits = detector_model(**inputs).logits
75
+ probs = torch.softmax(logits, dim=1).squeeze()
76
+ return {
77
+ "Likely Human": round(probs[0].item() * 100, 2),
78
+ "Likely AI-Generated": round(probs[1].item() * 100, 2)
79
+ }
80
+
81
+ # Rewrite to lower AI score
82
+ def rewrite_to_human(text):
83
+ return generate_phi_prompt(text, "Rewrite the following text so that it is indistinguishable from human writing and avoids AI detection. Be natural and fluent:")
84
+
85
+ # File I/O
86
  def load_file(file_obj):
87
  if file_obj is None:
88
  return ""
 
94
  f.write(text)
95
  return path
96
 
97
+ # UI
98
  with gr.Blocks() as demo:
99
+ gr.Markdown("# ✍️ AI Writing Assistant + Detector")
100
+ gr.Markdown("Detect AI likelihood first. Rewrite manually if high. Fix grammar, tone, fluency, paraphrase, upload/download text.")
101
 
102
  with gr.Row():
103
  file_input = gr.File(label="📂 Upload .txt File", file_types=[".txt"])
104
  load_btn = gr.Button("���� Load Text")
105
+ input_text = gr.Textbox(lines=12, label="Input Text")
106
 
107
  load_btn.click(fn=load_file, inputs=file_input, outputs=input_text)
108
 
 
111
  btn_tone = gr.Button("🎯 Improve Tone")
112
  btn_fluency = gr.Button("🔄 Improve Fluency")
113
  btn_paraphrase = gr.Button("🌀 Paraphrase")
 
114
 
115
  output_text = gr.Textbox(lines=12, label="Output")
 
 
116
 
117
  btn_grammar.click(fn=fix_grammar, inputs=input_text, outputs=output_text)
118
  btn_tone.click(fn=improve_tone, inputs=input_text, outputs=output_text)
119
  btn_fluency.click(fn=improve_fluency, inputs=input_text, outputs=output_text)
120
  btn_paraphrase.click(fn=paraphrase, inputs=input_text, outputs=output_text)
121
+
122
+ gr.Markdown("## 🕵️ AI Detection")
123
+ detect_btn = gr.Button("Detect AI Probability")
124
+ ai_output = gr.Label(label="AI Detection Result")
125
+ detect_btn.click(fn=detect_ai_percent, inputs=input_text, outputs=ai_output)
126
+
127
+ gr.Markdown("## 🔁 Rewrite to Sound Human")
128
+ rewrite_btn = gr.Button("Rewrite to Reduce AI Likelihood")
129
+ rewritten_text = gr.Textbox(lines=12, label="Rewritten Text")
130
+ rewrite_btn.click(fn=rewrite_to_human, inputs=input_text, outputs=rewritten_text)
131
 
132
  gr.Markdown("## 📤 Download Output")
133
+ download_btn = gr.Button("💾 Download")
134
  download_file = gr.File(label="Click to download", interactive=True)
135
  download_btn.click(fn=save_file, inputs=output_text, outputs=download_file)
136