Spaces:
Sleeping
Sleeping
File size: 11,167 Bytes
11873da |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
import xml.etree.ElementTree as ET
import re
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.
Here is the input text:
<text>
{TEXT}
</text>
Here is the list of tags:
<all_tags>
{TAGS}
</all_tags>
Follow these steps to complete the task:
1. Read through the entire text carefully.
2. For each tag in the list:
a. Identify all relevant information in the text that relates to the tag.
b. Extract and summarize the important points related to that tag.
c. If no relevant information is found for a tag, note that as well.
3. Format your output as follows:
- Use <tag_info> tags to enclose the information for each tag.
- Within each <tag_info> section:
- Start with the tag name in <tag> tags.
- Follow with the extracted information in <info> tags.
- 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.
4. Ensure that the extracted information is concise yet comprehensive, capturing all important points related to each tag.
Here's an example of how your output should be formatted:
<answer>
<tag_info>
<tag>Tag1</tag>
<info>Relevant information for Tag1 extracted from the text...</info>
</tag_info>
<tag_info>
<tag>Tag2</tag>
<info>Relevant information for Tag2 extracted from the text...</info>
</tag_info>
<tag_info>
<tag>Tag3</tag>
<info></info>
</tag_info>
</answer>
Remember to include all tags from the provided list, even if no relevant information is found for some of them.
Provide your complete answer within <answer> tags."""
INFORMATION_EXTRACTION_TAG_FORMAT = """<answer>
<tag_info>
<tag>Tag1</tag>
<info>Relevant information for Tag1 extracted from the text...</info>
</tag_info>
<tag_info>
<tag>Tag2</tag>
<info>Relevant information for Tag2 extracted from the text...</info>
</tag_info>
<tag_info>
<tag>Tag3</tag>
<info></info>
</tag_info>
</answer>"""
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:
<tag_info>
{TAG_INFO}
</tag_info>
Follow these steps to complete the task:
1. Read through the entire input carefully.
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.
3. When comparing the information:
- Look for differences in content, wording, or details.
- Note if one PDF provides information that the other doesn't.
- Identify any contradictions between the two PDFs.
4. Format your output as follows:
- Use <comparison> tags to enclose your entire response.
- For each tag, use <tag_difference> tags to enclose the explanation of differences.
- Within each <tag_difference> section, use <tag> to enclose the tag name, and <difference> to enclose the explanation of differences.
- Provide a clear, concise explanation of the differences between pdf1 and pdf2 for that tag.
- Your output should be in korean language
5. Here's an example of how your output should look:
<comparison>
<tag_difference>
<tag>Tag 1</tag>
<difference>
PDF1 states that the product is available in three colors (red, blue, green), while PDF2 mentions four colors (red, blue, green, yellow).
</difference>
</tag_difference>
<tag_difference>
<tag>Tag 2</tag>
<difference>
The information in PDF1 and PDF2 is identical for this tag, both describing the product's weight as 500g.
</difference>
</tag_difference>
</comparison>
6. Additional guidelines:
- Be thorough in your comparison, addressing all aspects of the information provided.
- If there are no differences for a particular tag, state this clearly.
- Use clear and concise language in your explanations.
- If one PDF lacks information that the other provides, explicitly mention this.
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."""
COMPARISON_INPUT_FORMAT = """<tag_data>
<tag>{tag}</tag>
<pdf1>{pdf1_information}</pdf1>
<pdf2>{pdf2_information}</pdf2>
</tag_data>"""
COMPARISON_TAG_FORMAT = """<comparison>
<tag_difference>
<tag>Tag 1</tag>
<difference>
PDF1 states that the product is available in three colors (red, blue, green), while PDF2 mentions four colors (red, blue, green, yellow).
</difference>
</tag_difference>
<tag_difference>
<tag>Tag 2</tag>
<difference>
The information in PDF1 and PDF2 is identical for this tag, both describing the product's weight as 500g.
</difference>
</tag_difference>
</comparison>"""
# def verify_INFORMATION_EXTRACTION_PROMPT(text):
# try:
# root = ET.fromstring(text)
# # Check if the root element is 'answer'
# if root.tag != 'answer':
# print("root tag is wrong")
# return False
# # Check if all child elements are 'tag_info'
# for child in root:
# if child.tag != 'tag_info':
# print("tag_info tag is wrong")
# return False
# # Verify each tag_info element
# for tag_info in root.findall('.//tag_info'):
# tag = tag_info.find('tag')
# info = tag_info.find('info')
# # Check if tag exists and is not empty
# if tag is None:
# print("tag is missing")
# return False
# if not tag.text.strip():
# print("tag is empty")
# return False
# # Check if info exists and is optional
# if info is None:
# print("info is missing")
# return False
# # If all checks pass, the format is valid
# return True
# except ET.ParseError:
# # If parsing fails, the text doesn't follow the expected format
# print(text)
# return False
def verify_INFORMATION_EXTRACTION_PROMPT(text):
# Regular expression pattern
pattern = r'<answer>(.*?)<\/answer>'
# Try to match the overall structure
match = re.search(pattern, text, flags=re.DOTALL)
# Extract the content between <answer> tags
content = match.group(1)
# Pattern for tag_info blocks
tag_info_pattern = r'<tag_info>\s*<tag>(.*?)<\/tag>\s*<info>(.*?)<\/info>\s*<\/tag_info>'
# Find all tag_info blocks
matches = re.findall(tag_info_pattern, content,flags=re.DOTALL)
# Check each match
for match in matches:
tag, info = match
# Check if tag exists and is not empty
if not tag.strip():
print("Tag is empty")
return False
# If all checks pass, the format is valid
return True
# def verify_COMPARISON_PROMPT(text):
# try:
# root = ET.fromstring(text)
# # Check if the root element is 'answer'
# if root.tag != 'comparison':
# return False
# # Check if all child elements are 'tag_difference'
# for child in root:
# if child.tag != 'tag_difference':
# return False
# # Verify each tag_difference element
# for tag_difference in root.findall('.//tag_difference'):
# tag = tag_difference.find('tag')
# difference = tag_difference.find('difference')
# # Check if tag exists and is not empty
# if tag is None:
# return False
# if not tag.text.strip():
# return False
# # Check if info exists and is optional
# if difference is None:
# return False
# # If all checks pass, the format is valid
# return True
# except ET.ParseError:
# # If parsing fails, the text doesn't follow the expected format
# return False
def verify_COMPARISON_PROMPT(text):
# Regular expression pattern
pattern = r'<comparison>(.*?)<\/comparison>'
# Try to match the overall structure
match = re.search(pattern, text, flags=re.DOTALL)
if not match:
print("no comparison match")
return False
# Extract the content between <comparison> tags
content = match.group(1)
# Pattern for tag_difference blocks
tag_difference_pattern = r'<tag_difference>\s*<tag>(.*?)<\/tag>\s*<difference>(.*?)<\/difference>\s*<\/tag_difference>'
# Find all tag_info blocks
matches = re.findall(tag_difference_pattern, content,flags=re.DOTALL)
# Check each match
for match in matches:
tag, difference = match
# Check if tag exists and is not empty
if not tag.strip():
print("Tag is empty")
return False
# If all checks pass, the format is valid
return True
def verify_all_tags_present(text, tags):
tag_pattern = r'<tag>(.*?)<\/tag>'
tags_llm = re.findall(tag_pattern, text,flags=re.DOTALL)
tags = [t.strip() for t in tags]
tags_llm = [t.strip() for t in tags_llm]
extra_tags = []
missing_tags = []
for tag in tags:
if tag not in tags_llm:
missing_tags.append(tag)
for tag in tags_llm:
if tag not in tags:
extra_tags.append(tag)
return missing_tags, extra_tags
def extract_INFORMATION_EXTRACTION_PROMPT(text):
# Regular expression pattern
pattern = r'<answer>(.*?)<\/answer>'
# Try to match the overall structure
match = re.search(pattern, text, flags=re.DOTALL)
# Extract the content between <answer> tags
content = match.group(1)
# Pattern for tag_info blocks
tag_info_pattern = r'<tag_info>\s*<tag>(.*?)<\/tag>\s*<info>(.*?)<\/info>\s*<\/tag_info>'
# Find all tag_info blocks
matches = re.findall(tag_info_pattern, content,flags=re.DOTALL)
# Check each match
data = {}
for match in matches:
tag, info = match
data.update({tag:info})
return data
def extract_COMPARISON_PROMPT(text):
# Regular expression pattern
pattern = r'<comparison>(.*?)<\/comparison>'
# Try to match the overall structure
match = re.search(pattern, text, flags=re.DOTALL)
# Extract the content between <comparison> tags
content = match.group(1)
# Pattern for tag_difference blocks
tag_difference_pattern = r'<tag_difference>\s*<tag>(.*?)<\/tag>\s*<difference>(.*?)<\/difference>\s*<\/tag_difference>'
# Find all tag_info blocks
matches = re.findall(tag_difference_pattern, content,flags=re.DOTALL)
# Check each match
data = {}
for match in matches:
tag, difference = match
data.update({tag:difference})
return data |