DTabs commited on
Commit
eb939d7
ยท
verified ยท
1 Parent(s): 834bfd1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -42
app.py CHANGED
@@ -3,15 +3,17 @@ from parrot import Parrot
3
  import nltk
4
  from nltk.tokenize import sent_tokenize, word_tokenize
5
  import re
 
6
 
7
  # -----------------------------
8
  # Setup
9
  # -----------------------------
10
- nltk.data.path.append("./nltk_data") # Local punkt
11
  parrot = None # Lazy-loaded global model
12
 
13
 
14
  def get_parrot():
 
15
  global parrot
16
  if parrot is None:
17
  print("โณ Loading Parrot model for the first time...")
@@ -24,7 +26,7 @@ MAX_TOKENS = 150 # limit per chunk for stability
24
 
25
 
26
  # -----------------------------
27
- # Helper: Common utilities
28
  # -----------------------------
29
  def clean_sentence(sent):
30
  sent = sent.strip()
@@ -46,6 +48,22 @@ def split_long_sentence(sentence, max_tokens=MAX_TOKENS):
46
  return [" ".join(words[i:i + max_tokens]) for i in range(0, len(words), max_tokens)]
47
 
48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  # -----------------------------
50
  # ๐Ÿ”น App 1: Full Paragraph Rephraser
51
  # -----------------------------
@@ -58,57 +76,47 @@ def rephrase(text):
58
  chunks = split_long_sentence(s)
59
  paraphrased_chunks = []
60
  for c in chunks:
61
- try:
62
- p = model.augment(
63
- input_phrase=c,
64
- do_diverse=True,
65
- adequacy_threshold=0.85,
66
- fluency_threshold=0.9,
67
- )
68
- paraphrased_chunks.append(p[0][0] if p else c)
69
- except Exception:
 
70
  paraphrased_chunks.append(c)
71
  rephrased.append(" ".join(paraphrased_chunks))
72
 
73
  return clean_sentences(rephrased)
74
 
75
 
76
- rephrase_iface = gr.Interface(
77
- fn=rephrase,
78
- inputs=gr.Textbox(lines=10, placeholder="Paste your text here..."),
79
- outputs="text",
80
- title="Parrot Rephraser (Long Text)",
81
- description="Paraphrases long text while maintaining punctuation and capitalization.",
82
- )
83
-
84
-
85
  # -----------------------------
86
  # ๐Ÿ”น App 2: Sentence-wise Multiple Paraphrases
87
  # -----------------------------
88
  def generate_unique_paraphrases(sentence, N_OPTIONS=3):
89
  model = get_parrot()
90
- try:
91
- paraphrases = model.augment(
92
- input_phrase=sentence,
93
- do_diverse=True,
94
- adequacy_threshold=0.85,
95
- fluency_threshold=0.9,
96
- )
97
- except Exception:
98
- paraphrases = []
99
-
100
- if paraphrases:
101
- texts = [p[0] for p in paraphrases]
102
- unique = []
103
- for t in texts:
104
- if t not in unique:
105
- unique.append(t)
106
- if len(unique) == N_OPTIONS:
107
- break
108
- return unique
109
- else:
110
  return [sentence]
111
 
 
 
 
 
 
 
 
 
 
112
 
113
  def rephrase_sentencewise_unique(text, N_OPTIONS=3):
114
  sentences = sent_tokenize(text.strip())
@@ -123,6 +131,34 @@ def rephrase_sentencewise_unique(text, N_OPTIONS=3):
123
  return "\n".join(results)
124
 
