eljanmahammadli commited on
Commit
3f69766
·
1 Parent(s): 2600e48

fix citation id ordering

Browse files
Files changed (2) hide show
  1. ai_generate.py +0 -3
  2. app.py +9 -2
ai_generate.py CHANGED
@@ -5,11 +5,8 @@ from langchain_core.documents import Document
5
  from langchain_community.embeddings.sentence_transformer import (
6
  SentenceTransformerEmbeddings,
7
  )
8
- from langchain.schema import StrOutputParser
9
  from langchain_community.vectorstores import Chroma
10
  from langchain_text_splitters import RecursiveCharacterTextSplitter
11
- from langchain import hub
12
- from langchain_core.output_parsers import StrOutputParser
13
  from langchain_core.runnables import RunnablePassthrough
14
  from langchain_groq import ChatGroq
15
  from langchain_openai import ChatOpenAI
 
5
  from langchain_community.embeddings.sentence_transformer import (
6
  SentenceTransformerEmbeddings,
7
  )
 
8
  from langchain_community.vectorstores import Chroma
9
  from langchain_text_splitters import RecursiveCharacterTextSplitter
 
 
10
  from langchain_core.runnables import RunnablePassthrough
11
  from langchain_groq import ChatGroq
12
  from langchain_openai import ChatOpenAI
app.py CHANGED
@@ -212,10 +212,12 @@ def generate_cited_html(cited_text, citations: dict):
212
  """
213
 
214
  # Function to replace each citation with a reference button
 
 
215
  citation_count = 0 # To track unique instances of each citation
216
 
217
  def replace_citations(match):
218
- nonlocal citation_count
219
  citation_id = match.group(1) # Extract citation number from the match
220
  ref_data = citations.get(int(citation_id))
221
 
@@ -236,6 +238,11 @@ def generate_cited_html(cited_text, citations: dict):
236
  else:
237
  source_html = f'<span class="source">{ref_data["source"]}</span>'
238
 
 
 
 
 
 
239
  # Unique id for each reference button and popup
240
  unique_id = f"{citation_id}-{citation_count}"
241
  citation_count += 1
@@ -243,7 +250,7 @@ def generate_cited_html(cited_text, citations: dict):
243
  # HTML code for the reference button and popup with formatted content
244
  button_html = f"""
245
  <span class="reference-container">
246
- <label for="ref-toggle-{unique_id}" class="reference-btn" onclick="closeReferencePanes(); document.getElementById('ref-toggle-{unique_id}').checked = true;">{int(citation_id)+1}</label>
247
  <input type="radio" id="ref-toggle-{unique_id}" name="reference" />
248
  <span class="reference-popup">
249
  <span class="close-btn" onclick="document.getElementById('ref-toggle-{unique_id}').checked = false;">&times;</span>
 
212
  """
213
 
214
  # Function to replace each citation with a reference button
215
+ citation_numbers = {}
216
+ next_number = 1
217
  citation_count = 0 # To track unique instances of each citation
218
 
219
  def replace_citations(match):
220
+ nonlocal citation_count, next_number
221
  citation_id = match.group(1) # Extract citation number from the match
222
  ref_data = citations.get(int(citation_id))
223
 
 
238
  else:
239
  source_html = f'<span class="source">{ref_data["source"]}</span>'
240
 
241
+ if citation_id not in citation_numbers:
242
+ citation_numbers[citation_id] = next_number
243
+ next_number += 1
244
+ citation_number = citation_numbers[citation_id]
245
+
246
  # Unique id for each reference button and popup
247
  unique_id = f"{citation_id}-{citation_count}"
248
  citation_count += 1
 
250
  # HTML code for the reference button and popup with formatted content
251
  button_html = f"""
252
  <span class="reference-container">
253
+ <label for="ref-toggle-{unique_id}" class="reference-btn" onclick="closeReferencePanes(); document.getElementById('ref-toggle-{unique_id}').checked = true;">{citation_number}</label>
254
  <input type="radio" id="ref-toggle-{unique_id}" name="reference" />
255
  <span class="reference-popup">
256
  <span class="close-btn" onclick="document.getElementById('ref-toggle-{unique_id}').checked = false;">&times;</span>