IPF commited on
Commit
86a7cca
·
verified ·
1 Parent(s): 0d53b0b

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -8
app.py CHANGED
@@ -493,12 +493,25 @@ def render_citations(text: str, browser: SimpleBrowser) -> str:
493
  if info and info.get('url'):
494
  # Return clickable index link pointing to reference section
495
  # Aligned with generate_html_example.py style (green via CSS class)
496
- return f'<a href="#ref-{index}" class="citation-link">[{index}]</a>'
 
 
 
497
  except:
498
  pass
499
  return m.group(0)
500
 
 
501
  result = re.sub(r'【(\d+)†L(\d+)(?:-L(\d+))?】', replace_citation, text)
 
 
 
 
 
 
 
 
 
502
 
503
  # Convert basic markdown to HTML
504
  result = re.sub(r'\*\*(.+?)\*\*', r'<strong>\1</strong>', result)
@@ -950,8 +963,8 @@ def run_agent_streaming(
950
 
951
  # Generate Reference Section
952
  if browser.used_citations:
953
- html_parts.append('<div class="reference-section">')
954
- html_parts.append('<div class="reference-title">References</div>')
955
 
956
  for i, cursor in enumerate(browser.used_citations):
957
  info = browser.get_page_info(cursor)
@@ -963,17 +976,17 @@ def run_agent_streaming(
963
  title = "Unknown Source"
964
 
965
  ref_item = f'''
966
- <a href="{html.escape(url)}" target="_blank" class="reference-item" id="ref-{i}">
967
  <div style="display: flex; align-items: baseline;">
968
  <span class="ref-number">[{i}]</span>
969
- <span class="ref-text">{html.escape(title)}</span>
970
  </div>
971
- <div class="ref-url">{html.escape(url)}</div>
972
- </a>
973
  '''
974
  html_parts.append(ref_item)
975
 
976
- html_parts.append('</div>')
977
  yield ''.join(html_parts)
978
 
979
  except Exception as e:
@@ -1464,6 +1477,26 @@ def create_interface():
1464
  border: 1px solid #7dd3fc;
1465
  text-align: right;
1466
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1467
 
1468
  .user-message-content {
1469
  line-height: 1.6;
 
493
  if info and info.get('url'):
494
  # Return clickable index link pointing to reference section
495
  # Aligned with generate_html_example.py style (green via CSS class)
496
+ return f'<a href="{html.escape(url)}" target="_blank" class="citation-link">[{index}]</a>'
497
+
498
+ # Fallback if no URL
499
+ return f'<span class="citation-link">[{index}]</span>'
500
  except:
501
  pass
502
  return m.group(0)
503
 
504
+ # First pass: replace citations with links
505
  result = re.sub(r'【(\d+)†L(\d+)(?:-L(\d+))?】', replace_citation, text)
506
+
507
+ # Second pass: Deduplicate adjacent identical citations
508
+ # Matches: <a ...>[N]</a> followed by optional whitespace and same link
509
+ # We repeat this until no more changes to handle multiple duplicates
510
+ while True:
511
+ new_result = re.sub(r'(<a [^>]+>\[\d+\]</a>)(\s*)\1', r'\1', result)
512
+ if new_result == result:
513
+ break
514
+ result = new_result
515
 
516
  # Convert basic markdown to HTML
517
  result = re.sub(r'\*\*(.+?)\*\*', r'<strong>\1</strong>', result)
 
963
 
964
  # Generate Reference Section
965
  if browser.used_citations:
966
+ html_parts.append('<details class="reference-section">')
967
+ html_parts.append('<summary class="reference-title">References</summary>')
968
 
969
  for i, cursor in enumerate(browser.used_citations):
970
  info = browser.get_page_info(cursor)
 
976
  title = "Unknown Source"
977
 
978
  ref_item = f'''
979
+ <div class="reference-item">
980
  <div style="display: flex; align-items: baseline;">
981
  <span class="ref-number">[{i}]</span>
982
+ <a href="{html.escape(url)}" target="_blank" class="ref-text">{html.escape(title)}</a>
983
  </div>
984
+ <div class="ref-url" style="text-align: left;">{html.escape(url)}</div>
985
+ </div>
986
  '''
987
  html_parts.append(ref_item)
988
 
989
+ html_parts.append('</details>')
990
  yield ''.join(html_parts)
991
 
992
  except Exception as e:
 
1477
  border: 1px solid #7dd3fc;
1478
  text-align: right;
1479
  }
1480
+
1481
+ /* Reference Section Collapsible */
1482
+ .reference-section {
1483
+ margin-top: 40px;
1484
+ border-top: 1px solid #e5e7eb;
1485
+ padding-top: 20px;
1486
+ }
1487
+
1488
+ .reference-title {
1489
+ font-size: 1.2rem;
1490
+ font-weight: 600;
1491
+ margin-bottom: 16px;
1492
+ color: #111827;
1493
+ cursor: pointer;
1494
+ outline: none;
1495
+ }
1496
+
1497
+ .ref-url {
1498
+ text-align: left !important;
1499
+ }
1500
 
1501
  .user-message-content {
1502
  line-height: 1.6;