Update app.py
Browse files
app.py
CHANGED
|
@@ -1,79 +1,138 @@
|
|
|
|
|
| 1 |
import pandas as pd
|
|
|
|
| 2 |
from datasets import load_dataset
|
| 3 |
-
from
|
| 4 |
-
from
|
|
|
|
|
|
|
|
|
|
| 5 |
import gradio as gr
|
| 6 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 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 |
demo = gr.ChatInterface(
|
| 68 |
-
fn=
|
| 69 |
-
title=
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
"How do I apply for a loan?"
|
| 74 |
-
],
|
| 75 |
-
theme="soft"
|
| 76 |
)
|
| 77 |
|
|
|
|
|
|
|
|
|
|
| 78 |
if __name__ == "__main__":
|
| 79 |
-
demo.launch(
|
|
|
|
| 1 |
+
import os
|
| 2 |
import pandas as pd
|
| 3 |
+
import logging
|
| 4 |
from datasets import load_dataset
|
| 5 |
+
from langchain_huggingface import HuggingFaceEmbeddings, HuggingFacePipeline
|
| 6 |
+
from langchain_chroma import Chroma
|
| 7 |
+
from langchain_core.prompts import PromptTemplate
|
| 8 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 9 |
+
from langchain_core.runnables import RunnablePassthrough
|
| 10 |
import gradio as gr
|
| 11 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline, BitsAndBytesConfig
|
| 12 |
+
|
| 13 |
+
# Set up logging
|
| 14 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 15 |
+
|
| 16 |
+
# ------------------------------------------------------------------
|
| 17 |
+
# 1. Load and Prepare the Bank FAQ Dataset (UNCHANGED)
|
| 18 |
+
# ------------------------------------------------------------------
|
| 19 |
+
ds = load_dataset("maxpro291/bankfaqs_dataset")
|
| 20 |
+
train_ds = ds['train']
|
| 21 |
+
data = train_ds[:] # load all examples
|
| 22 |
+
|
| 23 |
+
questions = []
|
| 24 |
+
answers = []
|
| 25 |
+
for entry in data['text']:
|
| 26 |
+
if entry.startswith("Q:"):
|
| 27 |
+
questions.append(entry)
|
| 28 |
+
elif entry.startswith("A:"):
|
| 29 |
+
answers.append(entry)
|
| 30 |
+
|
| 31 |
+
Bank_Data = pd.DataFrame({'question': questions, 'answer': answers})
|
| 32 |
+
|
| 33 |
+
context_data = []
|
| 34 |
+
for i in range(len(Bank_Data)):
|
| 35 |
+
context = f"Question: {Bank_Data.iloc[i]['question']} Answer: {Bank_Data.iloc[i]['answer']}"
|
| 36 |
+
context_data.append(context)
|
| 37 |
+
|
| 38 |
+
# ------------------------------------------------------------------
|
| 39 |
+
# 2. Create the Vector Store for Retrieval (UNCHANGED)
|
| 40 |
+
# ------------------------------------------------------------------
|
| 41 |
+
embed_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 42 |
+
|
| 43 |
+
vectorstore = Chroma.from_texts(
|
| 44 |
+
texts=context_data,
|
| 45 |
+
embedding=embed_model,
|
| 46 |
+
persist_directory="./chroma_db_bank"
|
| 47 |
+
)
|
| 48 |
+
retriever = vectorstore.as_retriever()
|
| 49 |
+
|
| 50 |
+
# ------------------------------------------------------------------
|
| 51 |
+
# 3. Initialize PHI-2 Model (MODIFIED SECTION)
|
| 52 |
+
# ------------------------------------------------------------------
|
| 53 |
+
model_name = "microsoft/phi-2"
|
| 54 |
+
|
| 55 |
+
# Configure 4-bit quantization for efficient loading
|
| 56 |
+
quantization_config = BitsAndBytesConfig(
|
| 57 |
+
load_in_4bit=True,
|
| 58 |
+
bnb_4bit_compute_dtype="float16",
|
| 59 |
+
bnb_4bit_quant_type="nf4"
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 63 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 64 |
+
model_name,
|
| 65 |
+
device_map="auto",
|
| 66 |
+
trust_remote_code=True,
|
| 67 |
+
quantization_config=quantization_config
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
# Create text-generation pipeline with Phi-2 specific settings
|
| 71 |
+
pipe = pipeline(
|
| 72 |
+
"text-generation",
|
| 73 |
+
model=model,
|
| 74 |
+
tokenizer=tokenizer,
|
| 75 |
+
max_new_tokens=512,
|
| 76 |
+
temperature=0.7,
|
| 77 |
+
top_p=0.95,
|
| 78 |
+
repetition_penalty=1.15,
|
| 79 |
+
do_sample=True
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
# Wrap the pipeline in LangChain's HuggingFacePipeline
|
| 83 |
+
huggingface_model = HuggingFacePipeline(pipeline=pipe)
|
| 84 |
+
|
| 85 |
+
# ------------------------------------------------------------------
|
| 86 |
+
# 4. Build the RAG Chain (UNCHANGED)
|
| 87 |
+
# ------------------------------------------------------------------
|
| 88 |
+
template = (
|
| 89 |
+
"You are a helpful banking assistant. "
|
| 90 |
+
"Use the provided context if it is relevant to answer the question. "
|
| 91 |
+
"If not, answer using your general banking knowledge.\n"
|
| 92 |
+
"Question: {question}\n"
|
| 93 |
+
"Answer:"
|
| 94 |
+
)
|
| 95 |
+
rag_prompt = PromptTemplate.from_template(template)
|
| 96 |
+
|
| 97 |
+
rag_chain = (
|
| 98 |
+
{"context": retriever, "question": RunnablePassthrough()}
|
| 99 |
+
| rag_prompt
|
| 100 |
+
| huggingface_model
|
| 101 |
+
| StrOutputParser()
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
# ------------------------------------------------------------------
|
| 105 |
+
# 5. Gradio Chat Interface (UNCHANGED)
|
| 106 |
+
# ------------------------------------------------------------------
|
| 107 |
+
def rag_memory_stream(message, history):
|
| 108 |
+
partial_text = ""
|
| 109 |
+
for new_text in rag_chain.stream(message):
|
| 110 |
+
partial_text += new_text
|
| 111 |
+
yield partial_text
|
| 112 |
+
|
| 113 |
+
examples = [
|
| 114 |
+
"I want to open an account",
|
| 115 |
+
"What is a savings account?",
|
| 116 |
+
"How do I use an ATM?",
|
| 117 |
+
"How can I resolve a bank account issue?"
|
| 118 |
+
]
|
| 119 |
+
|
| 120 |
+
title = "Your Personal Banking Assistant 💬"
|
| 121 |
+
description = (
|
| 122 |
+
"Welcome! I'm here to answer your questions about banking and related topics. "
|
| 123 |
+
"Ask me anything, and I'll do my best to assist you."
|
| 124 |
+
)
|
| 125 |
|
| 126 |
demo = gr.ChatInterface(
|
| 127 |
+
fn=rag_memory_stream,
|
| 128 |
+
title=title,
|
| 129 |
+
description=description,
|
| 130 |
+
examples=examples,
|
| 131 |
+
theme="glass",
|
|
|
|
|
|
|
|
|
|
| 132 |
)
|
| 133 |
|
| 134 |
+
# ------------------------------------------------------------------
|
| 135 |
+
# 6. Launch the App (UNCHANGED)
|
| 136 |
+
# ------------------------------------------------------------------
|
| 137 |
if __name__ == "__main__":
|
| 138 |
+
demo.launch(share=True)
|