#Import libraries import numpy as np import pandas as pd import transformers from transformers import GPT2LMHeadModel, GPT2Tokenizer import torch import re import gradio as gr #Load model device = "cuda" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu" model = torch.load('finance_chatbot_gpt2_complete_model.pt', map_location=torch.device('cpu')) model = model.to(device) #Get LLM tokenizer tokenizer = GPT2Tokenizer.from_pretrained("gpt2") tokenizer.add_special_tokens({"pad_token": "", "bos_token": "", "eos_token": ""}) tokenizer.add_tokens([":"]) #Inference function def infer_1(inp, chat_history): inp_1 = ""+inp+":" inp_tok = tokenizer(inp_1, return_tensors="pt") X = inp_tok["input_ids"].to(device) a = inp_tok["attention_mask"].to(device) output = model.generate(X, attention_mask=a) output = tokenizer.decode(output[0]) #Remove the user input part seq output = output[len(inp_1):] #Remove next line char output = re.sub(r'\n', '', output) #Remove | tokens from the end output = re.sub(r'|', '', output) #Append to chat history chat_history.append((inp, output)) return '', chat_history #Ancillary variables project_heading = '

Personal Finance ChatBot

' project_des = """## Project Description This Chatbot is based on a fine-tuned version of 'GPT2-Small'. Primarily, the text from Robert Kiyosaki's book, "Rich Dad Poor Dad," was processed and used in training. **Note:** This project is built for educational purposes, and it is important to note that the responses generated by the model should not be interpreted literally. """ sample_questions = ["Where should I invest in to protect my assets?", "What is the result of people working all their lives for someone else?", "Why do you want to earn more passive income?", "What inspires winners?", "What do most people fail to realize in life?", "Why do people struggle financially?", "What is the name of your developer?", "What do poor people do with their eggs?"] data_processing_story = """

Data Collection and Processing

Step 1: Extracted raw text data from two broad sources

1.1. Digital copy of 'Rich Dad Poor Dad' by Parsing the book, Link: Rich Dad Poor Dad Book

1.2. Scraping websites with summary or quotes from the book such as Link: Goodreads Quotes

Step 2: Data Cleaning and Preprocessing

2.1. Removed irregularities from the text data, e.g., additional comments, quotes, special characters, spaces, indentations, etc.

2.2. Created batches of text with 512 words each, ascertaining appropriate text boundaries for the next stage of the pipeline

Step 3: Extracted data in the form of question-answer to create a conversational dataset using open source models

3.1. Used FAQ module from QUESTgen.AI's API, which produced question and one-word answer pairs from the given text, Repo Link: QUESTgen.AI Repository

3.2. Resulting answers didn't carry the complete context and were incomplete, thus arising the need for better and short answers

3.3. Used the model 'roberta-base-squad2' by Deepset for extracting meaningful answers, Model Card Link: roberta-base-squad2 Model Card

Processed Dataset:

""" model_details_HTML = """

Model Details

Base Model Name: GPT-2 (Small)

Training Process: Fine-tuned using GPT-2 with 23 epochs, 64 batch size, and 100 input tokens

Source Code: GitHub Repository

Base Model Card: GPT-2 Model Card

Inference Time: Average response time is approximately 13 seconds

""" training_data = pd.read_csv('ques_ans_v5.csv', index_col = 0) #Launch with gradio with gr.Blocks() as demo: gr.HTML(project_heading) gr.Markdown(value = project_des) chatbot = gr.Chatbot(label = "Trained on scripts from \"Rich Dad Poor Dad\"") #value = [['Hey there User', 'Hey there CB']] msg = gr.Textbox(label = 'Press enter to submit!') clear = gr.ClearButton([msg, chatbot]) gr.Examples(sample_questions, msg) msg.submit(infer_1, [msg, chatbot], [msg, chatbot]) gr.HTML(data_processing_story) gr.DataFrame(training_data[['Question', 'answer_cb']].rename(columns = {'Question':'Question', 'answer_cb':'Answer'}), wrap = True) gr.HTML(model_details_HTML) demo.launch(share=True, debug=True)