Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
import pandas as pd
|
4 |
+
from transformers import T5ForConditionalGeneration, T5Tokenizer
|
5 |
+
import io
|
6 |
+
|
7 |
+
# Load the pre-trained T5 model for paraphrasing
|
8 |
+
model = T5ForConditionalGeneration.from_pretrained('ramsrigouthamg/t5_paraphraser')
|
9 |
+
tokenizer = T5Tokenizer.from_pretrained('t5-base')
|
10 |
+
|
11 |
+
def generate_paraphrases(text, num_return_sequences=5, num_beams=10):
|
12 |
+
input_text = "paraphrase: " + text + " </s>"
|
13 |
+
encoding = tokenizer.encode_plus(input_text, return_tensors="pt")
|
14 |
+
input_ids = encoding["input_ids"]
|
15 |
+
|
16 |
+
outputs = model.generate(
|
17 |
+
input_ids,
|
18 |
+
max_length=256,
|
19 |
+
num_beams=num_beams,
|
20 |
+
num_return_sequences=num_return_sequences,
|
21 |
+
no_repeat_ngram_size=2,
|
22 |
+
early_stopping=True
|
23 |
+
)
|
24 |
+
|
25 |
+
paraphrases = [
|
26 |
+
tokenizer.decode(output, skip_special_tokens=True, clean_up_tokenization_spaces=True)
|
27 |
+
for output in outputs
|
28 |
+
]
|
29 |
+
return paraphrases
|
30 |
+
|
31 |
+
def generate_and_export(text):
|
32 |
+
paraphrases = generate_paraphrases(text)
|
33 |
+
df = pd.DataFrame({
|
34 |
+
"Original": [text] * len(paraphrases),
|
35 |
+
"Perspective": paraphrases
|
36 |
+
})
|
37 |
+
csv_buffer = io.StringIO()
|
38 |
+
df.to_csv(csv_buffer, index=False)
|
39 |
+
csv_bytes = csv_buffer.getvalue().encode()
|
40 |
+
return paraphrases, csv_bytes
|
41 |
+
|
42 |
+
def gradio_generate(text):
|
43 |
+
paraphrases, csv_bytes = generate_and_export(text)
|
44 |
+
output_text = "\n\n".join(f"Perspective {i+1}: {p}" for i, p in enumerate(paraphrases))
|
45 |
+
return output_text, (csv_bytes, "generated_perspectives.csv")
|
46 |
+
|
47 |
+
iface = gr.Interface(
|
48 |
+
fn=gradio_generate,
|
49 |
+
inputs=gr.Textbox(lines=5, placeholder="Enter text here..."),
|
50 |
+
outputs=[
|
51 |
+
gr.Textbox(label="Generated Perspectives"),
|
52 |
+
gr.File(label="Download CSV")
|
53 |
+
],
|
54 |
+
title="Paraphrase Perspective Generator",
|
55 |
+
description="Enter any text and generate alternative perspectives (paraphrases). Download the results as a CSV file."
|
56 |
+
)
|
57 |
+
|
58 |
+
iface.launch()
|