125
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
  sentencewise_iface = gr.Interface(
127
  fn=rephrase_sentencewise_unique,
128
  inputs=gr.Textbox(lines=10, placeholder="Paste text here..."),
@@ -131,7 +167,6 @@ sentencewise_iface = gr.Interface(
131
  description="Generates top 3 unique paraphrases per sentence. Optimized for HF free-tier.",
132
  )
133
 
134
-
135
  # -----------------------------
136
  # ๐Ÿ”น Combine both interfaces into Tabs
137
  # -----------------------------
@@ -140,4 +175,4 @@ demo = gr.TabbedInterface(
140
  ["Full Text Rephraser", "Sentence-wise Paraphrases"],
141
  )
142
 
143
- demo.launch()
 
3
  import nltk
4
  from nltk.tokenize import sent_tokenize, word_tokenize
5
  import re
6
+ import time
7
 
8
  # -----------------------------
9
  # Setup
10
  # -----------------------------
11
+ nltk.data.path.append("./nltk_data") # Local punkt (no downloading)
12
  parrot = None # Lazy-loaded global model
13
 
14
 
15
  def get_parrot():
16
+ """Load the Parrot model lazily"""
17
  global parrot
18
  if parrot is None:
19
  print("โณ Loading Parrot model for the first time...")
 
26
 
27
 
28
  # -----------------------------
29
+ # Helper functions
30
  # -----------------------------
31
  def clean_sentence(sent):
32
  sent = sent.strip()
 
48
  return [" ".join(words[i:i + max_tokens]) for i in range(0, len(words), max_tokens)]
49
 
50
 
51
+ # -----------------------------
52
+ # ๐Ÿ”น Retry Wrapper
53
+ # -----------------------------
54
+ def with_retry(func, *args, retries=1, delay=3, **kwargs):
55
+ """Try running a function twice before giving up"""
56
+ for attempt in range(retries + 1):
57
+ try:
58
+ return func(*args, **kwargs)
59
+ except Exception as e:
60
+ print(f"โš ๏ธ Attempt {attempt + 1} failed: {e}")
61
+ if attempt < retries:
62
+ print("๐Ÿ” Retrying...")
63
+ time.sleep(delay)
64
+ return None
65
+
66
+
67
  # -----------------------------
68
  # ๐Ÿ”น App 1: Full Paragraph Rephraser
69
  # -----------------------------
 
76
  chunks = split_long_sentence(s)
77
  paraphrased_chunks = []
78
  for c in chunks:
79
+ p = with_retry(
80
+ model.augment,
81
+ input_phrase=c,
82
+ do_diverse=True,
83
+ adequacy_threshold=0.85,
84
+ fluency_threshold=0.9,
85
+ )
86
+ if p:
87
+ paraphrased_chunks.append(p[0][0])
88
+ else:
89
  paraphrased_chunks.append(c)
90
  rephrased.append(" ".join(paraphrased_chunks))
91
 
92
  return clean_sentences(rephrased)
93
 
94
 
 
 
 
 
 
 
 
 
 
95
  # -----------------------------
96
  # ๐Ÿ”น App 2: Sentence-wise Multiple Paraphrases
97
  # -----------------------------
98
  def generate_unique_paraphrases(sentence, N_OPTIONS=3):
99
  model = get_parrot()
100
+ paraphrases = with_retry(
101
+ model.augment,
102
+ input_phrase=sentence,
103
+ do_diverse=True,
104
+ adequacy_threshold=0.85,
105
+ fluency_threshold=0.9,
106
+ )
107
+
108
+ if not paraphrases:
 
 
 
 
 
 
 
 
 
 
 
109
  return [sentence]
110
 
111
+ texts = [p[0] for p in paraphrases]
112
+ unique = []
113
+ for t in texts:
114
+ if t not in unique:
115
+ unique.append(t)
116
+ if len(unique) == N_OPTIONS:
117
+ break
118
+ return unique
119
+
120
 
121
  def rephrase_sentencewise_unique(text, N_OPTIONS=3):
122
  sentences = sent_tokenize(text.strip())
 
131
  return "\n".join(results)
132
 
133
 
134
+ # -----------------------------
135
+ # ๐Ÿ”น Warm-up on startup
136
+ # -----------------------------
137
+ def warmup():
138
+ """Ping the model once to prevent timeout at first request"""
139
+ print("๐Ÿ”ฅ Warming up Parrot model...")
140
+ try:
141
+ model = get_parrot()
142
+ _ = model.augment(input_phrase="hello world", do_diverse=False)
143
+ print("โœ… Warmup complete.")
144
+ except Exception as e:
145
+ print(f"โš ๏ธ Warmup skipped: {e}")
146
+
147
+
148
+ warmup()
149
+
150
+
151
+ # -----------------------------
152
+ # ๐Ÿ”น Gradio Interfaces
153
+ # -----------------------------
154
+ rephrase_iface = gr.Interface(
155
+ fn=rephrase,
156
+ inputs=gr.Textbox(lines=10, placeholder="Paste your text here..."),
157
+ outputs="text",
158
+ title="Parrot Rephraser (Long Text)",
159
+ description="Paraphrases long text while maintaining punctuation and capitalization.",
160
+ )
161
+
162
  sentencewise_iface = gr.Interface(
163
  fn=rephrase_sentencewise_unique,
164
  inputs=gr.Textbox(lines=10, placeholder="Paste text here..."),
 
167
  description="Generates top 3 unique paraphrases per sentence. Optimized for HF free-tier.",
168
  )
169
 
 
170
  # -----------------------------
171
  # ๐Ÿ”น Combine both interfaces into Tabs
172
  # -----------------------------
 
175
  ["Full Text Rephraser", "Sentence-wise Paraphrases"],
176
  )
177
 
178
+ demo.launch(server_port=7860, server_name="0.0.0.0", show_error=True)