Rehman1603 commited on
Commit
78a4c42
1 Parent(s): 7c65eeb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -98
app.py CHANGED
@@ -4,12 +4,6 @@ import pandas as pd
4
  from langchain.chat_models import ChatOpenAI
5
  from langchain.document_loaders import CSVLoader
6
  from langchain_together import TogetherEmbeddings
7
- from langchain.prompts import ChatPromptTemplate
8
- from langchain.vectorstores import Chroma
9
- from langchain_core.output_parsers import StrOutputParser
10
- from langchain_core.runnables import RunnableLambda, RunnablePassthrough
11
- from langchain.document_loaders import CSVLoader
12
- from langchain.embeddings.sentence_transformer import SentenceTransformerEmbeddings
13
  from langchain.vectorstores import Chroma
14
  from langchain_core.vectorstores import InMemoryVectorStore
15
  from langchain import PromptTemplate
@@ -17,15 +11,14 @@ from langchain import LLMChain
17
  from langchain_together import Together
18
  import os
19
 
20
-
21
  os.environ['TOGETHER_API_KEY'] = "c2f52626b97118b71c0c36f66eda4f5957c8fc475e760c3d72f98ba07d3ed3b5"
22
 
23
  # Initialize global variable for vectorstore
24
  vectorstore = None
25
  embeddings = TogetherEmbeddings(model="togethercomputer/m2-bert-80M-8k-retrieval")
26
  llama3 = Together(model="meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo", max_tokens=1024)
 
27
  def update_csv_files():
28
- # Define the login URL and credentials
29
  login_url = "https://livesystem.hisabkarlay.com/auth/login"
30
  payload = {
31
  "username": "user@123",
@@ -34,26 +27,19 @@ def update_csv_files():
34
  "client_id": "5",
35
  "grant_type": "password"
36
  }
37
-
38
- # Send a POST request to the login URL
39
  response = requests.post(login_url, data=payload)
40
 
41
- # Check the status and get the response data
42
  if response.status_code == 200:
43
- print("Login successful!")
44
  access_token = response.json()['access_token']
45
  else:
46
  return f"Failed to log in: {response.status_code}"
47
 
48
- # Profit loss Fetch report
49
  report_url = "https://livesystem.hisabkarlay.com/connector/api/profit-loss-report"
50
- headers = {
51
- "Authorization": f"Bearer {access_token}"
52
- }
53
  response = requests.get(report_url, headers=headers)
54
  profit_loss_data = response.json()['data']
55
  keys = list(profit_loss_data.keys())
56
- del keys[23] # Adjust according to your needs
57
  del keys[20]
58
  del keys[19]
59
  data_dict = {}
@@ -62,7 +48,6 @@ def update_csv_files():
62
  df = pd.DataFrame(data_dict, index=[0])
63
  df.to_csv('profit_loss.csv', index=False)
64
 
65
- # API call to get purchase-sell data
66
  report_url = "https://livesystem.hisabkarlay.com/connector/api/purchase-sell"
67
  response = requests.get(report_url, headers=headers)
68
  sell_purchase_data = response.json()
@@ -70,7 +55,6 @@ def update_csv_files():
70
  df = pd.json_normalize(sell_purchase_data)
71
  df.to_csv('purchase_sell_report.csv', index=False)
72
 
73
- # API call to get trending product data
74
  report_url = "https://livesystem.hisabkarlay.com/connector/api/trending-products"
75
  response = requests.get(report_url, headers=headers)
76
  trending_product_data = response.json()['data']
@@ -82,23 +66,15 @@ def update_csv_files():
82
 
83
  def initialize_embedding():
84
  global vectorstore
85
- # Initialize the embedding function
86
-
87
 
88
- # Load CSV files
89
- file_paths = [
90
- "profit_loss.csv",
91
- "purchase_sell_report.csv",
92
- "trending_product.csv"
93
- ]
94
  documents = []
95
  for path in file_paths:
96
  loader = CSVLoader(path, encoding="windows-1252")
97
- documents.extend(loader.load()) # Combine documents from all files
98
 
99
- # Create an InMemoryVectorStore from the combined documents
100
  vectorstore = InMemoryVectorStore.from_texts(
101
- [doc.page_content for doc in documents], # Extract the page_content from Document objects
102
  embedding=embeddings,
103
  )
104
  return "Embeddings initialized successfully!"
@@ -109,65 +85,16 @@ def qa_chain(query):
109
 
110
  retriever = vectorstore.as_retriever()
111
  retrieved_documents = retriever.invoke(query)
112
- return retrieved_documents # Not shown directly in the UI
113
 
114
- def generate_response(query):
115
  if vectorstore is None:
