Spaces:
Sleeping
Sleeping
adding citation feature
Browse files
app.py
CHANGED
|
@@ -18,26 +18,34 @@ for path in file_names:
|
|
| 18 |
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 19 |
|
| 20 |
def preprocessText(text):
|
| 21 |
-
cleanedText = text.strip()
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
def createEmbeddings(textChunks):
|
| 28 |
chunkEmbeddings = model.encode(cleanedChunks, convert_to_tensor = True)
|
| 29 |
-
return chunkEmbeddings
|
| 30 |
|
| 31 |
-
def getTopChunks(query, chunkEmbeddings, textChunks):
|
| 32 |
queryEmbedding = model.encode(query, convert_to_tensor = True)
|
| 33 |
queryEmbeddingNormalized = queryEmbedding / queryEmbedding.norm()
|
| 34 |
chunkEmbeddingsNormalized = chunkEmbeddings / chunkEmbeddings.norm(dim = 1, keepdim = True)
|
| 35 |
similarities = torch.matmul(chunkEmbeddingsNormalized, queryEmbeddingNormalized)
|
| 36 |
topIndices = torch.topk(similarities, k=3).indices
|
| 37 |
topChunks = [textChunks[i] for i in topIndices]
|
| 38 |
-
|
|
|
|
| 39 |
|
| 40 |
-
cleanedChunks = preprocessText(financialText)
|
| 41 |
chunkEmbeddings = createEmbeddings(cleanedChunks)
|
| 42 |
|
| 43 |
client = Groq(api_key = os.environ.get("SF_TOKEN"))
|
|
@@ -54,8 +62,7 @@ def respond(message, history):
|
|
| 54 |
messages.extend([{"role": h["role"], "content": h["content"]} for h in history])
|
| 55 |
# helping grok to retain message history
|
| 56 |
|
| 57 |
-
topResults = getTopChunks(message, chunkEmbeddings, cleanedChunks)
|
| 58 |
-
print(f'Pulled chunks: {topResults}')
|
| 59 |
context = "\n\n".join(topResults)
|
| 60 |
|
| 61 |
messages.append({"role": "system",
|
|
@@ -76,6 +83,10 @@ def respond(message, history):
|
|
| 76 |
response += token
|
| 77 |
yield response
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
chatbot = gr.ChatInterface(respond, title = "Student Formula Bot 🔬",
|
| 80 |
description = 'Welcome to the core component of \"The Student Formula\": the RAG chatbot! With the ability to act as a finance tutor, accountability buddy, and goal-setting partner all in one, it\'s designed to best suit your needs on the way to productivity and success. To get started, ask about the basic principles of creating a budget!')
|
| 81 |
|
|
@@ -102,9 +113,7 @@ with gr.Blocks(css=custom_css) as demo:
|
|
| 102 |
"What is educational investment?"
|
| 103 |
]
|
| 104 |
)
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
gr.HTML(
|
| 109 |
"""
|
| 110 |
<iframe
|
|
|
|
| 18 |
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 19 |
|
| 20 |
def preprocessText(text):
|
| 21 |
+
cleanedText = text.strip().split("\n")
|
| 22 |
+
cleanedChunks = []
|
| 23 |
+
sectionLabels = []
|
| 24 |
+
for chunk in cleanedText:
|
| 25 |
+
chunk = chunk.strip()
|
| 26 |
+
if not chunk:
|
| 27 |
+
continue
|
| 28 |
+
if chunk.startsith("===") and "SECTION:" in chunk:
|
| 29 |
+
currentLabel = chunk.replace("===", "",).replace("SECTION:", "").strip()
|
| 30 |
+
continue
|
| 31 |
+
cleanedChunks.append(chunk)
|
| 32 |
+
sectionLabels.append(currentLabel)
|
| 33 |
+
return cleanedChunks, sectionLabels
|
| 34 |
|
| 35 |
def createEmbeddings(textChunks):
|
| 36 |
chunkEmbeddings = model.encode(cleanedChunks, convert_to_tensor = True)
|
|
|
|
| 37 |
|
| 38 |
+
def getTopChunks(query, chunkEmbeddings, textChunks, sectionLabels):
|
| 39 |
queryEmbedding = model.encode(query, convert_to_tensor = True)
|
| 40 |
queryEmbeddingNormalized = queryEmbedding / queryEmbedding.norm()
|
| 41 |
chunkEmbeddingsNormalized = chunkEmbeddings / chunkEmbeddings.norm(dim = 1, keepdim = True)
|
| 42 |
similarities = torch.matmul(chunkEmbeddingsNormalized, queryEmbeddingNormalized)
|
| 43 |
topIndices = torch.topk(similarities, k=3).indices
|
| 44 |
topChunks = [textChunks[i] for i in topIndices]
|
| 45 |
+
topSections = [sectionlabels[i] for i in topIndices]
|
| 46 |
+
return topChunks, topSections
|
| 47 |
|
| 48 |
+
cleanedChunks, sectionLabels = preprocessText(financialText)
|
| 49 |
chunkEmbeddings = createEmbeddings(cleanedChunks)
|
| 50 |
|
| 51 |
client = Groq(api_key = os.environ.get("SF_TOKEN"))
|
|
|
|
| 62 |
messages.extend([{"role": h["role"], "content": h["content"]} for h in history])
|
| 63 |
# helping grok to retain message history
|
| 64 |
|
| 65 |
+
topResults, topSections = getTopChunks(message, chunkEmbeddings, cleanedChunks, sectionLabels)
|
|
|
|
| 66 |
context = "\n\n".join(topResults)
|
| 67 |
|
| 68 |
messages.append({"role": "system",
|
|
|
|
| 83 |
response += token
|
| 84 |
yield response
|
| 85 |
|
| 86 |
+
usedSections = list(dict.fromskeys(topSections))
|
| 87 |
+
citation = "\n\n* Sources: " + ", ".join(usedSections) + "*"
|
| 88 |
+
yield response + citation
|
| 89 |
+
|
| 90 |
chatbot = gr.ChatInterface(respond, title = "Student Formula Bot 🔬",
|
| 91 |
description = 'Welcome to the core component of \"The Student Formula\": the RAG chatbot! With the ability to act as a finance tutor, accountability buddy, and goal-setting partner all in one, it\'s designed to best suit your needs on the way to productivity and success. To get started, ask about the basic principles of creating a budget!')
|
| 92 |
|
|
|
|
| 113 |
"What is educational investment?"
|
| 114 |
]
|
| 115 |
)
|
| 116 |
+
|
|
|
|
|
|
|
| 117 |
gr.HTML(
|
| 118 |
"""
|
| 119 |
<iframe
|