Spaces:
Running
Running
Commit
·
96b5d9c
1
Parent(s):
1a68111
initial commit
Browse files- app.py +72 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
3 |
+
import torch
|
4 |
+
import random
|
5 |
+
import re
|
6 |
+
|
7 |
+
# Set manual seed for reproducibility
|
8 |
+
torch.manual_seed(42)
|
9 |
+
|
10 |
+
# Check for GPU availability
|
11 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
12 |
+
|
13 |
+
# Load the model and tokenizer
|
14 |
+
tokenizer = AutoTokenizer.from_pretrained("humarin/chatgpt_paraphraser_on_T5_base")
|
15 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("humarin/chatgpt_paraphraser_on_T5_base").to(device)
|
16 |
+
|
17 |
+
# Function to paraphrase text
|
18 |
+
def humanize_text(text, temperature=0.7, max_length=512):
|
19 |
+
input_ids = tokenizer(
|
20 |
+
f"paraphrase: {text}",
|
21 |
+
return_tensors="pt",
|
22 |
+
padding=True,
|
23 |
+
max_length=max_length,
|
24 |
+
truncation=True,
|
25 |
+
).input_ids.to(device)
|
26 |
+
|
27 |
+
outputs = model.generate(
|
28 |
+
input_ids,
|
29 |
+
max_length=max_length,
|
30 |
+
temperature=temperature,
|
31 |
+
num_beams=3,
|
32 |
+
num_beam_groups=3,
|
33 |
+
num_return_sequences=3,
|
34 |
+
repetition_penalty=3.0,
|
35 |
+
diversity_penalty=0.5,
|
36 |
+
no_repeat_ngram_size=2,
|
37 |
+
)
|
38 |
+
|
39 |
+
paraphrased_texts = tokenizer.batch_decode(outputs, skip_special_tokens=True)
|
40 |
+
return random.choice(paraphrased_texts)
|
41 |
+
|
42 |
+
# Function to split input into sentences
|
43 |
+
def split_into_sentences(text):
|
44 |
+
return re.split(r"(?<=[.!?])\s+", text)
|
45 |
+
|
46 |
+
# Function to process multi-line text
|
47 |
+
def process_text(input_text):
|
48 |
+
lines = input_text.split("\n")
|
49 |
+
processed_lines = []
|
50 |
+
|
51 |
+
for line in lines:
|
52 |
+
if len(line) < 1:
|
53 |
+
processed_lines.append(line)
|
54 |
+
else:
|
55 |
+
sentences = split_into_sentences(line)
|
56 |
+
processed_sentences = [humanize_text(sentence, max_length=len(sentence)) for sentence in sentences]
|
57 |
+
processed_lines.append(" ".join(processed_sentences))
|
58 |
+
|
59 |
+
return "\n".join(processed_lines)
|
60 |
+
|
61 |
+
# Gradio Interface
|
62 |
+
iface = gr.Interface(
|
63 |
+
fn=process_text,
|
64 |
+
inputs=gr.Textbox(lines=5, placeholder="Enter text to humanize..."),
|
65 |
+
outputs="text",
|
66 |
+
title="AI Text Humanizer",
|
67 |
+
description="Enter text, and the AI will rewrite it in a more human-like way.",
|
68 |
+
)
|
69 |
+
|
70 |
+
# Launch the Gradio app
|
71 |
+
if __name__ == "__main__":
|
72 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
torch
|
3 |
+
transformers
|