yuki.tsutsumi commited on
Commit
2838414
1 Parent(s): 9730e4b

embed_output_deviation_occurrenceファイルも消去し、utilへ移動。

Browse files
Files changed (4) hide show
  1. main.py +1 -3
  2. src/embed_output_deviation_occurrence.py +0 -76
  3. src/forms.py +20 -26
  4. src/utils.py +38 -1
main.py CHANGED
@@ -1,8 +1,6 @@
1
  import streamlit as st
2
 
3
  from src.embed_output_deviation import main as embed_output_deviation
4
- from src.embed_output_deviation_occurrence import \
5
- main as embed_output_deviation_occurrence
6
  from src.forms import (change_request_form, deviation_occurrence_report_form,
7
  deviation_report_form)
8
 
@@ -19,6 +17,6 @@ match selected_page:
19
  case "変更申請書":
20
  pages["変更申請書"]()
21
  case "逸脱発生報告書":
22
- pages["逸脱発生報告書"](embed_output_deviation_occurrence)
23
  case "逸脱報告書":
24
  pages["逸脱報告書"](embed_output_deviation)
 
1
  import streamlit as st
2
 
3
  from src.embed_output_deviation import main as embed_output_deviation
 
 
4
  from src.forms import (change_request_form, deviation_occurrence_report_form,
5
  deviation_report_form)
6
 
 
17
  case "変更申請書":
18
  pages["変更申請書"]()
19
  case "逸脱発生報告書":
20
+ pages["逸脱発生報告書"]()
21
  case "逸脱報告書":
22
  pages["逸脱報告書"](embed_output_deviation)
src/embed_output_deviation_occurrence.py DELETED
@@ -1,76 +0,0 @@
1
- import json
2
-
3
- from docx import Document
4
-
5
-
6
- def replace_text_in_paragraphs(paragraphs, replacements):
7
- """
8
- パラグラフ内のテキストを置き換える
9
- """
10
- for paragraph in paragraphs:
11
- for key, value in replacements.items():
12
- if key in paragraph.text:
13
- paragraph.text = paragraph.text.replace(key, value)
14
-
15
-
16
- def replace_text_in_tables(tables, replacements):
17
- """
18
- テーブル内のテキストを置き換える
19
- """
20
- for table in tables:
21
- for row in table.rows:
22
- for cell in row.cells:
23
- replace_text_in_paragraphs(cell.paragraphs, replacements)
24
-
25
-
26
- def replace_text_in_docx(template_path, replacements):
27
- """
28
- ドキュメント内の指定されたテキストを置き換える
29
- """
30
- doc = Document(template_path)
31
- replace_text_in_paragraphs(doc.paragraphs, replacements)
32
- replace_text_in_tables(doc.tables, replacements)
33
- return doc
34
-
35
-
36
- def main():
37
- template_path = "data/template/template_deviation_occurrence.docx"
38
-
39
- with open("data/deviation_occurrence_report.json", "r") as file:
40
- data = json.load(file)
41
-
42
- if data["添付資料"] == "なし":
43
- yes = "□"
44
- no = "☑"
45
- attached_file = ""
46
- else:
47
- yes = "☑"
48
- no = "□"
49
- attached_file = data["添付資料"]
50
-
51
- replacements = {
52
- "[reporter_name]": data["報告者"],
53
- "[report_date]": data["報告日"],
54
- "[product_name]": data["品名"],
55
- "[lot_number]": data["ロットNo."],
56
- "[occurrence_datetime]": data["発生日時"],
57
- "[occurrence_place]": data["発生場所"],
58
- "[discoverer_name]": data["発見者"],
59
- "[worker_name]": data["作業者"],
60
- "[deviation_discovery]": data["逸脱内容・発見の経緯"],
61
- "[first_aid_reason]": data["応急処置・処置の理由"],
62
- "[impact_on_quality]": data["品質への影響の調査状況"],
63
- "[yes]": yes,
64
- "[no]": no,
65
- "[attached_file]": attached_file,
66
- "[supplementary_information]": data["備考"],
67
- }
68
-
69
- doc = replace_text_in_docx(template_path, replacements)
70
- output_path = "data/output_deviation_occurrence.docx"
71
- doc.save(output_path)
72
- print(f"Document saved to {output_path}")
73
-
74
-
75
- if __name__ == "__main__":
76
- main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
src/forms.py CHANGED
@@ -14,7 +14,7 @@ from src.prompts import (deviation_occurrence_system_template_review,
14
  deviation_systemprompt_writing_instructions,
15
  deviation_systemprompt_output_format_instructions)
16
  from src.questions import get_questions
