yuki.tsutsumi commited on
Commit
92fa0d1
1 Parent(s): 6d3e7b4

blackで理フォーマット。

Browse files
Files changed (3) hide show
  1. main.py +3 -1
  2. src/forms.py +113 -39
  3. src/utils.py +6 -2
main.py CHANGED
@@ -12,7 +12,9 @@ pages = {
12
  "逸脱報告書": deviation_report_form,
13
  }
14
 
15
- selected_page = st.sidebar.selectbox("作成する文書を選択してください。", list(pages.keys()))
 
 
16
  match selected_page:
17
  case "変更申請書":
18
  pages["変更申請書"]()
 
12
  "逸脱報告書": deviation_report_form,
13
  }
14
 
15
+ selected_page = st.sidebar.selectbox(
16
+ "作成する文書を選択してください。", list(pages.keys())
17
+ )
18
  match selected_page:
19
  case "変更申請書":
20
  pages["変更申請書"]()
src/forms.py CHANGED
@@ -214,7 +214,9 @@ def change_request_form() -> None:
214
  change_number = int(st.session_state["change_num"])
215
 
216
  start_from_one: int = 1
217
- for change_serial_number in range(start_from_one, change_number + upper_bound_correction):
 
 
218
  if f"type_{change_serial_number}" not in st.session_state:
219
  st.session_state[f"type_{change_serial_number}"] = ""
220
  if f"category_{change_serial_number}" not in st.session_state:
@@ -223,7 +225,9 @@ def change_request_form() -> None:
223
  st.session_state[f"subcategory_{change_serial_number}"] = ""
224
 
225
  if st.session_state["complete_common_questions"]:
226
- for change_serial_number in range(start_from_one, change_number + upper_bound_correction):
 
 
227
  if change_number != minimum_change_number:
228
  st.markdown(f"**{change_serial_number}つ目の変更**")
229
  st.selectbox(
@@ -270,7 +274,9 @@ def change_request_form() -> None:
270
  st.session_state[f"category_{change_serial_number}"] != ""
271
  and st.session_state[f"subcategory_{change_number}"] != ""
272
  ):
273
- for change_serial_number in range(start_from_one, change_number + upper_bound_correction):
 
 
274
  question_about_this_change = [
275
  f"Q{change_serial_number}-1 : 今回の変更内容を教えて下さい。変更前と変更後の両方を明記してください。",
276
  f"Q{change_serial_number}-2 : 今回の変更を行う理由を教えて下さい。",
@@ -291,25 +297,48 @@ def change_request_form() -> None:
291
  + " : その他、変更による品質への影響を考慮すべき点はありますか?考慮すべき点があれば、その理由とともに説明してください。"
292
  )
293
  questions_of_all_changes.append(question_about_this_change)
294
- logger.debug(f"{change_serial_number}つ目の変更に関する質問: " + ", ".join(question_about_this_change))
 
 
 
295
 
296
- for change_serial_number in range(start_from_one, change_number + upper_bound_correction):
297
- for question_index in range(len(questions_of_all_changes[change_serial_number - 1])):
298
- if f"answer_for_question{question_index+1}_of_change{change_serial_number}" not in st.session_state:
299
- st.session_state[f"answer_for_question{question_index}_of_change{change_serial_number}"] = ""
300
- if f"no_answer_for_question{question_index+1}_of_change{change_serial_number}" not in st.session_state:
301
- st.session_state[f"no_answer_for_question{question_index}_of_change{change_serial_number}"] = False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
302
 
303
  if "contents" not in st.session_state:
304
  st.session_state["contents"] = {}
305
 
306
  with st.form(key="individual_questions_form"):
307
- for change_serial_number in range(start_from_one, change_number + upper_bound_correction):
 
 
308
  if change_number != minimum_change_number:
309
  st.markdown(f"**{change_serial_number}つ目の変更**")
310
- for question_index in range(len(questions_of_all_changes[change_serial_number - 1])):
 
 
311
  st.text_area(
312
- questions_of_all_changes[change_serial_number - 1][question_index],
 
 
313
  key=f"answer_for_question{question_index+1}_of_change{change_serial_number}",
314
  )
