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