116
- return "Please initialize the embeddings first."
117
-
118
- retrieved_documents = qa_chain(query) # Call qa_chain internally
119
  chat_template = """
120
  You are a highly intelligent and professional AI assistant.
121
  Generate the response according to the user's query:
122
- - If the user enters a greeting (e.g., "Hi", "Hello", "Good day"), give the following response:
123
- "Welcome to HisabKarLay, your business partner! You may choose from the following services 👇:
124
- 1. Reports
125
- 2. Forecasts
126
- 3. Best Selling Items
127
- 4. Chat with AI Agent
128
- 5. Chat with our Customer Care Team
129
- 6. Share your Feedback
130
- 7. Checkout Latest Offers
131
- 🔆 Suggestion: To make a selection, send the relevant number like 1
132
- ⭕ Note: If at any stage you wish to go back to the previous menu, type back, and to go to the main menu, type main menu.
133
- ⭕ Note: If you want to change the language, type and send 'change language.'
134
- 💁🏻♂️ Help: If you need any help, you can call us at +923269498569."
135
- - If the user enters a specific number (1-7), give the following responses:
136
- - If the user enters only 1, give the following response:
137
- If you are interested in insights related to your business, please find the available reports below:
138
- -> Profit Loss Report: Detailed analysis of your financial performance.
139
- -> Stock Report: Overview of your current inventory status.
140
- -> Sales Report: Summary of sales activities.
141
- -> Purchase Report: Insights into procurement activities.
142
- -> Trending Item Report: Highlights of popular products in demand.
143
- - If the user enters only 2, give the following response:
144
- For strategic planning and inventory management, consider the following forecasts:
145
- -> Sales Forecast: Projected sales for upcoming periods.
146
- -> Product Sales Forecast: Expected sales performance of specific products.
147
- - If the user enters only 3, give the following response:
148
- -> You have expressed interest in identifying the best-selling item. Please allow me to provide you with detailed insights.
149
- - If the user enters only 4, give the following response:
150
- -> Feel free to ask any questions regarding the status of your business. I’m here to assist you.
151
- - If the user enters only 5, give the following response:
152
- For inquiries or further assistance, please send your query to:
153
- -> Contact Number: +923269498569
154
- - If the user enters only 6, give the following response:
155
- Your feedback is invaluable to us. Kindly share your thoughts and suggestions at:
156
- -> Contact Number: +923269498569
157
- - If the user enters only 7, give the following response:
158
- -> Check out our latest offers and promotions to maximize your business potential.
159
- - **Fallback**: If the query doesn't match a greeting or a specific command (1-7), provide a professional and clean response based on the user's question.
160
- When answering based on retrieved documents, make sure to exclude unnecessary metadata (like document IDs) and display only the relevant content. For example, extract the actual report details such as sales, purchases, and other key information without showing raw document metadata.
161
- Example response for a "purchase report":
162
- "It seems like you're asking about the purchase report. Here's what I found:
163
- - Total Purchase (including tax): 3150.00
164
- - Total Purchase (excluding tax): 3150.00
165
- - Purchase Due: 3150.00
166
- - Shipping Charges: 0.00
167
- - Additional Expenses: 0.00
168
- - Sales Total (including tax): 1000000000000953067.33
169
- - Sales Total (excluding tax): 1000000000000945928.50"
170
- Ensure the information is formatted clearly and no irrelevant document information (such as IDs or metadata) is displayed.
171
  Context: {retrieved_documents}
172
  Question: {query}
173
  """
@@ -178,28 +105,37 @@ Question: {query}
178
 
179
  Generated_chat = LLMChain(llm=llama3, prompt=prompt)
180
  response = Generated_chat.invoke({'retrieved_documents': retrieved_documents, 'query': query})
181
- return response['text']
 
 
 
182
 
183
  def gradio_app():
184
  with gr.Blocks() as app:
185
  gr.Markdown("# Embedding and QA Interface")
186
-
187
- update_btn = gr.Button("Update CSV Files")
188
- update_output = gr.Textbox(label="Update Output")
189
-
190
- initialize_btn = gr.Button("Initialize Embedding")
191
- initialize_output = gr.Textbox(label="Output")
192
 
 
 
193
  query_input = gr.Textbox(label="Enter your query")
194
  generate_response_btn = gr.Button("Generate Response")
195
- response_output = gr.Textbox(label="Generated Response")
196
-
197
- # Button actions
198
- update_btn.click(update_csv_files, outputs=update_output)
199
- initialize_btn.click(initialize_embedding, outputs=initialize_output)
200
- generate_response_btn.click(generate_response, inputs=query_input, outputs=response_output)
 
 
 
 
 
 
 
 
 
 
201
 
202
  app.launch()
203
 
204
  # Run the Gradio app
205
- gradio_app()
 
4
  from langchain.chat_models import ChatOpenAI
5
  from langchain.document_loaders import CSVLoader
6
  from langchain_together import TogetherEmbeddings
 
 
 
 
 
 
7
  from langchain.vectorstores import Chroma
8
  from langchain_core.vectorstores import InMemoryVectorStore
9
  from langchain import PromptTemplate
 
11
  from langchain_together import Together
12
  import os