315
  st.checkbox(
@@ -329,11 +358,21 @@ def change_request_form() -> None:
329
  open_api_key = os.environ.get("OPENAI_API_KEY")
330
  client = OpenAI(api_key=open_api_key)
331
  if submitted:
332
- system_prompt = system_template_review + systemprompt_template_1 + systemprompt_template_2
 
 
 
 
333
  user_prompt = ""
334
- for change_serial_number in range(start_from_one, change_number + upper_bound_correction):
335
- for question_index in range(len(questions_of_all_changes[change_serial_number - 1])):
336
- question: str = questions_of_all_changes[change_serial_number - 1][question_index]
 
 
 
 
 
 
337
  answer = ""
338
  if st.session_state[
339
  f"no_answer_for_question{question_index+1}_of_change{change_serial_number}"
@@ -362,9 +401,13 @@ def change_request_form() -> None:
362
  response_format={"type": "json_object"},
363
  temperature=0.0,
364
  )
365
- logger.info("OpenAIからのレスポンス(変更申請書作成用): " + str(response))
 
 
366
  response_text = response.choices[0].message.content
367
- response_data = json.loads(response_text) if response_text is not None else {}
 
 
368
 
369
  application_date = st.session_state["application_date"].isoformat()
370
  date_obj = datetime.strptime(application_date, "%Y-%m-%d")
@@ -382,7 +425,10 @@ def change_request_form() -> None:
382
  "添付資料": attached_files,
383
  "備考": supplementary_information,
384
  }
385
- logger.info("wordテンプレート変数の置換用のjson: " + str(st.session_state["contents"]))
 
 
 
386
 
387
  output_change_application(st.session_state["contents"])
388
 
@@ -402,10 +448,14 @@ def change_request_form() -> None:
402
  st.markdown(f'**申請者** : {st.session_state["contents"]["申請者"]}')
403
  st.markdown(f'**申請日** : {st.session_state["contents"]["申請日"]}')
404
  st.markdown(f'**変更対象** : {st.session_state["contents"]["変更対象"]}')
405
- st.markdown(f'**実施予定時期** : {st.session_state["contents"]["実施予定時期"]}')
 
 
406
  st.markdown(f'**変更内容** : {st.session_state["contents"]["変更内容"]}')
407
  st.markdown(f'**変更理由** : {st.session_state["contents"]["変更理由"]}')
408
- st.markdown(f'**品質への影響** : {st.session_state["contents"]["品質への影響"]}')
 
 
409
  st.markdown(f'**添付資料** : {st.session_state["contents"]["添付資料"]}')
410
  st.markdown(f'**備考** : {st.session_state["contents"]["備考"]}')
411
 
@@ -416,7 +466,9 @@ def change_request_form() -> None:
416
  query_vec = get_embedding(client, query_content)
417
 
418
  genres = [""] * change_number
419
- for change_serial_number in range(start_from_one, change_number + upper_bound_correction):
 
 
420
  match st.session_state[f"category_{change_serial_number}"]:
421
  case "充填設備の変更":
422
  genres[change_serial_number - 1] = "充填機変更"
@@ -469,7 +521,11 @@ def change_request_form() -> None:
469
  ],
470
  temperature=0.7,
471
  )
472
- review = completion.choices[0].message.content if completion.choices[0].message.content is not None else ""
 
 
 
 
473
  logger.info("レビュー結果: " + review)
474
  st.markdown(f"**レビュー結果** : {review}")
475
 
@@ -564,7 +620,9 @@ def deviation_occurrence_report_form() -> None:
564
  "Q15 : 備考があれば記入してください。",
565
  key="supplementary_information_deviation_occurrence",
566
  )
567
- no_supplementary_information = st.checkbox("なし", key="check_supplementary_information")
 
 
568
  submitted = st.form_submit_button("送信")
569
 
570
  if submitted or st.session_state["document_generated"]:
@@ -600,7 +658,9 @@ def deviation_occurrence_report_form() -> None:
600
  )
601
  response_text = response.choices[0].message.content
602
 
