paraphraser_SOP / app.py
Jaane's picture
Update app.py
d89ea11 verified
raw
history blame
5.18 kB
import gradio as gr
import os
import warnings
import random
import torch
import gradio as gr # Import Gradio for the UI
from parrot import Parrot
# Suppress warnings
warnings.filterwarnings("ignore")
os.environ['TRANSFORMERS_NO_ADVISORY_WARNINGS'] = '1' # Suppress warnings from Transformers
# Set random state for reproducibility
def random_state(seed):
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed_all(seed)
random_state(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:
# Randomly select one paraphrase from the generated ones
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
# Gradio interface function
def generate_content(input_text):
paraphrased_text = process_text_by_fullstop(input_text)
return paraphrased_text
css = """
body {
font-family: 'Roboto', sans-serif;
background-color: #ffffff;
color: #000000;
}
.container {
max-width: 800px;
margin: 0 auto;
display: flex;
justify-content: space-between;
align-items: flex-start;
}
.title-container {
display: flex;
align-items: center;
justify-content: center;
margin-bottom: 30px;
padding: 20px;
}
.title-text {
font-size: 48px;
font-weight: 700;
background: linear-gradient(45deg, #FFD700, #FF4500);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
text-align: center;
}
/*
.gradio-container label {
margin-left: -10px;
}*/
input[type="text"] {
background-color: #f0f0f0;
border: 1px solid #cccccc;
color: #000000;
min-width: 100%;
border-radius: 5px;
padding: 10px;
font-size: 16px;
width: 100%;
text-align: left;
}
button {
width: 100%;
background-color: #1f2937;
color: #ffffff;
border: none;
padding: 10px;
font-size: 16px;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: #161921;
}
.output-container {
display: flex;
align-items: center;
justify-content: flex-end;
}
.copy-button {
margin-left: 10px;
background-color: #4CAF50; /* Green button for copy */
}
.output-textbox {
text-align: left;
min-height: 150px;
width=100%;
background-color: #d0d0d0; /* Slightly Darker Gray for Bot Messages */
padding: 10px;
border-radius: 10px;
color: #000;
}
"""
# JavaScript to copy output to clipboard
copy_script = """
<script>
function copyToClipboard() {
var text = document.getElementById('output_text').innerText;
navigator.clipboard.writeText(text).then(function() {
alert('Copied to clipboard!');
}, function(err) {
alert('Error copying to clipboard');
});
}
</script>
"""
# Gradio Interface
with gr.Blocks(css=css) as demo:
# Title container with logo
gr.HTML("""
<div class="title-container">
<img src="https://raw.githubusercontent.com/juicjaane/blueai/main/logo_2.jpg" style="width: 80px; margin-right: 20px;">
<h1 class="title-text">Konect U</h1>
</div>
""")
with gr.Row(elem_id="container"):
# Left side (Input)
with gr.Column(scale=1):
input_text = gr.Textbox(placeholder="Enter your text here...", label="", lines=5, elem_id="input_text")
submit_button = gr.Button("Generate")
# Right side (Output)
with gr.Column(scale=1):
output_text = gr.HTML(f'<div class="output-textbox" id="output_text">Generated content will appear here...</div>')
copy_button = gr.HTML(f'<button class="copy-button" onclick="copyToClipboard()">Copy</button>{copy_script}')
# Connecting input to output
submit_button.click(generate_content, inputs=input_text, outputs=output_text)
# Launch the app
demo.launch()