17
- from src.utils import get_embedding, load_env_vars, make_change_application_output
18
 
19
  # 変更タイプとサブタイプの定義
20
  CHANGE_TYPES = [
@@ -318,7 +318,7 @@ def change_request_form():
318
  "備考": supplementary_information,
319
  }
320
 
321
- make_change_application_output(contents)
322
 
323
  st.session_state["document_generated"] = True
324
 
@@ -400,7 +400,7 @@ def change_request_form():
400
  st.markdown(f"**レビュー結果** : {review}")
401
 
402
 
403
- def deviation_occurrence_report_form(embed_output_deviation_occurrence):
404
  st.title("逸脱発生報告書 文書生成")
405
 
406
  load_env_vars()
@@ -505,7 +505,7 @@ def deviation_occurrence_report_form(embed_output_deviation_occurrence):
505
  )
506
  submitted = st.form_submit_button("送信")
507
 
508
- if submitted:
509
  for i in range(q_num - 3):
510
  if nos[i + 2]:
511
  input_values[i + 3] = q_not[i + 3]
@@ -545,7 +545,7 @@ def deviation_occurrence_report_form(embed_output_deviation_occurrence):
545
  occurrence_date, occurrence_time
546
  ).strftime("%Y年%m月%d日%H時%M分")
547
 
548
- json_data = {
549
  "報告者": reporter_name,
550
  "報告日": report_date,
551
  "品名": product_name,
@@ -568,11 +568,7 @@ def deviation_occurrence_report_form(embed_output_deviation_occurrence):
568
  "ユーザープロンプト": user_prompt,
569
  }
570
 
571
- filename = "data/deviation_occurrence_report.json"
572
- with open(filename, "w") as file:
573
- json.dump(json_data, file, ensure_ascii=False, indent=4)
574
-
575
- embed_output_deviation_occurrence()
576
  st.session_state["document_generated"] = True
577
 
578
  if st.session_state["document_generated"]:
@@ -586,31 +582,29 @@ def deviation_occurrence_report_form(embed_output_deviation_occurrence):
586
  mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document",
587
  )
588
 
589
- with open("data/deviation_occurrence_report.json", "r") as file:
590
- json_data = json.load(file)
591
- st.markdown(f'**報告者** : {json_data["報告者"]}')
592
- st.markdown(f'**報告日** : {json_data["報告日"]}')
593
- st.markdown(f'**品名** : {json_data["品名"]}')
594
- st.markdown(f'**ロットNo.** : {json_data["ロットNo."]}')
595
- st.markdown(f'**発生日時** : {json_data["発生日時"]}')
596
- st.markdown(f'**発生場所** : {json_data["発生場所"]}')
597
- st.markdown(f'**発見者** : {json_data["発見者"]}')
598
- st.markdown(f'**作業者** : {json_data["作業者"]}')
599
  st.markdown(
600
- f'**逸脱内容・発見の経緯** : {json_data["逸脱内容・発見の経緯"]}'
601
  )
602
  st.markdown(
603
- f'**応急処置・処置の理由** : {json_data["応急処置・処置の理由"]}'
604
  )
605
  st.markdown(
606
- f'**品質への影響の調査状況** : {json_data["品質への影響の調査状況"]}'
607
  )
608
- st.markdown(f'**添付資料** : {json_data["添付資料"]}')
609
- st.markdown(f'**備考** : {json_data["備考"]}')
610
 
611
  deviation_occurrence_user_template_review = f"""
612
  ## 今回の「逸脱内容・発見の経緯」:
613
- {json_data["逸脱内容・発見の経緯"]}
614
 
615
  返答:
616
  """
 
14
  deviation_systemprompt_writing_instructions,
15
  deviation_systemprompt_output_format_instructions)
16
  from src.questions import get_questions
17
+ from src.utils import get_embedding, load_env_vars, output_change_application, output_deviation_occurrence_report
18
 
19
  # 変更タイプとサブタイプの定義
20
  CHANGE_TYPES = [
 
318
  "備考": supplementary_information,
319
  }
320
 
321
+ output_change_application(contents)
322
 
323
  st.session_state["document_generated"] = True
324
 
 
400
  st.markdown(f"**レビュー結果** : {review}")
401
 
402
 
403
+ def deviation_occurrence_report_form():
404
  st.title("逸脱発生報告書 文書生成")
405
 
406
  load_env_vars()
 
505
  )
506
  submitted = st.form_submit_button("送信")
507
 
508
+ if submitted or st.session_state["document_generated"]:
509
  for i in range(q_num - 3):
510
  if nos[i + 2]:
511
  input_values[i + 3] = q_not[i + 3]
 
545
  occurrence_date, occurrence_time
546
  ).strftime("%Y年%m月%d日%H時%M分")
547
 
548
+ contents = {
549
  "報告者": reporter_name,
550
  "報告日": report_date,
551
  "品名": product_name,
 
568
  "ユーザープロンプト": user_prompt,
569
  }
570
 
571
+ output_deviation_occurrence_report(contents)
 
 
 
 
572
  st.session_state["document_generated"] = True
573
 
574
  if st.session_state["document_generated"]:
 
582
  mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document",
583
  )
584
 
585
+ st.markdown(f'**報告者** : {contents["報告者"]}')
586
+ st.markdown(f'**報告日** : {contents["報告日"]}')
587
+ st.markdown(f'**品名** : {contents["品名"]}')
588
+ st.markdown(f'**ロットNo.** : {contents["ロットNo."]}')
589
+ st.markdown(f'**発生日時** : {contents["発生日時"]}')
590
+ st.markdown(f'**発生場所** : {contents["発生場所"]}')
591
+ st.markdown(f'**発見者** : {contents["発見者"]}')
592
+ st.markdown(f'**作業者** : {contents["作業者"]}')
 
 
593
  st.markdown(
594
+ f'**逸脱内容・発見の経緯** : {contents["逸脱内容・発見の経緯"]}'
595
  )
596
  st.markdown(
597
+ f'**応急処置・処置の理由** : {contents["応急処置・処置の理由"]}'
598
  )
599
  st.markdown(
600
+ f'**品質への影響の調査状況** : {contents["品質への影響の調査状況"]}'
601
  )
602
+ st.markdown(f'**添付資料** : {contents["添付資料"]}')
603
+ st.markdown(f'**備考** : {contents["備考"]}')
604
 
605
  deviation_occurrence_user_template_review = f"""
606
  ## 今回の「逸脱内容・発見の経緯」:
607
+ {contents["逸脱内容・発見の経緯"]}
608
 
609
  返答:
610
  """
src/utils.py CHANGED
@@ -13,7 +13,7 @@ def get_embedding(text, model="text-embedding-3-large", client=None):
13
  )
14
 
15
 
16
- def make_change_application_output(contents: dict):
17
  change_application_form_template_path = "data/template/template.docx"
18
 
19
  replacements = {
@@ -36,6 +36,43 @@ def make_change_application_output(contents: dict):
36
  print(f"Document saved to {change_application_form_path}")
37
 
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  def replace_text_in_docx(template_path, replacements):
40
  """
41
  ドキュメント内の指定されたテキストを置き換える
 
13
  )
14
 
15
 
16
+ def output_change_application(contents: dict):
17
  change_application_form_template_path = "data/template/template.docx"
18
 
19
  replacements = {
 
36
  print(f"Document saved to {change_application_form_path}")
37
 
38
 
39
+ def output_deviation_occurrence_report(contents: dict):
40
+ template_path = "data/template/template_deviation_occurrence.docx"
41
+
42
+ if contents["添付資料"] == "なし":
43
+ yes = "□"
44
+ no = "☑"
45
+ attached_file = ""
46
+ else:
47
+ yes = "☑"
48
+ no = "□"
49
+ attached_file = contents["添付資料"]
50
+
51
+ replacements = {
52
+ "[reporter_name]": contents["報告者"],
53
+ "[report_date]": contents["報告日"],
54
+ "[product_name]": contents["品名"],
55
+ "[lot_number]": contents["ロットNo."],
56
+ "[occurrence_datetime]": contents["発生日時"],
57
+ "[occurrence_place]": contents["発生場所"],
58
+ "[discoverer_name]": contents["発見者"],
59
+ "[worker_name]": contents["作業者"],
60
+ "[deviation_discovery]": contents["逸脱内容・発見の経緯"],
61
+ "[first_aid_reason]": contents["応急処置・処置の理由"],
62
+ "[impact_on_quality]": contents["品質への影響の調査状況"],
63
+ "[yes]": yes,
64
+ "[no]": no,
65
+ "[attached_file]": attached_file,
66
+ "[supplementary_information]": contents["備考"],
67
+ }
68
+
69
+ doc = replace_text_in_docx(template_path, replacements)
70
+ output_path = "data/output_deviation_occurrence.docx"
71
+ doc.save(output_path)
72
+ # TODO: loggerを使う
73
+ print(f"Document saved to {output_path}")
74
+
75
+
76
  def replace_text_in_docx(template_path, replacements):
77
  """
78
  ドキュメント内の指定されたテキストを置き換える