cstr commited on
Commit
2b4c1a8
Β·
verified Β·
1 Parent(s): 72b2920

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -20
app.py CHANGED
@@ -502,44 +502,46 @@ def send_to_model_impl(prompt, model_selection, hf_model_choice, hf_custom_model
502
 
503
  try:
504
  if model_selection == "Clipboard only":
 
 
505
 
506
- # Return special message that will trigger clipboard copy in JavaScript
507
- return gr.HTML(
508
- f"""
509
  <script>
510
- try {{
511
- navigator.clipboard.writeText(`{prompt.replace('`', '\\`')}`)
512
- .then(() => {{
 
513
  document.getElementById('clipboard_status').textContent = 'βœ… Copied to clipboard!';
514
- setTimeout(() => {{
515
  document.getElementById('clipboard_status').textContent = '';
516
- }}, 2000);
517
- }})
518
- .catch(err => {{
519
  console.error('Copy failed:', err);
520
  // Fallback
521
  const textarea = document.createElement('textarea');
522
- textarea.value = `{prompt.replace('`', '\\`')}`;
523
  document.body.appendChild(textarea);
524
  textarea.select();
525
  document.execCommand('copy');
526
  document.body.removeChild(textarea);
527
  document.getElementById('clipboard_status').textContent = 'βœ… Copied to clipboard!';
528
- setTimeout(() => {{
529
  document.getElementById('clipboard_status').textContent = '';
530
- }}, 2000);
531
- }});
532
- }} catch(err) {{
533
  console.error('Copy error:', err);
534
  document.getElementById('clipboard_status').textContent = '❌ Copy failed. Try again.';
535
- setTimeout(() => {{
536
  document.getElementById('clipboard_status').textContent = '';
537
- }}, 2000);
538
- }}
539
  </script>
540
  <div id="clipboard_status" style="color: green; font-weight: bold;"></div>
541
- """
542
- ), None
543
 
544
  # Get the summary based on model selection
545
  if model_selection == "HuggingFace Inference":
 
502
 
503
  try:
504
  if model_selection == "Clipboard only":
505
+ # Escape the prompt for JavaScript
506
+ escaped_prompt = prompt.replace('"', '\\"').replace("'", "\\'").replace('\n', '\\n')
507
 
508
+ # Create HTML with JavaScript using string formatting
509
+ html_template = '''
 
510
  <script>
511
+ try {
512
+ const textToCopy = "%s";
513
+ navigator.clipboard.writeText(textToCopy)
514
+ .then(() => {
515
  document.getElementById('clipboard_status').textContent = 'βœ… Copied to clipboard!';
516
+ setTimeout(() => {
517
  document.getElementById('clipboard_status').textContent = '';
518
+ }, 2000);
519
+ })
520
+ .catch(err => {
521
  console.error('Copy failed:', err);
522
  // Fallback
523
  const textarea = document.createElement('textarea');
524
+ textarea.value = textToCopy;
525
  document.body.appendChild(textarea);
526
  textarea.select();
527
  document.execCommand('copy');
528
  document.body.removeChild(textarea);
529
  document.getElementById('clipboard_status').textContent = 'βœ… Copied to clipboard!';
530
+ setTimeout(() => {
531
  document.getElementById('clipboard_status').textContent = '';
532
+ }, 2000);
533
+ });
534
+ } catch(err) {
535
  console.error('Copy error:', err);
536
  document.getElementById('clipboard_status').textContent = '❌ Copy failed. Try again.';
537
+ setTimeout(() => {
538
  document.getElementById('clipboard_status').textContent = '';
539
+ }, 2000);
540
+ }
541
  </script>
542
  <div id="clipboard_status" style="color: green; font-weight: bold;"></div>
543
+ '''
544
+ return gr.HTML(html_template % escaped_prompt), None
545
 
546
  # Get the summary based on model selection
547
  if model_selection == "HuggingFace Inference":