sirenice commited on
Commit
6419fa7
Β·
verified Β·
1 Parent(s): bd4fa7b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -178
app.py CHANGED
@@ -15,16 +15,14 @@ print(f"βœ“ Tokenizer vocab size: {len(tokenizer)}")
15
  print(f"βœ“ Model parameters: {model.num_parameters()}")
16
 
17
  def smart_fallback_optimize(prompt):
18
- """Fallback optimization using heuristics when model fails"""
19
- # Remove common filler words and phrases
20
  fillers = {
21
  'please', 'could', 'would', 'can', 'you', 'the', 'a', 'an',
22
  'very', 'really', 'quite', 'just', 'actually', 'basically',
23
  'literally', 'honestly', 'i think', 'in my opinion',
24
- 'it seems', 'kind of', 'sort of'
25
  }
26
 
27
- # Common verbose phrases to simplify
28
  replacements = {
29
  r'\bcan you please\b': '',
30
  r'\bcould you please\b': '',
@@ -33,32 +31,23 @@ def smart_fallback_optimize(prompt):
33
  r'\bi want to\b': '',
34
  r'\bhelp me\b': '',
35
  r'\bfor me\b': '',
36
- r'\bthat is\b': '',
37
- r'\bwhich is\b': '',
38
  }
39
 
40
  optimized = prompt.lower()
41
 
42
- # Apply replacements
43
  for pattern, replacement in replacements.items():
44
  optimized = re.sub(pattern, replacement, optimized)
45
 
46
- # Remove filler words
47
  words = optimized.split()
48
  words = [w for w in words if w not in fillers]
49
-
50
- # Join and clean up
51
  optimized = ' '.join(words).strip()
52
 
53
- # Capitalize first letter
54
  if optimized:
55
  optimized = optimized[0].upper() + optimized[1:]
56
 
57
- # If too short or empty, return a simplified version
58
  if not optimized or len(optimized) < 5:
59
- # Just take key words
60
  words = prompt.split()
61
- optimized = ' '.join(words[:5]) # First 5 words
62
 
63
  return optimized
64
 
@@ -70,15 +59,9 @@ def optimize_prompt(prompt, preserve_meaning=True):
70
  print(f"\n=== OPTIMIZING ===")
71
  print(f"Input: {prompt[:100]}")
72
 
73
- # Try model first
74
  try:
75
  input_text = "optimize: " + prompt
76
- input_ids = tokenizer.encode(
77
- input_text,
78
- return_tensors="pt",
79
- truncation=True,
80
- max_length=512
81
- )
82
 
83
  with torch.no_grad():
84
  outputs = model.generate(
@@ -94,34 +77,27 @@ def optimize_prompt(prompt, preserve_meaning=True):
94
 
95
  optimized = tokenizer.decode(outputs[0], skip_special_tokens=True)
96
 
97
- # Calculate how much it actually optimized
98
  original_tokens = len(tokenizer.encode(prompt))
99
  optimized_tokens = len(tokenizer.encode(optimized))
100
  reduction_rate = (original_tokens - optimized_tokens) / original_tokens
101
 
102
- # If model barely optimized (less than 10% reduction), use fallback
103
  if reduction_rate < 0.1 or not optimized or len(optimized.strip()) < 3:
104
- print("⚠ Model optimization weak, using smart fallback")
105
  optimized = smart_fallback_optimize(prompt)
106
  else:
107
- print(f"βœ“ Model optimization successful")
108
 
109
  except Exception as e:
110
- print(f"⚠ Model error: {e}, using fallback")
111
  optimized = smart_fallback_optimize(prompt)
112
 
113
  print(f"Output: {optimized}")
114
 
115
- # Calculate final metrics
116
  original_tokens = len(tokenizer.encode(prompt))
117
  optimized_tokens = len(tokenizer.encode(optimized))
118
  tokens_saved = max(0, original_tokens - optimized_tokens)
119
  reduction = (tokens_saved / max(original_tokens, 1)) * 100
120
-
121
- # Energy: 0.000002 Wh per token
122
  energy = tokens_saved * 0.000002
123
-
124
- # CO2: 475g per kWh
125
  co2 = energy * 475 / 1000
126
 
127
  print(f"Tokens: {original_tokens} β†’ {optimized_tokens} (saved {tokens_saved})")
@@ -136,151 +112,9 @@ def optimize_prompt(prompt, preserve_meaning=True):
136
  f"{co2:.6f} g"
137
  )
138
 
