NSamson1 commited on
Commit
ca80a11
·
verified ·
1 Parent(s): 25cb203

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +130 -71
app.py CHANGED
@@ -1,79 +1,138 @@
 
1
  import pandas as pd
 
2
  from datasets import load_dataset
3
- from langchain.embeddings import HuggingFaceEmbeddings
4
- from langchain.vectorstores import Chroma
 
 
 
5
  import gradio as gr
6
- from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
7
-
8
- # 1. Load and prepare dataset
9
- def load_bank_data():
10
- ds = load_dataset("maxpro291/bankfaqs_dataset")
11
- data = ds['train'][:]
12
- return pd.DataFrame({
13
- 'question': [entry for entry in data['text'] if entry.startswith("Q:")],
14
- 'answer': [entry for entry in data['text'] if entry.startswith("A:")]
15
- })
16
-
17
- # 2. Setup vector store
18
- def setup_retriever(data):
19
- embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
20
- vectorstore = Chroma.from_texts(
21
- texts=[f"Q: {q}\nA: {a}" for q, a in zip(data['question'], data['answer'])],
22
- embedding=embeddings,
23
- persist_directory="./chroma_db_bank"
24
- )
25
- return vectorstore.as_retriever()
26
-
27
- # 3. Load LLM
28
- def load_llm():
29
- model_name = "microsoft/phi-2"
30
- tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
31
- model = AutoModelForCausalLM.from_pretrained(
32
- model_name,
33
- device_map="auto",
34
- torch_dtype="auto",
35
- trust_remote_code=True
36
- )
37
- return pipeline(
38
- "text-generation",
39
- model=model,
40
- tokenizer=tokenizer,
41
- max_new_tokens=150,
42
- temperature=0.7
43
- )
44
-
45
- # 4. Response generation
46
- def generate_response(question, retriever, llm):
47
- docs = retriever.get_relevant_documents(question)
48
- context = "\n".join([doc.page_content for doc in docs][:2]) if docs else ""
49
-
50
- prompt = f"""Instruct: You're a banking expert. {
51
- f"Context: {context}" if context else ""
52
- }\nQuestion: {question}\nAnswer: """
53
-
54
- response = llm(prompt)[0]['generated_text']
55
- return response.split("Answer: ")[-1].strip()
56
-
57
- # 5. Initialize components
58
- bank_data = load_bank_data()
59
- retriever = setup_retriever(bank_data)
60
- llm = load_llm()
61
-
62
- # 6. Gradio interface
63
- def chat_interface(question, history):
64
- response = generate_response(question, retriever, llm)
65
- return response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
  demo = gr.ChatInterface(
68
- fn=chat_interface,
69
- title="Banking Assistant 🏦",
70
- examples=[
71
- "How do I open a savings account?",
72
- "What's the difference between debit and credit cards?",
73
- "How do I apply for a loan?"
74
- ],
75
- theme="soft"
76
  )
77
 
 
 
 
78
  if __name__ == "__main__":
79
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
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)