603
- response_data = json.loads(response_text) if response_text is not None else {}
 
 
604
 
605
  if isinstance(report_date, (date, datetime)):
606
  formatted_date = report_date.strftime("%Y年%m月%d日")
@@ -608,11 +668,13 @@ def deviation_occurrence_report_form() -> None:
608
  formatted_date = "報告日が指定されていません。"
609
 
610
  if isinstance(occurrence_date, date) and isinstance(occurrence_time, time):
611
- occurrence_datetime = datetime.combine(occurrence_date, occurrence_time).strftime(
612
- "%Y年%m月%d日%H時%M分"
613
- )
614
  else:
615
- st.error("発生日または発生時間の入力が無効です。システムエラーのため、管理者に問い合わせてください。")
 
 
616
 
617
  contents = {
618
  "報告者": reporter_name,
@@ -655,7 +717,9 @@ def deviation_occurrence_report_form() -> None:
655
  st.markdown(f'**作業者** : {contents["作業者"]}')
656
  st.markdown(f'**逸脱内容・発見の経緯** : {contents["逸脱内容・発見の経緯"]}')
657
  st.markdown(f'**応急処置・処置の理由** : {contents["応急処置・処置の理由"]}')
658
- st.markdown(f'**品質への影響の調査状況** : {contents["品質への影響の調査状況"]}')
 
 
659
  st.markdown(f'**添付資料** : {contents["添付資料"]}')
660
  st.markdown(f'**備考** : {contents["備考"]}')
661
 
@@ -740,7 +804,9 @@ def deviation_report_form() -> None:
740
  "Q8 : 出荷の制限について記入してください。",
741
  key="shipping_restriction_deviation",
742
  )
743
- no_shipping_restriction = st.checkbox("出荷の制限なし", key="check_shipping_restriction")
 
 
744
  attached_files = st.text_area(
745
  "Q9 : 添付資料名を記入してください。",
746
  key="attached_files_deviation",
@@ -750,7 +816,9 @@ def deviation_report_form() -> None:
750
  "Q10 : 備考があれば記入してください。",
751
  key="supplementary_information_deviation",
752
  )
753
- no_supplementary_information = st.checkbox("なし", key="check_supplementary_information")
 
 
754
  submitted = st.form_submit_button("送信")
755
 
756
  if submitted or st.session_state["document_generated"]:
@@ -788,7 +856,9 @@ def deviation_report_form() -> None:
788
  response_text = response.choices[0].message.content
789
 
790
  # 応答をjsonに変換
791
- response_data = json.loads(response_text) if response_text is not None else {}
 
 
792
 
793
  if isinstance(report_date, (date, datetime)):
794
  formatted_date = report_date.strftime("%Y年%m月%d日")
@@ -796,11 +866,13 @@ def deviation_report_form() -> None:
796
  formatted_date = "報告日が指定されていません。"
797
 
798
  if isinstance(occurrence_date, date) and isinstance(occurrence_time, time):
799
- occurrence_datetime = datetime.combine(occurrence_date, occurrence_time).strftime(
800
- "%Y年%m月%d日%H時%M分"
801
- )
802
  else:
803
- st.error("発生日または発生時間の入力が無効です。システムエラーのため、管理者に問い合わせてください。")
 
 
804
 
805
  # jsonデータを作成
