Spaces:
Sleeping
Sleeping
creating the SOP paraphrasers
Browse files
app.py
ADDED
@@ -0,0 +1,165 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
import warnings
|
4 |
+
import random
|
5 |
+
import torch
|
6 |
+
import gradio as gr # Import Gradio for the UI
|
7 |
+
from parrot import Parrot
|
8 |
+
|
9 |
+
# Suppress warnings
|
10 |
+
warnings.filterwarnings("ignore")
|
11 |
+
os.environ['TRANSFORMERS_NO_ADVISORY_WARNINGS'] = '1' # Suppress warnings from Transformers
|
12 |
+
|
13 |
+
# Set random state for reproducibility
|
14 |
+
def random_state(seed):
|
15 |
+
torch.manual_seed(seed)
|
16 |
+
if torch.cuda.is_available():
|
17 |
+
torch.cuda.manual_seed_all(seed)
|
18 |
+
|
19 |
+
random_state(1234)
|
20 |
+
|
21 |
+
# Initialize Parrot model
|
22 |
+
parrot = Parrot(model_tag="prithivida/parrot_paraphraser_on_T5")
|
23 |
+
|
24 |
+
# Function to paraphrase a single sentence
|
25 |
+
def paraphrase_sentence(sentence):
|
26 |
+
paraphrases = parrot.augment(input_phrase=sentence, max_return_phrases=10, max_length=100, adequacy_threshold=0.75, fluency_threshold=0.75)
|
27 |
+
if paraphrases:
|
28 |
+
# Randomly select one paraphrase from the generated ones
|
29 |
+
return random.choice(paraphrases)[0] # Select a random paraphrase
|
30 |
+
return sentence # Return the original sentence if no paraphrase is available
|
31 |
+
|
32 |
+
# Function to split text by periods (full stops)
|
33 |
+
def split_text_by_fullstop(text):
|
34 |
+
sentences = [sentence.strip() for sentence in text.split('.') if sentence] # Split and remove empty strings
|
35 |
+
return sentences
|
36 |
+
|
37 |
+
# Main function to process and paraphrase text by splitting at periods
|
38 |
+
def process_text_by_fullstop(text):
|
39 |
+
sentences = split_text_by_fullstop(text) # Split text into sentences by full stops
|
40 |
+
paraphrased_sentences = [paraphrase_sentence(sentence + '.') for sentence in sentences] # Paraphrase each sentence
|
41 |
+
return ' '.join(paraphrased_sentences) # Join paraphrased sentences back into a single text
|
42 |
+
|
43 |
+
# Gradio interface function
|
44 |
+
def generate_content(input_text):
|
45 |
+
paraphrased_text = process_text_by_fullstop(input_text)
|
46 |
+
return paraphrased_text
|
47 |
+
|
48 |
+
|
49 |
+
css = """
|
50 |
+
body {
|
51 |
+
font-family: 'Roboto', sans-serif;
|
52 |
+
background-color: #ffffff;
|
53 |
+
color: #000000;
|
54 |
+
}
|
55 |
+
.container {
|
56 |
+
max-width: 800px;
|
57 |
+
margin: 0 auto;
|
58 |
+
display: flex;
|
59 |
+
justify-content: space-between;
|
60 |
+
align-items: flex-start;
|
61 |
+
}
|
62 |
+
.title-container {
|
63 |
+
display: flex;
|
64 |
+
align-items: center;
|
65 |
+
justify-content: center;
|
66 |
+
margin-bottom: 30px;
|
67 |
+
padding: 20px;
|
68 |
+
}
|
69 |
+
.title-text {
|
70 |
+
font-size: 48px;
|
71 |
+
font-weight: 700;
|
72 |
+
background: linear-gradient(45deg, #FFD700, #FF4500);
|
73 |
+
-webkit-background-clip: text;
|
74 |
+
-webkit-text-fill-color: transparent;
|
75 |
+
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
|
76 |
+
text-align: center;
|
77 |
+
}
|
78 |
+
/*
|
79 |
+
.gradio-container label {
|
80 |
+
margin-left: -10px;
|
81 |
+
}*/
|
82 |
+
input[type="text"] {
|
83 |
+
background-color: #f0f0f0;
|
84 |
+
border: 1px solid #cccccc;
|
85 |
+
color: #000000;
|
86 |
+
border-radius: 5px;
|
87 |
+
padding: 10px;
|
88 |
+
font-size: 16px;
|
89 |
+
width: 100%;
|
90 |
+
text-align: left;
|
91 |
+
}
|
92 |
+
button {
|
93 |
+
width: 100%;
|
94 |
+
background-color: #1f2937;
|
95 |
+
color: #ffffff;
|
96 |
+
border: none;
|
97 |
+
padding: 10px;
|
98 |
+
font-size: 16px;
|
99 |
+
border-radius: 5px;
|
100 |
+
cursor: pointer;
|
101 |
+
transition: background-color 0.3s;
|
102 |
+
}
|
103 |
+
button:hover {
|
104 |
+
background-color: #161921;
|
105 |
+
}
|
106 |
+
.output-container {
|
107 |
+
display: flex;
|
108 |
+
align-items: center;
|
109 |
+
justify-content: flex-end;
|
110 |
+
}
|
111 |
+
.copy-button {
|
112 |
+
margin-left: 10px;
|
113 |
+
background-color: #4CAF50; /* Green button for copy */
|
114 |
+
}
|
115 |
+
.output-textbox {
|
116 |
+
text-align: left;
|
117 |
+
min-height: 150px;
|
118 |
+
width=100%;
|
119 |
+
background-color: #d0d0d0; /* Slightly Darker Gray for Bot Messages */
|
120 |
+
padding: 10px;
|
121 |
+
border-radius: 10px;
|
122 |
+
color: #000;
|
123 |
+
}
|
124 |
+
"""
|
125 |
+
|
126 |
+
# JavaScript to copy output to clipboard
|
127 |
+
copy_script = """
|
128 |
+
<script>
|
129 |
+
function copyToClipboard() {
|
130 |
+
var text = document.getElementById('output_text').innerText;
|
131 |
+
navigator.clipboard.writeText(text).then(function() {
|
132 |
+
alert('Copied to clipboard!');
|
133 |
+
}, function(err) {
|
134 |
+
alert('Error copying to clipboard');
|
135 |
+
});
|
136 |
+
}
|
137 |
+
</script>
|
138 |
+
"""
|
139 |
+
|
140 |
+
# Gradio Interface
|
141 |
+
with gr.Blocks(css=css) as demo:
|
142 |
+
# Title container with logo
|
143 |
+
gr.HTML("""
|
144 |
+
<div class="title-container">
|
145 |
+
<img src="https://raw.githubusercontent.com/juicjaane/blueai/main/logo_2.jpg" style="width: 80px; margin-right: 20px;">
|
146 |
+
<h1 class="title-text">Konect U</h1>
|
147 |
+
</div>
|
148 |
+
""")
|
149 |
+
|
150 |
+
with gr.Row(elem_id="container"):
|
151 |
+
# Left side (Input)
|
152 |
+
with gr.Column(scale=1):
|
153 |
+
input_text = gr.Textbox(placeholder="Enter your text here...", label="You", lines=5, elem_id="input_text")
|
154 |
+
submit_button = gr.Button("Generate")
|
155 |
+
|
156 |
+
# Right side (Output)
|
157 |
+
with gr.Column(scale=1):
|
158 |
+
output_text = gr.HTML(f'<div class="output-textbox" id="output_text">Generated content will appear here...</div>')
|
159 |
+
copy_button = gr.HTML(f'<button class="copy-button" onclick="copyToClipboard()">Copy</button>{copy_script}')
|
160 |
+
|
161 |
+
# Connecting input to output
|
162 |
+
submit_button.click(generate_content, inputs=input_text, outputs=output_text)
|
163 |
+
|
164 |
+
# Launch the app
|
165 |
+
demo.launch()
|