Spaces:
Sleeping
Sleeping
File size: 5,103 Bytes
3a2543a fc1c2aa 3a2543a fb4ca2a 3a2543a 21d8378 275943b fc1c2aa 04cb90a fc1c2aa 04aeb37 32f5239 fc1c2aa 04aeb37 fc1c2aa 04cb90a fc1c2aa 04cb90a fc1c2aa 04cb90a fc1c2aa 04cb90a fc1c2aa 3a2543a |
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
import os
import warnings
import random
import torch
import gradio as gr
from parrot import Parrot
from concurrent.futures import ThreadPoolExecutor, as_completed
warnings.filterwarnings("ignore")
os.environ['TRANSFORMERS_NO_ADVISORY_WARNINGS'] = '1'
def random_state(seed):
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed_all(seed)
random_state(1234)
parrot = Parrot(model_tag="prithivida/parrot_paraphraser_on_T5")
def paraphrase_sentence(sentence):
paraphrases = parrot.augment(input_phrase=sentence, max_return_phrases=10, max_length=100, adequacy_threshold=0.75,
fluency_threshold=0.75)
return random.choice(paraphrases)[0] if paraphrases else sentence
def split_text_by_fullstop(text):
return [sentence.strip() for sentence in text.split('.') if sentence]
def process_text_by_fullstop(text):
sentences = split_text_by_fullstop(text)
with ThreadPoolExecutor() as executor:
future_to_sentence = {executor.submit(paraphrase_sentence, sentence + '.'): sentence for sentence in sentences}
paraphrased_sentences = []
for future in as_completed(future_to_sentence):
paraphrased_sentences.append(future.result())
return ' '.join(paraphrased_sentences)
def gradio_interface(input_text):
paraphrased_text = process_text_by_fullstop(input_text)
return paraphrased_text
interface = gr.Interface(fn=gradio_interface,
inputs="text",
outputs="text",
title="Text Paraphraser",
description="Enter text to get paraphrased output.")
interface.launch()
'''import warnings
import random
import torch
import os
import gradio as gr
from parrot import Parrot
# Suppress warnings
warnings.filterwarnings("ignore")
os.environ['TRANSFORMERS_NO_ADVISORY_WARNINGS'] = '1'
# Set random state for reproducibility
def random_state(seed):
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed_all(1234)
# Initialize Parrot model
parrot = Parrot(model_tag="prithivida/parrot_paraphraser_on_T5")
# Function to paraphrase a single sentence
def paraphrase_sentence(sentence):
paraphrases = parrot.augment(input_phrase=sentence, max_return_phrases=10, max_length=100, adequacy_threshold=0.75, fluency_threshold=0.75)
if paraphrases:
return random.choice(paraphrases)[0] # Select a random paraphrase
return sentence # Return the original sentence if no paraphrase is available
# Function to split text by periods (full stops)
def split_text_by_fullstop(text):
sentences = [sentence.strip() for sentence in text.split('.') if sentence] # Split and remove empty strings
return sentences
# Main function to process and paraphrase text by splitting at periods
def process_text_by_fullstop(text):
sentences = split_text_by_fullstop(text) # Split text into sentences by full stops
paraphrased_sentences = [paraphrase_sentence(sentence + '.') for sentence in sentences] # Paraphrase each sentence
return ' '.join(paraphrased_sentences) # Join paraphrased sentences back into a single text
# Function to copy output text to the clipboard
def copy_to_clipboard(output_text):
# JavaScript code to copy the output text to the clipboard
return f"""
<script>
var text = `{output_text}`;
navigator.clipboard.writeText(text).then(function() {{
alert('Copied to clipboard!');
}}, function(err) {{
alert('Failed to copy text');
}});
</script>
"""
# Gradio interface function
def generate_content(input_text):
paraphrased_text = process_text_by_fullstop(input_text)
return paraphrased_text
# Gradio Interface with new layout
with gr.Blocks() as demo:
# Adding a logo and title
gr.HTML("""
<div style="display: flex; align-items: center; justify-content: center; margin-bottom: 30px;">
<img src="https://raw.githubusercontent.com/juicjaane/blueai/main/logo_2.jpg" style="width: 80px;margin: 0 auto;">
</div>
""")
with gr.Row():
# Input column with Submit and Clear buttons below the text area
with gr.Column():
input_text = gr.Textbox(placeholder="Enter sop to get paraphrased output...", label="User", lines=5)
with gr.Row():
submit_button = gr.Button("Submit")
clear_button = gr.Button("Clear")
# Output column with Copy button below the output text area
with gr.Column():
output_text = gr.Textbox(label="output", lines=5)
copy_button = gr.Button("Copy")
# Define button actions
submit_button.click(generate_content, inputs=input_text, outputs=output_text)
clear_button.click(lambda: "", None, input_text) # Clear input
clear_button.click(lambda: "", None, output_text) # Clear output
copy_button.click(copy_to_clipboard, inputs=output_text, outputs=None) # Copy the output to clipboard
# Launch the app
demo.launch()'''
|