Change Liao
commited on
Commit
·
ddf0f0c
1
Parent(s):
1d9d591
1. 調整法務AI layout
Browse files2. 可以存或回覆預設版本prompt
3. 更新 unstructure all docs
- app.py +77 -24
- data/legal_contract_prompt.txt +20 -0
- data/legal_quotation_prompt.txt +23 -0
- requirements.txt +1 -2
app.py
CHANGED
@@ -41,7 +41,7 @@ from langchain.vectorstores import Chroma
|
|
41 |
from langchain.text_splitter import CharacterTextSplitter
|
42 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
43 |
|
44 |
-
from langchain.document_loaders import DirectoryLoader
|
45 |
from langchain.document_loaders import UnstructuredFileLoader
|
46 |
|
47 |
|
@@ -456,6 +456,7 @@ def content_summary(split_documents):
|
|
456 |
def pdf_summary(file_name):
|
457 |
print("file_name: "+file_name)
|
458 |
loader = UnstructuredFileLoader(file_name)
|
|
|
459 |
document = loader.load()
|
460 |
text_splitter = RecursiveCharacterTextSplitter(
|
461 |
chunk_size=1000,
|
@@ -860,42 +861,94 @@ def gradio_run():
|
|
860 |
], label="訊息範例", inputs=tmp_msg)
|
861 |
|
862 |
with gr.Tab("法務AI幫手"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
863 |
def change_prompt(inputString):
|
864 |
global prompt_string
|
865 |
prompt_string = inputString
|
866 |
return inputString
|
867 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
868 |
with gr.Row():
|
869 |
with gr.Column(scale=2):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
870 |
gr.Markdown("""
|
871 |
-
|
872 |
-
|
873 |
-
1. 可以選擇任何合約或是報價單, 目前僅支援text/pdf/docx/csv 格式; 上傳後即可使用下方的 prompt 來做摘要
|
874 |
-
2. 如果要修改prompt, 直接點選 `prompt 對話框` 修改即可; 若有覺得摘要後內容不足, 不夠清楚, 可以在prompt 裡修正或新增定義. 新增完再上傳一次檔案即可
|
875 |
-
3. 再煩請覺得更好的prompt 內容給Change; 若後續有需要長時間微調prompt, 會再加上可以線上儲存 prompt 功能.
|
876 |
-
4. 下方有prompt 範例, 可以直接點選就會更改prompt 內容.
|
877 |
-
|
878 |
""")
|
879 |
-
|
880 |
-
|
881 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
882 |
gr.Markdown("""
|
883 |
-
|
884 |
-
|
885 |
""")
|
886 |
-
|
887 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
888 |
with gr.Column(scale=3):
|
889 |
summary_text = gr.Textbox()
|
890 |
-
summary_text.label = "
|
891 |
summary_text.change = False
|
892 |
-
summary_text.lines =
|
893 |
-
summary_text.max_lines =
|
894 |
-
pass
|
895 |
-
|
896 |
-
|
897 |
-
gr.Examples([default_legal_contract_prompt, default_legal_quotation_prompt], label="Prompt 範例",
|
898 |
-
inputs=prompt_textbox, outputs=prompt_textbox, fn=change_prompt)
|
899 |
|
900 |
upload_button.upload(upload_large_file, upload_button, file_name_field).\
|
901 |
then(change_prompt,inputs=prompt_textbox).\
|
|
|
41 |
from langchain.text_splitter import CharacterTextSplitter
|
42 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
43 |
|
44 |
+
from langchain.document_loaders import DirectoryLoader, UnstructuredAPIFileLoader
|
45 |
from langchain.document_loaders import UnstructuredFileLoader
|
46 |
|
47 |
|
|
|
456 |
def pdf_summary(file_name):
|
457 |
print("file_name: "+file_name)
|
458 |
loader = UnstructuredFileLoader(file_name)
|
459 |
+
|
460 |
document = loader.load()
|
461 |
text_splitter = RecursiveCharacterTextSplitter(
|
462 |
chunk_size=1000,
|
|
|
861 |
], label="訊息範例", inputs=tmp_msg)
|
862 |
|
863 |
with gr.Tab("法務AI幫手"):
|
864 |
+
legal_path = "./data/"
|
865 |
+
quotation_file = "legal_quotation_prompt.txt"
|
866 |
+
contract_file = "legal_contract_prompt.txt"
|
867 |
+
|
868 |
+
def load_prompt_from_file(typeString):
|
869 |
+
if typeString == "保密合約":
|
870 |
+
_path_string = legal_path + contract_file
|
871 |
+
else:
|
872 |
+
_path_string = legal_path + quotation_file
|
873 |
+
f = open(_path_string, 'r', encoding="utf-8")
|
874 |
+
return_string= f.read()
|
875 |
+
f.close()
|
876 |
+
return return_string
|
877 |
+
def save_func(typeString, prompt_string):
|
878 |
+
if typeString == "保密合約":
|
879 |
+
_path_string = legal_path + contract_file
|
880 |
+
else:
|
881 |
+
_path_string = legal_path + quotation_file
|
882 |
+
f = open(_path_string, "w", encoding="utf-8")
|
883 |
+
f.write(prompt_string)
|
884 |
+
f.close()
|
885 |
+
def restore_func(typeString):
|
886 |
+
if typeString == "保密合約":
|
887 |
+
content_string = default_legal_contract_prompt
|
888 |
+
else:
|
889 |
+
content_string = default_legal_quotation_prompt
|
890 |
+
save_func(typeString, content_string)
|
891 |
+
return content_string
|
892 |
def change_prompt(inputString):
|
893 |
global prompt_string
|
894 |
prompt_string = inputString
|
895 |
return inputString
|
896 |
+
gr.Markdown("""
|
897 |
+
### 面版說明:
|
898 |
+
操作介面全部都在左側, 右側是摘要內容.
|
899 |
+
### 操作步驟
|
900 |
+
1. 選擇摘要的類型: 選 `保密合約` 或 `報價單`
|
901 |
+
2. 微調prompt內容: 直接點選 `prompt對話框` 修改文字內容
|
902 |
+
3. 上傳檔案: 支援PDF/doc/docx 等格式
|
903 |
+
|
904 |
+
""")
|
905 |
+
gr.Markdown("""
|
906 |
+
---
|
907 |
+
""")
|
908 |
with gr.Row():
|
909 |
with gr.Column(scale=2):
|
910 |
+
contract_type = gr.Radio(choices=["報價單","保密合約"],
|
911 |
+
label="1. 請選擇摘要類型",
|
912 |
+
info="選擇不一樣的摘要類型,會改變下方的prompt 內容",
|
913 |
+
type="value",
|
914 |
+
value="報價單",
|
915 |
+
interactive=True)
|
916 |
+
|
917 |
gr.Markdown("""
|
918 |
+
---
|
|
|
|
|
|
|
|
|
|
|
|
|
919 |
""")
|
920 |
+
_firstString = load_prompt_from_file("報價單")
|
921 |
+
prompt_textbox = gr.Textbox(_firstString,
|
922 |
+
lines=20,
|
923 |
+
max_lines=20,
|
924 |
+
label="2. Prompt",
|
925 |
+
interactive=True)
|
926 |
+
prompt_textbox.change(change_prompt, inputs=prompt_textbox)
|
927 |
+
with gr.Row():
|
928 |
+
with gr.Column():
|
929 |
+
saveBtn = gr.Button("保存現有Prompt")
|
930 |
+
with gr.Column():
|
931 |
+
restoreBtn = gr.Button("回覆預設Prompt")
|
932 |
+
|
933 |
gr.Markdown("""
|
934 |
+
---
|
|
|
935 |
""")
|
936 |
+
|
937 |
+
file_name_field = gr.Textbox(max_lines=1, label="3. 上傳檔案", placeholder="目前沒有上傳檔案")
|
938 |
+
|
939 |
+
#event
|
940 |
+
saveBtn.click(save_func, inputs=[contract_type, prompt_textbox],)
|
941 |
+
restoreBtn.click(restore_func, inputs=contract_type, outputs=prompt_textbox)
|
942 |
+
upload_button = gr.UploadButton("請上傳保密合約或報價單(可接受text, pdf, docx, csv 格式)",
|
943 |
+
file_types=["text", ".pdf", ".csv", ".docx", ".doc"], file_count="multiple")
|
944 |
+
contract_type.change(fn=load_prompt_from_file, inputs=contract_type, outputs=prompt_textbox)
|
945 |
+
|
946 |
with gr.Column(scale=3):
|
947 |
summary_text = gr.Textbox()
|
948 |
+
summary_text.label = "AI 摘要:"
|
949 |
summary_text.change = False
|
950 |
+
summary_text.lines = 38
|
951 |
+
summary_text.max_lines = 38
|
|
|
|
|
|
|
|
|
|
|
952 |
|
953 |
upload_button.upload(upload_large_file, upload_button, file_name_field).\
|
954 |
then(change_prompt,inputs=prompt_textbox).\
|
data/legal_contract_prompt.txt
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
你是一位超級助理, 十分擅長從大量文字中擷取摘要.
|
3 |
+
以下用 ''' 包含的是保密合約的內容,幫我生成一份2,000個中文字以內保密合約摘要,摘要需要包含以下項目:
|
4 |
+
1.背景: 介紹對方公司的背景、為什麼要跟該公司簽訂保密合約
|
5 |
+
2.目的: 要與對方交換什麼資料, 資料內容與範圍
|
6 |
+
3.合約期間:保密合約的時間範圍
|
7 |
+
4.提前解約條款: 發生什麼樣的條件就會要提前解約
|
8 |
+
5.保密期間: 保密的時間範圍
|
9 |
+
6.管轄法院: 如有爭端,雙方同意的管轄法院是哪個法院
|
10 |
+
|
11 |
+
AI 風險評估: 希望AI 可以評估該資料交換是否有高風險的疑慮; 評估準測:
|
12 |
+
高風險: 涉及到營業秘密的內容
|
13 |
+
中風險: 沒有營業秘密, 但有涉及敏感資料(足以辨識個人的訊息)
|
14 |
+
低風險: 僅涉及作業面向的訊息
|
15 |
+
|
16 |
+
保密合約:
|
17 |
+
'''
|
18 |
+
{text}
|
19 |
+
'''
|
20 |
+
|
data/legal_quotation_prompt.txt
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
你是一位超級助理, 十分擅長從大量文字中擷取摘要.
|
3 |
+
以下用 ''' 包含的是報價單的內容,幫我生成一份2,000個中文字以內報價單摘要,摘要需要包含以下項目:
|
4 |
+
|
5 |
+
1. 標的名稱: 報價單中所列出的產品或服務的名稱。
|
6 |
+
2. 價格: 報價單中所列出的每個產品或服務的價格, 一定要有正確的幣別與金額數字.
|
7 |
+
3. 付款內容: 報價單中所列出的付款方式和相關內容, 包括訂金, 交貨款和保留款的金額和支付方式; 除了各款項的交付百分比, 也需要有正確的金額與幣別.
|
8 |
+
4. 交貨時間: 報價單中所列出的產品或服務的交付的日期或時間範圍。
|
9 |
+
5. 保固(英文為Warranty): 請摘要報價單中所有關於保固內容.
|
10 |
+
6. 維修費用:報價單中所列出的產品或服務的維修費用或相關條款, 有任何維修的金額請一定要列出.
|
11 |
+
7. 貿易條件(Trade Term)
|
12 |
+
8. 其他注意事項:報價單中所列出的其他重要事項或注意事項。
|
13 |
+
|
14 |
+
請根據報價單的內容, 生成一份清晰明確的摘要, 條列式地把摘要列出, 確保所有項目都被包含在內. 如果內容超過三句話, 請以子項目的方式逐一列舉出來.
|
15 |
+
|
16 |
+
請注意,生成的摘要應該是簡潔且易於理解的, 要詳細條列出內容, 不可產生 "依其他文件說明" 等說明方式.
|
17 |
+
在報價單裡沒有找到符合的資訊, 你被允許回答 "無相關資料".
|
18 |
+
|
19 |
+
報價單內容:
|
20 |
+
|
21 |
+
'''
|
22 |
+
{text}
|
23 |
+
'''
|
requirements.txt
CHANGED
@@ -9,8 +9,7 @@ chromadb
|
|
9 |
google-search-results
|
10 |
tabulate
|
11 |
pydantic==1.10.8
|
12 |
-
unstructured
|
13 |
hnswlib
|
14 |
gradio_client==0.2.7
|
15 |
tiktoken
|
16 |
-
unstructured[
|
|
|
9 |
google-search-results
|
10 |
tabulate
|
11 |
pydantic==1.10.8
|
|
|
12 |
hnswlib
|
13 |
gradio_client==0.2.7
|
14 |
tiktoken
|
15 |
+
unstructured[all-docs]
|