File size: 5,905 Bytes
48e003d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d14568c
48e003d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d14568c
 
48e003d
 
 
 
 
 
 
 
 
 
 
 
 
 
c3b815e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d14568c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5c9c65
14a5a97
d14568c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48e003d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213

import re

def make_pairs(lst):
    """from a list of even lenght, make tupple pairs"""
    return [(lst[i], lst[i + 1]) for i in range(0, len(lst), 2)]


def serialize_docs(docs):
    new_docs = []
    for doc in docs:
        new_doc = {}
        new_doc["page_content"] = doc.page_content
        new_doc["metadata"] = doc.metadata
        new_docs.append(new_doc)
    return new_docs



def parse_output_llm_with_sources(output):
    # Split the content into a list of text and "[Doc X]" references
    content_parts = re.split(r'\[(Doc\s?\d+(?:,\s?Doc\s?\d+)*)\]', output)
    parts = []
    for part in content_parts:
        if part.startswith("Doc"):
            subparts = part.split(",")
            subparts = [subpart.lower().replace("doc","").strip() for subpart in subparts]
            subparts = [f"""<a href="#doc{subpart}" class="a-doc-ref" target="_self"><span class='doc-ref'><sup>{subpart}</sup></span></a>""" for subpart in subparts]
            parts.append("".join(subparts))
        else:
            parts.append(part)
    content_parts = "".join(parts)
    return content_parts


def make_html_source(source,i):
    meta = source.metadata
    # content = source.page_content.split(":",1)[1].strip()
    content = source.page_content.strip()

    toc_levels = []
    for j in range(2):
        level = meta[f"toc_level{j}"]
        if level != "N/A":
            toc_levels.append(level)
        else:
            break
    toc_levels = " > ".join(toc_levels)

    if len(toc_levels) > 0:
        name = f"<b>{toc_levels}</b><br/>{meta['name']}"
    else:
        name = meta['name']

    score = meta['reranking_score']
    if score > 0.8:
        color = "score-green"
    elif score > 0.5:
        color = "score-orange"
    else:
        color = "score-red"

    relevancy_score = f"<p class=relevancy-score>Relevancy score: <span class='{color}'>{score:.1%}</span></p>"

    if meta["chunk_type"] == "text":

        card = f"""
    <div class="card" id="doc{i}">
        <div class="card-content">
            <h2>Doc {i} - {meta['short_name']} - Page {int(meta['page_number'])}</h2>
            <p>{content}</p>
            {relevancy_score}
        </div>
        <div class="card-footer">
            <span>{name}</span>
            <a href="{meta['url']}#page={int(meta['page_number'])}" target="_blank" class="pdf-link">
                <span role="img" aria-label="Open PDF">πŸ”—</span>
            </a>
        </div>
    </div>
    """
    
    else:

        if meta["figure_code"] != "N/A":
            title = f"{meta['figure_code']} - {meta['short_name']}"
        else:
            title = f"{meta['short_name']}"

        card = f"""
    <div class="card card-image">
        <div class="card-content">
            <h2>Image {i} - {title} - Page {int(meta['page_number'])}</h2>
            <p class='ai-generated'>AI-generated description</p>
            <p>{content}</p>

            {relevancy_score}
        </div>
        <div class="card-footer">
            <span>{name}</span>
            <a href="{meta['url']}#page={int(meta['page_number'])}" target="_blank" class="pdf-link">
                <span role="img" aria-label="Open PDF">πŸ”—</span>
            </a>
        </div>
    </div>
    """
        
    return card


def make_html_df(df,i):
    title = df['title'][i]
    content = df['abstract'][i]
    url = df['doi'][i]
    publication_date = df['publication_year'][i]

    card = f"""
    <div class="card" id="doc{i}">
        <div class="card-content">
            <h2>Doc {i+1} - {title}</h2>
            <p>{content}</p>
        </div>
        <div class="card-footer">
            <span>{publication_date}</span>
            <a href="{url}" target="_blank" class="pdf-link">
        </div>
    </div>
        """
    
    return card
        

def make_html_figure_sources(source,i,img_str):
    meta = source.metadata
    content = source.page_content.strip()
    
    score = meta['reranking_score']
    if score > 0.8:
        color = "score-green"
    elif score > 0.5:
        color = "score-orange"
    else:
        color = "score-red"
        
    toc_levels = []
    if len(toc_levels) > 0:
        name = f"<b>{toc_levels}</b><br/>{meta['name']}"
    else:
        name = meta['name']
        
    relevancy_score = f"<p class=relevancy-score>Relevancy score: <span class='{color}'>{score:.1%}</span></p>"

    if meta["figure_code"] != "N/A":
        title = f"{meta['figure_code']} - {meta['short_name']}"
    else:
        title = f"{meta['short_name']}"

    card = f"""
    <div class="card card-image">
        <div class="card-content">
            <h2>Image {i} - {title} - Page {int(meta['page_number'])}</h2>
            <img src="data:image/png;base64, { img_str }" alt="Alt text" />
            <p class='ai-generated'>AI-generated description</p>

            <p>{content}</p>

            {relevancy_score}
        </div>
        <div class="card-footer">
            <span>{name}</span>
            <a href="{meta['url']}#page={int(meta['page_number'])}" target="_blank" class="pdf-link">
                <span role="img" aria-label="Open PDF">πŸ”—</span>
            </a>
        </div>
    </div>
    """
    return card

    

def make_toolbox(tool_name,description = "",checked = False,elem_id = "toggle"):

    if checked:
        span = "<span class='checkmark'>&#10003;</span>"
    else:
        span = "<span class='loader'></span>"

#     toolbox = f"""
# <div class="dropdown">
# <label for="{elem_id}" class="dropdown-toggle">
#     {span}
#     {tool_name}
#     <span class="caret"></span>
# </label>
# <input type="checkbox" id="{elem_id}" hidden/>
# <div class="dropdown-content">
#     <p>{description}</p>
# </div>
# </div>
# """
    

    toolbox = f"""
<div class="dropdown">
<label for="{elem_id}" class="dropdown-toggle">
    {span}
    {tool_name}
</label>
</div>
"""

    return toolbox