budhadityac24 commited on
Commit
c3fe34b
1 Parent(s): e6dcb03

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +422 -170
app.py CHANGED
@@ -156,7 +156,7 @@ def question_create(json_template):
156
  messages=[
157
  {
158
  "role": "system",
159
- "content": "You are an experienced writer. You will be given an array of questions. \nSome questions will ask to upload images. Ignore any of these type of questions.\nSome questions ask about different identities or descriptions of the same thing. I want you to merge the questions so as to ask input from them once.\nConvert all questions so that more of a professional but also a bit of a funny tone is maintained. AVOID REDUNDANCY. DONT RETURN MORE THAN 15 QUESTIONS.\nRETURN AN ARRAY OF THE QUESTIONS ONLY. DO NOT RETURN ANYTHING ELSE. "
160
  },
161
  {
162
  "role": "user",
@@ -175,60 +175,7 @@ def question_create(json_template):
175
 
176
  return final
177
 
178
- # def answer_refill(questions,answers,obs_json_template,bizobj_json_template):
179
-
180
- # client = Groq(api_key=os.getenv("GROQ_API_KEY"))
181
- # completion = client.chat.completions.create(
182
- # model="llama-3.1-70b-versatile",
183
- # messages=[
184
- # {
185
- # "role": "system",
186
- # "content": "You are a helpful assistant. You will be given two arrays, questions and answer. I want you to create a question answer pair. For example, \n#INPUT\nQuestion=['What is my name?', 'What is your age?']\nAnswer=['Mohan','69']\n\n#OUTPUT\n['Question:What is my name? Answer:Mohan','What is your age? Answer:69']\n\nDONT RETURN ANYTHING OTHER THAN THE FINAL ARRAY"
187
- # },
188
- # {
189
- # "role": "user",
190
- # "content": "Question="+str(questions)+"\nAnswer="+str(answers)
191
- # }
192
- # ],
193
- # temperature=0.5,
194
- # max_tokens=4048,
195
- # top_p=1,
196
- # stream=True,
197
- # stop=None,
198
- # )
199
-
200
- # qapair = ""
201
- # for chunk in completion:
202
- # qapair += chunk.choices[0].delta.content or ""
203
- # # print(qapair)
204
- # # print(obs_json_template+bizobj_json_template)
205
- # # print("Question Answer:"+str(qapair)+"\nJSON:\n"+str(obs_json_template+bizobj_json_template))
206
- # # print(str(obs_json_template+bizobj_json_template))
207
- # completion2 = client.chat.completions.create(
208
- # model="llama-3.1-70b-versatile",
209
- # messages=[
210
- # {
211
- # "role": "system",
212
- # "content": "You are a helpful assistant. You will be given a Question-answer pair. You will be given a json. Some subproperties in the JSONs labelled \"User Answer\" are marked as TBD. Based on the question answer pair, I want you to fill the Answer of the question answer pair as it is into the \"User answer\" subproperty. Make sure you return the full JSON, without missing any field. After filling, merge the two filled JSONs. Then return the final completely filled JSON. DONT OUTPUT ANYTHING OTHER THAN THE JSONS."
213
- # },
214
- # {
215
- # "role": "user",
216
- # "content": "Question Answer:"+str(qapair)+"\nJSON:\n"+str(obs_json_template+bizobj_json_template)
217
- # }
218
- # ],
219
- # temperature=1,
220
- # max_tokens=8000,
221
- # top_p=1,
222
- # stream=True,
223
- # stop=None,
224
- # )
225
- # filled_json=""
226
- # for chunk in completion2:
227
- # filled_json+=chunk.choices[0].delta.content or ""
228
- # # print(filled_json)
229
- # return filled_json
230
-
231
- def qapair_create(questions,answers):
232
 
233
  client = Groq(api_key=os.getenv("GROQ_API_KEY"))
234
  completion = client.chat.completions.create(
@@ -240,7 +187,7 @@ def qapair_create(questions,answers):
240
  },
241
  {
242
  "role": "user",
243
- "content": "Question="+str(questions)+"\nAnswer="+str(answers)
244
  }
245
  ],
246
  temperature=0.5,
@@ -258,6 +205,55 @@ def qapair_create(questions,answers):
258
  # print(obs_json_template+bizobj_json_template)
259
  # print("Question Answer:"+str(qapair)+"\nJSON:\n"+str(obs_json_template+bizobj_json_template))
260
  # print(str(obs_json_template+bizobj_json_template))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
261
  def answer_refill(qapair,json_template):
262
  client = Groq(api_key=os.getenv("GROQ_API_KEY"))
263
  completion2 = client.chat.completions.create(
@@ -265,7 +261,7 @@ def answer_refill(qapair,json_template):
265
  messages=[
266
  {
267
  "role": "system",
268
- "content": "You are a helpful assistant. You will be given a Question-answer pair. You will be given a json. Some subproperties in the JSONs labelled \"User Answer\" are marked as TBD. Based on the question answer pair, I want you to fill the Answer of the question answer pair as it is into the \"User answer\" subproperty. Make sure you return the full JSON, without missing any field. After filling, merge the two filled JSONs. Then return the final completely filled JSON. DONT OUTPUT ANYTHING OTHER THAN THE JSONS."
269
  },
270
  {
271
  "role": "user",
@@ -284,7 +280,35 @@ def answer_refill(qapair,json_template):
284
  # print(filled_json)
285
  return filled_json
286
 
287
- def executive_summary(json_template):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
288
 
289
 
290
  client = Groq(api_key=os.getenv("GROQ_API_KEY"))
@@ -311,6 +335,177 @@ def executive_summary(json_template):
311
  final_summ+=chunk.choices[0].delta.content or ""
312
  return final_summ
313
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
314
  def chunk_data(data, chunk_size=10):
315
  if isinstance(data, dict):
316
  # If data is a dictionary, convert it to a list of key-value pairs
@@ -393,44 +588,16 @@ def airtable_write(json_template):
393
  else:
394
  print(f"Failed to add batch. Status code: {response.status_code}, Error: {response.text}")
395
 
396
-
397
- def main():
398
- # local_css("style.css")
399
- # st.markdown('<div class="my-custom-class">Custom styled box</div>', unsafe_allow_html=True)
400
- st.image('images.png', use_column_width=True)
401
- st.title("Qualitas Sales Data Collection Chatbot")
402
- st.caption("Welcome to the Qualitas Bot. First upload a PDF document which should be customer correspondence, detailing some requirements")
403
-
404
- # Initialize session state variables
405
- init_session_state()
406
-
407
- # File uploader for the PDF
408
  uploaded_file = st.file_uploader("Upload a PDF document", type="pdf")
409
- if uploaded_file is not None and not st.session_state.file_processed:
410
- st.write("Processing your document...")
411
- process_document(uploaded_file)
412
- # Display the first question immediately after processing the document
413
- show_question()
414
-
415
- # Simulate chat interaction
416
- chat_interaction()
417
-
418
- def init_session_state():
419
- if "file_processed" not in st.session_state:
420
- st.session_state.file_processed = False
421
- if "questionnaire_started" not in st.session_state:
422
- st.session_state.questionnaire_started = False
423
- if "current_question_index" not in st.session_state:
424
- st.session_state.current_question_index = 0
425
- if "messages" not in st.session_state:
426
- st.session_state.messages = []
427
- if "questions" not in st.session_state:
428
- st.session_state.questions = []
429
- if "questionnaire_complete" not in st.session_state:
430
- st.session_state.questionnaire_complete = False
431
-
432
- def process_document(uploaded_file):
433
- # Simulate file processing (replace with actual logic)
434
  st.write("Parsing Document ...")
435
  st.session_state.text = extract_text_from_pdf(uploaded_file)
436
  st.write("Running Classification Algorithm...")
@@ -485,82 +652,167 @@ def process_document(uploaded_file):
485
  question_software = question_create(final_software_json)
486
  question_customerdependency = question_create(final_customerdependency_json)
487
  question_acceptance = question_create(final_acceptance_json)
488
- while True:
489
- try:
490
- # Attempt to evaluate the expressions and assign them to session state
491
- st.session_state.questions = ast.literal_eval(question_bizobj) + ast.literal_eval(question_prodvarinfo) + ast.literal_eval(question_materialhandling) + ast.literal_eval(question_software) + ast.literal_eval(question_acceptance) + ast.literal_eval(question_customerdependency) + ast.literal_eval(questionobs)
492
- # If successful, break out of the loop
493
- break
494
- except Exception as e:
495
- # Print the error for debugging purposes (optional)
496
- print(f"An error occurred: {e}")
497
- # Wait for 1 second before trying again
498
- time.sleep(1)
499
- continue
500
- # st.write(st.session_state.questions)
501
- # Mark file as processed
502
- st.session_state.file_processed = True
503
- st.success("Document processed successfully.")
504
-
505
- def chat_interaction():
506
- # Display chat messages from history in the correct order
507
- for message in st.session_state.messages[::-1]:
508
- with st.chat_message(message["role"]):
509
- st.markdown(message["content"])
510
-
511
- # React to user input
512
- if prompt := st.chat_input("What is your question?"):
513
- # Display user message in chat message container
514
- st.chat_message("user").markdown(prompt)
515
-
516
- # Add user message to chat history
517
- st.session_state.messages.append({"role": "user", "content": prompt})
518
-
519
- # Process the user's input and show the next question
520
- show_question()
521
-
522
- def show_question():
523
- if st.session_state.current_question_index < len(st.session_state.questions):
524
- # Display the next question
525
- with st.chat_message("assistant"):
526
- st.markdown(st.session_state.questions[st.session_state.current_question_index])
527
-
528
- # Add the question to the chat history
529
- st.session_state.messages.append({"role": "assistant", "content": st.session_state.questions[st.session_state.current_question_index]})
530
-
531
- # Move to the next question
532
- st.session_state.current_question_index += 1
533
-
534
- # Check if all questions have been answered
535
- if st.session_state.current_question_index >= len(st.session_state.questions):
536
- st.session_state.questionnaire_complete = True
537
-
538
- else:
539
- # Display the answers after completing the questionnaire
540
- st.success("Questions Complete! Please wait for final summary!")
541
- answers = [message["content"] for message in st.session_state.messages if message["role"] == "user"]
542
- # with st.chat_message("assistant"):
543
- # st.subheader("Answers")
544
- # st.write(answers)
545
- qap=qapair_create(st.session_state.questions,answers)
546
- # completed_obs = answer_refill(st.session_state.questions, answers, st.session_state.obs, st.session_state.bizobj)
547
- completed_obs= answer_refill(qap,st.session_state.obs)
548
- completed_bizobj= answer_refill(qap,st.session_state.bizobj)
549
- completed_prodvarinfo= answer_refill(qap,st.session_state.prodvarinfo)
550
- completed_materialhandling= answer_refill(qap,st.session_state.materialhandling)
551
- completed_software= answer_refill(qap,st.session_state.software)
552
- completed_customerdependency= answer_refill(qap,st.session_state.customerdependency)
553
- completed_acceptance= answer_refill(qap,st.session_state.acceptance)
554
- # st.write(completed_json)
555
- airtable_write(completed_obs)
556
- airtable_write(completed_bizobj)
557
- airtable_write(completed_prodvarinfo)
558
- airtable_write(completed_materialhandling)
559
- airtable_write(completed_software)
560
- airtable_write(completed_customerdependency)
561
- airtable_write(completed_acceptance)
562
- exec_summ = executive_summary(str(completed_obs+completed_bizobj+completed_prodvarinfo+completed_materialhandling+completed_software+completed_customerdependency+completed_acceptance))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
563
  st.write(exec_summ)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
564
 
565
  if __name__ == "__main__":
566
  main()
 
156
  messages=[
157
  {
158
  "role": "system",
159
+ "content": "You are an experienced writer. You will be given an array of questions. \nSome questions will ask to upload images. Ignore any of these type of questions.\nSome questions ask about different identities or descriptions of the same thing. I want you to merge the questions so as to ask input from them once.\nConvert all questions so that more of a professional is maintained. AVOID REDUNDANCY. DONT RETURN MORE THAN 15 QUESTIONS.\nRETURN AN ARRAY OF THE QUESTIONS ONLY. DO NOT RETURN ANYTHING ELSE. "
160
  },
161
  {
162
  "role": "user",
 
175
 
176
  return final
177
 
178
+ def qapair_create(script):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
179
 
180
  client = Groq(api_key=os.getenv("GROQ_API_KEY"))
181
  completion = client.chat.completions.create(
 
187
  },
188
  {
189
  "role": "user",
190
+ "content": str(script)
191
  }
192
  ],
193
  temperature=0.5,
 
205
  # print(obs_json_template+bizobj_json_template)
206
  # print("Question Answer:"+str(qapair)+"\nJSON:\n"+str(obs_json_template+bizobj_json_template))
207
  # print(str(obs_json_template+bizobj_json_template))
208
+ def conflict_detect(json1,json2):
209
+
210
+ client = Groq(api_key=os.getenv("GROQ_API_KEY"))
211
+ completion = client.chat.completions.create(
212
+ model="llama-3.1-70b-versatile",
213
+ messages=[
214
+ {
215
+ "role": "system",
216
+ "content": "You are an experienced copywriter who understands engineering concepts. You will be given 2 JSONs. I want you to compare the \"User Answer\" field between the 2 JSONs, and then check for conflicts. Check for any conflicts within numerical values. If you find a conflict, mark the field as CONFLICT and always give the reasoning behind the conflict. However be careful to check if the 2 fields to be compared essentially mean the same thing, in that case return either answer. If either field says TBD, ignore the test and output the test where user answer s not TBD.\nFor example:\nUser Answer 1: \"TBD\"\nUser Answer 2: \"The budget is 15000\"\nResult: \"The budget is 15000\"\n\nUser Answer 1: \"The expected ROI is 5 years\"\nUser Answer 2:\"The ROI is to be within 15 years\"\nResult:\"CONFLICT - The first answer mentions ROI as 5 years while the second answer mentions ROI as 15 years\"\n\nUser Answer 1: \"The KPIs to be measured are speed and accuracy\"\nUser Answer 2: \"TBD\"\nResult: \"The KPIs to be measured are speed and accuracy\"\n\nUser Answer 1: \"There will be 1GB Internet Connection\"\nUser Answer 2: \"There will be EthernetIP present\"\nResult: \"There will be EthernetIP with 1GB internet connection present\"\n\nIn the end only return the filled JSON. DONT RETURN ANYTHING OTHER THAN THE JSON."
217
+ },
218
+ {
219
+ "role": "user",
220
+ "content": "JSON 1:\n"+str(json1)+"\nJSON 2:\n"+str(json2)
221
+ }
222
+ ],
223
+ temperature=0.24,
224
+ max_tokens=5220,
225
+ top_p=1,
226
+ stream=True,
227
+ stop=None,
228
+ )
229
+
230
+ filled_json=""
231
+ for chunk in completion:
232
+ filled_json+=chunk.choices[0].delta.content or ""
233
+
234
+ # completion2 = client.chat.completions.create(
235
+ # model="llama-3.1-70b-versatile",
236
+ # messages=[
237
+ # {
238
+ # "role": "system",
239
+ # "content": "You are a helpful assistant. You will be given a JSON where some subproperties marked as \"User Answer\" which have fields marked as \"CONFLICT\". \nFor these fields, there is also a reason. In a professional tone, formulate a question asking as to what the correct answer should be while explaining the reason clearly. Return an array of all these questions. DONT OUTPUT ANYTHING OTHER THAN THE ARRAY.\n"
240
+ # },
241
+ # {
242
+ # "role": "user",
243
+ # "content": filled_json
244
+ # }
245
+ # ],
246
+ # temperature=0.24,
247
+ # max_tokens=5220,
248
+ # top_p=1,
249
+ # stream=True,
250
+ # stop=None,
251
+ # )
252
+ # questions=""
253
+ # for chunk in completion2:
254
+ # questions+=chunk.choices[0].delta.content or ""
255
+ return filled_json
256
+
257
  def answer_refill(qapair,json_template):
258
  client = Groq(api_key=os.getenv("GROQ_API_KEY"))
259
  completion2 = client.chat.completions.create(
 
261
  messages=[
262
  {
263
  "role": "system",
264
+ "content": "You are a helpful assistant. You will be given a Question-answer pair. You will be given a json. Some subproperties in the JSONs labelled \"User Answer\" are marked as TBD. Based on the question answer pair, I want you to fill the Answer of the question answer pair as it is into the \"User answer\" subproperty. Check the description of the field against the question and be sure to fill the correct field for the correct question. Make sure you return the full JSON, without missing any field. After filling, merge the two filled JSONs. Then return the final completely filled JSON. DONT OUTPUT ANYTHING OTHER THAN THE JSONS."
265
  },
266
  {
267
  "role": "user",
 
280
  # print(filled_json)
281
  return filled_json
282
 
283
+ def response_filter(record,key,opt):
284
+ if opt==1:
285
+ client = Groq(api_key=os.getenv("GROQ_API_KEY_1"))
286
+ else:
287
+ client = Groq(api_key=os.getenv("GROQ_API_KEY"))
288
+ completion = client.chat.completions.create(
289
+ model="llama-3.1-70b-versatile",
290
+ messages=[
291
+ {
292
+ "role": "system",
293
+ "content": "You are a helpful assistant. You will be given a big JSON. I want to only extract one key out of that. RETURN THE VARIABLE ASKED FOR ONLY"
294
+ },
295
+ {
296
+ "role": "user",
297
+ "content": "Var="+str(key)+"\nJSON:\n"+str(record)
298
+ }
299
+ ],
300
+ temperature=0.38,
301
+ max_tokens=7830,
302
+ top_p=1,
303
+ stream=True,
304
+ stop=None,
305
+ )
306
+ filled_json=""
307
+ for chunk in completion:
308
+ filled_json+=chunk.choices[0].delta.content or ""
309
+ # print(filled_json)
310
+ return filled_json
311
+ def executive_summary_complete(json_template):
312
 
313
 
314
  client = Groq(api_key=os.getenv("GROQ_API_KEY"))
 
335
  final_summ+=chunk.choices[0].delta.content or ""
336
  return final_summ
337
 
338
+ def executive_summary(questions):
339
+
340
+
341
+ client = Groq(api_key=os.getenv("GROQ_API_KEY"))
342
+
343
+ completion = client.chat.completions.create(
344
+ model="llama-3.1-70b-versatile",
345
+ messages=[
346
+ {
347
+ "role": "system",
348
+ "content": "You are a helpful assistant. You will be given an array of engineering questions. There maybe some repetitions in the questions. Remove them.\n\nNow create a professional mail with this approach:\nyou thank the recipient for the last meeting. Then after talking with your team you have a series of questions. You then list out the questions in a bullet point fashion."
349
+ },
350
+ {
351
+ "role": "user",
352
+ "content": str(questions)
353
+ }
354
+ ],
355
+ temperature=0.65,
356
+ max_tokens=5220,
357
+ top_p=1,
358
+ stream=True,
359
+ stop=None,
360
+ )
361
+ final_summ=""
362
+ for chunk in completion:
363
+ final_summ+=chunk.choices[0].delta.content or ""
364
+ return final_summ
365
+
366
+ def conflict_summary(json):
367
+
368
+
369
+ client = Groq(api_key=os.getenv("GROQ_API_KEY"))
370
+
371
+ completion = client.chat.completions.create(
372
+ model="llama-3.1-70b-versatile",
373
+ messages=[
374
+ {
375
+ "role": "system",
376
+ "content": "You are a helpful assistant. You will be given a JSON. The fields where \"User Answer\" is marked as \"CONFLICT\", I want you to make questions asking the difference only for these fields. If either User Answer is marked as TBD, DONT CREATE QUESTIONS FOR THAT. Remember to always include the reason for conflict. The final output should be a mail which outlines all the questions. The mail should say how after discussion with the team you have come up with these questions. Maintain a professional tone. Refer to JSON1 as initial communication. Refer to JSON2 as latest communication. Dont mention the words \"JSON1\" and \"JSON2\" anywhere"
377
+ },
378
+ {
379
+ "role": "user",
380
+ "content": str(json)
381
+ }
382
+ ],
383
+ temperature=0.65,
384
+ max_tokens=7220,
385
+ top_p=1,
386
+ stream=True,
387
+ stop=None,
388
+ )
389
+ final_summ=""
390
+ for chunk in completion:
391
+ final_summ+=chunk.choices[0].delta.content or ""
392
+ return final_summ
393
+
394
+ def airtable_retrieve(identifier):
395
+ base_id = 'appcl0egQeE4pP5ID'
396
+ table_name = 'tblfQBynpcfdDUywV'
397
+ api_key = os.getenv("AIRTABLE_KEY")
398
+
399
+ # API endpoint
400
+ base_url = f'https://api.airtable.com/v0/{base_id}/{table_name}'
401
+
402
+ # Headers for authentication
403
+ headers = {
404
+ 'Authorization': f'Bearer {api_key}',
405
+ 'Content-Type': 'application/json'
406
+ }
407
+
408
+ def get_record_by_identifier(identifier):
409
+ try:
410
+ # Construct the filter formula
411
+ filter_formula = f"{{Identifier}} = '{identifier}'"
412
+
413
+ # Make the API request
414
+ response = requests.get(
415
+ base_url,
416
+ headers=headers,
417
+ params={'filterByFormula': filter_formula}
418
+ )
419
+
420
+ # Check if the request was successful
421
+ response.raise_for_status()
422
+
423
+ # Parse the JSON response
424
+ data = response.json()
425
+
426
+ if data['records']:
427
+ if len(data['records']) > 1:
428
+ print(f"Warning: Multiple records found for identifier '{identifier}'. Returning the first one.")
429
+ return data['records'][-1]['fields']
430
+ else:
431
+ raise ValueError(f"No record found for identifier '{identifier}'")
432
+
433
+ except requests.exceptions.RequestException as e:
434
+ print(f"An error occurred while making the request: {str(e)}")
435
+ return None
436
+ except ValueError as e:
437
+ print(str(e))
438
+ return None
439
+ except Exception as e:
440
+ print(f"An unexpected error occurred: {str(e)}")
441
+ return None
442
+
443
+ # Example usage
444
+
445
+ record = get_record_by_identifier(identifier)
446
+ # print(record)
447
+ if record:
448
+ # print("Record found:")
449
+ # for field, value in record.items():
450
+ # print(f"{field}: {value}")
451
+ return record
452
+ else:
453
+ return "404"
454
+
455
+ def combine_json_files(directory):
456
+ combined_data = {}
457
+ for filename in os.listdir(directory):
458
+ if filename.endswith('.json'):
459
+ file_path = os.path.join(directory, filename)
460
+ key = os.path.splitext(filename)[0] # Use filename without extension as key
461
+ with open(file_path, 'r') as file:
462
+ combined_data[key] = json.load(file)
463
+ return combined_data
464
+
465
+ def prepare_json_string(json_data):
466
+ # Convert the JSON data to a string, escaping any problematic characters
467
+ return json.dumps(json.dumps(json_data))
468
+
469
+ def airtable_write_main(json_strings,id):
470
+ API_KEY = os.getenv("AIRTABLE_KEY")
471
+ BASE_ID = 'appcl0egQeE4pP5ID'
472
+ TABLE_ID = 'tblfQBynpcfdDUywV'
473
+
474
+ url = f'https://api.airtable.com/v0/{BASE_ID}/{TABLE_ID}'
475
+
476
+ headers = {
477
+ 'Authorization': f'Bearer {API_KEY}',
478
+ 'Content-Type': 'application/json'
479
+ }
480
+
481
+ # Prepare the record for Airtable
482
+ record = {
483
+ "fields": {
484
+ "Identifier": id, # You may want to generate a unique identifier here
485
+ "BIZ_OBS_JSON": json_strings.get("BIZ_OBS_JSON", "[]"),
486
+ "PROD_VAR_INFO_JSON": json_strings.get("PROD_VAR_INFO_JSON", "[]"),
487
+ "CUSTOMER_DEPENDENCY_JSON": json_strings.get("CUSTOMER_DEPENDENCY_JSON", "[]"),
488
+ "MATERIAL_HANDLING_JSON": json_strings.get("MATERIAL_HANDLING_JSON", "[]"),
489
+ "SOFTWARE_JSON": json_strings.get("SOFTWARE_JSON", "[]"),
490
+ "ACCEPTANCE_JSON": json_strings.get("ACCEPTANCE_JSON", "[]"),
491
+ "OBS_JSON": json_strings.get("OBS_JSON", "[]")
492
+ }
493
+ }
494
+
495
+ # Prepare the payload for Airtable
496
+ payload = {
497
+ "records": [record]
498
+ }
499
+
500
+ # Make the POST request to add the record
501
+ response = requests.post(url, headers=headers, data=json.dumps(payload))
502
+
503
+ # Check if the request was successful
504
+ if response.status_code == 200:
505
+ print("Record added successfully!")
506
+ else:
507
+ print(f"Failed to add record. Status code: {response.status_code}, Error: {response.text}")
508
+
509
  def chunk_data(data, chunk_size=10):
510
  if isinstance(data, dict):
511
  # If data is a dictionary, convert it to a list of key-value pairs
 
588
  else:
589
  print(f"Failed to add batch. Status code: {response.status_code}, Error: {response.text}")
590
 
591
+ def process_new_customer(unique_id):
592
+ st.write(f"Processing new customer with ID: {unique_id}")
593
+ st.write("Please upload the first communication:")
 
 
 
 
 
 
 
 
 
594
  uploaded_file = st.file_uploader("Upload a PDF document", type="pdf")
595
+ if uploaded_file is not None:
596
+ button1(uploaded_file,unique_id)
597
+ # # Add your document processing steps for new customers here
598
+ # # For example:
599
+ # uploaded_file = st.file_uploader("Upload a PDF document", type="pdf")
600
+ def button1(uploaded_file,unique_id):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
601
  st.write("Parsing Document ...")
602
  st.session_state.text = extract_text_from_pdf(uploaded_file)
603
  st.write("Running Classification Algorithm...")
 
652
  question_software = question_create(final_software_json)
653
  question_customerdependency = question_create(final_customerdependency_json)
654
  question_acceptance = question_create(final_acceptance_json)
655
+ totquestions=questionobs+question_bizobj+question_prodvarinfo+question_materialhandling+question_software+question_customerdependency+question_acceptance
656
+ st.write("Creating Question Email...")
657
+ exec_questions=executive_summary(totquestions)
658
+ final_json=final_bizobj_json+final_prodvarinfo_json+final_materialhandling_json+final_software_json+final_customerdependency_json+final_acceptance_json+final_obs_json
659
+ exec_summ=executive_summary_complete(final_json)
660
+ st.write(exec_summ)
661
+ st.write("Here is the Composed Mail for the customers ->")
662
+ st.write(exec_questions)
663
+ json_strings = {
664
+ "BIZ_OBS_JSON": st.session_state.bizobj,
665
+ "PROD_VAR_INFO_JSON": st.session_state.prodvarinfo,
666
+ "CUSTOMER_DEPENDENCY_JSON": st.session_state.customerdependency,
667
+ "MATERIAL_HANDLING_JSON": st.session_state.materialhandling,
668
+ "SOFTWARE_JSON": st.session_state.software,
669
+ "ACCEPTANCE_JSON": st.session_state.acceptance,
670
+ "OBS_JSON": st.session_state.obs
671
+ }
672
+ airtable_write_main(json_strings,unique_id)
673
+
674
+
675
+ def process_registered_customer(unique_id):
676
+ st.write(f"Processing registered customer with ID: {unique_id}")
677
+ # Add your document processing steps for registered customers here
678
+ # For example:
679
+ record=airtable_retrieve(unique_id)
680
+ if record == '404':
681
+ st.write("Record Not Found. Please restart and check identifier")
682
+ return
683
+ json_data=record
684
+ # st.write(record)
685
+ # parsed_data=json.loads(str(record))
686
+ obs_json = json_data.get("OBS_JSON")
687
+ biz_obs_json = json_data.get("BIZ_OBS_JSON")
688
+ software_json = json_data.get("SOFTWARE_JSON")
689
+ customer_dependency_json = json_data.get("CUSTOMER_DEPENDENCY_JSON")
690
+ prod_var_info_json = json_data.get("PROD_VAR_INFO_JSON")
691
+ material_handling_json = json_data.get("MATERIAL_HANDLING_JSON")
692
+ acceptance_json = json_data.get("ACCEPTANCE_JSON")
693
+ # st.write(biz_obs_json)
694
+ # biz_obs_json = response_filter(record,"BIZ_OBS_JSON",1)
695
+ # prod_var_info_json = response_filter(record,"PROD_VAR_INFO_JSON",2)
696
+ # material_handling_json = response_filter(record,"MATERIAL_HANDLING_JSON",1)
697
+ # software_json = response_filter(record,"SOFTWARE_JSON",2)
698
+ # customer_dependency_json = response_filter(record,"CUSTOMER_DEPENDENCY_JSON",1)
699
+ # acceptance_json = response_filter(record,"ACCEPTANCE_JSON",2)
700
+ # obs_json = response_filter(record,"OBS_JSON",1)
701
+ st.write("Records Retrieved. Please enter the Questions and Answers:")
702
+ qa=st.chat_input("Please enter the questions given to customer and their answers:")
703
+ if qa:
704
+ qapair=qapair_create(qa)
705
+ # st.write(qapair)
706
+ json_path='Biz_Obj.json'
707
+ with open(json_path, 'r') as file:
708
+ new_bizobj_json = json.load(file)
709
+ filled_bizobj = answer_refill(qapair,new_bizobj_json)
710
+
711
+ json_path='Prod_var_info.json'
712
+ with open(json_path, 'r') as file:
713
+ new_prodvarinfo_json = json.load(file)
714
+ filled_prodvar = answer_refill(qapair,new_prodvarinfo_json)
715
+
716
+ json_path='Material_handling.json'
717
+ with open(json_path, 'r') as file:
718
+ new_materialhandling_json = json.load(file)
719
+ filled_material_handling = answer_refill(qapair,new_materialhandling_json)
720
+
721
+ json_path='software.json'
722
+ with open(json_path, 'r') as file:
723
+ new_software_json = json.load(file)
724
+ filled_software = answer_refill(qapair,new_software_json)
725
+
726
+ json_path='Customer_dependency.json'
727
+ with open(json_path, 'r') as file:
728
+ new_customerdependency_json = json.load(file)
729
+ filled_customer_dependency = answer_refill(qapair,new_customerdependency_json)
730
+
731
+ json_path='acceptance.json'
732
+ with open(json_path, 'r') as file:
733
+ new_acceptance_json = json.load(file)
734
+ filled_acceptance = answer_refill(qapair,new_acceptance_json)
735
+
736
+ # filled_bizobj=answer_refill(qapair,biz_obs_json)
737
+ # filled_prodvar=answer_refill(qapair,prod_var_info_json)
738
+ # filled_material_handling=answer_refill(qapair,material_handling_json)
739
+ # filled_software=answer_refill(qapair,software_json)
740
+ # filled_customer_dependency=answer_refill(qapair,customer_dependency_json)
741
+ filled_obs=answer_refill(qapair,obs_json)
742
+ # filled_acceptance=answer_refill(qapair,acceptance_json)
743
+ # st.write("Biz_obs_JSON")
744
+ # st.write(biz_obs_json)
745
+ # st.write("Filled_JSON")
746
+ # st.write(filled_bizobj)
747
+ print("reached")
748
+ # st.write(filled_bizobj)
749
+ st.write("Checking for Conflicts...")
750
+ conquest_bizobj=conflict_detect(biz_obs_json,filled_bizobj)
751
+ conquest_prodvar=conflict_detect(prod_var_info_json,filled_prodvar)
752
+ conquest_material_handling=conflict_detect(material_handling_json,filled_material_handling)
753
+ conquest_software=conflict_detect(software_json,filled_software)
754
+ conquest_customer_dependency=conflict_detect(customer_dependency_json,filled_customer_dependency)
755
+ conquest_acceptance=conflict_detect(acceptance_json,filled_acceptance)
756
+ conquest_obs=conflict_detect(obs_json,filled_obs)
757
+ # st.write(conquest_bizobj)
758
+ st.write("Executive summary ....")
759
+
760
+ st.write("Creating Question Email...")
761
+ totquestions=conquest_obs+conquest_bizobj+conquest_prodvar+conquest_material_handling+conquest_software+conquest_customer_dependency+conquest_acceptance
762
+ final_json=filled_bizobj+filled_prodvar+filled_material_handling+filled_software+filled_customer_dependency+filled_acceptance+filled_obs
763
+ exec_summ=conflict_summary(totquestions)
764
+ # exec_summ=executive_summary(totquestions)
765
  st.write(exec_summ)
766
+ # final_json=filled_bizobj+filled_prodvar+filled_material_handling+filled_software+filled_customer_dependency+filled_acceptance+filled_obs
767
+ exec_summ=executive_summary_complete(final_json)
768
+ st.write(exec_summ)
769
+ json_strings = {
770
+ "BIZ_OBS_JSON": filled_bizobj,
771
+ "PROD_VAR_INFO_JSON": filled_prodvar,
772
+ "CUSTOMER_DEPENDENCY_JSON": filled_customer_dependency,
773
+ "MATERIAL_HANDLING_JSON": filled_material_handling,
774
+ "SOFTWARE_JSON": filled_software,
775
+ "ACCEPTANCE_JSON": filled_acceptance,
776
+ "OBS_JSON": filled_obs
777
+ }
778
+ airtable_write_main(json_strings,unique_id)
779
+
780
+
781
+
782
+ def main():
783
+ st.title("Qualitas Sales Chatbot")
784
+
785
+ col1, col2 = st.columns(2)
786
+
787
+ with col1:
788
+ new_customer = st.button("New Customer")
789
+ with col2:
790
+ registered_customer = st.button("Registered Customer")
791
+
792
+ if new_customer:
793
+ st.session_state.workflow = "new"
794
+ st.session_state.step = "id_input"
795
+ elif registered_customer:
796
+ st.session_state.workflow = "registered"
797
+ st.session_state.step = "id_input"
798
+
799
+ if "workflow" in st.session_state:
800
+ if st.session_state.step == "id_input":
801
+ st.write("Please Enter a Unique Identifier:")
802
+ unique_id = st.chat_input("Please enter a unique identifier:")
803
+ if unique_id:
804
+ st.session_state.unique_id = unique_id
805
+ st.session_state.step = "processing"
806
+ st.rerun()
807
+
808
+ if st.session_state.step == "processing":
809
+ if st.session_state.workflow == "new":
810
+ process_new_customer(st.session_state.unique_id)
811
+
812
+ else:
813
+ process_registered_customer(st.session_state.unique_id)
814
+
815
+ # st.session_state.clear()
816
 
817
  if __name__ == "__main__":
818
  main()