Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,6 @@ import os
|
|
2 |
import pandas as pd
|
3 |
import logging
|
4 |
from datasets import load_dataset
|
5 |
-
from sentence_transformers import SentenceTransformer # Optional: if you want to compute embeddings separately
|
6 |
from langchain_huggingface import HuggingFaceEmbeddings, HuggingFacePipeline
|
7 |
from langchain_chroma import Chroma
|
8 |
from langchain_core.prompts import PromptTemplate
|
@@ -17,11 +16,10 @@ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(
|
|
17 |
# ------------------------------------------------------------------
|
18 |
# 1. Load and Prepare the Bank FAQ Dataset
|
19 |
# ------------------------------------------------------------------
|
20 |
-
|
21 |
-
# Load the dataset from Hugging Face (your bank FAQs dataset)
|
22 |
ds = load_dataset("maxpro291/bankfaqs_dataset")
|
23 |
train_ds = ds['train']
|
24 |
-
data = train_ds[:] #
|
25 |
|
26 |
# Separate questions and answers from the 'text' field
|
27 |
questions = []
|
@@ -32,11 +30,10 @@ for entry in data['text']:
|
|
32 |
elif entry.startswith("A:"):
|
33 |
answers.append(entry)
|
34 |
|
35 |
-
# Create a DataFrame with
|
36 |
Bank_Data = pd.DataFrame({'question': questions, 'answer': answers})
|
37 |
|
38 |
-
# Build context strings
|
39 |
-
# These will be stored in the vector store.
|
40 |
context_data = []
|
41 |
for i in range(len(Bank_Data)):
|
42 |
context = f"Question: {Bank_Data.iloc[i]['question']} Answer: {Bank_Data.iloc[i]['answer']}"
|
@@ -45,32 +42,32 @@ for i in range(len(Bank_Data)):
|
|
45 |
# ------------------------------------------------------------------
|
46 |
# 2. Create the Vector Store for Retrieval
|
47 |
# ------------------------------------------------------------------
|
48 |
-
|
49 |
-
# Initialize the embedding model using LangChain's HuggingFaceEmbeddings
|
50 |
embed_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
51 |
|
52 |
-
# Create a Chroma vector store from the context data
|
53 |
vectorstore = Chroma.from_texts(
|
54 |
texts=context_data,
|
55 |
embedding=embed_model,
|
56 |
-
persist_directory="./chroma_db_bank"
|
57 |
)
|
58 |
|
59 |
-
# Create a retriever from the vector store
|
60 |
retriever = vectorstore.as_retriever()
|
61 |
|
62 |
# ------------------------------------------------------------------
|
63 |
# 3. Initialize the LLM for Generation
|
64 |
# ------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
65 |
|
66 |
-
#
|
67 |
-
model_name = "meta-llama/Llama-2-7b-chat-hf" # Change if you want a different model
|
68 |
-
|
69 |
-
# Load the tokenizer and model.
|
70 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
71 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
72 |
|
73 |
-
# Create a
|
74 |
pipe = pipeline(
|
75 |
"text-generation",
|
76 |
model=model,
|
@@ -81,14 +78,13 @@ pipe = pipeline(
|
|
81 |
repetition_penalty=1.15
|
82 |
)
|
83 |
|
84 |
-
# Wrap the pipeline in LangChain's HuggingFacePipeline
|
85 |
huggingface_model = HuggingFacePipeline(pipeline=pipe)
|
86 |
|
87 |
# ------------------------------------------------------------------
|
88 |
# 4. Build the Retrieval-Augmented Generation (RAG) Chain
|
89 |
# ------------------------------------------------------------------
|
90 |
-
|
91 |
-
# Define a prompt template that instructs the model to use provided context.
|
92 |
template = (
|
93 |
"You are a helpful banking assistant. "
|
94 |
"Use the provided context if it is relevant to answer the question. "
|
@@ -98,11 +94,7 @@ template = (
|
|
98 |
)
|
99 |
rag_prompt = PromptTemplate.from_template(template)
|
100 |
|
101 |
-
# Build the RAG chain by piping
|
102 |
-
# (a) the retriever providing context,
|
103 |
-
# (b) the prompt template formatting the question and context,
|
104 |
-
# (c) the LLM generating the answer,
|
105 |
-
# (d) and finally parsing the output.
|
106 |
rag_chain = (
|
107 |
{"context": retriever, "question": RunnablePassthrough()}
|
108 |
| rag_prompt
|
@@ -113,15 +105,14 @@ rag_chain = (
|
|
113 |
# ------------------------------------------------------------------
|
114 |
# 5. Set Up the Gradio Chat Interface
|
115 |
# ------------------------------------------------------------------
|
116 |
-
|
117 |
def rag_memory_stream(message, history):
|
118 |
partial_text = ""
|
119 |
-
#
|
120 |
for new_text in rag_chain.stream(message):
|
121 |
partial_text += new_text
|
122 |
yield partial_text
|
123 |
|
124 |
-
#
|
125 |
examples = [
|
126 |
"I want to open an account",
|
127 |
"What is a savings account?",
|
@@ -135,7 +126,7 @@ description = (
|
|
135 |
"Ask me anything, and I’ll do my best to assist you."
|
136 |
)
|
137 |
|
138 |
-
# Create
|
139 |
demo = gr.ChatInterface(
|
140 |
fn=rag_memory_stream,
|
141 |
title=title,
|
@@ -147,6 +138,5 @@ demo = gr.ChatInterface(
|
|
147 |
# ------------------------------------------------------------------
|
148 |
# 6. Launch the App
|
149 |
# ------------------------------------------------------------------
|
150 |
-
|
151 |
if __name__ == "__main__":
|
152 |
demo.launch(share=True)
|
|
|
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
|
|
|
16 |
# ------------------------------------------------------------------
|
17 |
# 1. Load and Prepare the Bank FAQ Dataset
|
18 |
# ------------------------------------------------------------------
|
19 |
+
# Load the dataset from Hugging Face (Bank FAQs)
|
|
|
20 |
ds = load_dataset("maxpro291/bankfaqs_dataset")
|
21 |
train_ds = ds['train']
|
22 |
+
data = train_ds[:] # load all examples
|
23 |
|
24 |
# Separate questions and answers from the 'text' field
|
25 |
questions = []
|
|
|
30 |
elif entry.startswith("A:"):
|
31 |
answers.append(entry)
|
32 |
|
33 |
+
# Create a DataFrame with questions and answers
|
34 |
Bank_Data = pd.DataFrame({'question': questions, 'answer': answers})
|
35 |
|
36 |
+
# Build context strings (combining question and answer) for the vector store
|
|
|
37 |
context_data = []
|
38 |
for i in range(len(Bank_Data)):
|
39 |
context = f"Question: {Bank_Data.iloc[i]['question']} Answer: {Bank_Data.iloc[i]['answer']}"
|
|
|
42 |
# ------------------------------------------------------------------
|
43 |
# 2. Create the Vector Store for Retrieval
|
44 |
# ------------------------------------------------------------------
|
45 |
+
# Initialize the embedding model
|
|
|
46 |
embed_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
47 |
|
48 |
+
# Create a Chroma vector store from the context data
|
49 |
vectorstore = Chroma.from_texts(
|
50 |
texts=context_data,
|
51 |
embedding=embed_model,
|
52 |
+
persist_directory="./chroma_db_bank"
|
53 |
)
|
54 |
|
55 |
+
# Create a retriever from the vector store
|
56 |
retriever = vectorstore.as_retriever()
|
57 |
|
58 |
# ------------------------------------------------------------------
|
59 |
# 3. Initialize the LLM for Generation
|
60 |
# ------------------------------------------------------------------
|
61 |
+
# Note:
|
62 |
+
# The model "meta-llama/Llama-2-7b-chat-hf" is gated. If you have access,
|
63 |
+
# authenticate using `huggingface-cli login`. Otherwise, switch to a public model.
|
64 |
+
model_name = "gpt2" # Replace with "meta-llama/Llama-2-7b-chat-hf" if you are authenticated.
|
65 |
|
66 |
+
# Load tokenizer and model
|
|
|
|
|
|
|
67 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
68 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
69 |
|
70 |
+
# Create a text-generation pipeline
|
71 |
pipe = pipeline(
|
72 |
"text-generation",
|
73 |
model=model,
|
|
|
78 |
repetition_penalty=1.15
|
79 |
)
|
80 |
|
81 |
+
# Wrap the pipeline in LangChain's HuggingFacePipeline
|
82 |
huggingface_model = HuggingFacePipeline(pipeline=pipe)
|
83 |
|
84 |
# ------------------------------------------------------------------
|
85 |
# 4. Build the Retrieval-Augmented Generation (RAG) Chain
|
86 |
# ------------------------------------------------------------------
|
87 |
+
# Define a prompt template that instructs the assistant to use provided context
|
|
|
88 |
template = (
|
89 |
"You are a helpful banking assistant. "
|
90 |
"Use the provided context if it is relevant to answer the question. "
|
|
|
94 |
)
|
95 |
rag_prompt = PromptTemplate.from_template(template)
|
96 |
|
97 |
+
# Build the RAG chain by piping the retriever, prompt, LLM, and an output parser
|
|
|
|
|
|
|
|
|
98 |
rag_chain = (
|
99 |
{"context": retriever, "question": RunnablePassthrough()}
|
100 |
| rag_prompt
|
|
|
105 |
# ------------------------------------------------------------------
|
106 |
# 5. Set Up the Gradio Chat Interface
|
107 |
# ------------------------------------------------------------------
|
|
|
108 |
def rag_memory_stream(message, history):
|
109 |
partial_text = ""
|
110 |
+
# Stream the generated answer
|
111 |
for new_text in rag_chain.stream(message):
|
112 |
partial_text += new_text
|
113 |
yield partial_text
|
114 |
|
115 |
+
# Example questions
|
116 |
examples = [
|
117 |
"I want to open an account",
|
118 |
"What is a savings account?",
|
|
|
126 |
"Ask me anything, and I’ll do my best to assist you."
|
127 |
)
|
128 |
|
129 |
+
# Create a chat interface using Gradio
|
130 |
demo = gr.ChatInterface(
|
131 |
fn=rag_memory_stream,
|
132 |
title=title,
|
|
|
138 |
# ------------------------------------------------------------------
|
139 |
# 6. Launch the App
|
140 |
# ------------------------------------------------------------------
|
|
|
141 |
if __name__ == "__main__":
|
142 |
demo.launch(share=True)
|