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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -68
app.py CHANGED
@@ -479,79 +479,66 @@ def send_to_model_impl(prompt, model_selection, hf_model_choice, hf_custom_model
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 string formatting
488
- html_template = '''
489
  <div style="text-align: center; margin: 10px;">
490
- <button
491
- onclick="
492
- try {
493
- // Try all possible selectors for the prompt textarea
494
- const promptArea =
495
- document.querySelector('#generated_prompt textarea') ||
496
- document.querySelector('textarea#generated_prompt') ||
497
- document.querySelector('.generated_prompt textarea') ||
498
- Array.from(document.querySelectorAll('textarea')).find(el => el.value.includes('Summarize'));
499
-
500
- if (promptArea && promptArea.value) {
501
- navigator.clipboard.writeText(promptArea.value)
502
- .then(() => {
503
- this.textContent = 'βœ… Copied to clipboard!';
504
- setTimeout(() => {
505
- this.textContent = 'πŸ“‹ Copy Text to Clipboard';
506
- }, 2000);
507
- })
508
- .catch(err => {
509
- console.error('Modern copy failed:', err);
510
- // Fallback method
511
- promptArea.select();
512
- document.execCommand('copy');
513
- this.textContent = 'βœ… Copied to clipboard!';
514
- setTimeout(() => {
515
- this.textContent = 'πŸ“‹ Copy Text to Clipboard';
516
- }, 2000);
517
- });
518
- } else {
519
- this.textContent = '❌ No text found. Generate one first.';
520
- setTimeout(() => {
521
- this.textContent = 'πŸ“‹ Copy Text to Clipboard';
522
- }, 2000);
523
- }
524
- } catch (err) {
525
- console.error('Copy error:', err);
526
- this.textContent = '❌ Copy failed. Try again.';
527
- setTimeout(() => {
528
- this.textContent = 'πŸ“‹ Copy Text to Clipboard';
529
- }, 2000);
530
- }
531
- "
532
- style="
533
- padding: 10px 20px;
534
- background-color: #2C3E50;
535
- color: white;
536
- border: none;
537
- border-radius: 5px;
538
- font-weight: bold;
539
- cursor: pointer;
540
- transition: background-color 0.3s ease;
541
- "
542
- onmouseover="this.style.backgroundColor='#34495E'"
543
- onmouseout="this.style.backgroundColor='#2C3E50'"
544
- >
545
- πŸ“‹ Copy Text to Clipboard
546
- </button>
547
- </div>
548
- '''
549
 
550
- # Return all three expected outputs:
551
- # 1. HTML component for status
552
- # 2. Text message for summary output
553
- # 3. Download file
554
- return gr.HTML(html_template % escaped_prompt), "Text copied to clipboard. Use paste for processing.", download_file
555
 
556
  # Get the summary based on model selection
557
  if model_selection == "HuggingFace Inference":
 
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":