Spaces:
Sleeping
Sleeping
File size: 2,556 Bytes
5620e1f c997974 5620e1f c997974 5620e1f c997974 5620e1f b3ec1fd 5620e1f fba1e90 5620e1f fba1e90 5620e1f ec039dd fba1e90 5620e1f |
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 |
import re
import logging
import json
from langchain.schema import (
HumanMessage,
SystemMessage,
)
def save_logs(scheduler, JSON_DATASET_PATH, logs, feedback=None) -> None:
""" Every interaction with app saves the log of question and answer,
this is to get the usage statistics of app and evaluate model performances.
Also saves user feedback (when provided).
"""
if feedback:
logs["feedback"] = feedback #optional
with scheduler.lock:
with JSON_DATASET_PATH.open("a") as f:
json.dump(logs, f)
f.write("\n")
print("logging done")
def get_message_template(type, SYSTEM_PROMPT, USER_PROMPT):
if type == 'NVIDIA':
messages = [{"role": "system", "content": SYSTEM_PROMPT},
{"role":"user","content":USER_PROMPT}]
elif type == 'DEDICATED':
messages = [
SystemMessage(content=SYSTEM_PROMPT),
HumanMessage(content=USER_PROMPT),]
else:
messages = None
return messages
def make_html_source(source,i):
"""
takes the text and converts it into html format for display in "source" side tab
"""
meta = source.metadata
content = source.page_content.strip()
name = meta['subtype']
card = f"""
<div class="card" id="doc{i}">
<div class="card-content">
<h2>Doc {i} - {meta['subtype']} - Page {int(meta['page'])}</h2>
<p>{content}</p>
</div>
<div class="card-footer">
<span>{name}</span>
<a href="{meta['subtype']}#page={int(meta['page'])}" target="_blank" class="pdf-link">
<span role="img" aria-label="Open PDF">🔗</span>
</a>
</div>
</div>
"""
return card
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 |