Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,12 +19,12 @@ phi_pipe = pipeline("text-generation", model=phi_model, tokenizer=phi_tokenizer)
|
|
| 19 |
# Load T5 for paraphrasing
|
| 20 |
t5_pipe = pipeline("text2text-generation", model="google-t5/t5-base")
|
| 21 |
|
| 22 |
-
# Load AI
|
| 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 |
-
#
|
| 28 |
def chunk_text(text, max_tokens=300):
|
| 29 |
paragraphs = text.split("\n\n")
|
| 30 |
chunks, current = [], ""
|
|
@@ -38,7 +38,7 @@ def chunk_text(text, max_tokens=300):
|
|
| 38 |
chunks.append(current.strip())
|
| 39 |
return chunks
|
| 40 |
|
| 41 |
-
# Phi-based
|
| 42 |
def generate_phi_prompt(text, instruction):
|
| 43 |
chunks = chunk_text(text)
|
| 44 |
outputs = []
|
|
@@ -51,7 +51,36 @@ def generate_phi_prompt(text, instruction):
|
|
| 51 |
outputs.append(result.strip())
|
| 52 |
return "\n\n".join(outputs)
|
| 53 |
|
| 54 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
def fix_grammar(text):
|
| 56 |
return generate_phi_prompt(text, "Correct all grammar and punctuation errors in the following text. Provide only the corrected version:")
|
| 57 |
|
|
@@ -69,7 +98,7 @@ def paraphrase(text):
|
|
| 69 |
outputs.append(output)
|
| 70 |
return "\n\n".join(outputs)
|
| 71 |
|
| 72 |
-
#
|
| 73 |
def load_file(file_obj):
|
| 74 |
if file_obj is None:
|
| 75 |
return ""
|
|
@@ -81,21 +110,10 @@ def save_file(text):
|
|
| 81 |
f.write(text)
|
| 82 |
return path
|
| 83 |
|
| 84 |
-
# AI Detection function
|
| 85 |
-
def detect_ai_text(text):
|
| 86 |
-
inputs = ai_tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
| 87 |
-
with torch.no_grad():
|
| 88 |
-
logits = ai_model(**inputs).logits
|
| 89 |
-
probs = torch.softmax(logits, dim=1).squeeze()
|
| 90 |
-
return {
|
| 91 |
-
"Likely Human": round(probs[0].item(), 2),
|
| 92 |
-
"Likely AI-Generated": round(probs[1].item(), 2)
|
| 93 |
-
}
|
| 94 |
-
|
| 95 |
# Gradio UI
|
| 96 |
with gr.Blocks() as demo:
|
| 97 |
-
gr.Markdown("# ✍️ AI Writing Assistant + Detector")
|
| 98 |
-
gr.Markdown("Fix grammar, improve tone
|
| 99 |
|
| 100 |
with gr.Row():
|
| 101 |
file_input = gr.File(label="📂 Upload .txt File", file_types=[".txt"])
|
|
@@ -109,16 +127,17 @@ with gr.Blocks() as demo:
|
|
| 109 |
btn_tone = gr.Button("🎯 Improve Tone")
|
| 110 |
btn_fluency = gr.Button("🔄 Improve Fluency")
|
| 111 |
btn_paraphrase = gr.Button("🌀 Paraphrase")
|
| 112 |
-
btn_detect = gr.Button("🕵️ Detect
|
| 113 |
|
| 114 |
output_text = gr.Textbox(lines=12, label="Output")
|
| 115 |
ai_output = gr.Label(label="AI Detection Result")
|
|
|
|
| 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 |
-
btn_detect.click(fn=
|
| 122 |
|
| 123 |
gr.Markdown("## 📤 Download Output")
|
| 124 |
download_btn = gr.Button("💾 Download as .txt")
|
|
|
|
| 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 |
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 |
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 |
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 |
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"])
|
|
|
|
| 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")
|