806
  contents = {
@@ -842,7 +914,9 @@ def deviation_report_form() -> None:
842
  st.markdown(f'**発生場所** : {contents["発生場所"]}')
843
  st.markdown(f'**逸脱内容・発見の経緯** : {contents["逸脱内容・発見の経緯"]}')
844
  st.markdown(f'**応急処置・処置の理由** : {contents["応急処置・処置の理由"]}')
845
- st.markdown(f'**品質への影響の調査結果** : {contents["品質への影響の調査結果"]}')
 
 
846
  st.markdown(f'**原因調査結果** : {contents["原因調査結果"]}')
847
  st.markdown(f'**是正措置・予防措置** : {contents["是正措置・予防措置"]}')
848
  st.markdown(f'**出荷の制限** : {contents["出荷の制限"]}')
 
214
  change_number = int(st.session_state["change_num"])
215
 
216
  start_from_one: int = 1
217
+ for change_serial_number in range(
218
+ start_from_one, change_number + upper_bound_correction
219
+ ):
220
  if f"type_{change_serial_number}" not in st.session_state:
221
  st.session_state[f"type_{change_serial_number}"] = ""
222
  if f"category_{change_serial_number}" not in st.session_state:
 
225
  st.session_state[f"subcategory_{change_serial_number}"] = ""
226
 
227
  if st.session_state["complete_common_questions"]:
228
+ for change_serial_number in range(
229
+ start_from_one, change_number + upper_bound_correction
230
+ ):
231
  if change_number != minimum_change_number:
232
  st.markdown(f"**{change_serial_number}つ目の変更**")
233
  st.selectbox(
 
274
  st.session_state[f"category_{change_serial_number}"] != ""
275
  and st.session_state[f"subcategory_{change_number}"] != ""
276
  ):
277
+ for change_serial_number in range(
278
+ start_from_one, change_number + upper_bound_correction
279
+ ):
280
  question_about_this_change = [
281
  f"Q{change_serial_number}-1 : 今回の変更内容を教えて下さい。変更前と変更後の両方を明記してください。",
282
  f"Q{change_serial_number}-2 : 今回の変更を行う理由を教えて下さい。",
 
297
  + " : その他、変更による品質への影響を考慮すべき点はありますか?考慮すべき点があれば、その理由とともに説明してください。"
298
  )
299
  questions_of_all_changes.append(question_about_this_change)
300
+ logger.debug(
301
+ f"{change_serial_number}つ目の変更に関する質問: "
302
+ + ", ".join(question_about_this_change)
303
+ )
304
 
305
+ for change_serial_number in range(
306
+ start_from_one, change_number + upper_bound_correction
307
+ ):
308
+ for question_index in range(
309
+ len(questions_of_all_changes[change_serial_number - 1])
310
+ ):
311
+ if (
312
+ f"answer_for_question{question_index+1}_of_change{change_serial_number}"
313
+ not in st.session_state
314
+ ):
315
+ st.session_state[
316
+ f"answer_for_question{question_index}_of_change{change_serial_number}"
317
+ ] = ""
318
+ if (
319
+ f"no_answer_for_question{question_index+1}_of_change{change_serial_number}"
320
+ not in st.session_state
321
+ ):
322
+ st.session_state[
323
+ f"no_answer_for_question{question_index}_of_change{change_serial_number}"
324
+ ] = False
325
 
326
  if "contents" not in st.session_state:
327
  st.session_state["contents"] = {}
328
 
329
  with st.form(key="individual_questions_form"):
330
+ for change_serial_number in range(
331
+ start_from_one, change_number + upper_bound_correction
332
+ ):
333
  if change_number != minimum_change_number:
334
  st.markdown(f"**{change_serial_number}つ目の変更**")
335
+ for question_index in range(
336
+ len(questions_of_all_changes[change_serial_number - 1])
337
+ ):
338
  st.text_area(
339
+ questions_of_all_changes[change_serial_number - 1][
340
+ question_index
341
+ ],
342
  key=f"answer_for_question{question_index+1}_of_change{change_serial_number}",
343
  )
344
  st.checkbox(
 
358
  open_api_key = os.environ.get("OPENAI_API_KEY")
359
  client = OpenAI(api_key=open_api_key)
360
  if submitted:
361
+ system_prompt = (
362
+ system_template_review
363
+ + systemprompt_template_1
364
+ + systemprompt_template_2
365
+ )
366
  user_prompt = ""
367
+ for change_serial_number in range(
368
+ start_from_one, change_number + upper_bound_correction
369
+ ):
370
+ for question_index in range(
371
+ len(questions_of_all_changes[change_serial_number - 1])
372
+ ):
373
+ question: str = questions_of_all_changes[
374
+ change_serial_number - 1
375
+ ][question_index]
376
  answer = ""
377
  if st.session_state[
378
  f"no_answer_for_question{question_index+1}_of_change{change_serial_number}"
 
401
  response_format={"type": "json_object"},
402
  temperature=0.0,
403
  )
404
+ logger.info(
405
+ "OpenAIからのレスポンス(変更申請書作成用): " + str(response)
406
+ )
407
  response_text = response.choices[0].message.content
408
+ response_data = (
409
+ json.loads(response_text) if response_text is not None else {}
410
+ )
411
 
412
  application_date = st.session_state["application_date"].isoformat()
413
  date_obj = datetime.strptime(application_date, "%Y-%m-%d")
 
425
  "添付資料": attached_files,
426
  "備考": supplementary_information,
427
  }
428
+ logger.info(
429
+ "wordテンプレート変数の置換用のjson: "
430
+ + str(st.session_state["contents"])
431
+ )
432
 
433
  output_change_application(st.session_state["contents"])
434
 
 
448
  st.markdown(f'**申請者** : {st.session_state["contents"]["申請者"]}')
449
  st.markdown(f'**申請日** : {st.session_state["contents"]["申請日"]}')
450
  st.markdown(f'**変更対象** : {st.session_state["contents"]["変更対象"]}')
451
+ st.markdown(
452
+ f'**実施予定時期** : {st.session_state["contents"]["実施予定時期"]}'
453
+ )
454
  st.markdown(f'**変更内容** : {st.session_state["contents"]["変更内容"]}')
455
  st.markdown(f'**変更理由** : {st.session_state["contents"]["変更理由"]}')
456
+ st.markdown(
457
+ f'**品質への影響** : {st.session_state["contents"]["品質への影響"]}'
458
+ )
459
  st.markdown(f'**添付資料** : {st.session_state["contents"]["添付資料"]}')
460
  st.markdown(f'**備考** : {st.session_state["contents"]["備考"]}')
461
 
 
466
  query_vec = get_embedding(client, query_content)
467
 
468
  genres = [""] * change_number
469
+ for change_serial_number in range(
470
+ start_from_one, change_number + upper_bound_correction
471
+ ):
472
  match st.session_state[f"category_{change_serial_number}"]:
473
  case "充填設備の変更":
474
  genres[change_serial_number - 1] = "充填機変更"
 
521
  ],
