SatyamD31 commited on
Commit
e2b8671
·
1 Parent(s): a9df818

added assignment files

Browse files
Nestle_Financtial_report_till2023.xlsx ADDED
Binary file (11.9 kB). View file
 
app.py ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import torch
3
+ from rag import FinancialChatbot
4
+ import warnings
5
+ from transformers import logging
6
+
7
+ # Suppress warnings
8
+ warnings.filterwarnings("ignore")
9
+ logging.set_verbosity_error()
10
+
11
+ torch.classes.__path__ = []
12
+
13
+ # Use session state to persist the chatbot instance
14
+ if "chatbot" not in st.session_state:
15
+ st.session_state.chatbot = FinancialChatbot(data_path="Nestle_Financtial_report_till2023.xlsx")
16
+
17
+ def fetch_answer_from_backend(query):
18
+ """Calls the backend function to get an answer."""
19
+ return st.session_state.chatbot.get_answer(query) # Use session state chatbot instance
20
+
21
+
22
+
23
+ # Initialize Session State for Chat History
24
+ if "chat_history" not in st.session_state:
25
+ st.session_state.chat_history = []
26
+
27
+ if "loading" not in st.session_state:
28
+ st.session_state.loading = False
29
+
30
+ # Layout and Title
31
+ st.title("Financial RAG Chat Assistant")
32
+
33
+
34
+ # st.markdown(
35
+ # """
36
+ # <style>
37
+ # body, .stApp {
38
+ # background: url('https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQlSQ-kkQFs_BAOhel_cd_CkFoHN1V-5bUO7vv8NwRp0cjftW6Wm2utQSE&s=10');
39
+ # background-size: cover;
40
+ # # background-attachment: fixed;
41
+ # # background-position: center;
42
+ # # background-repeat: no-repeat;
43
+ # }
44
+ # # .stApp {
45
+ # # background: transparent;
46
+ # # }
47
+ # # .stTextInput, .stTextArea, .stButton {
48
+ # # background-color: rgba(0, 0, 0, 0.5);
49
+ # # border-radius: 10px;
50
+ # # color: #ffffff;
51
+ # # }
52
+ # # .stTextInput > div > input {
53
+ # # color: #ffffff;
54
+ # # }
55
+ # # .stButton button {
56
+ # # color: #ffffff;
57
+ # # background-color: #4CAF50;
58
+ # # border: none;
59
+ # # border-radius: 10px;
60
+ # # }
61
+ # </style>
62
+ # """,
63
+ # unsafe_allow_html=True
64
+ # )
65
+
66
+
67
+
68
+
69
+
70
+ # Display Chat History
71
+ for chat in st.session_state.chat_history:
72
+ # User's query on the right
73
+ st.markdown(
74
+ f"""
75
+ <div style='text-align: right; max-width: 75%; float: right; clear: both;'>
76
+ <div style='font-size: 13px; color: #4a4a4a; margin: 0 10px;'>You</div>
77
+ <div style='background-color: #d3d3d3; color: black; padding: 10px; border-radius: 10px;
78
+ box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.1);'>
79
+ {chat['question']}
80
+ </div>
81
+ </div>
82
+ """,
83
+ unsafe_allow_html=True
84
+ )
85
+
86
+ # Assistant's label and response on the left
87
+ if chat["answer"] is not None:
88
+ st.markdown(
89
+ f"""
90
+ <div style='text-align: left; max-width: 75%; float: left; clear: both;'>
91
+ <div style='font-size: 13px; color: #4a4a4a; margin: 0 10px;'>Assistant</div>
92
+ <div style='background-color: #add8e6; color: black; padding: 10px; border-radius: 10px;
93
+ box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.1);'>
94
+ {chat['answer']}
95
+ </div>
96
+ </div>
97
+ """,
98
+ unsafe_allow_html=True
99
+ )
100
+
101
+ # Confidence Score (Below the answer)
102
+ if chat["confidence"] is not None:
103
+ st.markdown(
104
+ f"<div style='clear: both; color: #4a4a4a; font-size: 13px;'>Confidence: {chat['confidence'] * 100}%</div>",
105
+ unsafe_allow_html=True
106
+ )
107
+ st.divider() # Adds a visual divider between Q&A pairs
108
+
109
+ # User Input (Always at the Bottom)
110
+ user_input = st.chat_input("Ask a financial question...")
111
+
112
+ # If user inputs a question
113
+ if user_input:
114
+ # Add question to chat history and show loading animation
115
+ st.session_state.chat_history.append({
116
+ "question": user_input,
117
+ "answer": None, # Placeholder for the answer
118
+ "confidence": None # Placeholder for the confidence score
119
+ })
120
+ st.session_state.loading = True
121
+ st.rerun() # Refresh to display the question immediately
122
+
123
+ # If loading, simulate the API call and update the last question's answer
124
+ if st.session_state.loading:
125
+ with st.spinner("Fetching answer..."):
126
+ # Get the last question
127
+ last_question = st.session_state.chat_history[-1]["question"]
128
+
129
+ # API Call
130
+ answer, confidence = fetch_answer_from_backend(last_question)
131
+
132
+ # Update the last chat history item with the answer and confidence
133
+ st.session_state.chat_history[-1]["answer"] = answer
134
+ st.session_state.chat_history[-1]["confidence"] = confidence
135
+
136
+ # Stop loading and refresh to show the answer
137
+ st.session_state.loading = False
138
+ st.rerun()
financial_faiss.index ADDED
Binary file (384 kB). View file
 
