catninja123 commited on
Commit
1e184ae
·
verified ·
1 Parent(s): 9f16e37

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +93 -0
app.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ DIPPER Paraphrase API — HuggingFace Spaces deployment.
3
+ Exposes DIPPER-lite (T5-large, 1.0B) as a Gradio API for de-AI rewriting.
4
+ """
5
+ import gradio as gr
6
+ import torch
7
+ from transformers import T5Tokenizer, T5ForConditionalGeneration
8
+ import time
9
+
10
+ print("[DIPPER] Loading model...")
11
+ t0 = time.time()
12
+ tokenizer = T5Tokenizer.from_pretrained("SamSJackson/paraphrase-dipper-no-ctx")
13
+ model = T5ForConditionalGeneration.from_pretrained("SamSJackson/paraphrase-dipper-no-ctx")
14
+ model.eval()
15
+ print(f"[DIPPER] Model loaded in {time.time()-t0:.1f}s")
16
+
17
+ def paraphrase(text: str, lex: int = 40, order: int = 40) -> str:
18
+ """
19
+ Paraphrase text using DIPPER.
20
+
21
+ Args:
22
+ text: Input text (one paragraph recommended, max ~300 words)
23
+ lex: Lexical control (0=max change, 100=no change). Default 40.
24
+ order: Order control (0=max reorder, 100=no reorder). Default 40.
25
+
26
+ Returns:
27
+ Paraphrased text.
28
+ """
29
+ if not text or not text.strip():
30
+ return ""
31
+
32
+ text = text.strip()
33
+ prefix = f"lexical = {lex}, order = {order} "
34
+ input_text = prefix + text
35
+
36
+ inputs = tokenizer(input_text, return_tensors="pt", max_length=512, truncation=True)
37
+
38
+ t0 = time.time()
39
+ with torch.no_grad():
40
+ outputs = model.generate(
41
+ **inputs,
42
+ max_length=512,
43
+ do_sample=False,
44
+ num_beams=5,
45
+ num_return_sequences=1
46
+ )
47
+ elapsed = time.time() - t0
48
+
49
+ result = tokenizer.decode(outputs[0], skip_special_tokens=True)
50
+ print(f"[DIPPER] {len(text)} -> {len(result)} chars in {elapsed:.1f}s")
51
+ return result
52
+
53
+ def batch_paraphrase(text: str, lex: int = 40, order: int = 40) -> str:
54
+ """
55
+ Paraphrase multi-paragraph text by processing each paragraph separately.
56
+
57
+ Args:
58
+ text: Input text (can be multiple paragraphs separated by blank lines)
59
+ lex: Lexical control (0=max change, 100=no change). Default 40.
60
+ order: Order control (0=max reorder, 100=no reorder). Default 40.
61
+
62
+ Returns:
63
+ Paraphrased text with paragraphs rejoined.
64
+ """
65
+ if not text or not text.strip():
66
+ return ""
67
+
68
+ paragraphs = [p.strip() for p in text.split('\n\n') if p.strip()]
69
+ results = []
70
+
71
+ for i, para in enumerate(paragraphs):
72
+ print(f"[DIPPER] Paragraph {i+1}/{len(paragraphs)}")
73
+ result = paraphrase(para, lex=lex, order=order)
74
+ results.append(result)
75
+
76
+ return '\n\n'.join(results)
77
+
78
+ # Gradio interface
79
+ with gr.Blocks(title="DIPPER Paraphrase API") as demo:
80
+ gr.Markdown("# DIPPER Paraphrase API\nRewrite text to bypass AI detection. Use the API endpoint for programmatic access.")
81
+
82
+ with gr.Row():
83
+ with gr.Column():
84
+ input_text = gr.Textbox(label="Input Text", lines=10, placeholder="Paste text here...")
85
+ lex_slider = gr.Slider(0, 100, value=40, step=10, label="Lexical Control (40=optimal)")
86
+ ord_slider = gr.Slider(0, 100, value=40, step=10, label="Order Control (40=optimal)")
87
+ btn = gr.Button("Paraphrase", variant="primary")
88
+ with gr.Column():
89
+ output_text = gr.Textbox(label="Paraphrased Text", lines=10)
90
+
91
+ btn.click(fn=batch_paraphrase, inputs=[input_text, lex_slider, ord_slider], outputs=output_text)
92
+
93
+ demo.launch()