522
  temperature=0.7,
523
  )
524
+ review = (
525
+ completion.choices[0].message.content
526
+ if completion.choices[0].message.content is not None
527
+ else ""
528
+ )
529
  logger.info("レビュー結果: " + review)
530
  st.markdown(f"**レビュー結果** : {review}")
531
 
 
620
  "Q15 : 備考があれば記入してください。",
621
  key="supplementary_information_deviation_occurrence",
622
  )
623
+ no_supplementary_information = st.checkbox(
624
+ "なし", key="check_supplementary_information"
625
+ )
626
  submitted = st.form_submit_button("送信")
627
 
628
  if submitted or st.session_state["document_generated"]:
 
658
  )
659
  response_text = response.choices[0].message.content
660
 
661
+ response_data = (
662
+ json.loads(response_text) if response_text is not None else {}
663
+ )
664
 
665
  if isinstance(report_date, (date, datetime)):
666
  formatted_date = report_date.strftime("%Y年%m月%d日")
 
668
  formatted_date = "報告日が指定されていません。"
669
 
670
  if isinstance(occurrence_date, date) and isinstance(occurrence_time, time):
671
+ occurrence_datetime = datetime.combine(
672
+ occurrence_date, occurrence_time
673
+ ).strftime("%Y年%m月%d日%H時%M分")
674
  else:
675
+ st.error(
676
+ "発生日または発生時間の入力が無効です。システムエラーのため、管理者に問い合わせてください。"
677
+ )
678
 
679
  contents = {
680
  "報告者": reporter_name,
 
717
  st.markdown(f'**作業者** : {contents["作業者"]}')
718
  st.markdown(f'**逸脱内容・発見の経緯** : {contents["逸脱内容・発見の経緯"]}')
719
  st.markdown(f'**応急処置・処置の理由** : {contents["応急処置・処置の理由"]}')
720
+ st.markdown(
721
+ f'**品質への影響の調査状況** : {contents["品質への影響の調査状況"]}'
722
+ )
723
  st.markdown(f'**添付資料** : {contents["添付資料"]}')
724
  st.markdown(f'**備考** : {contents["備考"]}')
725
 
 
804
  "Q8 : 出荷の制限について記入してください。",
805
  key="shipping_restriction_deviation",
806
  )
