Spaces:
Sleeping
Sleeping
removing huggingface_hub + major bug fixes
Browse files
app.py
CHANGED
|
@@ -3,7 +3,6 @@ import datetime
|
|
| 3 |
import gradio as gr
|
| 4 |
from sentence_transformers import SentenceTransformer
|
| 5 |
import torch
|
| 6 |
-
from huggingface_hub import InferenceClient
|
| 7 |
from groq import Groq
|
| 8 |
|
| 9 |
financialText = ""
|
|
@@ -22,6 +21,7 @@ def preprocessText(text):
|
|
| 22 |
cleanedText = text.strip().split("\n")
|
| 23 |
cleanedChunks = []
|
| 24 |
sectionLabels = []
|
|
|
|
| 25 |
for chunk in cleanedText:
|
| 26 |
chunk = chunk.strip()
|
| 27 |
if not chunk:
|
|
@@ -35,6 +35,7 @@ def preprocessText(text):
|
|
| 35 |
|
| 36 |
def createEmbeddings(textChunks):
|
| 37 |
chunkEmbeddings = model.encode(cleanedChunks, convert_to_tensor = True)
|
|
|
|
| 38 |
|
| 39 |
def getTopChunks(query, chunkEmbeddings, textChunks, sectionLabels):
|
| 40 |
queryEmbedding = model.encode(query, convert_to_tensor = True)
|
|
@@ -43,7 +44,7 @@ def getTopChunks(query, chunkEmbeddings, textChunks, sectionLabels):
|
|
| 43 |
similarities = torch.matmul(chunkEmbeddingsNormalized, queryEmbeddingNormalized)
|
| 44 |
topIndices = torch.topk(similarities, k=3).indices
|
| 45 |
topChunks = [textChunks[i] for i in topIndices]
|
| 46 |
-
topSections = [
|
| 47 |
return topChunks, topSections
|
| 48 |
|
| 49 |
cleanedChunks, sectionLabels = preprocessText(financialText)
|
|
@@ -84,7 +85,7 @@ def respond(message, history):
|
|
| 84 |
response += token
|
| 85 |
yield response
|
| 86 |
|
| 87 |
-
usedSections = list(dict.
|
| 88 |
citation = "\n\n* Sources: " + ", ".join(usedSections) + "*"
|
| 89 |
yield response + citation
|
| 90 |
|
|
@@ -144,7 +145,7 @@ def goalTracker(goal, savings, targetDate):
|
|
| 144 |
return "Please enter the date in YYYY-MM-DD format! (ex: 2026-06-12)"
|
| 145 |
|
| 146 |
today = datetime.date.today()
|
| 147 |
-
daysLeft = (
|
| 148 |
|
| 149 |
if daysLeft <= 0:
|
| 150 |
return "Try to set your goal for sometime in the future!"
|
|
@@ -159,7 +160,7 @@ def goalTracker(goal, savings, targetDate):
|
|
| 159 |
return (
|
| 160 |
f"### Goal Tracker Results\n\n"
|
| 161 |
f"- **Amount Remaining:** ${remaining:,.2f}\n"
|
| 162 |
-
f"- **Days Remaining:**
|
| 163 |
f"- **Save this much per week:** ${weeklyAmount:,.2f}\n"
|
| 164 |
f"- **Save this much per month:** ${monthlyAmount:,.2f}\n"
|
| 165 |
)
|
|
@@ -183,8 +184,8 @@ with gr.Blocks(css=custom_css) as demo:
|
|
| 183 |
with gr.Tab("Budget Calculator"):
|
| 184 |
gr.Markdown("## 50/30/20 Budget Calculator")
|
| 185 |
gr.Markdown("Want to see what your monthly budget might look like when following financial literacy principles? Enter your monthly income to see the suggested breakdown!")
|
| 186 |
-
incomeInput = gr.Number(label = "Monthly Income ($)
|
| 187 |
-
budgetOutput = gr.Markdown(label = "Monthly Budget")
|
| 188 |
budgetButton = gr.Button("Calculate")
|
| 189 |
budgetButton.click(fn = budgetCalculator, inputs = incomeInput, outputs = budgetOutput)
|
| 190 |
|
|
@@ -193,7 +194,7 @@ with gr.Blocks(css=custom_css) as demo:
|
|
| 193 |
gr.Markdown("Saving up to buy something special? Want to figure out how long it'll take to reach that goal? Enter your goal amount, current savings, and target date to see the suggested timeline!")
|
| 194 |
goalInput = gr.Number(label = "Savings Goal ($)", minimum = 0),
|
| 195 |
savingsInput = gr.Number(label = "Current Savings ($)", minimum = 0),
|
| 196 |
-
targetInput = gr.Textbox(label = "Target Date (YYYY-MM-DD", placeholder = "2026-12-31")
|
| 197 |
goalOutput = gr.Markdown(label = "Your Savings Plan")
|
| 198 |
goalButton = gr.Button("Calculate")
|
| 199 |
goalButton.click(fn = goalTracker, inputs = [goalInput, savingsInput, targetInput], outputs = goalOutput)
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
from sentence_transformers import SentenceTransformer
|
| 5 |
import torch
|
|
|
|
| 6 |
from groq import Groq
|
| 7 |
|
| 8 |
financialText = ""
|
|
|
|
| 21 |
cleanedText = text.strip().split("\n")
|
| 22 |
cleanedChunks = []
|
| 23 |
sectionLabels = []
|
| 24 |
+
currentLabel = "Knowledge Base"
|
| 25 |
for chunk in cleanedText:
|
| 26 |
chunk = chunk.strip()
|
| 27 |
if not chunk:
|
|
|
|
| 35 |
|
| 36 |
def createEmbeddings(textChunks):
|
| 37 |
chunkEmbeddings = model.encode(cleanedChunks, convert_to_tensor = True)
|
| 38 |
+
return chunkEmbeddings
|
| 39 |
|
| 40 |
def getTopChunks(query, chunkEmbeddings, textChunks, sectionLabels):
|
| 41 |
queryEmbedding = model.encode(query, convert_to_tensor = True)
|
|
|
|
| 44 |
similarities = torch.matmul(chunkEmbeddingsNormalized, queryEmbeddingNormalized)
|
| 45 |
topIndices = torch.topk(similarities, k=3).indices
|
| 46 |
topChunks = [textChunks[i] for i in topIndices]
|
| 47 |
+
topSections = [sectionLabels[i] for i in topIndices]
|
| 48 |
return topChunks, topSections
|
| 49 |
|
| 50 |
cleanedChunks, sectionLabels = preprocessText(financialText)
|
|
|
|
| 85 |
response += token
|
| 86 |
yield response
|
| 87 |
|
| 88 |
+
usedSections = list(dict.fromkeys(topSections))
|
| 89 |
citation = "\n\n* Sources: " + ", ".join(usedSections) + "*"
|
| 90 |
yield response + citation
|
| 91 |
|
|
|
|
| 145 |
return "Please enter the date in YYYY-MM-DD format! (ex: 2026-06-12)"
|
| 146 |
|
| 147 |
today = datetime.date.today()
|
| 148 |
+
daysLeft = (target - today).days
|
| 149 |
|
| 150 |
if daysLeft <= 0:
|
| 151 |
return "Try to set your goal for sometime in the future!"
|
|
|
|
| 160 |
return (
|
| 161 |
f"### Goal Tracker Results\n\n"
|
| 162 |
f"- **Amount Remaining:** ${remaining:,.2f}\n"
|
| 163 |
+
f"- **Days Remaining:** {daysLeft}\n"
|
| 164 |
f"- **Save this much per week:** ${weeklyAmount:,.2f}\n"
|
| 165 |
f"- **Save this much per month:** ${monthlyAmount:,.2f}\n"
|
| 166 |
)
|
|
|
|
| 184 |
with gr.Tab("Budget Calculator"):
|
| 185 |
gr.Markdown("## 50/30/20 Budget Calculator")
|
| 186 |
gr.Markdown("Want to see what your monthly budget might look like when following financial literacy principles? Enter your monthly income to see the suggested breakdown!")
|
| 187 |
+
incomeInput = gr.Number(label = "Monthly Income ($)", minimum = 0)
|
| 188 |
+
budgetOutput = gr.Markdown(label = "Monthly Budget")
|
| 189 |
budgetButton = gr.Button("Calculate")
|
| 190 |
budgetButton.click(fn = budgetCalculator, inputs = incomeInput, outputs = budgetOutput)
|
| 191 |
|
|
|
|
| 194 |
gr.Markdown("Saving up to buy something special? Want to figure out how long it'll take to reach that goal? Enter your goal amount, current savings, and target date to see the suggested timeline!")
|
| 195 |
goalInput = gr.Number(label = "Savings Goal ($)", minimum = 0),
|
| 196 |
savingsInput = gr.Number(label = "Current Savings ($)", minimum = 0),
|
| 197 |
+
targetInput = gr.Textbox(label = "Target Date (YYYY-MM-DD)", placeholder = "2026-12-31")
|
| 198 |
goalOutput = gr.Markdown(label = "Your Savings Plan")
|
| 199 |
goalButton = gr.Button("Calculate")
|
| 200 |
goalButton.click(fn = goalTracker, inputs = [goalInput, savingsInput, targetInput], outputs = goalOutput)
|