AliceRolan commited on
Commit
6d43375
Β·
verified Β·
1 Parent(s): 1053045
Files changed (1) hide show
  1. app.py +138 -40
app.py CHANGED
@@ -1,5 +1,20 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
  """
5
  For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
@@ -7,58 +22,141 @@ For more information on `huggingface_hub` Inference API support, please check th
7
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
9
 
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
 
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
 
26
- messages.append({"role": "user", "content": message})
27
 
28
- response = ""
29
 
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
 
 
 
 
 
 
 
42
 
 
 
43
  """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
  ],
 
 
60
  )
61
 
62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  if __name__ == "__main__":
64
  demo.launch()
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
+ import os
4
+ import torch
5
+ import transformers
6
+ from tensorflow import keras
7
+ from transformers import AutoTokenizer, pipeline, AutoModelForSeq2SeqLM,AutoModelForCausalLM
8
+ from langchain.document_loaders import PyPDFLoader
9
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
10
+ from langchain.embeddings import HuggingFaceEmbeddings
11
+ from langchain.vectorstores import Chroma
12
+ from langchain.prompts import ChatPromptTemplate
13
+ from langchain.schema.runnable import RunnablePassthrough
14
+ from langchain.schema.output_parser import StrOutputParser
15
+ from langchain.llms import HuggingFacePipeline
16
+ import gradio as gr
17
+
18
 
19
  """
20
  For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
 
22
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
23
 
24
 
25
+ print("GPU Available:", torch.cuda.is_available())
26
+ print("GPU Name:", torch.cuda.get_device_name(0) if torch.cuda.is_available() else "No GPU Found")
 
 
 
 
 
 
 
27
 
28
+ pdf_files = ["Apple-10K-2023.pdf", "Apple-10K-2024.pdf"]
 
 
 
 
29
 
30
+ """### πŸ“Œ Step 1: Load Multiple 10-K Financial Report PDFs """
31
 
32
+ all_documents = []
33
 
34
+ def preprocess_text(text):
35
+ text = text.replace("\n", " ").strip()
36
+ text = ' '.join(text.split()) # Remove extra spaces
37
+ return text
 
 
 
 
 
 
 
38
 
39
+ for pdf_path in pdf_files:
40
+ loader = PyPDFLoader(pdf_path)
41
+ documents = loader.load()
42
+ for doc in documents:
43
+ doc.page_content = preprocess_text(doc.page_content)
44
+ all_documents.extend(documents)
45
 
46
+ """### πŸ“Œ Step 2: Split Text into Chunks
47
+ <p> Here each split will also have a metadata defining the location of the chunk in the actual document for citation,also other details.As the pdf text is clean with no html tags etc , we use it as such with no cleaning
48
  """
49
+
50
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=20)
51
+ all_splits = text_splitter.split_documents(all_documents)
52
+
53
+ """### πŸ“Œ Step 3: Create Embeddings using Sentence Transformers"""
54
+
55
+ # Check if CUDA (GPU) is available; otherwise, use CPU
56
+ device = "cuda" if torch.cuda.is_available() else "cpu"
57
+ model_name = "sentence-transformers/all-MiniLM-L6-v2"
58
+ embeddings = HuggingFaceEmbeddings(model_name=model_name, model_kwargs={"device": device})
59
+
60
+ """### πŸ“Œ Step 4: Store & Retrieve using ChromaDB"""
61
+
62
+ vectordb = Chroma.from_documents(documents=all_splits, embedding=embeddings, persist_directory="content/drive/MyDrive/RAG_DB/chroma_db")
63
+ retriever = vectordb.as_retriever()
64
+
65
+ # Choose a smaller T5 model
66
+ model_name = "google/flan-t5-large"
67
+
68
+ # Load model & tokenizer
69
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
70
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name, device_map="auto")
71
+
72
+ # Create Hugging Face pipeline
73
+ hf_pipeline = pipeline( "text2text-generation",
74
+ model=model,
75
+ tokenizer=tokenizer,
76
+ truncation=True)
77
+
78
+ # Integrate with LangChain
79
+ llm = HuggingFacePipeline(pipeline=hf_pipeline)
80
+
81
+ """### πŸ“Œ Step 6: Define RAG Prompt"""
82
+
83
+ # Define RAG Prompt
84
+ template = """You are an AI assistant answering financial questions using retrieved financial reports.
85
+ Use the following retrieved context to answer the question concisely.
86
+ Question: {question}
87
+ Context: {context}
88
+ Answer:
89
  """
90
+ prompt = ChatPromptTemplate.from_template(template)
91
+
92
+ """### πŸ“Œ Step 7: Create RAG pipeline"""
93
+
94
+ # Create RAG Pipeline
95
+ conversation_chain = (
96
+ {"context": retriever, "question": RunnablePassthrough()}
97
+ | prompt
98
+ | llm
99
+ | StrOutputParser()
100
+ )
101
+
102
+ """### πŸ“Œ Step 8: Create a function to get the confidence score"""
103
+
104
+ # Function to Get Confidence Score
105
+ def get_confidence_score(question):
106
+ retrieved_docs_with_scores = vectordb.similarity_search_with_score(question, k=5)
107
+ max_score = max([doc[1] for doc in retrieved_docs_with_scores]) if retrieved_docs_with_scores else 0
108
+ print(max_score)
109
+ return min(1.0, round(max_score, 2)) # Normalize to 0-1 scale
110
+
111
+
112
+ """### πŸ“Œ Step 10: Integrate with Gradio UI"""
113
+
114
+ # Define Chatbot Function
115
+ def chat_with_rag(message, history):
116
+ try:
117
+ response = conversation_chain.invoke(message)
118
+ confidence_score = get_confidence_score(message)
119
+ formatted_response = f"**Answer:** {response}\n\n**Confidence Score:** {confidence_score:.2f}"
120
+ return formatted_response
121
+ except Exception as e:
122
+ return f"Error: {str(e)}"
123
+
124
+ # Create Gradio Chatbot UI with Auto-Clearing Input
125
  demo = gr.ChatInterface(
126
+ fn=chat_with_rag, # Function to generate responses
127
+ title="πŸ“Š Financial RAG Chatbot",
128
+ description="Ask questions about financial reports and get AI-powered answers!",
129
+ theme="soft", # Aesthetic theme
130
+ examples=[
131
+ ["What was Apple's total revenue in 2024?"],
132
+ ["What are the biggest financial risks for Apple?"]
133
+ ["What are the biggest challenges for Apple?"],
134
+ ["What is the capital of France?"],
 
 
 
135
  ],
136
+ submit_btn="Ask", # Customize the submit button text
137
+ stop_btn=None, # Remove unnecessary stop button
138
  )
139
 
140
 
141
+ """
142
+ For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
143
+ """
144
+ # demo = gr.ChatInterface(
145
+ # respond,
146
+ # additional_inputs=[
147
+ # gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
148
+ # gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
149
+ # gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
150
+ # gr.Slider(
151
+ # minimum=0.1,
152
+ # maximum=1.0,
153
+ # value=0.95,
154
+ # step=0.05,
155
+ # label="Top-p (nucleus sampling)",
156
+ # ),
157
+ # ],
158
+ # )
159
+
160
+
161
  if __name__ == "__main__":
162
  demo.launch()