Spaces:
Sleeping
Sleeping
Commit ·
1f6ea2d
1
Parent(s): 580e21c
rephraser
Browse files- app.py +103 -0
- requirements.txt +7 -0
app.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import PegasusForConditionalGeneration, PegasusTokenizer
|
| 4 |
+
from sentence_splitter import SentenceSplitter, split_text_into_sentences
|
| 5 |
+
|
| 6 |
+
model_name = 'tuner007/pegasus_paraphrase'
|
| 7 |
+
torch_device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 8 |
+
tokenizer = PegasusTokenizer.from_pretrained(model_name)
|
| 9 |
+
model = PegasusForConditionalGeneration.from_pretrained(model_name).to(torch_device)
|
| 10 |
+
|
| 11 |
+
def get_response(input_text, num_return_sequences):
|
| 12 |
+
batch = tokenizer.prepare_seq2seq_batch([input_text], truncation=True, padding='longest', max_length=10000,
|
| 13 |
+
return_tensors="pt").to(torch_device)
|
| 14 |
+
translated = model.generate(**batch, num_beams=10, num_return_sequences=num_return_sequences,
|
| 15 |
+
temperature=1.5)
|
| 16 |
+
tgt_text = tokenizer.batch_decode(translated, skip_special_tokens=True)
|
| 17 |
+
return tgt_text
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def get_response_from_text(
|
| 21 |
+
context="I am a student at the University of Washington. I am taking a course called Data Science."):
|
| 22 |
+
splitter = SentenceSplitter(language='en')
|
| 23 |
+
sentence_list = splitter.split(context)
|
| 24 |
+
|
| 25 |
+
paraphrase = []
|
| 26 |
+
|
| 27 |
+
for i in sentence_list:
|
| 28 |
+
a = get_response(i, 1)
|
| 29 |
+
paraphrase.append(a)
|
| 30 |
+
paraphrase2 = [' '.join(x) for x in paraphrase]
|
| 31 |
+
paraphrase3 = [' '.join(x for x in paraphrase2)]
|
| 32 |
+
paraphrased_text = str(paraphrase3).strip('[]').strip("'")
|
| 33 |
+
return paraphrased_text
|
| 34 |
+
|
| 35 |
+
def greet(context):
|
| 36 |
+
return get_response_from_text(context)
|
| 37 |
+
|
| 38 |
+
examples = [["Begin your professional career by learning data science skills with CodeTechDevX, an e-learning platform where we teach students how to learn data science, data analytics, machine learning, C++, AI, and more."], ["Rephrasing is the process of expressing the meaning of a statement in different words to make it clearer and easier for the audience to understand. This app leverages natural language processing (NLP) to generate paraphrased versions of your input text."]]
|
| 39 |
+
|
| 40 |
+
css = """
|
| 41 |
+
footer {display:none !important}
|
| 42 |
+
.output-markdown{display:none !important}
|
| 43 |
+
|
| 44 |
+
.gr-button-primary {
|
| 45 |
+
z-index: 14;
|
| 46 |
+
height: 36px !important;
|
| 47 |
+
width: 113px !important;
|
| 48 |
+
left: 0px;
|
| 49 |
+
top: 0px;
|
| 50 |
+
padding: 0px;
|
| 51 |
+
cursor: pointer !important;
|
| 52 |
+
background: none rgb(17, 20, 45) !important;
|
| 53 |
+
border: none !important;
|
| 54 |
+
text-align: center !important;
|
| 55 |
+
font-family: Poppins !important;
|
| 56 |
+
font-size: 14px !important;
|
| 57 |
+
font-weight: 500 !important;
|
| 58 |
+
color: rgb(255, 255, 255) !important;
|
| 59 |
+
line-height: 1 !important;
|
| 60 |
+
border-radius: 6px !important;
|
| 61 |
+
transition: box-shadow 200ms ease 0s, background 200ms ease 0s !important;
|
| 62 |
+
box-shadow: none !important;
|
| 63 |
+
}
|
| 64 |
+
.gr-button-primary:hover{
|
| 65 |
+
z-index: 14;
|
| 66 |
+
height: 36px !important;
|
| 67 |
+
width: 113px !important;
|
| 68 |
+
left: 0px;
|
| 69 |
+
top: 0px;
|
| 70 |
+
padding: 0px;
|
| 71 |
+
cursor: pointer !important;
|
| 72 |
+
background: none rgb(66, 133, 244) !important;
|
| 73 |
+
border: none !important;
|
| 74 |
+
text-align: center !important;
|
| 75 |
+
font-family: Poppins !important;
|
| 76 |
+
font-size: 14px !important;
|
| 77 |
+
font-weight: 500 !important;
|
| 78 |
+
color: rgb(255, 255, 255) !important;
|
| 79 |
+
line-height: 1 !important;
|
| 80 |
+
border-radius: 6px !important;
|
| 81 |
+
transition: box-shadow 200ms ease 0s, background 200ms ease 0s !important;
|
| 82 |
+
box-shadow: rgb(0 0 0 / 23%) 0px 1px 7px 0px !important;
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
.gr-button-secondary{
|
| 86 |
+
height: 36px !important;
|
| 87 |
+
width: 113px !important;
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
.gr-button-secondary:hover{
|
| 91 |
+
height: 36px !important;
|
| 92 |
+
width: 113px !important;
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
.hover\:bg-orange-50:hover {
|
| 96 |
+
--tw-bg-opacity: 1 !important;
|
| 97 |
+
background-color: rgb(229,225,255) !important;
|
| 98 |
+
}
|
| 99 |
+
"""
|
| 100 |
+
|
| 101 |
+
demo = gr.Interface(fn=greet, inputs=gr.Textbox(lines=3, placeholder="Enter sample text here", label="Original text"), outputs=gr.Textbox(label="Rephraser"), title="Rephraser | CodeTechDevX", examples=examples, css=css)
|
| 102 |
+
|
| 103 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
sentence-splitter
|
| 2 |
+
transformers
|
| 3 |
+
SentencePiece
|
| 4 |
+
torch
|
| 5 |
+
uvicorn>=0.2.2
|
| 6 |
+
httpx==0.24.1
|
| 7 |
+
gradio
|