139
- # CSS matching your website style
140
- custom_css = """
141
- /* Main container - darker background like website */
142
- .gradio-container {
143
- background: linear-gradient(135deg, #1a2332 0%, #0d1520 100%) !important;
144
- padding: 2rem !important;
145
- font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif !important;
146
- }
147
-
148
- /* Primary button - gradient like website */
149
- .gr-button-primary {
150
- background: linear-gradient(135deg, #34d399, #3b82f6) !important;
151
- border: none !important;
152
- border-radius: 10px !important;
153
- font-weight: 600 !important;
154
- padding: 14px 28px !important;
155
- font-size: 1.1rem !important;
156
- transition: all 0.3s ease !important;
157
- box-shadow: 0 4px 12px rgba(52, 211, 153, 0.3) !important;
158
- }
159
-
160
- .gr-button-primary:hover {
161
- transform: translateY(-3px) !important;
162
- box-shadow: 0 6px 20px rgba(52, 211, 153, 0.5) !important;
163
- }
164
-
165
- /* Textareas - match website input boxes */
166
- textarea {
167
- background: rgba(20, 30, 45, 0.95) !important;
168
- border: 2px solid rgba(52, 211, 153, 0.3) !important;
169
- color: #e2e8f0 !important;
170
- border-radius: 10px !important;
171
- padding: 1rem !important;
172
- font-size: 1rem !important;
173
- line-height: 1.6 !important;
174
- }
175
-
176
- textarea:focus {
177
- border-color: #34d399 !important;
178
- box-shadow: 0 0 0 3px rgba(52, 211, 153, 0.15) !important;
179
- outline: none !important;
180
- }
181
-
182
- /* Labels - green accent */
183
- label {
184
- color: #34d399 !important;
185
- font-weight: 600 !important;
186
- font-size: 1.1rem !important;
187
- margin-bottom: 0.5rem !important;
188
- }
189
-
190
- /* Title styling */
191
- h1 {
192
- background: linear-gradient(135deg, #34d399, #3b82f6);
193
- -webkit-background-clip: text !important;
194
- -webkit-text-fill-color: transparent !important;
195
- font-size: 2.8rem !important;
196
- text-align: center !important;
197
- font-weight: 700 !important;
198
- margin-bottom: 0.5rem !important;
199
- }
200
-
201
- /* Subtitle */
202
- .gradio-container h3 {
203
- color: #94a3b8 !important;
204
- text-align: center !important;
205
- font-weight: 400 !important;
206
- font-size: 1.2rem !important;
207
- margin-bottom: 2rem !important;
208
- }
209
-
210
- /* Result boxes - colorful like website */
211
- .gr-box {
212
- background: rgba(20, 30, 45, 0.8) !important;
213
- border: 2px solid rgba(52, 211, 153, 0.3) !important;
214
- border-radius: 10px !important;
215
- padding: 0.75rem !important;
216
- }
217
-
218
- /* Specific result box colors */
219
- div[class*="token_info"] .gr-box,
220
- div:nth-child(1) .gr-box {
221
- border-color: #34d399 !important;
222
- background: linear-gradient(135deg, rgba(52, 211, 153, 0.15), rgba(59, 130, 246, 0.15)) !important;
223
- }
224
-
225
- div[class*="tokens_saved"] .gr-box,
226
- div:nth-child(2) .gr-box {
227
- border-color: #fbbf24 !important;
228
- background: linear-gradient(135deg, rgba(251, 191, 36, 0.15), rgba(245, 158, 11, 0.15)) !important;
229
- }
230
-
231
- div[class*="reduction"] .gr-box,
232
- div:nth-child(3) .gr-box {
233
- border-color: #8b5cf6 !important;
234
- background: linear-gradient(135deg, rgba(139, 92, 246, 0.15), rgba(124, 58, 237, 0.15)) !important;
235
- }
236
-
237
- div[class*="energy"] .gr-box,
238
- div:nth-child(4) .gr-box {
239
- border-color: #22d3ee !important;
240
- background: linear-gradient(135deg, rgba(34, 211, 238, 0.15), rgba(6, 182, 212, 0.15)) !important;
241
- }
242
-
243
- div[class*="co2"] .gr-box,
244
- div:nth-child(5) .gr-box {
245
- border-color: #10b981 !important;
246
- background: linear-gradient(135deg, rgba(16, 185, 129, 0.15), rgba(52, 211, 153, 0.15)) !important;
247
- }
248
-
249
- /* Checkbox */
250
- .gr-checkbox {
251
- color: #cbd5e1 !important;
252
- }
253
-
254
- /* Info text */
255
- .gr-form span {
256
- color: #94a3b8 !important;
257
- }
258
-
259
- /* Remove footer */
260
- .footer {
261
- display: none !important;
262
- }
263
-
264
- /* Section headers */
265
- .gr-markdown h3 {
266
- color: #34d399 !important;
267
- font-size: 1.5rem !important;
268
- margin-top: 1.5rem !important;
269
- margin-bottom: 1rem !important;
270
- }
271
-
272
- /* Bottom text */
273
- .gr-markdown p {
274
- color: #94a3b8 !important;
275
- text-align: center !important;
276
- }
277
-
278
- /* Horizontal rule */
279
- .gr-markdown hr {
280
- border-color: rgba(52, 211, 153, 0.2) !important;
281
- margin: 1.5rem 0 !important;
282
- }
283
- """
284
 
