ghostai1 commited on
Commit
40f0e62
·
verified ·
1 Parent(s): 84d8da1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -0
app.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 🔄 Text Paraphraser | CPU-only HF Space
2
+
3
+ import gradio as gr
4
+ from transformers import pipeline
5
+
6
+ # Load the paraphrase pipeline once at startup
7
+ paraphraser = pipeline(
8
+ "text2text-generation",
9
+ model="Vamsi/T5_Paraphrase_Paws",
10
+ device=-1, # CPU
11
+ )
12
+
13
+ def paraphrase(text: str, num_variations: int):
14
+ if not text.strip():
15
+ return []
16
+ # T5 paraphrase prompt
17
+ prompt = "paraphrase: " + text.strip()
18
+ outputs = paraphraser(
19
+ prompt,
20
+ max_length=128,
21
+ num_return_sequences=num_variations,
22
+ do_sample=True,
23
+ top_k=120,
24
+ top_p=0.95
25
+ )
26
+ # Extract generated_text
27
+ return [out["generated_text"].strip() for out in outputs]
28
+
29
+ with gr.Blocks(title="🔄 Text Paraphraser") as demo:
30
+ gr.Markdown(
31
+ "# 🔄 Text Paraphraser\n"
32
+ "Enter a sentence and get multiple alternative rewrites—all on CPU."
33
+ )
34
+
35
+ with gr.Row():
36
+ input_text = gr.Textbox(
37
+ label="Input Sentence",
38
+ placeholder="Type something to paraphrase…",
39
+ lines=3
40
+ )
41
+ variations = gr.Slider(
42
+ 1, 5, value=3, step=1,
43
+ label="Number of Variations"
44
+ )
45
+ run_btn = gr.Button("Paraphrase 🔁", variant="primary")
46
+
47
+ output_df = gr.Dataframe(
48
+ label="Paraphrases",
49
+ headers=[f"Variant #{i}" for i in range(1, 6)],
50
+ datatype=["str"]*5,
51
+ interactive=False,
52
+ row_count=1
53
+ )
54
+
55
+ def format_for_dataframe(results):
56
+ # Pad out to 5 columns
57
+ variants = results + [""]*(5 - len(results))
58
+ return [variants]
59
+
60
+ run_btn.click(
61
+ fn=lambda text, n: format_for_dataframe(paraphrase(text, n)),
62
+ inputs=[input_text, variations],
63
+ outputs=output_df
64
+ )
65
+
66
+ if __name__ == "__main__":
67
+ demo.launch(server_name="0.0.0.0")