Spaces:
Sleeping
Sleeping
viboognesh
commited on
Commit
•
11873da
1
Parent(s):
b819f8d
Upload folder using huggingface_hub
Browse files- app.py +50 -0
- doaz_image.png +0 -0
- pdf_processing.py +144 -0
- prompts.py +342 -0
- requirements.txt +10 -0
app.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
from PIL import Image
|
4 |
+
import io
|
5 |
+
from pdf_processing import process_comparison_data, extract_text_with_pypdf
|
6 |
+
|
7 |
+
def simulate_processing(pdf1, pdf2, tags):
|
8 |
+
# This is a placeholder function. Replace with actual processing logic
|
9 |
+
return [
|
10 |
+
('key', 'Sample Data 1', 'Sample Data 2'),
|
11 |
+
('index', 'More Sample Data 1', 'More Sample Data 2')
|
12 |
+
]
|
13 |
+
|
14 |
+
# App title
|
15 |
+
st.title("PDF Tag Processing")
|
16 |
+
|
17 |
+
# Sidebar configuration
|
18 |
+
st.sidebar.header("Input Configuration")
|
19 |
+
uploaded_file1 = st.sidebar.file_uploader("Upload First PDF", type="pdf")
|
20 |
+
uploaded_file2 = st.sidebar.file_uploader("Upload Second PDF", type="pdf")
|
21 |
+
tags_input = st.sidebar.text_area("Enter Tags (comma-separated)")
|
22 |
+
|
23 |
+
# Process button
|
24 |
+
if st.button("Process"):
|
25 |
+
# pdf1_text = extract_text_with_pypdf(uploaded_file1)
|
26 |
+
if not uploaded_file1:
|
27 |
+
st.error("Please upload a PDF file in the first pdf space")
|
28 |
+
elif not uploaded_file2:
|
29 |
+
st.error("Please upload a PDF file in the second pdf space")
|
30 |
+
elif not tags_input:
|
31 |
+
st.error("Please add some tags in the text area")
|
32 |
+
else:
|
33 |
+
df = process_comparison_data(uploaded_file1, uploaded_file2, [t.strip() for t in tags_input.split(',') if t.strip()])
|
34 |
+
# Display results in a table
|
35 |
+
st.subheader("Results")
|
36 |
+
st.dataframe(df)
|
37 |
+
|
38 |
+
|
39 |
+
|
40 |
+
# Display instructions
|
41 |
+
st.write("""
|
42 |
+
This app allows you to upload two PDF files and enter tags. When you click "Process",
|
43 |
+
it extracts information related to the tags from both the pdfs and compares the information
|
44 |
+
in each pdf for each tag and displays the results in a table.
|
45 |
+
""")
|
46 |
+
|
47 |
+
# Add an image to illustrate the concept
|
48 |
+
image = Image.open('doaz_image.png') # Make sure to replace with your own image
|
49 |
+
st.image(image, caption='Doaz')
|
50 |
+
|
doaz_image.png
ADDED
pdf_processing.py
ADDED
@@ -0,0 +1,144 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import fitz
|
2 |
+
from PyPDF2 import PdfReader
|
3 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
4 |
+
from anthropic import Anthropic
|
5 |
+
from prompts import INFORMATION_EXTRACTION_PROMPT, INFORMATION_EXTRACTION_TAG_FORMAT, verify_INFORMATION_EXTRACTION_PROMPT, extract_INFORMATION_EXTRACTION_PROMPT
|
6 |
+
from prompts import verify_all_tags_present
|
7 |
+
from prompts import COMPARISON_INPUT_FORMAT, COMPARISON_PROMPT, COMPARISON_TAG_FORMAT, verify_COMPARISON_PROMPT, extract_COMPARISON_PROMPT
|
8 |
+
import pandas as pd
|
9 |
+
from concurrent.futures import ThreadPoolExecutor
|
10 |
+
import streamlit as st
|
11 |
+
from dotenv import load_dotenv
|
12 |
+
load_dotenv()
|
13 |
+
|
14 |
+
def make_llm_api_call(messages):
|
15 |
+
print("Making LLM api call")
|
16 |
+
client = Anthropic()
|
17 |
+
message = client.messages.create(
|
18 |
+
model="claude-3-haiku-20240307",
|
19 |
+
max_tokens=4096,
|
20 |
+
temperature=0,
|
21 |
+
messages=messages,
|
22 |
+
)
|
23 |
+
print("LLM response received")
|
24 |
+
return message
|
25 |
+
|
26 |
+
def loop_verify_format(answer_text, tag_format, messages, verify_func,root_tag):
|
27 |
+
i = 0
|
28 |
+
while not verify_func(answer_text):
|
29 |
+
print("Wrong format")
|
30 |
+
assistant_message = {"role": "assistant", "content": [{"type":"text", "text":answer_text}]}
|
31 |
+
corrective_message = {"role":"user", "content":[{"type": "text", "text": f"You did not provide your answer in the correct format. Please provide your answer in the following format:\n{tag_format}"}]}
|
32 |
+
messages.append(assistant_message)
|
33 |
+
messages.append(corrective_message)
|
34 |
+
message = make_llm_api_call(messages)
|
35 |
+
message_text = message.content[0].text
|
36 |
+
answer_text = f"<{root_tag}>\n{message_text.split(f'<{root_tag}>')[1].split(f'</{root_tag}>')[0].strip()}\n</{root_tag}>"
|
37 |
+
if i > 3:
|
38 |
+
raise Exception(f"LLM failed to provide a valid answer in {i-1} attempts")
|
39 |
+
return answer_text
|
40 |
+
|
41 |
+
def loop_verify_all_tags_present(answer_text, tags, user_message, tag_format, verify_func, root_tag):
|
42 |
+
missing_tags, _ = verify_all_tags_present(answer_text, tags)
|
43 |
+
if missing_tags:
|
44 |
+
print("There are missing tags", missing_tags)
|
45 |
+
assistant_message = {"role":"assistant", "content":[{"type":"text", "text":answer_text}]}
|
46 |
+
corrective_message = [{"role":"user", "content":[{"type":"text", "text":("In your response, the following tags are missing:\n" + "\n".join([f"<tag>{tag}</tag>" for tag in missing_tags]) + "\n\nPlease add information about the above missing tags and give a complete correct response.")}]}]
|
47 |
+
messages = [user_message, assistant_message, corrective_message]
|
48 |
+
message = make_llm_api_call(messages)
|
49 |
+
message_text = message.content[0].text
|
50 |
+
answer_text = f"<{root_tag}>\n{message_text.split(f'<{root_tag}>')[1].split(f'</{root_tag}>')[0].strip()}\n</{root_tag}>"
|
51 |
+
answer_text = loop_verify_format(answer_text, tag_format, [user_message], verify_func, root_tag)
|
52 |
+
missing_tags, _ = verify_all_tags_present(answer_text, tags)
|
53 |
+
return answer_text
|
54 |
+
|
55 |
+
def extract_information_from_pdf(pdf_text, tags):
|
56 |
+
tag_text = "\n".join([f"<tag>{tag}</tag>" for tag in tags])
|
57 |
+
prompt = INFORMATION_EXTRACTION_PROMPT.format(TEXT=pdf_text, TAGS=tag_text)
|
58 |
+
user_message = {"role": "user", "content": [{"type": "text", "text": prompt}]}
|
59 |
+
answer_text = ""
|
60 |
+
messages = [user_message]
|
61 |
+
message = make_llm_api_call(messages)
|
62 |
+
message_text = message.content[0].text
|
63 |
+
answer_text = f"<answer>\n{message_text.split('<answer>')[1].split('</answer>')[0].strip()}\n</answer>"
|
64 |
+
answer_text = loop_verify_format(answer_text, INFORMATION_EXTRACTION_TAG_FORMAT, messages, verify_INFORMATION_EXTRACTION_PROMPT, 'answer')
|
65 |
+
answer_text = loop_verify_all_tags_present(answer_text, tags, user_message, INFORMATION_EXTRACTION_PROMPT, verify_INFORMATION_EXTRACTION_PROMPT, 'answer')
|
66 |
+
|
67 |
+
return extract_INFORMATION_EXTRACTION_PROMPT(answer_text)
|
68 |
+
|
69 |
+
|
70 |
+
def extract_text_with_pypdf(pdf_path):
|
71 |
+
reader = PdfReader(pdf_path)
|
72 |
+
text = ""
|
73 |
+
for page in reader.pages:
|
74 |
+
text += f"{page.extract_text()}\n"
|
75 |
+
return text.strip()
|
76 |
+
|
77 |
+
def get_tag_info_for_pdf(pdf, tags):
|
78 |
+
text = extract_text_with_pypdf(pdf)
|
79 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=100000, chunk_overlap=0)
|
80 |
+
chunks = text_splitter.split_text(text)
|
81 |
+
tag_data = {tag:"" for tag in tags}
|
82 |
+
print("chunk length",len(chunks))
|
83 |
+
for chunk in chunks:
|
84 |
+
data = extract_information_from_pdf(chunk, tags)
|
85 |
+
for tag in tags:
|
86 |
+
tag_data.update({tag:f"{tag_data.get(tag)}\n{data.get(tag)}"})
|
87 |
+
return tag_data
|
88 |
+
|
89 |
+
def do_comparison_process(pdf1_data, pdf2_data, tags):
|
90 |
+
tag_data_list = []
|
91 |
+
for tag in tags:
|
92 |
+
tag_info_text = COMPARISON_INPUT_FORMAT.format(tag=tag, pdf1_information=pdf1_data.get(tag), pdf2_information=pdf2_data.get(tag))
|
93 |
+
tag_data_list.append(tag_info_text)
|
94 |
+
tag_data_text = "\n".join(tag_data_list)
|
95 |
+
prompt = COMPARISON_PROMPT.format(TAG_INFO= tag_data_text)
|
96 |
+
user_message = {"role": "user", "content": [{"type": "text", "text": prompt}]}
|
97 |
+
message = make_llm_api_call([user_message])
|
98 |
+
message_text = message.content[0].text
|
99 |
+
comparison_text = f"<comparison>\n{message_text.split('<comparison>')[1].split('</comparison>')[0].strip()}\n</comparison>"
|
100 |
+
comparison_text = loop_verify_format(comparison_text, COMPARISON_TAG_FORMAT, [user_message], verify_COMPARISON_PROMPT, 'comparison')
|
101 |
+
comparison_text = loop_verify_all_tags_present(comparison_text, tags, user_message, COMPARISON_TAG_FORMAT, verify_COMPARISON_PROMPT, 'comparison')
|
102 |
+
|
103 |
+
return extract_COMPARISON_PROMPT(comparison_text)
|
104 |
+
|
105 |
+
# def get_pdf_data(pdf1, pdf2, tags):
|
106 |
+
# def get_tag_info_for_pdf(pdf, tags):
|
107 |
+
# text = extract_text_with_pypdf(pdf)
|
108 |
+
# text_splitter = RecursiveCharacterTextSplitter(chunk_size=100000, chunk_overlap=0)
|
109 |
+
# chunks = text_splitter.split_text(text)
|
110 |
+
# tag_data = {tag:"" for tag in tags}
|
111 |
+
# for chunk in chunks:
|
112 |
+
# data = extract_information_from_pdf(chunk, tags)
|
113 |
+
# for tag in tags:
|
114 |
+
# tag_data.update({tag:f"{tag_data.get(tag)}\n{data.get(tag)}"})
|
115 |
+
# return tag_data
|
116 |
+
|
117 |
+
# # Create a ThreadPoolExecutor (or ProcessPoolExecutor for CPU-bound tasks)
|
118 |
+
# with ThreadPoolExecutor(max_workers=2) as executor:
|
119 |
+
# # Submit the functions to the executor
|
120 |
+
# pdf1_future = executor.submit(get_tag_info_for_pdf, pdf1, tags)
|
121 |
+
# pdf2_future = executor.submit(get_tag_info_for_pdf, pdf2, tags)
|
122 |
+
|
123 |
+
# # Collect the results
|
124 |
+
# pdf1_data = pdf1_future.result()
|
125 |
+
# pdf2_data = pdf2_future.result()
|
126 |
+
|
127 |
+
# return pdf1_data, pdf2_data
|
128 |
+
|
129 |
+
|
130 |
+
def process_comparison_data(pdf1, pdf2, tags):
|
131 |
+
with st.spinner("Processing PDF 1"):
|
132 |
+
pdf1_data = get_tag_info_for_pdf(pdf1, tags)
|
133 |
+
with st.spinner("Processing PDF 2"):
|
134 |
+
pdf2_data = get_tag_info_for_pdf(pdf2, tags)
|
135 |
+
with st.spinner("Generating Comparison Data"):
|
136 |
+
comparison_data = do_comparison_process(pdf1_data, pdf2_data, tags)
|
137 |
+
# pdf1_data, pdf2_data = get_pdf_data(pdf1, pdf2, tags)
|
138 |
+
# comparison_data = do_comparison_process(pdf1_data, pdf2_data, tags)
|
139 |
+
table_data = []
|
140 |
+
for tag in tags:
|
141 |
+
table_data.append((tag, pdf1_data.get(tag), pdf2_data.get(tag), comparison_data.get(tag)))
|
142 |
+
df = pd.DataFrame(table_data, columns=['Tags', 'PDF 1', 'PDF 2', 'Difference'])
|
143 |
+
df.set_index('Tags', inplace=True)
|
144 |
+
return df
|
prompts.py
ADDED
@@ -0,0 +1,342 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import xml.etree.ElementTree as ET
|
2 |
+
import re
|
3 |
+
|
4 |
+
INFORMATION_EXTRACTION_PROMPT = """You will be given a text and a list of tags. Your task is to extract all important information from the text related to each tag separately and provide the information with each tag and their corresponding relevant information.
|
5 |
+
|
6 |
+
Here is the input text:
|
7 |
+
<text>
|
8 |
+
{TEXT}
|
9 |
+
</text>
|
10 |
+
|
11 |
+
Here is the list of tags:
|
12 |
+
<all_tags>
|
13 |
+
{TAGS}
|
14 |
+
</all_tags>
|
15 |
+
|
16 |
+
Follow these steps to complete the task:
|
17 |
+
|
18 |
+
1. Read through the entire text carefully.
|
19 |
+
|
20 |
+
2. For each tag in the list:
|
21 |
+
a. Identify all relevant information in the text that relates to the tag.
|
22 |
+
b. Extract and summarize the important points related to that tag.
|
23 |
+
c. If no relevant information is found for a tag, note that as well.
|
24 |
+
|
25 |
+
3. Format your output as follows:
|
26 |
+
- Use <tag_info> tags to enclose the information for each tag.
|
27 |
+
- Within each <tag_info> section:
|
28 |
+
- Start with the tag name in <tag> tags.
|
29 |
+
- Follow with the extracted information in <info> tags.
|
30 |
+
- If no relevant information is found for a tag, just leave section empty within the <info> tags but make sure the <info> tags are present.
|
31 |
+
|
32 |
+
4. Ensure that the extracted information is concise yet comprehensive, capturing all important points related to each tag.
|
33 |
+
|
34 |
+
Here's an example of how your output should be formatted:
|
35 |
+
|
36 |
+
<answer>
|
37 |
+
<tag_info>
|
38 |
+
<tag>Tag1</tag>
|
39 |
+
<info>Relevant information for Tag1 extracted from the text...</info>
|
40 |
+
</tag_info>
|
41 |
+
|
42 |
+
<tag_info>
|
43 |
+
<tag>Tag2</tag>
|
44 |
+
<info>Relevant information for Tag2 extracted from the text...</info>
|
45 |
+
</tag_info>
|
46 |
+
|
47 |
+
<tag_info>
|
48 |
+
<tag>Tag3</tag>
|
49 |
+
<info></info>
|
50 |
+
</tag_info>
|
51 |
+
</answer>
|
52 |
+
|
53 |
+
Remember to include all tags from the provided list, even if no relevant information is found for some of them.
|
54 |
+
|
55 |
+
Provide your complete answer within <answer> tags."""
|
56 |
+
|
57 |
+
INFORMATION_EXTRACTION_TAG_FORMAT = """<answer>
|
58 |
+
<tag_info>
|
59 |
+
<tag>Tag1</tag>
|
60 |
+
<info>Relevant information for Tag1 extracted from the text...</info>
|
61 |
+
</tag_info>
|
62 |
+
|
63 |
+
<tag_info>
|
64 |
+
<tag>Tag2</tag>
|
65 |
+
<info>Relevant information for Tag2 extracted from the text...</info>
|
66 |
+
</tag_info>
|
67 |
+
|
68 |
+
<tag_info>
|
69 |
+
<tag>Tag3</tag>
|
70 |
+
<info></info>
|
71 |
+
</tag_info>
|
72 |
+
</answer>"""
|
73 |
+
|
74 |
+
COMPARISON_PROMPT = """You are tasked with comparing information from two PDFs for a given set of tags. The input will be provided in the following format:
|
75 |
+
|
76 |
+
<tag_info>
|
77 |
+
{TAG_INFO}
|
78 |
+
</tag_info>
|
79 |
+
|
80 |
+
Follow these steps to complete the task:
|
81 |
+
|
82 |
+
1. Read through the entire input carefully.
|
83 |
+
|
84 |
+
2. For each tag data, you will see the tag and the information related to the tag from pdf1 and pdf2. Your job is to compare this information and explain the differences.
|
85 |
+
|
86 |
+
3. When comparing the information:
|
87 |
+
- Look for differences in content, wording, or details.
|
88 |
+
- Note if one PDF provides information that the other doesn't.
|
89 |
+
- Identify any contradictions between the two PDFs.
|
90 |
+
|
91 |
+
4. Format your output as follows:
|
92 |
+
- Use <comparison> tags to enclose your entire response.
|
93 |
+
- For each tag, use <tag_difference> tags to enclose the explanation of differences.
|
94 |
+
- Within each <tag_difference> section, use <tag> to enclose the tag name, and <difference> to enclose the explanation of differences.
|
95 |
+
- Provide a clear, concise explanation of the differences between pdf1 and pdf2 for that tag.
|
96 |
+
- Your output should be in korean language
|
97 |
+
|
98 |
+
5. Here's an example of how your output should look:
|
99 |
+
|
100 |
+
<comparison>
|
101 |
+
<tag_difference>
|
102 |
+
<tag>Tag 1</tag>
|
103 |
+
<difference>
|
104 |
+
PDF1 states that the product is available in three colors (red, blue, green), while PDF2 mentions four colors (red, blue, green, yellow).
|
105 |
+
</difference>
|
106 |
+
</tag_difference>
|
107 |
+
<tag_difference>
|
108 |
+
<tag>Tag 2</tag>
|
109 |
+
<difference>
|
110 |
+
The information in PDF1 and PDF2 is identical for this tag, both describing the product's weight as 500g.
|
111 |
+
</difference>
|
112 |
+
</tag_difference>
|
113 |
+
</comparison>
|
114 |
+
|
115 |
+
6. Additional guidelines:
|
116 |
+
- Be thorough in your comparison, addressing all aspects of the information provided.
|
117 |
+
- If there are no differences for a particular tag, state this clearly.
|
118 |
+
- Use clear and concise language in your explanations.
|
119 |
+
- If one PDF lacks information that the other provides, explicitly mention this.
|
120 |
+
|
121 |
+
Begin your analysis with the first tag in the input and proceed through all tags sequentially. Ensure that your output is comprehensive and accurately reflects the differences between the two PDFs for each tag and is in korean language."""
|
122 |
+
|
123 |
+
COMPARISON_INPUT_FORMAT = """<tag_data>
|
124 |
+
<tag>{tag}</tag>
|
125 |
+
<pdf1>{pdf1_information}</pdf1>
|
126 |
+
<pdf2>{pdf2_information}</pdf2>
|
127 |
+
</tag_data>"""
|
128 |
+
|
129 |
+
COMPARISON_TAG_FORMAT = """<comparison>
|
130 |
+
<tag_difference>
|
131 |
+
<tag>Tag 1</tag>
|
132 |
+
<difference>
|
133 |
+
PDF1 states that the product is available in three colors (red, blue, green), while PDF2 mentions four colors (red, blue, green, yellow).
|
134 |
+
</difference>
|
135 |
+
</tag_difference>
|
136 |
+
<tag_difference>
|
137 |
+
<tag>Tag 2</tag>
|
138 |
+
<difference>
|
139 |
+
The information in PDF1 and PDF2 is identical for this tag, both describing the product's weight as 500g.
|
140 |
+
</difference>
|
141 |
+
</tag_difference>
|
142 |
+
</comparison>"""
|
143 |
+
|
144 |
+
# def verify_INFORMATION_EXTRACTION_PROMPT(text):
|
145 |
+
# try:
|
146 |
+
# root = ET.fromstring(text)
|
147 |
+
|
148 |
+
# # Check if the root element is 'answer'
|
149 |
+
# if root.tag != 'answer':
|
150 |
+
# print("root tag is wrong")
|
151 |
+
# return False
|
152 |
+
|
153 |
+
# # Check if all child elements are 'tag_info'
|
154 |
+
# for child in root:
|
155 |
+
# if child.tag != 'tag_info':
|
156 |
+
# print("tag_info tag is wrong")
|
157 |
+
# return False
|
158 |
+
|
159 |
+
# # Verify each tag_info element
|
160 |
+
# for tag_info in root.findall('.//tag_info'):
|
161 |
+
# tag = tag_info.find('tag')
|
162 |
+
# info = tag_info.find('info')
|
163 |
+
|
164 |
+
# # Check if tag exists and is not empty
|
165 |
+
# if tag is None:
|
166 |
+
# print("tag is missing")
|
167 |
+
# return False
|
168 |
+
|
169 |
+
# if not tag.text.strip():
|
170 |
+
# print("tag is empty")
|
171 |
+
# return False
|
172 |
+
|
173 |
+
# # Check if info exists and is optional
|
174 |
+
# if info is None:
|
175 |
+
# print("info is missing")
|
176 |
+
# return False
|
177 |
+
|
178 |
+
# # If all checks pass, the format is valid
|
179 |
+
# return True
|
180 |
+
|
181 |
+
# except ET.ParseError:
|
182 |
+
# # If parsing fails, the text doesn't follow the expected format
|
183 |
+
# print(text)
|
184 |
+
# return False
|
185 |
+
|
186 |
+
|
187 |
+
def verify_INFORMATION_EXTRACTION_PROMPT(text):
|
188 |
+
# Regular expression pattern
|
189 |
+
pattern = r'<answer>(.*?)<\/answer>'
|
190 |
+
|
191 |
+
# Try to match the overall structure
|
192 |
+
match = re.search(pattern, text, flags=re.DOTALL)
|
193 |
+
|
194 |
+
# Extract the content between <answer> tags
|
195 |
+
content = match.group(1)
|
196 |
+
|
197 |
+
# Pattern for tag_info blocks
|
198 |
+
tag_info_pattern = r'<tag_info>\s*<tag>(.*?)<\/tag>\s*<info>(.*?)<\/info>\s*<\/tag_info>'
|
199 |
+
|
200 |
+
# Find all tag_info blocks
|
201 |
+
matches = re.findall(tag_info_pattern, content,flags=re.DOTALL)
|
202 |
+
# Check each match
|
203 |
+
for match in matches:
|
204 |
+
tag, info = match
|
205 |
+
|
206 |
+
# Check if tag exists and is not empty
|
207 |
+
if not tag.strip():
|
208 |
+
print("Tag is empty")
|
209 |
+
return False
|
210 |
+
|
211 |
+
|
212 |
+
# If all checks pass, the format is valid
|
213 |
+
return True
|
214 |
+
|
215 |
+
# def verify_COMPARISON_PROMPT(text):
|
216 |
+
# try:
|
217 |
+
# root = ET.fromstring(text)
|
218 |
+
|
219 |
+
# # Check if the root element is 'answer'
|
220 |
+
# if root.tag != 'comparison':
|
221 |
+
# return False
|
222 |
+
|
223 |
+
# # Check if all child elements are 'tag_difference'
|
224 |
+
# for child in root:
|
225 |
+
# if child.tag != 'tag_difference':
|
226 |
+
# return False
|
227 |
+
|
228 |
+
# # Verify each tag_difference element
|
229 |
+
# for tag_difference in root.findall('.//tag_difference'):
|
230 |
+
# tag = tag_difference.find('tag')
|
231 |
+
# difference = tag_difference.find('difference')
|
232 |
+
|
233 |
+
# # Check if tag exists and is not empty
|
234 |
+
# if tag is None:
|
235 |
+
# return False
|
236 |
+
|
237 |
+
# if not tag.text.strip():
|
238 |
+
# return False
|
239 |
+
|
240 |
+
# # Check if info exists and is optional
|
241 |
+
# if difference is None:
|
242 |
+
# return False
|
243 |
+
|
244 |
+
# # If all checks pass, the format is valid
|
245 |
+
# return True
|
246 |
+
|
247 |
+
# except ET.ParseError:
|
248 |
+
# # If parsing fails, the text doesn't follow the expected format
|
249 |
+
# return False
|
250 |
+
|
251 |
+
def verify_COMPARISON_PROMPT(text):
|
252 |
+
# Regular expression pattern
|
253 |
+
pattern = r'<comparison>(.*?)<\/comparison>'
|
254 |
+
|
255 |
+
# Try to match the overall structure
|
256 |
+
match = re.search(pattern, text, flags=re.DOTALL)
|
257 |
+
if not match:
|
258 |
+
print("no comparison match")
|
259 |
+
return False
|
260 |
+
|
261 |
+
# Extract the content between <comparison> tags
|
262 |
+
content = match.group(1)
|
263 |
+
|
264 |
+
# Pattern for tag_difference blocks
|
265 |
+
tag_difference_pattern = r'<tag_difference>\s*<tag>(.*?)<\/tag>\s*<difference>(.*?)<\/difference>\s*<\/tag_difference>'
|
266 |
+
|
267 |
+
# Find all tag_info blocks
|
268 |
+
matches = re.findall(tag_difference_pattern, content,flags=re.DOTALL)
|
269 |
+
# Check each match
|
270 |
+
for match in matches:
|
271 |
+
tag, difference = match
|
272 |
+
|
273 |
+
# Check if tag exists and is not empty
|
274 |
+
if not tag.strip():
|
275 |
+
print("Tag is empty")
|
276 |
+
return False
|
277 |
+
|
278 |
+
|
279 |
+
# If all checks pass, the format is valid
|
280 |
+
return True
|
281 |
+
|
282 |
+
def verify_all_tags_present(text, tags):
|
283 |
+
tag_pattern = r'<tag>(.*?)<\/tag>'
|
284 |
+
tags_llm = re.findall(tag_pattern, text,flags=re.DOTALL)
|
285 |
+
tags = [t.strip() for t in tags]
|
286 |
+
tags_llm = [t.strip() for t in tags_llm]
|
287 |
+
extra_tags = []
|
288 |
+
missing_tags = []
|
289 |
+
for tag in tags:
|
290 |
+
if tag not in tags_llm:
|
291 |
+
missing_tags.append(tag)
|
292 |
+
for tag in tags_llm:
|
293 |
+
if tag not in tags:
|
294 |
+
extra_tags.append(tag)
|
295 |
+
return missing_tags, extra_tags
|
296 |
+
|
297 |
+
def extract_INFORMATION_EXTRACTION_PROMPT(text):
|
298 |
+
# Regular expression pattern
|
299 |
+
pattern = r'<answer>(.*?)<\/answer>'
|
300 |
+
|
301 |
+
# Try to match the overall structure
|
302 |
+
match = re.search(pattern, text, flags=re.DOTALL)
|
303 |
+
|
304 |
+
# Extract the content between <answer> tags
|
305 |
+
content = match.group(1)
|
306 |
+
|
307 |
+
# Pattern for tag_info blocks
|
308 |
+
tag_info_pattern = r'<tag_info>\s*<tag>(.*?)<\/tag>\s*<info>(.*?)<\/info>\s*<\/tag_info>'
|
309 |
+
|
310 |
+
# Find all tag_info blocks
|
311 |
+
matches = re.findall(tag_info_pattern, content,flags=re.DOTALL)
|
312 |
+
# Check each match
|
313 |
+
data = {}
|
314 |
+
for match in matches:
|
315 |
+
tag, info = match
|
316 |
+
|
317 |
+
data.update({tag:info})
|
318 |
+
|
319 |
+
return data
|
320 |
+
|
321 |
+
def extract_COMPARISON_PROMPT(text):
|
322 |
+
# Regular expression pattern
|
323 |
+
pattern = r'<comparison>(.*?)<\/comparison>'
|
324 |
+
|
325 |
+
# Try to match the overall structure
|
326 |
+
match = re.search(pattern, text, flags=re.DOTALL)
|
327 |
+
|
328 |
+
# Extract the content between <comparison> tags
|
329 |
+
content = match.group(1)
|
330 |
+
|
331 |
+
# Pattern for tag_difference blocks
|
332 |
+
tag_difference_pattern = r'<tag_difference>\s*<tag>(.*?)<\/tag>\s*<difference>(.*?)<\/difference>\s*<\/tag_difference>'
|
333 |
+
|
334 |
+
# Find all tag_info blocks
|
335 |
+
matches = re.findall(tag_difference_pattern, content,flags=re.DOTALL)
|
336 |
+
# Check each match
|
337 |
+
data = {}
|
338 |
+
for match in matches:
|
339 |
+
tag, difference = match
|
340 |
+
data.update({tag:difference})
|
341 |
+
|
342 |
+
return data
|
requirements.txt
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
pandas
|
3 |
+
pillow
|
4 |
+
PyPDF2
|
5 |
+
fitz
|
6 |
+
PyMuPDF
|
7 |
+
langchain-text-splitters
|
8 |
+
anthropic
|
9 |
+
python-dotenv
|
10 |
+
tiktoken
|