285
  # Create interface
286
  with gr.Blocks(css=custom_css) as demo:
@@ -319,7 +153,6 @@ with gr.Blocks(css=custom_css) as demo:
319
 
320
  co2 = gr.Textbox(label="🌍 COβ‚‚ Reduced", interactive=False)
321
 
322
- # Connect button
323
  btn.click(
324
  optimize_prompt,
325
  inputs=[prompt_box, preserve_box],
@@ -329,6 +162,5 @@ with gr.Blocks(css=custom_css) as demo:
329
  gr.Markdown("---")
330
  gr.Markdown("βœ… **Built with πŸ’š for a greener AI future | Powered by T5**")
331
 
332
- # Launch
333
  if __name__ == "__main__":
334
  demo.launch()
 
15
  print(f"βœ“ Model parameters: {model.num_parameters()}")
16
 
17
  def smart_fallback_optimize(prompt):
18
+ """Fallback optimization when model fails"""
 
19
  fillers = {
20
  'please', 'could', 'would', 'can', 'you', 'the', 'a', 'an',
21
  'very', 'really', 'quite', 'just', 'actually', 'basically',
22
  'literally', 'honestly', 'i think', 'in my opinion',
23
+ 'it seems', 'kind of', 'sort of', 'i want', 'i would like'
24
  }
25
 
 
26
  replacements = {
27
  r'\bcan you please\b': '',
28
  r'\bcould you please\b': '',
 
31
  r'\bi want to\b': '',
32
  r'\bhelp me\b': '',
33
  r'\bfor me\b': '',
 
 
34
  }
35
 
36
  optimized = prompt.lower()
37
 
 
38
  for pattern, replacement in replacements.items():
39
  optimized = re.sub(pattern, replacement, optimized)
40
 
 
41
  words = optimized.split()
42
  words = [w for w in words if w not in fillers]
 
 
43
  optimized = ' '.join(words).strip()
44
 
 
45
  if optimized:
46
  optimized = optimized[0].upper() + optimized[1:]
47
 
 
48
  if not optimized or len(optimized) < 5:
 
49
  words = prompt.split()
50
+ optimized = ' '.join(words[:8])
51
 
52
  return optimized
53
 
 
59
  print(f"\n=== OPTIMIZING ===")
60
  print(f"Input: {prompt[:100]}")
61
 
 
62
  try:
63
  input_text = "optimize: " + prompt
64
+ input_ids = tokenizer.encode(input_text, return_tensors="pt", truncation=True, max_length=512)
 
 
 
 
 
65
 
66
  with torch.no_grad():
67
  outputs = model.generate(
 
77
 
78
  optimized = tokenizer.decode(outputs[0], skip_special_tokens=True)
79
 
 
80
  original_tokens = len(tokenizer.encode(prompt))
81
  optimized_tokens = len(tokenizer.encode(optimized))
82
  reduction_rate = (original_tokens - optimized_tokens) / original_tokens
83
 
 
84
  if reduction_rate < 0.1 or not optimized or len(optimized.strip()) < 3:
85
+ print("⚠ Model weak, using fallback")
86
  optimized = smart_fallback_optimize(prompt)
87
  else:
88
+ print(f"βœ“ Model success")
89
 
90
  except Exception as e:
91
+ print(f"⚠ Error: {e}, using fallback")
92
  optimized = smart_fallback_optimize(prompt)
93
 
94
  print(f"Output: {optimized}")
95
 
 
96
  original_tokens = len(tokenizer.encode(prompt))
97
  optimized_tokens = len(tokenizer.encode(optimized))
98
  tokens_saved = max(0, original_tokens - optimized_tokens)
99
  reduction = (tokens_saved / max(original_tokens, 1)) * 100
 
 
100
  energy = tokens_saved * 0.000002
 
 
101
  co2 = energy * 475 / 1000
102
 
103
  print(f"Tokens: {original_tokens} β†’ {optimized_tokens} (saved {tokens_saved})")
 
112
  f"{co2:.6f} g"
113
  )
114
 
115
+ # Load custom CSS
116
+ with open("custom.css", "r") as f:
117
+ custom_css = f.read()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
 
119
  # Create interface
120
  with gr.Blocks(css=custom_css) as demo:
 
153
 
154
  co2 = gr.Textbox(label="🌍 COβ‚‚ Reduced", interactive=False)
155
 
 
156
  btn.click(
157
  optimize_prompt,
158
  inputs=[prompt_box, preserve_box],
 
162
  gr.Markdown("---")
163
  gr.Markdown("βœ… **Built with πŸ’š for a greener AI future | Powered by T5**")
164
 
 
165
  if __name__ == "__main__":
166
  demo.launch()