807
+ no_shipping_restriction = st.checkbox(
808
+ "出荷の制限なし", key="check_shipping_restriction"
809
+ )
810
  attached_files = st.text_area(
811
  "Q9 : 添付資料名を記入してください。",
812
  key="attached_files_deviation",
 
816
  "Q10 : 備考があれば記入してください。",
817
  key="supplementary_information_deviation",
818
  )
819
+ no_supplementary_information = st.checkbox(
820
+ "なし", key="check_supplementary_information"
821
+ )
822
  submitted = st.form_submit_button("送信")
823
 
824
  if submitted or st.session_state["document_generated"]:
 
856
  response_text = response.choices[0].message.content
857
 
858
  # 応答をjsonに変換
859
+ response_data = (
860
+ json.loads(response_text) if response_text is not None else {}
861
+ )
862
 
863
  if isinstance(report_date, (date, datetime)):
864
  formatted_date = report_date.strftime("%Y年%m月%d日")
 
866
  formatted_date = "報告日が指定されていません。"
867
 
868
  if isinstance(occurrence_date, date) and isinstance(occurrence_time, time):
869
+ occurrence_datetime = datetime.combine(
870
+ occurrence_date, occurrence_time
871
+ ).strftime("%Y年%m月%d日%H時%M分")
872
  else:
873
+ st.error(
874
+ "発生日または発生時間の入力が無効です。システムエラーのため、管理者に問い合わせてください。"
875
+ )
876
 
877
  # jsonデータを作成
878
  contents = {
 
914
  st.markdown(f'**発生場所** : {contents["発生場所"]}')
915
  st.markdown(f'**逸脱内容・発見の経緯** : {contents["逸脱内容・発見の経緯"]}')
916
  st.markdown(f'**応急処置・処置の理由** : {contents["応急処置・処置の理由"]}')
917
+ st.markdown(
918
+ f'**品質への影響の調査結果** : {contents["品質への影響の調査結果"]}'
919
+ )
920
  st.markdown(f'**原因調査結果** : {contents["原因調査結果"]}')
921
  st.markdown(f'**是正措置・予防措置** : {contents["是正措置・予防措置"]}')
922
  st.markdown(f'**出荷の制限** : {contents["出荷の制限"]}')
src/utils.py CHANGED
@@ -26,7 +26,9 @@ def load_env_vars() -> None:
26
  load_dotenv()
27
 
28
 
29
- def get_embedding(client: OpenAI, text: str, model: str = "text-embedding-3-large") -> List[float]:
 
 
30
  text = text.replace("\n", " ")
31
  return client.embeddings.create(input=[text], model=model).data[0].embedding
32
 
@@ -171,7 +173,9 @@ def replace_text_in_tables(tables: List[Table], replacements: Dict[str, str]) ->
171
  replace_text_in_paragraphs(cell.paragraphs, replacements)
172
 
173
 
174
- def replace_text_in_paragraphs(paragraphs: List[Paragraph], replacements: Dict[str, str]) -> None:
 
 
175
  """
176
  パラグラフ内のテキストを置き換える
177
  """
 
26
  load_dotenv()
27
 
28
 
29
+ def get_embedding(
30
+ client: OpenAI, text: str, model: str = "text-embedding-3-large"
31
+ ) -> List[float]:
32
  text = text.replace("\n", " ")
33
  return client.embeddings.create(input=[text], model=model).data[0].embedding
34
 
 
173
  replace_text_in_paragraphs(cell.paragraphs, replacements)
174
 
175
 
176
+ def replace_text_in_paragraphs(
177
+ paragraphs: List[Paragraph], replacements: Dict[str, str]
178
+ ) -> None:
179
  """
180
  パラグラフ内のテキストを置き換える
181
  """