Jaane commited on
Commit
04cb90a
·
verified ·
1 Parent(s): d46d471

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -116
app.py CHANGED
@@ -3,12 +3,11 @@ 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):
@@ -25,7 +24,6 @@ parrot = Parrot(model_tag="prithivida/parrot_paraphraser_on_T5")
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
 
@@ -40,127 +38,52 @@ def process_text_by_fullstop(text):
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
- min-width: 350px;
87
- border-radius: 5px;
88
- padding: 10px;
89
- font-size: 16px;
90
- width: 100%;
91
- text-align: left;
92
- }
93
- button {
94
- width: 100%;
95
- background-color: #1f2937;
96
- color: #ffffff;
97
- border: none;
98
- padding: 10px;
99
- font-size: 16px;
100
- border-radius: 5px;
101
- cursor: pointer;
102
- transition: background-color 0.3s;
103
- }
104
- button:hover {
105
- background-color: #161921;
106
- }
107
- .output-container {
108
- display: flex;
109
- align-items: center;
110
- justify-content: flex-end;
111
- }
112
- .copy-button {
113
- margin-left: 10px;
114
- background-color: #4CAF50; /* Green button for copy */
115
- }
116
- .output-textbox {
117
- text-align: left;
118
- min-height: 150px;
119
- width=100%;
120
- background-color: #d0d0d0; /* Slightly Darker Gray for Bot Messages */
121
- padding: 10px;
122
- border-radius: 10px;
123
- color: #000;
124
- }
125
- """
126
-
127
- # JavaScript to copy output to clipboard
128
- copy_script = """
129
- <script>
130
- function copyToClipboard() {
131
- var text = document.getElementById('output_text').innerText;
132
- navigator.clipboard.writeText(text).then(function() {
133
- alert('Copied to clipboard!');
134
- }, function(err) {
135
- alert('Error copying to clipboard');
136
- });
137
- }
138
- </script>
139
- """
140
-
141
- # Gradio Interface
142
- with gr.Blocks(css=css) as demo:
143
- # Title container with logo
144
  gr.HTML("""
145
- <div class="title-container">
146
- <img src="https://raw.githubusercontent.com/juicjaane/blueai/main/logo_2.jpg" style="width: 80px; margin-right: 20px;">
147
- <h1 class="title-text">Konect U</h1>
148
  </div>
149
  """)
150
 
151
- with gr.Row(elem_id="container"):
152
- # Left side (Input)
153
- with gr.Column(scale=1):
154
- input_text = gr.Textbox(placeholder="Enter your text here...", label="", lines=5, elem_id="input_text")
155
- submit_button = gr.Button("Generate")
156
-
157
- # Right side (Output)
158
- with gr.Column(scale=1):
159
- output_text = gr.HTML(f'<div class="output-textbox" id="output_text">Generated content will appear here...</div>')
160
- copy_button = gr.HTML(f'<button class="copy-button" onclick="copyToClipboard()">Copy</button>{copy_script}')
161
-
162
- # Connecting input to output
163
- submit_button.click(generate_content, inputs=input_text, outputs=output_text)
 
 
 
 
 
164
 
165
  # Launch the app
166
- demo.launch()
 
3
  import warnings
4
  import random
5
  import torch
 
6
  from parrot import Parrot
7
 
8
  # Suppress warnings
9
  warnings.filterwarnings("ignore")
10
+ os.environ['TRANSFORMERS_NO_ADVISORY_WARNINGS'] = '1'
11
 
12
  # Set random state for reproducibility
13
  def random_state(seed):
 
24
  def paraphrase_sentence(sentence):
25
  paraphrases = parrot.augment(input_phrase=sentence, max_return_phrases=10, max_length=100, adequacy_threshold=0.75, fluency_threshold=0.75)
26
  if paraphrases:
 
27
  return random.choice(paraphrases)[0] # Select a random paraphrase
28
  return sentence # Return the original sentence if no paraphrase is available
29
 
 
38
  paraphrased_sentences = [paraphrase_sentence(sentence + '.') for sentence in sentences] # Paraphrase each sentence
39
  return ' '.join(paraphrased_sentences) # Join paraphrased sentences back into a single text
40
 
41
+ # Function to copy output text to the clipboard
42
+ def copy_to_clipboard(output_text):
43
+ # JavaScript code to copy the output text to the clipboard
44
+ return f"""
45
+ <script>
46
+ var text = `{output_text}`;
47
+ navigator.clipboard.writeText(text).then(function() {{
48
+ alert('Copied to clipboard!');
49
+ }}, function(err) {{
50
+ alert('Failed to copy text');
51
+ }});
52
+ </script>
53
+ """
54
+
55
  # Gradio interface function
56
  def generate_content(input_text):
57
  paraphrased_text = process_text_by_fullstop(input_text)
58
  return paraphrased_text
59
 
60
+ # Gradio Interface with new layout
61
+ with gr.Blocks() as demo:
62
+ # Adding a logo and title
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  gr.HTML("""
64
+ <div style="display: flex; align-items: center; justify-content: center; margin-bottom: 30px;">
65
+ <img src="https://raw.githubusercontent.com/juicjaane/blueai/main/logo_2.jpg" style="width: 80px;margin: 0 auto;">
 
66
  </div>
67
  """)
68
 
69
+ with gr.Row():
70
+ # Input column with Submit and Clear buttons below the text area
71
+ with gr.Column():
72
+ input_text = gr.Textbox(placeholder="Enter sop to get paraphrased output...", label="User", lines=5)
73
+ with gr.Row():
74
+ submit_button = gr.Button("Submit")
75
+ clear_button = gr.Button("Clear")
76
+
77
+ # Output column with Copy button below the output text area
78
+ with gr.Column():
79
+ output_text = gr.Textbox(label="output", lines=5)
80
+ copy_button = gr.Button("Copy")
81
+
82
+ # Define button actions
83
+ submit_button.click(generate_content, inputs=input_text, outputs=output_text)
84
+ clear_button.click(lambda: "", None, input_text) # Clear input
85
+ clear_button.click(lambda: "", None, output_text) # Clear output
86
+ copy_button.click(copy_to_clipboard, inputs=output_text, outputs=None) # Copy the output to clipboard
87
 
88
  # Launch the app
89
+ demo.launch()