roemmele commited on
Commit
58bad40
·
1 Parent(s): 9d1b8ec

Updated app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -48
app.py CHANGED
@@ -50,17 +50,17 @@ def call_creative_help(prompt: str, token: str, url: str) -> dict:
50
 
51
  def extract_prompt_and_trigger(text: str) -> tuple[str, bool]:
52
  """
53
- If text ends with \\help\\, return (text_without_trigger, True).
54
  Otherwise return (text, False).
55
  """
56
  if not text or not text.strip():
57
  return text, False
58
- # Match \\help\\ at end (with optional trailing whitespace after it)
59
- # Use greedy (.*) to preserve whitespace (e.g. paragraph breaks) before \help\
60
- match = re.search(r"(.*)\\help\\\s*$", text, re.DOTALL)
61
- if match:
62
- # Don't rstrip; preserve trailing whitespace
63
- return match.group(1), True
64
  return text, False
65
 
66
 
@@ -162,9 +162,52 @@ APP_EXPLANATION = (
162
  )
163
 
164
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165
  def create_ui():
166
  """Build the Creative Help Gradio interface."""
167
- with gr.Blocks(title="Creative Help", js=CURSOR_JS) as demo:
 
 
 
 
 
168
  model_url = get_model_repo_url()
169
  paper_url = get_paper_url()
170
  header_icons = (
@@ -197,7 +240,11 @@ def create_ui():
197
  def on_text_input(text: str):
198
  prompt, had_trigger = extract_prompt_and_trigger(text or "")
199
  if not had_trigger:
200
- yield gr.skip()
 
 
 
 
201
  return
202
 
203
  # Preserve text (dimmed) while waiting, then replace with result
@@ -233,42 +280,4 @@ def create_ui():
233
 
234
  if __name__ == "__main__":
235
  demo = create_ui()
236
- demo.launch(
237
- theme=gr.themes.Soft(primary_hue="red", secondary_hue="slate"),
238
- css="""
239
- .header-row { display: flex !important; align-items: center !important; gap: 0.75rem !important; margin-bottom: 0.25rem !important; }
240
- .header-icons { display: flex !important; gap: 0.5rem !important; }
241
- .header-icon { color: #718096 !important; cursor: pointer !important; transition: color 0.2s !important; }
242
- .header-icon:hover { color: #c53030 !important; }
243
- .header-icon svg { display: block !important; }
244
- .header-icon-help { position: relative !important; }
245
- .header-icon-help .help-tooltip {
246
- visibility: hidden; opacity: 0; position: absolute; left: 50%; transform: translateX(-50%);
247
- top: 100%; margin-top: 8px; padding: 10px 14px; background: #2d3748; color: #fff;
248
- font-size: 0.85rem; line-height: 1.4; border-radius: 8px; width: 280px; text-align: left;
249
- box-shadow: 0 4px 12px rgba(0,0,0,0.2); z-index: 100; transition: opacity 0.2s, visibility 0.2s;
250
- }
251
- .header-icon-help:hover .help-tooltip { visibility: visible; opacity: 1; }
252
- .creative-help-title { font-size: 2rem !important; font-weight: 700 !important; color: #c53030 !important; margin: 0 !important; }
253
- .footer { margin-top: 1rem !important; padding-top: 0.75rem !important; border-top: 1px solid #e2e8f0 !important; }
254
- .footer { display: flex !important; flex-wrap: wrap !important; gap: 1rem !important; }
255
- .footer-link { color: #718096 !important; font-size: 0.875rem !important; text-decoration: none !important; display: inline-flex !important; align-items: center !important; gap: 0.35rem !important; }
256
- .footer-link:hover { color: #c53030 !important; }
257
- .instructions { color: #4a5568; font-size: 0.95rem; margin-bottom: 1rem; }
258
- /* Dim textbox while processing - remove border/outline from textarea and container */
259
- #story-input textarea:disabled {
260
- opacity: 0.25 !important;
261
- background-color: #e2e8f0 !important;
262
- filter: blur(0.5px);
263
- border: none !important;
264
- box-shadow: none !important;
265
- outline: none !important;
266
- }
267
- #story-input:has(textarea:disabled),
268
- #story-input:has(textarea:disabled) * {
269
- border: none !important;
270
- box-shadow: none !important;
271
- outline: none !important;
272
- }
273
- """,
274
- )
 
50
 
51
  def extract_prompt_and_trigger(text: str) -> tuple[str, bool]:
52
  """
53
+ If text ends with \\help\\ or /help/, return (text_without_trigger, True).
54
  Otherwise return (text, False).
55
  """
56
  if not text or not text.strip():
57
  return text, False
58
+ # Match \help\ or /help/ at end (with optional trailing whitespace)
59
+ # Use greedy (.*) to preserve whitespace (e.g. paragraph breaks) before trigger
60
+ for pattern in (r"(.*)\\help\\\s*$", r"(.*)/help/\s*$"):
61
+ match = re.search(pattern, text, re.DOTALL)
62
+ if match:
63
+ return match.group(1), True
64
  return text, False
65
 
66
 
 
162
  )
