Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Load model (choose Falcon or Mistral)
|
| 6 |
+
model_name = "mistralai/Mistral-7B-Instruct-v0.2"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
+
model_name,
|
| 10 |
+
device_map="auto",
|
| 11 |
+
torch_dtype=torch.float16
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
# Humanizer function
|
| 15 |
+
def humanize_text(ai_text):
|
| 16 |
+
if not ai_text.strip():
|
| 17 |
+
return "⚠️ Please enter some text.", ""
|
| 18 |
+
|
| 19 |
+
# Prompt for rewriting
|
| 20 |
+
prompt = (
|
| 21 |
+
"Rewrite the following AI-generated text so it sounds natural and human-like. "
|
| 22 |
+
"Avoid robotic tone, vary sentence length, and make it engaging:\n\n"
|
| 23 |
+
f"Text: {ai_text}\n\n"
|
| 24 |
+
"Humanized version:"
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
|
| 28 |
+
outputs = model.generate(
|
| 29 |
+
**inputs,
|
| 30 |
+
max_new_tokens=400,
|
| 31 |
+
temperature=1.0,
|
| 32 |
+
top_p=0.95
|
| 33 |
+
)
|
| 34 |
+
humanized = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 35 |
+
|
| 36 |
+
# Prompt for feedback
|
| 37 |
+
feedback_prompt = (
|
| 38 |
+
"Analyze the following text and provide constructive feedback on how it was humanized. "
|
| 39 |
+
"Highlight improvements and remaining issues:\n\n"
|
| 40 |
+
f"Original: {ai_text}\n\n"
|
| 41 |
+
f"Humanized: {humanized}\n\n"
|
| 42 |
+
"Feedback:"
|
| 43 |
+
)
|
| 44 |
+
fb_inputs = tokenizer(feedback_prompt, return_tensors="pt").to("cuda")
|
| 45 |
+
fb_outputs = model.generate(**fb_inputs, max_new_tokens=250, temperature=0.8, top_p=0.9)
|
| 46 |
+
feedback = tokenizer.decode(fb_outputs[0], skip_special_tokens=True)
|
| 47 |
+
|
| 48 |
+
return humanized, feedback
|
| 49 |
+
|
| 50 |
+
# Gradio UI
|
| 51 |
+
with gr.Blocks() as demo:
|
| 52 |
+
gr.Markdown("## ✨ AI Text Humanizer with Feedback")
|
| 53 |
+
gr.Markdown("Paste AI-generated text below, and get a natural, human-like rewrite plus feedback.")
|
| 54 |
+
|
| 55 |
+
with gr.Row():
|
| 56 |
+
ai_input = gr.Textbox(label="AI-Generated Text", placeholder="Paste your AI text here...", lines=10)
|
| 57 |
+
|
| 58 |
+
with gr.Row():
|
| 59 |
+
humanized_output = gr.Textbox(label="✅ Humanized Text", lines=10)
|
| 60 |
+
feedback_output = gr.Textbox(label="💡 Feedback", lines=10)
|
| 61 |
+
|
| 62 |
+
run_button = gr.Button("🔄 Humanize Now")
|
| 63 |
+
|
| 64 |
+
run_button.click(
|
| 65 |
+
fn=humanize_text,
|
| 66 |
+
inputs=ai_input,
|
| 67 |
+
outputs=[humanized_output, feedback_output]
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
demo.launch()
|