13
 
 
14
  os.environ['TOGETHER_API_KEY'] = "c2f52626b97118b71c0c36f66eda4f5957c8fc475e760c3d72f98ba07d3ed3b5"
15
 
16
  # Initialize global variable for vectorstore
17
  vectorstore = None
18
  embeddings = TogetherEmbeddings(model="togethercomputer/m2-bert-80M-8k-retrieval")
19
  llama3 = Together(model="meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo", max_tokens=1024)
20
+
21
  def update_csv_files():
 
22
  login_url = "https://livesystem.hisabkarlay.com/auth/login"
23
  payload = {
24
  "username": "user@123",
 
27
  "client_id": "5",
28
  "grant_type": "password"
29
  }
 
 
30
  response = requests.post(login_url, data=payload)
31
 
 
32
  if response.status_code == 200:
 
33
  access_token = response.json()['access_token']
34
  else:
35
  return f"Failed to log in: {response.status_code}"
36
 
 
37
  report_url = "https://livesystem.hisabkarlay.com/connector/api/profit-loss-report"
38
+ headers = {"Authorization": f"Bearer {access_token}"}
 
 
39
  response = requests.get(report_url, headers=headers)
40
  profit_loss_data = response.json()['data']
41
  keys = list(profit_loss_data.keys())
42
+ del keys[23]
43
  del keys[20]
44
  del keys[19]
45
  data_dict = {}
 
48
  df = pd.DataFrame(data_dict, index=[0])
49
  df.to_csv('profit_loss.csv', index=False)
50
 
 
51
  report_url = "https://livesystem.hisabkarlay.com/connector/api/purchase-sell"
52
  response = requests.get(report_url, headers=headers)
53
  sell_purchase_data = response.json()
 
55
  df = pd.json_normalize(sell_purchase_data)
56
  df.to_csv('purchase_sell_report.csv', index=False)
57
 
 
58
  report_url = "https://livesystem.hisabkarlay.com/connector/api/trending-products"
59
  response = requests.get(report_url, headers=headers)
60
  trending_product_data = response.json()['data']
 
66
 
67
  def initialize_embedding():
68
  global vectorstore
 
 
69
 
70
+ file_paths = ["profit_loss.csv", "purchase_sell_report.csv", "trending_product.csv"]
 
 
 
 
 
71
  documents = []
72
  for path in file_paths:
73
  loader = CSVLoader(path, encoding="windows-1252")
74
+ documents.extend(loader.load())
75
 
 
76
  vectorstore = InMemoryVectorStore.from_texts(
77
+ [doc.page_content for doc in documents],
78
  embedding=embeddings,
79
  )
80
  return "Embeddings initialized successfully!"
 
85
 
86
  retriever = vectorstore.as_retriever()
87
  retrieved_documents = retriever.invoke(query)
88
+ return retrieved_documents
89
 
90
+ def generate_response(query, history):
91
  if vectorstore is None:
92
+ return "Please initialize the embeddings first.", history
93
+
94
+ retrieved_documents = qa_chain(query)
95
  chat_template = """
96
  You are a highly intelligent and professional AI assistant.
97
  Generate the response according to the user's query:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
  Context: {retrieved_documents}
99
  Question: {query}
100
  """
 
105
 
106
  Generated_chat = LLMChain(llm=llama3, prompt=prompt)
107
  response = Generated_chat.invoke({'retrieved_documents': retrieved_documents, 'query': query})
108
+
109
+ # Ensure that the history is a list of two-element lists [query, response]
110
+ history.append([query, response['text']])
111
+ return response['text'], history
112
 
113
  def gradio_app():
114
  with gr.Blocks() as app:
115
  gr.Markdown("# Embedding and QA Interface")
 
 
 
 
 
 
116
 
117
+ # Chatbox elements
118
+ chatbot = gr.Chatbot(label="Chat")
119
  query_input = gr.Textbox(label="Enter your query")
120
  generate_response_btn = gr.Button("Generate Response")
121
+
122
+ # Status output textboxes for CSV update and embedding initialization
123
+ update_csv_status = gr.Textbox(label="CSV Update Status", interactive=False)
124
+ initialize_status = gr.Textbox(label="Embedding Initialization Status", interactive=False)
125
+
126
+ # Buttons for CSV update and embedding initialization
127
+ update_csv_button = gr.Button("Update CSV Files")
128
+ initialize_button = gr.Button("Initialize Embedding")
129
+
130
+ # Button click actions
131
+ update_csv_button.click(update_csv_files, outputs=update_csv_status)
132
+ initialize_button.click(initialize_embedding, outputs=initialize_status)
133
+
134
+ # Chatbot functionality with history
135
+ history = gr.State([]) # Chat history state
136
+ generate_response_btn.click(generate_response, inputs=[query_input, history], outputs=[chatbot, history])
137
 
138
  app.launch()
139
 
140
  # Run the Gradio app
141
+ gradio_app()