Spaces:
Sleeping
Sleeping
File size: 10,151 Bytes
a84e6da eda0502 a84e6da eda0502 a84e6da eda0502 a84e6da eba1eb3 a84e6da 483c2a3 a84e6da eba1eb3 a665d60 eba1eb3 a84e6da eba1eb3 a84e6da eba1eb3 a84e6da eba1eb3 a84e6da eba1eb3 a84e6da eba1eb3 a84e6da 45353b4 a84e6da eba1eb3 a84e6da 483c2a3 a84e6da 834f95b |
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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
import gradio as gr
import random
from huggingface_hub import InferenceClient
import pandas as pd
from sentence_transformers import SentenceTransformer
import torch
# LOAD FILES
def load_files(path):
with open(path, "r", encoding = "utf-8") as f:
return f.read()
charities_text = load_files("charities.txt")
financial_advice_text = load_files("financial_advice.txt")
un_text = load_files("un.txt")
investopedia_text = load_files("investopedia.txt")
cgf_text = load_files("cgf.txt")
#
###
def preprocess_text(text):
# Strip extra whitespace from the beginning and the end of the text
cleaned_text = text.strip()
# Split the cleaned_text by every newline character (\n)
chunks = cleaned_text.split("\n")
# Create an empty list to store cleaned chunks
cleaned_chunks = []
# Write your for-in loop below to clean each chunk and add it to the cleaned_chunks list
for chunk in chunks:
stripped_chunk = chunk.strip()
if len(stripped_chunk) > 0:
cleaned_chunks.append(stripped_chunk)
# Print the length of cleaned_chunks
num_of_chunks = len(cleaned_chunks)
# print(num_of_chunks)
return cleaned_chunks
cleaned_charities = preprocess_text(charities_text)
cleaned_finance = preprocess_text(financial_advice_text)
cleaned_un = preprocess_text(un_text)
cleaned_investopedia = preprocess_text(investopedia_text)
cleaned_cgf = preprocess_text(cgf_text)
# Load the pre-trained embedding model that converts text to vectors
model = SentenceTransformer('all-MiniLM-L6-v2')
### STEP 4
def create_embeddings(text_chunks):
# Convert each text chunk into a vector embedding and store as a tensor
chunk_embeddings = model.encode(text_chunks, convert_to_tensor=True) # Replace ... with the text_chunks list
# Print the chunk embeddings
print(chunk_embeddings)
# Print the shape of chunk_embeddings
print(chunk_embeddings.shape)
# Return the chunk_embeddings
return chunk_embeddings
charity_embeddings = create_embeddings(cleaned_charities)
finance_embeddings = create_embeddings(cleaned_finance)
un_embeddings = create_embeddings(cleaned_un)
investopedia_embeddings = create_embeddings(cleaned_investopedia)
cgf_embeddings = create_embeddings(cleaned_cgf)
###STEP 5
# Define a function to find the most relevant text chunks for a given query, chunk_embeddings, and text_chunks
def get_top_chunks(query, chunk_embeddings, text_chunks):
# Convert the query text into a vector embedding
query_embedding = model.encode(query, convert_to_tensor = True) # Complete this line
# Normalize the query embedding to unit length for accurate similarity comparison
query_embedding_normalized = query_embedding / query_embedding.norm()
# Normalize all chunk embeddings to unit length for consistent comparison
chunk_embeddings_normalized = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)
# Calculate cosine similarity between query and all chunks using matrix multiplication
similarities = torch.matmul(chunk_embeddings_normalized, query_embedding_normalized) # Complete this line
# Find the indices of the 3 chunks with highest similarity scores
top_indices = torch.topk(similarities, k=3).indices
# Create an empty list to store the most relevant chunks
top_chunks = []
# Loop through the top indices and retrieve the corresponding text chunks
for i in top_indices:
relevant_info = text_chunks[i]
top_chunks.append(relevant_info)
# Return the list of most relevant chunks
return top_chunks
#CSV files
columns = ["TransactionID", "UserID", "Date", "Description", "Amount", "Type", "Extra1", "Extra2"]
spendings = pd.read_csv("september_transactions_detailed.csv", names = columns)
spendings['Amount'] = pd.to_numeric(spendings['Amount'], errors='coerce').fillna(0)
def get_advice(user_id):
user_data = spendings[spendings['UserID'] == user_id]
if user_data.empty:
return "No spending data found for this user."
# Only consider expenses
expenses = user_data[user_data['Type'].str.lower() == "expense"]
total_spent = expenses['Amount'].sum()
category_spent = expenses.groupby('Description')['Amount'].sum().to_dict()
advice = []
for cat, amt in category_spent.items():
if amt > total_spent * 0.3:
advice.append(f"You spend a lot on {cat}. Consider budgeting here.")
advice_text = " | ".join(advice) if advice else "Your spending looks balanced across categories."
summary_text = f"Total spent: ${total_spent:.2f}. Category breakdown: {category_spent}. Advice: {advice_text}"
return summary_text
#AI API being used
client= InferenceClient("openai/gpt-oss-20b")
#defining role of AI and user
information=""
def respond(message, history, chatbot_topic_values, user_id=1):
topic_chunks = []
if chatbot_topic_values and "Helping Charities" in chatbot_topic_values:
topic_chunks = get_top_chunks(message, charity_embeddings, cleaned_charities)
elif chatbot_topic_values and "Financial Aid" in chatbot_topic_values:
topic_chunks = get_top_chunks(message, finance_embeddings, cleaned_finance)
topic_chunks += get_top_chunks(message, un_embeddings, cleaned_un)
topic_chunks += get_top_chunks(message, investopedia_embeddings, cleaned_investopedia)
topic_chunks += get_top_chunks(message, cgf_embeddings, cleaned_cgf)
csv_advice = get_advice(user_id)
role_message = (
"You are a helpful and insightful chatbot who acts like a financial "
"advisor for a university student. "
"DO NOT ask the user for additional numbers or input. "
"If the user asks something irrelevant or not related to finance in any way, tell them it is not related and you can only reply to finance related things"
f"Use the following spending data from the CSV file to provide advice: {csv_advice}. "
f"Also consider this context from your knowledge base: {topic_chunks}"
)
#if chatbot_mode_values and "General Advice" in chatbot_mode_values:
# role_message = (
# "You are a helpful and insightful chatbot who acts like a financial "
# "advisor of a university student. Respond in under five bullet points, "
# f"under 500 characters, using this context: {topic_chunks}"
#)
#elif chatbot_mode_values and "Personal Advice" in chatbot_mode_values:
# role_message = (
# "You are a helpful and insightful chatbot who acts like a financial "
# "DO NOT ask the user for additional numbers or input"
# f"Use the following spending data from the CSV file to provide advice {csv_advice}"
#)
#else:
# role_message = f"You are a helpful chatbot. Use this context: {topic_chunks}"
messages = [{"role": "system", "content": role_message}]
if history:
messages.extend(history)
messages.append({"role": "user", "content": message})
response = client.chat_completion(messages, temperature=0.2)
return response['choices'][0]['message']['content'].strip()
### STEP 6
# Call the preprocess_text function and store the result in a cleaned_chunks variable
cleaned_chunks = preprocess_text(financial_advice_text) # Complete this line
top_results = get_top_chunks("What financial advice you give me?", finance_embeddings, cleaned_finance)
#Defining chatbot giving user a UI to interact, see their conversation history, and see new messages using built in gr feature
#ChatInterface requires at least one parameter(a function)
chatbot = gr.ChatInterface(respond,type="messages", title="Finance Management Hub", theme="Taithrah/Minimal")
#def save_chat_history(history, username):
# if not username:
# username = "anonymous"
# timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
# filename = f"chat_history_{username}_{timestamp}.txt"
# with open(filename, "w", encoding="utf-8") as f:
# f.write(f"Chat History for {username} - {timestamp}\n\n")
# for exchange in history:
# if isinstance(exchange, tuple) and len(exchange) == 2:
# user_msg, bot_msg = exchange
# f.write(f"User: {user_msg}\n")
# f.write(f"Bot: {bot_msg}\n\n")
# elif isinstance(exchange, dict):
# Handle dictionary format if needed
# role = exchange.get("role", "unknown")
# content = exchange.get("content", "")
# f.write(f"{role.capitalize()}: {content}\n\n")
#return filename
with gr.Blocks(
theme=gr.themes.Soft(
primary_hue="blue",
secondary_hue="fuchsia",
neutral_hue="gray",
text_size="lg",
).set(
background_fill_primary='*neutral_200',
background_fill_secondary='neutral_100',
background_fill_secondary_dark='secondary_500',
border_color_accent='*secondary_400',
border_color_accent_dark='*secondary_800',
color_accent='*secondary_600',
color_accent_soft='*secondary_200',
color_accent_soft_dark='*secondary_800',
button_primary_background_fill='*secondary_400',
button_primary_background_fill_dark='*secondary_600',
button_primary_text_color='white',
button_primary_border_color='*secondary_700',
button_primary_border_color_dark='*secondary_900'
)
) as demo:
with gr.Row(scale=1):
chatbot_topic=gr.CheckboxGroup(["Helping Charities", "Financial Aid"], label="What would you like advice about?")
gr.ChatInterface(
fn=lambda msg, hist, topic_vals: respond(msg, hist, topic_vals),
title="Finance Management Hub",
description="Ask about your personal finance",
type="messages",
additional_inputs=[chatbot_topic]
)
#launching chatbot
demo.launch(debug=True) |