budhadityac24 commited on
Commit
ac764a6
1 Parent(s): 048e38b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +124 -16
app.py CHANGED
@@ -105,7 +105,7 @@ def bizobjjsoncreate(json_template,text):
105
  messages=[
106
  {
107
  "role": "system",
108
- "content": "You are a helpful classification assistant. You understand engineering concepts. You will be given a JSON where there are properties and their descriptions. You need to fill up the JSON subproperty \"USer Answer\" from the details given in the text. If you are not sure of any field, leave the \"User Answer\" as TBD. Give the JSON output with the filled fields only. ENSURE THE JSON IS VALID AND PROPERLY FORMATTED. DO NOT OUTPUT ANYTHING OTHER THAN THE JSON."
109
  },
110
  {
111
  "role": "user",
@@ -149,7 +149,7 @@ def question_create(json_template):
149
  for chunk in completion:
150
  answer += chunk.choices[0].delta.content or ""
151
 
152
-
153
  client = Groq()
154
  completion = client.chat.completions.create(
155
  model="llama-3.1-70b-versatile",
@@ -163,8 +163,8 @@ def question_create(json_template):
163
  "content": answer
164
  }
165
  ],
166
- temperature=0.73,
167
- max_tokens=2240,
168
  top_p=1,
169
  stream=True,
170
  stop=None,
@@ -175,7 +175,60 @@ 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(
@@ -200,9 +253,13 @@ def answer_refill(questions,answers,obs_json_template,bizobj_json_template):
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
  completion2 = client.chat.completions.create(
207
  model="llama-3.1-70b-versatile",
208
  messages=[
@@ -212,7 +269,7 @@ def answer_refill(questions,answers,obs_json_template,bizobj_json_template):
212
  },
213
  {
214
  "role": "user",
215
- "content": "Question Answer:"+str(qapair)+"\nJSON:\n"+str(obs_json_template+bizobj_json_template)
216
  }
217
  ],
218
  temperature=1,
@@ -227,7 +284,6 @@ def answer_refill(questions,answers,obs_json_template,bizobj_json_template):
227
  # print(filled_json)
228
  return filled_json
229
 
230
-
231
  def executive_summary(json_template):
232
 
233
 
@@ -339,6 +395,8 @@ def airtable_write(json_template):
339
 
340
 
341
  def main():
 
 
342
  st.title("Qualitas Sales Data Collection Chatbot")
343
  st.caption("Welcome to the Qualitas Bot. First upload a PDF document which should be customer correspondence, detailing some requirements. Also sometimes the Submit button for the questions is a bit sticky. So You might have to click it twice!")
344
 
@@ -381,18 +439,55 @@ def process_document(uploaded_file):
381
  obs_json_template = json.load(file)
382
  final_obs_json = obsjsoncreate(obs_json_template, st.session_state.classification_result, st.session_state.text)
383
  st.session_state.obs = final_obs_json
384
- json_path='BizObjJSON.json'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
385
  with open(json_path, 'r') as file:
386
- bizobj_json_template = json.load(file)
387
- final_bizobj_json = bizobjjsoncreate(bizobj_json_template, st.session_state.text)
 
 
 
 
 
 
388
  st.session_state.bizobj = final_bizobj_json
 
 
 
 
 
389
  st.write("Creating Questions...")
390
  questionobs = question_create(final_obs_json)
391
- questionbizobj = question_create(final_bizobj_json)
 
 
 
 
 
392
  while True:
393
  try:
394
  # Attempt to evaluate the expressions and assign them to session state
395
- st.session_state.questions = ast.literal_eval(questionbizobj) + ast.literal_eval(questionobs)
396
  # If successful, break out of the loop
397
  break
398
  except Exception as e:
@@ -446,11 +541,24 @@ def show_question():
446
  # with st.chat_message("assistant"):
447
  # st.subheader("Answers")
448
  # st.write(answers)
449
-
450
- completed_json = answer_refill(st.session_state.questions, answers, st.session_state.obs, st.session_state.bizobj)
 
 
 
 
 
 
 
451
  # st.write(completed_json)
452
- airtable_write(completed_json)
453
- exec_summ = executive_summary(completed_json)
 
 
 
 
 
 
454
  st.write(exec_summ)
455
 
456
  if __name__ == "__main__":
 
105
  messages=[
106
  {
107
  "role": "system",
108
+ "content": "You are a helpful classification assistant. You understand engineering concepts. You will be given a JSON where there are properties and their descriptions. You need to fill up the JSON subproperty \"USer Answer\" from the details given in the text. If you are not sure of any field, leave the \"User Answer\" as TBD. Make sure you dont leave out a single field of the JSON. ENSURE THE JSON IS VALID AND PROPERLY FORMATTED. DO NOT OUTPUT ANYTHING OTHER THAN THE JSON."
109
  },
110
  {
111
  "role": "user",
 
149
  for chunk in completion:
150
  answer += chunk.choices[0].delta.content or ""
151
 
152
+ # print(answer)
153
  client = Groq()
154
  completion = client.chat.completions.create(
155
  model="llama-3.1-70b-versatile",
 
163
  "content": answer
164
  }
165
  ],
166
+ temperature=0.45,
167
+ max_tokens=4240,
168
  top_p=1,
169
  stream=True,
170
  stop=None,
 
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(
 
253
  qapair = ""
254
  for chunk in completion:
255
  qapair += chunk.choices[0].delta.content or ""
256
+ return qapair
257
  # print(qapair)
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(
264
  model="llama-3.1-70b-versatile",
265
  messages=[
 
269
  },
270
  {
271
  "role": "user",
272
+ "content": "Question Answer:"+str(qapair)+"\nJSON:\n"+str(json_template)
273
  }
274
  ],
275
  temperature=1,
 
284
  # print(filled_json)
285
  return filled_json
286
 
 
287
  def executive_summary(json_template):
288
 
289
 
 
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.title("Qualitas Sales Data Collection Chatbot")
401
  st.caption("Welcome to the Qualitas Bot. First upload a PDF document which should be customer correspondence, detailing some requirements. Also sometimes the Submit button for the questions is a bit sticky. So You might have to click it twice!")
402
 
 
439
  obs_json_template = json.load(file)
440
  final_obs_json = obsjsoncreate(obs_json_template, st.session_state.classification_result, st.session_state.text)
441
  st.session_state.obs = final_obs_json
442
+
443
+ json_path='Biz_Obj.json'
444
+ with open(json_path, 'r') as file:
445
+ bizobj_json = json.load(file)
446
+ final_bizobj_json = bizobjjsoncreate(bizobj_json, st.session_state.text)
447
+
448
+ json_path='Prod_var_info.json'
449
+ with open(json_path, 'r') as file:
450
+ prodvarinfo_json = json.load(file)
451
+ final_prodvarinfo_json = bizobjjsoncreate(prodvarinfo_json, st.session_state.text)
452
+
453
+ json_path='Material_handling.json'
454
+ with open(json_path, 'r') as file:
455
+ materialhandling_json = json.load(file)
456
+ final_materialhandling_json = bizobjjsoncreate(materialhandling_json, st.session_state.text)
457
+
458
+ json_path='software.json'
459
+ with open(json_path, 'r') as file:
460
+ software_json = json.load(file)
461
+ final_software_json = bizobjjsoncreate(software_json, st.session_state.text)
462
+
463
+ json_path='Customer_dependency.json'
464
  with open(json_path, 'r') as file:
465
+ customerdependency_json = json.load(file)
466
+ final_customerdependency_json = bizobjjsoncreate(customerdependency_json, st.session_state.text)
467
+
468
+ json_path='acceptance.json'
469
+ with open(json_path, 'r') as file:
470
+ acceptance_json = json.load(file)
471
+ final_acceptance_json = bizobjjsoncreate(acceptance_json, st.session_state.text)
472
+
473
  st.session_state.bizobj = final_bizobj_json
474
+ st.session_state.prodvarinfo = final_prodvarinfo_json
475
+ st.session_state.materialhandling = final_materialhandling_json
476
+ st.session_state.software = final_software_json
477
+ st.session_state.customerdependency = final_customerdependency_json
478
+ st.session_state.acceptance = final_acceptance_json
479
  st.write("Creating Questions...")
480
  questionobs = question_create(final_obs_json)
481
+ question_bizobj = question_create(final_bizobj_json)
482
+ question_prodvarinfo = question_create(final_prodvarinfo_json)
483
+ question_materialhandling = question_create(final_materialhandling_json)
484
+ question_software = question_create(final_software_json)
485
+ question_customerdependency = question_create(final_customerdependency_json)
486
+ question_acceptance = question_create(final_acceptance_json)
487
  while True:
488
  try:
489
  # Attempt to evaluate the expressions and assign them to session state
490
+ 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)
491
  # If successful, break out of the loop
492
  break
493
  except Exception as e:
 
541
  # with st.chat_message("assistant"):
542
  # st.subheader("Answers")
543
  # st.write(answers)
544
+ qap=qapair_create(st.session_state.questions,answers)
545
+ # completed_obs = answer_refill(st.session_state.questions, answers, st.session_state.obs, st.session_state.bizobj)
546
+ completed_obs= answer_refill(qap,st.session_state.obs)
547
+ completed_bizobj= answer_refill(qap,st.session_state.bizobj)
548
+ completed_prodvarinfo= answer_refill(qap,st.session_state.prodvarinfo)
549
+ completed_materialhandling= answer_refill(qap,st.session_state.materialhandling)
550
+ completed_software= answer_refill(qap,st.session_state.software)
551
+ completed_customerdependency= answer_refill(qap,st.session_state.customerdependency)
552
+ completed_acceptance= answer_refill(qap,st.session_state.acceptance)
553
  # st.write(completed_json)
554
+ airtable_write(completed_obs)
555
+ airtable_write(completed_bizobj)
556
+ airtable_write(completed_prodvarinfo)
557
+ airtable_write(completed_materialhandling)
558
+ airtable_write(completed_software)
559
+ airtable_write(completed_customerdependency)
560
+ airtable_write(completed_acceptance)
561
+ exec_summ = executive_summary(str(completed_obs+completed_bizobj+completed_prodvarinfo+completed_materialhandling+completed_software+completed_customerdependency+completed_acceptance))
562
  st.write(exec_summ)
563
 
564
  if __name__ == "__main__":