163
 
164
 
165
+ CUSTOM_CSS = """
166
+ .header-row { display: flex !important; align-items: center !important; gap: 0.75rem !important; margin-bottom: 0.25rem !important; }
167
+ .header-icons { display: flex !important; gap: 0.5rem !important; }
168
+ .header-icon { color: #718096 !important; cursor: pointer !important; transition: color 0.2s !important; }
169
+ .header-icon:hover { color: #c53030 !important; }
170
+ .header-icon svg { display: block !important; }
171
+ .header-icon-help { position: relative !important; }
172
+ .header-icon-help .help-tooltip {
173
+ visibility: hidden; opacity: 0; position: absolute; left: 50%; transform: translateX(-50%);
174
+ top: 100%; margin-top: 8px; padding: 10px 14px; background: #2d3748; color: #fff;
175
+ font-size: 0.85rem; line-height: 1.4; border-radius: 8px; width: 280px; text-align: left;
176
+ box-shadow: 0 4px 12px rgba(0,0,0,0.2); z-index: 100; transition: opacity 0.2s, visibility 0.2s;
177
+ }
178
+ .header-icon-help:hover .help-tooltip { visibility: visible; opacity: 1; }
179
+ .creative-help-title { font-size: 2rem !important; font-weight: 700 !important; color: #c53030 !important; margin: 0 !important; }
180
+ .footer { margin-top: 1rem !important; padding-top: 0.75rem !important; border-top: 1px solid #e2e8f0 !important; }
181
+ .footer { display: flex !important; flex-wrap: wrap !important; gap: 1rem !important; }
182
+ .footer-link { color: #718096 !important; font-size: 0.875rem !important; text-decoration: none !important; display: inline-flex !important; align-items: center !important; gap: 0.35rem !important; }
183
+ .footer-link:hover { color: #c53030 !important; }
184
+ .instructions { color: #4a5568; font-size: 0.95rem; margin-bottom: 1rem; }
185
+ /* Dim textbox while processing - remove border/outline from textarea and container */
186
+ #story-input textarea:disabled {
187
+ opacity: 0.25 !important;
188
+ background-color: #e2e8f0 !important;
189
+ filter: blur(0.5px);
190
+ border: none !important;
191
+ box-shadow: none !important;
192
+ outline: none !important;
193
+ }
194
+ #story-input:has(textarea:disabled),
195
+ #story-input:has(textarea:disabled) * {
196
+ border: none !important;
197
+ box-shadow: none !important;
198
+ outline: none !important;
199
+ }
200
+ """
201
+
202
+
203
  def create_ui():
204
  """Build the Creative Help Gradio interface."""
205
+ with gr.Blocks(
206
+ title="Creative Help",
207
+ js=CURSOR_JS,
208
+ theme=gr.themes.Soft(primary_hue="red", secondary_hue="slate"),
209
+ css=CUSTOM_CSS,
210
+ ) as demo:
211
  model_url = get_model_repo_url()
212
  paper_url = get_paper_url()
213
  header_icons = (
 
240
  def on_text_input(text: str):
241
  prompt, had_trigger = extract_prompt_and_trigger(text or "")
242
  if not had_trigger:
243
+ # Use gr.skip() if available (Gradio 5+), else no-op update for Gradio 4
244
+ if hasattr(gr, "skip"):
245
+ yield gr.skip()
246
+ else:
247
+ yield gr.update(value=text)
248
  return
249
 
250
  # Preserve text (dimmed) while waiting, then replace with result
 
280
 
281
  if __name__ == "__main__":
282
  demo = create_ui()
283
+ demo.launch()