Rehman1603 commited on
Commit
62c55b2
β€’
1 Parent(s): 0f83a12

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -41
app.py CHANGED
@@ -24,6 +24,7 @@ os.environ['TOGETHER_API_KEY'] = "c2f52626b97118b71c0c36f66eda4f5957c8fc475e760c
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"
@@ -111,19 +112,16 @@ def qa_chain(query):
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
-
122
  Generate the response according to the user's query:
123
-
124
  - If the user enters a greeting (e.g., "Hi", "Hello", "Good day"), give the following response:
125
  "Welcome to HisabKarLay, your business partner! You may choose from the following services πŸ‘‡:
126
-
127
  1. Reports
128
  2. Forecasts
129
  3. Best Selling Items
@@ -131,15 +129,10 @@ Generate the response according to the user's query:
131
  5. Chat with our Customer Care Team
132
  6. Share your Feedback
133
  7. Checkout Latest Offers
134
-
135
  πŸ”† Suggestion: To make a selection, send the relevant number like 1
136
-
137
  β­• 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.
138
-
139
  β­• Note: If you want to change the language, type and send 'change language.'
140
-
141
  πŸ’πŸ»β™‚οΈ Help: If you need any help, you can call us at +923269498569."
142
-
143
  - If the user enters a specific number (1-7), give the following responses:
144
  - If the user enters only 1, give the following response:
145
  If you are interested in insights related to your business, please find the available reports below:
@@ -164,11 +157,8 @@ Generate the response according to the user's query:
164
  -> Contact Number: +923269498569
165
  - If the user enters only 7, give the following response:
166
  -> Check out our latest offers and promotions to maximize your business potential.
167
-
168
  - **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.
169
-
170
  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.
171
-
172
  Example response for a "purchase report":
173
  "It seems like you're asking about the purchase report. Here's what I found:
174
  - Total Purchase (including tax): 3150.00
@@ -178,9 +168,7 @@ Generate the response according to the user's query:
178
  - Additional Expenses: 0.00
179
  - Sales Total (including tax): 1000000000000953067.33
180
  - Sales Total (excluding tax): 1000000000000945928.50"
181
-
182
  Ensure the information is formatted clearly and no irrelevant document information (such as IDs or metadata) is displayed.
183
-
184
  Context: {retrieved_documents}
185
  Question: {query}
186
  """
@@ -190,29 +178,31 @@ Question: {query}
190
  )
191
 
192
  Generated_chat = LLMChain(llm=llama3, prompt=prompt)
193
- response = Generated_chat.invoke({'retrieved_documents': retrieved_documents, 'query': query})
194
- return response['text']
195
-
196
- def gradio_app():
197
- with gr.Blocks() as app:
198
- gr.Markdown("# Embedding and QA Interface")
199
-
200
- update_btn = gr.Button("Update CSV Files")
201
- update_output = gr.Textbox(label="Update Output")
202
-
203
- initialize_btn = gr.Button("Initialize Embedding")
204
- initialize_output = gr.Textbox(label="Output")
205
-
206
- query_input = gr.Textbox(label="Enter your query")
207
- generate_response_btn = gr.Button("Generate Response")
208
- response_output = gr.Textbox(label="Generated Response")
209
-
210
- # Button actions
211
- update_btn.click(update_csv_files, outputs=update_output)
212
- initialize_btn.click(initialize_embedding, outputs=initialize_output)
213
- generate_response_btn.click(generate_response, inputs=query_input, outputs=response_output)
214
-
215
- app.launch()
216
-
217
- # Run the Gradio app
218
- gradio_app()
 
 
 
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
+
28
  def update_csv_files():
29
  # Define the login URL and credentials
30
  login_url = "https://livesystem.hisabkarlay.com/auth/login"
 
112
  retrieved_documents = retriever.invoke(query)
113
  return retrieved_documents # Not shown directly in the UI
114
 
115
+ def generate_response(query, history):
116
  if vectorstore is None:
117
+ return history, "Please initialize the embeddings first."
118
 
119
  retrieved_documents = qa_chain(query) # Call qa_chain internally
120
  chat_template = """
121
  You are a highly intelligent and professional AI assistant.
 
122
  Generate the response according to the user's query:
 
123
  - If the user enters a greeting (e.g., "Hi", "Hello", "Good day"), give the following response:
124
  "Welcome to HisabKarLay, your business partner! You may choose from the following services πŸ‘‡:
 
125
  1. Reports
126
  2. Forecasts
127
  3. Best Selling Items
 
129
  5. Chat with our Customer Care Team
130
  6. Share your Feedback
131
  7. Checkout Latest Offers
 
132
  πŸ”† Suggestion: To make a selection, send the relevant number like 1
 
133
  β­• 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.
 
134
  β­• Note: If you want to change the language, type and send 'change language.'
 
135
  πŸ’πŸ»β™‚οΈ Help: If you need any help, you can call us at +923269498569."
 
136
  - If the user enters a specific number (1-7), give the following responses:
137
  - If the user enters only 1, give the following response:
138
  If you are interested in insights related to your business, please find the available reports below:
 
157
  -> Contact Number: +923269498569
158
  - If the user enters only 7, give the following response:
159
  -> Check out our latest offers and promotions to maximize your business potential.
 
160
  - **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.
 
161
  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.
 
162
  Example response for a "purchase report":
163
  "It seems like you're asking about the purchase report. Here's what I found:
164
  - Total Purchase (including tax): 3150.00
 
168
  - Additional Expenses: 0.00
169
  - Sales Total (including tax): 1000000000000953067.33
170
  - Sales Total (excluding tax): 1000000000000945928.50"
 
171
  Ensure the information is formatted clearly and no irrelevant document information (such as IDs or metadata) is displayed.
 
172
  Context: {retrieved_documents}
173
  Question: {query}
174
  """
 
178
  )
179
 
180
  Generated_chat = LLMChain(llm=llama3, prompt=prompt)
181
+ result = Generated_chat.run({
182
+ "retrieved_documents": retrieved_documents,
183
+ "query": query
184
+ })
185
+
186
+ # Append the conversation history
187
+ history.append((query, result))
188
+
189
+ return history, result
190
+
191
+ # Define Gradio UI
192
+ with gr.Blocks() as demo:
193
+ chatbot = gr.Chatbot(label="AI Chat")
194
+ query = gr.Textbox(label="Ask anything!")
195
+ initialize_button = gr.Button("Initialize Embeddings")
196
+ update_csv_button = gr.Button("Update CSV Files")
197
+
198
+ def on_query(query, history):
199
+ return generate_response(query, history)
200
+
201
+ query.submit(on_query, [query, chatbot], [chatbot, query])
202
+
203
+ initialize_button.click(initialize_embedding, outputs=None)
204
+ update_csv_button.click(update_csv_files, outputs=None)
205
+
206
+ # Launch Gradio App
207
+ demo.launch()
208
+