cstr commited on
Commit
a545c06
Β·
verified Β·
1 Parent(s): 24bc431

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -55
app.py CHANGED
@@ -478,67 +478,70 @@ def send_to_model_impl(prompt, model_selection, hf_model_choice, hf_custom_model
478
  if model_selection == "Clipboard only":
479
  # Escape the prompt for JavaScript
480
  escaped_prompt = prompt.replace('"', '\\"').replace("'", "\\'").replace('\n', '\\n')
481
-
482
- # Create a temporary file for download
483
  with tempfile.NamedTemporaryFile(delete=False, mode='w', suffix='.txt') as f:
484
  f.write(prompt)
485
  download_file = f.name
486
 
487
- # Use f-string for HTML
488
  html_template = f'''
489
- <div style="text-align: center; margin: 10px;">
490
- <button
491
- onclick="
492
- try {{
493
- const textToCopy = `{escaped_prompt}`;
494
- navigator.clipboard.writeText(textToCopy)
495
- .then(() => {{
496
- this.textContent = 'βœ… Copied to clipboard!';
497
- setTimeout(() => {{
498
- this.textContent = 'πŸ“‹ Copy Text to Clipboard';
499
- }}, 2000);
500
- }})
501
- .catch(err => {{
502
- console.error('Copy failed:', err);
503
- const textarea = document.createElement('textarea');
504
- textarea.value = textToCopy;
505
- document.body.appendChild(textarea);
506
- textarea.select();
507
- document.execCommand('copy');
508
- document.body.removeChild(textarea);
509
- this.textContent = 'βœ… Copied to clipboard!';
510
- setTimeout(() => {{
511
- this.textContent = 'πŸ“‹ Copy Text to Clipboard';
512
- }}, 2000);
513
- }});
514
- }} catch(err) {{
515
- console.error('Copy error:', err);
516
- this.textContent = '❌ Copy failed. Try again.';
517
- setTimeout(() => {{
518
- this.textContent = 'πŸ“‹ Copy Text to Clipboard';
519
- }}, 2000);
520
- }}
521
- "
522
- style="
523
- padding: 10px 20px;
524
- background-color: #2C3E50;
525
- color: white;
526
- border: none;
527
- border-radius: 5px;
528
- font-weight: bold;
529
- cursor: pointer;
530
- transition: background-color 0.3s ease;
531
- "
532
- onmouseover="this.style.backgroundColor='#34495E'"
533
- onmouseout="this.style.backgroundColor='#2C3E50'"
534
- >
535
- πŸ“‹ Copy Text to Clipboard
536
- </button>
537
- </div>
538
  '''
539
-
540
- # Return all three values
541
- return gr.HTML(html_template), "Text copied to clipboard.", download_file
 
 
 
 
542
 
543
  # Get the summary based on model selection
544
  if model_selection == "HuggingFace Inference":
 
478
  if model_selection == "Clipboard only":
479
  # Escape the prompt for JavaScript
480
  escaped_prompt = prompt.replace('"', '\\"').replace("'", "\\'").replace('\n', '\\n')
481
+
482
+ # Create temporary file for download
483
  with tempfile.NamedTemporaryFile(delete=False, mode='w', suffix='.txt') as f:
484
  f.write(prompt)
485
  download_file = f.name
486
 
487
+ # Create HTML with JavaScript using fallback methods
488
  html_template = f'''
489
+ <button
490
+ onclick="
491
+ try {{
492
+ const textToCopy = `{escaped_prompt}`;
493
+ navigator.clipboard.writeText(textToCopy)
494
+ .then(() => {{
495
+ this.textContent = 'βœ… Copied to clipboard!';
496
+ setTimeout(() => {{
497
+ this.textContent = 'πŸ“‹ Copy Text to Clipboard';
498
+ }}, 2000);
499
+ }})
500
+ .catch(err => {{
501
+ console.error('Modern copy failed:', err);
502
+ // Fallback to textarea method
503
+ const textarea = document.createElement('textarea');
504
+ textarea.value = textToCopy;
505
+ document.body.appendChild(textarea);
506
+ textarea.select();
507
+ document.execCommand('copy');
508
+ document.body.removeChild(textarea);
509
+ this.textContent = 'βœ… Copied using fallback!';
510
+ setTimeout(() => {{
511
+ this.textContent = 'πŸ“‹ Copy Text to Clipboard';
512
+ }}, 2000);
513
+ }});
514
+ }} catch(err) {{
515
+ console.error('Copy error:', err);
516
+ this.textContent = '❌ Copy failed. Try again.';
517
+ setTimeout(() => {{
518
+ this.textContent = 'πŸ“‹ Copy Text to Clipboard';
519
+ }}, 2000);
520
+ }}
521
+ "
522
+ style="
523
+ padding: 10px 20px;
524
+ background-color: #2C3E50;
525
+ color: white;
526
+ border: none;
527
+ border-radius: 5px;
528
+ font-weight: bold;
529
+ cursor: pointer;
530
+ transition: background-color 0.3s ease;
531
+ "
532
+ onmouseover="this.style.backgroundColor='#34495E'"
533
+ onmouseout="this.style.backgroundColor='#2C3E50'"
534
+ >
535
+ πŸ“‹ Copy Text to Clipboard
536
+ </button>
 
537
  '''
538
+
539
+ # Return all three expected outputs:
540
+ # 1. HTML component for clipboard action
541
+ # 2. A success message for summary output
542
+ # 3. The download file
543
+ return gr.HTML(html_template), "Text copied to clipboard. Use paste for processing.", download_file
544
+
545
 
546
  # Get the summary based on model selection
547
  if model_selection == "HuggingFace Inference":