File size: 5,177 Bytes
fc1c2aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d89ea11
fc1c2aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60d62a0
fc1c2aa
 
 
 
 
 
 
 
 
 
 
 
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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()