index_map.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:887ad3476658fdd9c5b5ddac6c642b3682beb5b1b67c262026fad69dbc64ddcd
3
+ size 13547
index_map.txt ADDED
@@ -0,0 +1,250 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Net Sales/Income from operations - year Dec '23 is: 19126.3
2
+ Net Sales/Income from operations - year Dec '22 is: 16787.43
3
+ Net Sales/Income from operations - year Dec '21 is: 14633.72
4
+ Net Sales/Income from operations - year Dec '20 is: 13290.16
5
+ Net Sales/Income from operations - year Dec '19 is: 12295.27
6
+ Other Operating Income - year Dec '23 is: --
7
+ Other Operating Income - year Dec '22 is: 77.63
8
+ Other Operating Income - year Dec '21 is: 75.69
9
+ Other Operating Income - year Dec '20 is: 59.87
10
+ Other Operating Income - year Dec '19 is: 73.63
11
+ Total Income From Operations - year Dec '23 is: 19126.3
12
+ Total Income From Operations - year Dec '22 is: 16865.06
13
+ Total Income From Operations - year Dec '21 is: 14709.41
14
+ Total Income From Operations - year Dec '20 is: 13350.03
15
+ Total Income From Operations - year Dec '19 is: 12368.9
16
+ EXPENDITURE - year Dec '23 is: nan
17
+ EXPENDITURE - year Dec '22 is: nan
18
+ EXPENDITURE - year Dec '21 is: nan
19
+ EXPENDITURE - year Dec '20 is: nan
20
+ EXPENDITURE - year Dec '19 is: nan
21
+ Consumption of Raw Materials - year Dec '23 is: 8054.95
22
+ Consumption of Raw Materials - year Dec '22 is: 7652.11
23
+ Consumption of Raw Materials - year Dec '21 is: 6154.1
24
+ Consumption of Raw Materials - year Dec '20 is: 5554.24
25
+ Consumption of Raw Materials - year Dec '19 is: 5150.3
26
+ Purchase of Traded Goods - year Dec '23 is: 417.77
27
+ Purchase of Traded Goods - year Dec '22 is: 317.13
28
+ Purchase of Traded Goods - year Dec '21 is: 227.52
29
+ Purchase of Traded Goods - year Dec '20 is: 189
30
+ Purchase of Traded Goods - year Dec '19 is: 217.81
31
+ Increase/Decrease in Stocks - year Dec '23 is: -40.01
32
+ Increase/Decrease in Stocks - year Dec '22 is: -245.79
33
+ Increase/Decrease in Stocks - year Dec '21 is: -62.7
34
+ Increase/Decrease in Stocks - year Dec '20 is: -69.33
35
+ Increase/Decrease in Stocks - year Dec '19 is: -144.19
36
+ Power & Fuel - year Dec '23 is: --
37
+ Power & Fuel - year Dec '22 is: --
38
+ Power & Fuel - year Dec '21 is: --
39
+ Power & Fuel - year Dec '20 is: --
40
+ Power & Fuel - year Dec '19 is: --
41
+ Employees Cost - year Dec '23 is: 1849.18
42
+ Employees Cost - year Dec '22 is: 1629.87
43
+ Employees Cost - year Dec '21 is: 1521.3
44
+ Employees Cost - year Dec '20 is: 1500.95
45
+ Employees Cost - year Dec '19 is: 1262.95
46
+ Depreciation - year Dec '23 is: 428.91
47
+ Depreciation - year Dec '22 is: 402.6
48
+ Depreciation - year Dec '21 is: 390.19
49
+ Depreciation - year Dec '20 is: 370.38
50
+ Depreciation - year Dec '19 is: 316.36
51
+ Excise Duty - year Dec '23 is: --
52
+ Excise Duty - year Dec '22 is: --
53
+ Excise Duty - year Dec '21 is: --
54
+ Excise Duty - year Dec '20 is: --
55
+ Excise Duty - year Dec '19 is: --
56
+ Admin. And Selling Expenses - year Dec '23 is: --
57
+ Admin. And Selling Expenses - year Dec '22 is: --
58
+ Admin. And Selling Expenses - year Dec '21 is: --
59
+ Admin. And Selling Expenses - year Dec '20 is: --
60
+ Admin. And Selling Expenses - year Dec '19 is: --
61
+ R & D Expenses - year Dec '23 is: --
62
+ R & D Expenses - year Dec '22 is: --
63
+ R & D Expenses - year Dec '21 is: --
64
+ R & D Expenses - year Dec '20 is: --
65
+ R & D Expenses - year Dec '19 is: --
66
+ Provisions And Contingencies - year Dec '23 is: --
67
+ Provisions And Contingencies - year Dec '22 is: 9.85
68
+ Provisions And Contingencies - year Dec '21 is: -25.17
69
+ Provisions And Contingencies - year Dec '20 is: 13.97
70
+ Provisions And Contingencies - year Dec '19 is: 24.88
71
+ Exp. Capitalised - year Dec '23 is: --
72
+ Exp. Capitalised - year Dec '22 is: --
73
+ Exp. Capitalised - year Dec '21 is: --
74
+ Exp. Capitalised - year Dec '20 is: --
75
+ Exp. Capitalised - year Dec '19 is: --
76
+ Other Expenses - year Dec '23 is: 4373.42
77
+ Other Expenses - year Dec '22 is: 3774.06
78
+ Other Expenses - year Dec '21 is: 3302.82
79
+ Other Expenses - year Dec '20 is: 2959.7
80
+ Other Expenses - year Dec '19 is: 2992.85
81
+ P/L Before Other Inc. , Int., Excpt. Items & Tax - year Dec '23 is: 4042.08
82
+ P/L Before Other Inc. , Int., Excpt. Items & Tax - year Dec '22 is: 3325.23
83
+ P/L Before Other Inc. , Int., Excpt. Items & Tax - year Dec '21 is: 3201.35
84
+ P/L Before Other Inc. , Int., Excpt. Items & Tax - year Dec '20 is: 2831.12
85
+ P/L Before Other Inc. , Int., Excpt. Items & Tax - year Dec '19 is: 2547.94
86
+ Other Income - year Dec '23 is: 121.21
87
+ Other Income - year Dec '22 is: 101
88
+ Other Income - year Dec '21 is: 120.11
89
+ Other Income - year Dec '20 is: 145.85
90
+ Other Income - year Dec '19 is: 246.88
91
+ P/L Before Int., Excpt. Items & Tax - year Dec '23 is: 4163.29
92
+ P/L Before Int., Excpt. Items & Tax - year Dec '22 is: 3426.23
93
+ P/L Before Int., Excpt. Items & Tax - year Dec '21 is: 3321.46
94
+ P/L Before Int., Excpt. Items & Tax - year Dec '20 is: 2976.97
95
+ P/L Before Int., Excpt. Items & Tax - year Dec '19 is: 2794.82
96
+ Interest - year Dec '23 is: 119.29
97
+ Interest - year Dec '22 is: 154.33
98
+ Interest - year Dec '21 is: 201.19
99
+ Interest - year Dec '20 is: 164.18
100
+ Interest - year Dec '19 is: 119.83
101
+ P/L Before Exceptional Items & Tax - year Dec '23 is: 4044
102
+ P/L Before Exceptional Items & Tax - year Dec '22 is: 3271.9
103
+ P/L Before Exceptional Items & Tax - year Dec '21 is: 3120.27
104
+ P/L Before Exceptional Items & Tax - year Dec '20 is: 2812.79
105
+ P/L Before Exceptional Items & Tax - year Dec '19 is: 2674.99
106
+ Exceptional Items - year Dec '23 is: -5.71
107
+ Exceptional Items - year Dec '22 is: --
108
+ Exceptional Items - year Dec '21 is: -236.5
109
+ Exceptional Items - year Dec '20 is: --
110
+ Exceptional Items - year Dec '19 is: --
111
+ P/L Before Tax - year Dec '23 is: 4038.29
112
+ P/L Before Tax - year Dec '22 is: 3271.9
113
+ P/L Before Tax - year Dec '21 is: 2883.77
114
+ P/L Before Tax - year Dec '20 is: 2812.79
115
+ P/L Before Tax - year Dec '19 is: 2674.99
116
+ Tax - year Dec '23 is: 1039.62
117
+ Tax - year Dec '22 is: 865.45
118
+ Tax - year Dec '21 is: 738.91
119
+ Tax - year Dec '20 is: 730.36
120
+ Tax - year Dec '19 is: 705.44
121
+ P/L After Tax from Ordinary Activities - year Dec '23 is: 2998.67
122
+ P/L After Tax from Ordinary Activities - year Dec '22 is: 2406.45
123
+ P/L After Tax from Ordinary Activities - year Dec '21 is: 2144.86
124
+ P/L After Tax from Ordinary Activities - year Dec '20 is: 2082.43
125
+ P/L After Tax from Ordinary Activities - year Dec '19 is: 1969.55
126
+ Prior Year Adjustments - year Dec '23 is: --
127
+ Prior Year Adjustments - year Dec '22 is: --
128
+ Prior Year Adjustments - year Dec '21 is: --
129
+ Prior Year Adjustments - year Dec '20 is: --
130
+ Prior Year Adjustments - year Dec '19 is: --
131
+ Extra Ordinary Items - year Dec '23 is: --
132
+ Extra Ordinary Items - year Dec '22 is: --
133
+ Extra Ordinary Items - year Dec '21 is: --
134
+ Extra Ordinary Items - year Dec '20 is: --
135
+ Extra Ordinary Items - year Dec '19 is: --
136
+ Net Profit/(Loss) For the Period - year Dec '23 is: 2998.67
137
+ Net Profit/(Loss) For the Period - year Dec '22 is: 2406.45
138
+ Net Profit/(Loss) For the Period - year Dec '21 is: 2144.86
139
+ Net Profit/(Loss) For the Period - year Dec '20 is: 2082.43
140
+ Net Profit/(Loss) For the Period - year Dec '19 is: 1969.55
141
+ Equity Share Capital - year Dec '23 is: 96.42
142
+ Equity Share Capital - year Dec '22 is: 96.42
143
+ Equity Share Capital - year Dec '21 is: 96.42
144
+ Equity Share Capital - year Dec '20 is: 96.42
145
+ Equity Share Capital - year Dec '19 is: 96.42
146
+ Reserves Excluding Revaluation Reserves - year Dec '23 is: --
147
+ Reserves Excluding Revaluation Reserves - year Dec '22 is: 2362.75
148
+ Reserves Excluding Revaluation Reserves - year Dec '21 is: 1988.06
149
+ Reserves Excluding Revaluation Reserves - year Dec '20 is: 1922.92
150
+ Reserves Excluding Revaluation Reserves - year Dec '19 is: 1822.45
151
+ Equity Dividend Rate (%) - year Dec '23 is: --
152
+ Equity Dividend Rate (%) - year Dec '22 is: 2200
153
+ Equity Dividend Rate (%) - year Dec '21 is: 2000
154
+ Equity Dividend Rate (%) - year Dec '20 is: 2000
155
+ Equity Dividend Rate (%) - year Dec '19 is: 3420
156
+ EPS Before Extra Ordinary - year Dec '23 is: nan
157
+ EPS Before Extra Ordinary - year Dec '22 is: nan
158
+ EPS Before Extra Ordinary - year Dec '21 is: nan
159
+ EPS Before Extra Ordinary - year Dec '20 is: nan
160
+ EPS Before Extra Ordinary - year Dec '19 is: nan
161
+ Basic EPS - year Dec '23 is: 31.1
162
+ Basic EPS - year Dec '22 is: 249.58
163
+ Basic EPS - year Dec '21 is: 222.46
164
+ Basic EPS - year Dec '20 is: 215.98
165
+ Basic EPS - year Dec '19 is: 204.28
166
+ Diluted EPS - year Dec '23 is: 31.1
167
+ Diluted EPS - year Dec '22 is: --
168
+ Diluted EPS - year Dec '21 is: 222.46
169
+ Diluted EPS - year Dec '20 is: 215.98
170
+ Diluted EPS - year Dec '19 is: 204.28
171
+ EPS After Extra Ordinary - year Dec '23 is: nan
172
+ EPS After Extra Ordinary - year Dec '22 is: nan
173
+ EPS After Extra Ordinary - year Dec '21 is: nan
174
+ EPS After Extra Ordinary - year Dec '20 is: nan
175
+ EPS After Extra Ordinary - year Dec '19 is: nan
176
+ Basic EPS - year Dec '23 is: 31.1
177
+ Basic EPS - year Dec '22 is: 249.58
178
+ Basic EPS - year Dec '21 is: 222.46
179
+ Basic EPS - year Dec '20 is: 215.98
180
+ Basic EPS - year Dec '19 is: 204.28
181
+ Diluted EPS - year Dec '23 is: 31.1
182
+ Diluted EPS - year Dec '22 is: --
183
+ Diluted EPS - year Dec '21 is: 222.46
184
+ Diluted EPS - year Dec '20 is: 215.98
185
+ Diluted EPS - year Dec '19 is: 204.28
186
+ Public Share Holding - year Dec '23 is: nan
187
+ Public Share Holding - year Dec '22 is: nan
188
+ Public Share Holding - year Dec '21 is: nan
189
+ Public Share Holding - year Dec '20 is: nan
190
+ Public Share Holding - year Dec '19 is: nan
191
+ No Of Shares (Crores) - year Dec '23 is: --
192
+ No Of Shares (Crores) - year Dec '22 is: --
193
+ No Of Shares (Crores) - year Dec '21 is: --
194
+ No Of Shares (Crores) - year Dec '20 is: --
195
+ No Of Shares (Crores) - year Dec '19 is: --
196
+ Share Holding (%) - year Dec '23 is: --
197
+ Share Holding (%) - year Dec '22 is: --
198
+ Share Holding (%) - year Dec '21 is: --
199
+ Share Holding (%) - year Dec '20 is: --
200
+ Share Holding (%) - year Dec '19 is: --
201
+ Promoters and Promoter Group Shareholding - year Dec '23 is: nan
202
+ Promoters and Promoter Group Shareholding - year Dec '22 is: nan
203
+ Promoters and Promoter Group Shareholding - year Dec '21 is: nan
204
+ Promoters and Promoter Group Shareholding - year Dec '20 is: nan
205
+ Promoters and Promoter Group Shareholding - year Dec '19 is: nan
206
+ a) Pledged/Encumbered - year Dec '23 is: nan
207
+ a) Pledged/Encumbered - year Dec '22 is: nan
208
+ a) Pledged/Encumbered - year Dec '21 is: nan
209
+ a) Pledged/Encumbered - year Dec '20 is: nan
210
+ a) Pledged/Encumbered - year Dec '19 is: nan
211
+ - Number of shares (Crores) - year Dec '23 is: --
212
+ - Number of shares (Crores) - year Dec '22 is: --
213
+ - Number of shares (Crores) - year Dec '21 is: --
214
+ - Number of shares (Crores) - year Dec '20 is: --
215
+ - Number of shares (Crores) - year Dec '19 is: --
216
+ - Per. of shares (as a % of the total sh. of prom. and promoter group) - year Dec '23 is: --
217
+ - Per. of shares (as a % of the total sh. of prom. and promoter group) - year Dec '22 is: --
218
+ - Per. of shares (as a % of the total sh. of prom. and promoter group) - year Dec '21 is: --
219
+ - Per. of shares (as a % of the total sh. of prom. and promoter group) - year Dec '20 is: --
220
+ - Per. of shares (as a % of the total sh. of prom. and promoter group) - year Dec '19 is: --
221
+ - Per. of shares (as a % of the total Share Cap. of the company) - year Dec '23 is: --
222
+ - Per. of shares (as a % of the total Share Cap. of the company) - year Dec '22 is: --
223
+ - Per. of shares (as a % of the total Share Cap. of the company) - year Dec '21 is: --
224
+ - Per. of shares (as a % of the total Share Cap. of the company) - year Dec '20 is: --
225
+ - Per. of shares (as a % of the total Share Cap. of the company) - year Dec '19 is: --
226
+ b) Non-encumbered - year Dec '23 is: nan
227
+ b) Non-encumbered - year Dec '22 is: nan
228
+ b) Non-encumbered - year Dec '21 is: nan
229
+ b) Non-encumbered - year Dec '20 is: nan
230
+ b) Non-encumbered - year Dec '19 is: nan
231
+ - Number of shares (Crores) - year Dec '23 is: --
232
+ - Number of shares (Crores) - year Dec '22 is: --
233
+ - Number of shares (Crores) - year Dec '21 is: --
234
+ - Number of shares (Crores) - year Dec '20 is: --
235
+ - Number of shares (Crores) - year Dec '19 is: --
236
+ - Per. of shares (as a % of the total sh. of prom. and promoter group) - year Dec '23 is: --
237
+ - Per. of shares (as a % of the total sh. of prom. and promoter group) - year Dec '22 is: --
238
+ - Per. of shares (as a % of the total sh. of prom. and promoter group) - year Dec '21 is: --
239
+ - Per. of shares (as a % of the total sh. of prom. and promoter group) - year Dec '20 is: --
240
+ - Per. of shares (as a % of the total sh. of prom. and promoter group) - year Dec '19 is: --
241
+ - Per. of shares (as a % of the total Share Cap. of the company) - year Dec '23 is: --
242
+ - Per. of shares (as a % of the total Share Cap. of the company) - year Dec '22 is: --
243
+ - Per. of shares (as a % of the total Share Cap. of the company) - year Dec '21 is: --
244
+ - Per. of shares (as a % of the total Share Cap. of the company) - year Dec '20 is: --
245
+ - Per. of shares (as a % of the total Share Cap. of the company) - year Dec '19 is: --
246
+ Notes - year Dec '23 is: |202312
247
+ Notes - year Dec '22 is: |202212
248
+ Notes - year Dec '21 is: |202112
249
+ Notes - year Dec '20 is: |202012
250
+ Notes - year Dec '19 is: |201912
rag.py ADDED
@@ -0,0 +1,147 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import pandas as pd
3
+ import faiss
4
+ import numpy as np
5
+ import re
6
+ import os
7
+ from sentence_transformers import SentenceTransformer
8
+ from transformers import AutoModelForCausalLM, AutoTokenizer
9
+
10
+ class FinancialChatbot:
11
+ def __init__(self, data_path, model_name="all-MiniLM-L6-v2", qwen_model_name="Qwen/Qwen2-0.5B-Instruct"):
12
+ self.device = "cpu"
13
+ self.data_path = data_path # Store data path
14
+
15
+ # Load SBERT for embeddings
16
+ self.sbert_model = SentenceTransformer(model_name, device=self.device)
17
+ self.sbert_model = self.sbert_model.half()
18
+
19
+ # Load Qwen model for text generation
20
+ self.qwen_model = AutoModelForCausalLM.from_pretrained(
21
+ qwen_model_name, torch_dtype=torch.float16, trust_remote_code=True
22
+ ).to(self.device)
23
+
24
+ self.qwen_tokenizer = AutoTokenizer.from_pretrained(qwen_model_name, trust_remote_code=True)
25
+
26
+ # Load or create FAISS index
27
+ self.load_or_create_index()
28
+
29
+ import os # Import os for file checks
30
+
31
+ def load_or_create_index(self):
32
+ """Loads FAISS index and index_map if they exist, otherwise creates new ones."""
33
+ if os.path.exists("financial_faiss.index") and os.path.exists("index_map.txt"):
34
+ try:
35
+ self.faiss_index = faiss.read_index("financial_faiss.index")
36
+ with open("index_map.txt", "r", encoding="utf-8") as f:
37
+ self.index_map = {i: line.strip() for i, line in enumerate(f)}
38
+ print("FAISS index and index_map loaded successfully.")
39
+ except Exception as e:
40
+ print(f"Error loading FAISS index: {e}. Recreating index...")
41
+ self.create_faiss_index()
42
+ else:
43
+ print("FAISS index or index_map not found. Creating a new one...")
44
+ self.create_faiss_index()
45
+
46
+
47
+ def create_faiss_index(self):
48
+ """Creates a FAISS index from the provided Excel file."""
49
+ df = pd.read_excel(self.data_path)
50
+ sentences = []
51
+ self.index_map = {} # Initialize index_map
52
+
53
+ for row_idx, row in df.iterrows():
54
+ for col in df.columns[1:]: # Ignore the first column (assumed to be labels)
55
+ sentence = f"{row[df.columns[0]]} - year {col} is: {row[col]}"
56
+ sentences.append(sentence)
57
+ self.index_map[len(self.index_map)] = sentence # Store mapping
58
+
59
+ # Encode the sentences into embeddings
60
+ embeddings = self.sbert_model.encode(sentences, convert_to_numpy=True)
61
+
62
+ # Create FAISS index (FlatL2 for simplicity)
63
+ self.faiss_index = faiss.IndexFlatL2(embeddings.shape[1])
64
+ self.faiss_index.add(embeddings)
65
+
66
+ # Save index and index map
67
+ faiss.write_index(self.faiss_index, "financial_faiss.index")
68
+ with open("index_map.txt", "w", encoding="utf-8") as f:
69
+ for sentence in self.index_map.values():
70
+ f.write(sentence + "\n")
71
+
72
+ def query_faiss(self, query, top_k=3):
73
+ """Retrieves the top_k closest sentences from FAISS index."""
74
+ query_embedding = self.sbert_model.encode([query], convert_to_numpy=True)
75
+ distances, indices = self.faiss_index.search(query_embedding, top_k)
76
+
77
+ results = [self.index_map[idx] for idx in indices[0] if idx in self.index_map]
78
+ confidences = [1 - (dist / (np.max(distances[0]) or 1)) for dist in distances[0]]
79
+
80
+ return results, confidences
81
+
82
+ def moderate_query(self, query):
83
+ """Blocks inappropriate queries containing restricted words."""
84
+ BLOCKED_WORDS = re.compile(r"\b(hack|bypass|illegal|exploit|scam|kill|laundering|murder|suicide|self-harm)\b", re.IGNORECASE)
85
+ return not bool(BLOCKED_WORDS.search(query))
86
+
87
+ def generate_answer(self, context, question):
88
+ messages = [
89
+ {"role": "system", "content": "You are a financial assistant. Answer only finance-related questions. If the question is not related to finance, reply: 'I'm sorry, but I can only answer financial-related questions.' If the user greets you (e.g., 'Hello', 'Hi', 'Good morning'), respond politely with 'Hello! How can I assist you today?'."},
90
+ {"role": "user", "content": f"{question} - related contect extracted form db {context}"}
91
+ ]
92
+
93
+ # Use Qwen's chat template
94
+ input_text = self.qwen_tokenizer.apply_chat_template(
95
+ messages, tokenize=False, add_generation_prompt=True
96
+ )
97
+
98
+ # Tokenize and move input to device
99
+ inputs = self.qwen_tokenizer([input_text], return_tensors="pt").to(self.device)
100
+ self.qwen_model.config.pad_token_id = self.qwen_tokenizer.eos_token_id
101
+
102
+ # Generate response
103
+ outputs = self.qwen_model.generate(
104
+ inputs.input_ids,
105
+ max_new_tokens=50,
106
+ pad_token_id=self.qwen_tokenizer.eos_token_id,
107
+ )
108
+
109
+ # Extract only the newly generated part
110
+ generated_ids = outputs[:, inputs.input_ids.shape[1]:] # Remove prompt part
111
+ response = self.qwen_tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
112
+
113
+ return response
114
+
115
+
116
+
117
+ def get_answer(self, query):
118
+ """Main function to process a user query and return an answer."""
119
+
120
+ # Check if query is appropriate
121
+ if not self.moderate_query(query):
122
+ return "Inappropriate request.", 0.0
123
+
124
+ # Retrieve relevant documents and their confidence scores
125
+ retrieved_docs, confidences = self.query_faiss(query)
126
+ if not retrieved_docs:
127
+ return "No relevant information found.", 0.0
128
+
129
+ # Combine retrieved documents as context
130
+ context = " ".join(retrieved_docs)
131
+ avg_confidence = round(sum(confidences) / len(confidences), 2)
132
+
133
+ # Generate model response
134
+ model_response = self.generate_answer(context, query)
135
+
136
+ # Extract only the relevant part of the response
137
+ model_response = model_response.strip()
138
+
139
+ # Ensure only the actual answer is returned
140
+ if model_response.lower() in ["i don't know", "no relevant information found"]:
141
+ return "I don't know.", avg_confidence
142
+ #print(avg_confidence)
143
+ if avg_confidence == 0.0:
144
+ return "Not relevant ", avg_confidence
145
+
146
+
147
+ return model_response, avg_confidence
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ faiss-cpu
2
+ sentence-transformers
3
+ accelerate>=0.26.0
4
+ openpyxl