Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
3 |
+
|
4 |
+
# Load the pre-trained model and tokenizer
|
5 |
+
model_name = "Vamsi/T5_Paraphrase_Paws" # Replace with your desired paraphrasing model
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
7 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
8 |
+
|
9 |
+
# Function to perform paraphrasing
|
10 |
+
def paraphrase_text(input_text, max_length=200, num_beams=5):
|
11 |
+
"""
|
12 |
+
Paraphrases the input text using a Hugging Face T5 model.
|
13 |
+
|
14 |
+
Args:
|
15 |
+
input_text (str): The text to paraphrase.
|
16 |
+
max_length (int): Maximum length of the paraphrased text.
|
17 |
+
num_beams (int): Number of beams for beam search.
|
18 |
+
|
19 |
+
Returns:
|
20 |
+
str: The paraphrased text.
|
21 |
+
"""
|
22 |
+
# Add the paraphrasing prefix
|
23 |
+
input_text = f"paraphrase: {input_text}"
|
24 |
+
|
25 |
+
# Tokenize the input text
|
26 |
+
inputs = tokenizer.encode(input_text, return_tensors="pt", max_length=512, truncation=True)
|
27 |
+
|
28 |
+
# Generate paraphrased text
|
29 |
+
outputs = model.generate(
|
30 |
+
inputs,
|
31 |
+
max_length=max_length,
|
32 |
+
num_beams=num_beams,
|
33 |
+
early_stopping=True
|
34 |
+
)
|
35 |
+
|
36 |
+
# Decode and return the paraphrased text
|
37 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
38 |
+
|
39 |
+
# Streamlit UI
|
40 |
+
st.title("Paraphrasing Tool")
|
41 |
+
st.write("Enter a paragraph below, and this tool will provide a human-like paraphrase.")
|
42 |
+
|
43 |
+
# Input text box
|
44 |
+
input_paragraph = st.text_area("Input Text:", placeholder="Type your text here...")
|
45 |
+
|
46 |
+
# Paraphrasing options
|
47 |
+
max_length = st.slider("Maximum Length of Paraphrased Text:", min_value=50, max_value=300, value=200)
|
48 |
+
num_beams = st.slider("Beam Search Width (Quality vs Speed):", min_value=1, max_value=10, value=5)
|
49 |
+
|
50 |
+
# Paraphrase button
|
51 |
+
if st.button("Paraphrase"):
|
52 |
+
if input_paragraph.strip(): # Check if input is not empty
|
53 |
+
with st.spinner("Paraphrasing in progress..."):
|
54 |
+
try:
|
55 |
+
paraphrased_text = paraphrase_text(input_paragraph, max_length=max_length, num_beams=num_beams)
|
56 |
+
st.subheader("Paraphrased Output:")
|
57 |
+
st.write(paraphrased_text)
|
58 |
+
except Exception as e:
|
59 |
+
st.error(f"An error occurred: {e}")
|
60 |
+
else:
|
61 |
+
st.warning("Please enter some text to paraphrase.")
|