Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,6 +16,9 @@ import re
|
|
| 16 |
|
| 17 |
import logging
|
| 18 |
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
HF_API_KEY = os.getenv("HF_API_KEY")
|
| 21 |
|
|
@@ -247,7 +250,28 @@ def process_long_table(rows):
|
|
| 247 |
|
| 248 |
table_data.append(row_data)
|
| 249 |
|
| 250 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
|
| 252 |
def extract_tables(root):
|
| 253 |
"""Extracts tables from the DOCX document and returns structured data."""
|
|
@@ -418,50 +442,110 @@ Contract data in JSON format:""" + f"""
|
|
| 418 |
return json.dumps(contract_summary, ensure_ascii=False, indent=4)
|
| 419 |
|
| 420 |
|
| 421 |
-
def deepseek_extract_price_list(
|
| 422 |
-
"""
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
|
| 427 |
-
#
|
| 428 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 429 |
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
|
| 435 |
-
|
| 436 |
-
|
| 437 |
|
| 438 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 439 |
|
| 440 |
-
|
| 441 |
-
|
|
|
|
| 442 |
|
| 443 |
-
|
| 444 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 445 |
"role": "user",
|
| 446 |
-
"content":
|
| 447 |
-
}
|
| 448 |
-
]
|
| 449 |
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
api_key=HF_API_KEY,
|
| 453 |
-
)
|
| 454 |
|
| 455 |
-
completion = client.chat.completions.create(
|
| 456 |
-
model="deepseek/deepseek-r1-distill-qwen-14b",
|
| 457 |
-
messages=messages,
|
| 458 |
-
)
|
| 459 |
-
|
| 460 |
-
price_list = re.sub(r"<think>.*?</think>\s*", "", completion.choices[0].message.content, flags=re.DOTALL)
|
| 461 |
-
|
| 462 |
-
price_list = re.sub(r"^```json\n|```$", "", price_list, flags=re.DOTALL)
|
| 463 |
|
| 464 |
-
|
| 465 |
def json_to_excel(contract_summary, json_data, excel_path):
|
| 466 |
"""Converts extracted JSON tables to an Excel file."""
|
| 467 |
|
|
@@ -495,28 +579,40 @@ def extract_po(docx_path):
|
|
| 495 |
# Step 1: Extract XML content from DOCX
|
| 496 |
print("Extracting Docs data to XML...")
|
| 497 |
xml_filename = os.path.splitext(os.path.basename(docx_path))[0] + "_document.xml"
|
| 498 |
-
xml_file = extract_docx_as_xml(docx_bytes, save_xml=
|
| 499 |
|
| 500 |
get_namespace(ET.fromstring(xml_file))
|
| 501 |
|
| 502 |
# Step 2: Extract tables from DOCX and save JSON
|
| 503 |
print("Extracting XML data to JSON...")
|
| 504 |
json_filename = os.path.splitext(os.path.basename(docx_path))[0] + "_extracted_data.json"
|
| 505 |
-
extracted_data = xml_to_json(xml_file, save_json=
|
| 506 |
|
| 507 |
# Step 3: Process JSON with OpenAI to get structured output
|
| 508 |
-
print("Processing
|
| 509 |
contract_summary_filename = os.path.splitext(os.path.basename(docx_path))[0] + "_contract_summary.json"
|
| 510 |
-
contract_summary = deepseek_extract_contract_summary(extracted_data, save_json=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 511 |
|
| 512 |
# Step 4: Combine contract summary and long table data into a single JSON object
|
| 513 |
print("Combining AI Generated JSON with Extracted Data...")
|
|
|
|
| 514 |
combined_data = {
|
| 515 |
"contract_summary": json.loads(json.loads(contract_summary)),
|
| 516 |
-
"price_list":
|
| 517 |
-
table for key, table in json.loads(extracted_data).items()
|
| 518 |
-
if "long_table" in key and "summary" not in key
|
| 519 |
-
]
|
| 520 |
}
|
| 521 |
|
| 522 |
# Logging
|
|
@@ -538,6 +634,8 @@ def extract_po(docx_path):
|
|
| 538 |
# extract_po("test-contract-converted.docx")
|
| 539 |
# extract_po("test-contract.docx")
|
| 540 |
|
|
|
|
|
|
|
| 541 |
# Gradio Interface ------------------------------
|
| 542 |
|
| 543 |
import gradio as gr
|
|
@@ -553,3 +651,4 @@ interface = gr.Interface(
|
|
| 553 |
)
|
| 554 |
|
| 555 |
interface.launch()
|
|
|
|
|
|
| 16 |
|
| 17 |
import logging
|
| 18 |
|
| 19 |
+
from pydantic import BaseModel, Field, ValidationError, RootModel
|
| 20 |
+
from typing import List, Optional
|
| 21 |
+
|
| 22 |
|
| 23 |
HF_API_KEY = os.getenv("HF_API_KEY")
|
| 24 |
|
|
|
|
| 250 |
|
| 251 |
table_data.append(row_data)
|
| 252 |
|
| 253 |
+
# Filter out rows where the "序号" column contains non-numeric values
|
| 254 |
+
filtered_table_data = []
|
| 255 |
+
for row in table_data:
|
| 256 |
+
# Check potential serial number columns (use both Chinese and English variants)
|
| 257 |
+
serial_number = None
|
| 258 |
+
for column in row:
|
| 259 |
+
if any(term in column for term in ["序号"]):
|
| 260 |
+
serial_number = row[column]
|
| 261 |
+
break
|
| 262 |
+
|
| 263 |
+
# If we found a serial number column, check if its value is numeric
|
| 264 |
+
if serial_number is not None:
|
| 265 |
+
# Strip any non-numeric characters and check if there's still a value
|
| 266 |
+
# This keeps values like "1", "2." etc. but filters out "No." or other text
|
| 267 |
+
cleaned_number = re.sub(r'[^\d]', '', serial_number)
|
| 268 |
+
if cleaned_number: # If there are any digits left, keep the row
|
| 269 |
+
filtered_table_data.append(row)
|
| 270 |
+
else:
|
| 271 |
+
# If we couldn't find a serial number column, keep the row
|
| 272 |
+
filtered_table_data.append(row)
|
| 273 |
+
|
| 274 |
+
return filtered_table_data
|
| 275 |
|
| 276 |
def extract_tables(root):
|
| 277 |
"""Extracts tables from the DOCX document and returns structured data."""
|
|
|
|
| 442 |
return json.dumps(contract_summary, ensure_ascii=False, indent=4)
|
| 443 |
|
| 444 |
|
| 445 |
+
def deepseek_extract_price_list(price_list, save_json=False, json_name="price_list.json"):
|
| 446 |
+
"""
|
| 447 |
+
Extracts structured price list using DeepSeek LLM and validates output with Pydantic.
|
| 448 |
+
Retries up to 3 times with error feedback if output is not valid JSON.
|
| 449 |
+
"""
|
| 450 |
|
| 451 |
+
# Pydantic schema
|
| 452 |
+
class PriceItem(BaseModel):
|
| 453 |
+
序号: str
|
| 454 |
+
名称: str
|
| 455 |
+
名称_英文: str = Field(..., alias="名称(英文)")
|
| 456 |
+
品牌: str
|
| 457 |
+
规格: str
|
| 458 |
+
所属机型: str
|
| 459 |
+
采购数量: str
|
| 460 |
+
单位: str
|
| 461 |
+
单价: str
|
| 462 |
+
总价: str
|
| 463 |
+
几郎单价: str
|
| 464 |
+
几郎总额: str
|
| 465 |
+
备注: str
|
| 466 |
+
计划来源: str
|
| 467 |
+
其他: dict = Field(default_factory=dict, alias="其他")
|
| 468 |
+
|
| 469 |
+
class PriceListModel(BaseModel):
|
| 470 |
+
items: List[PriceItem]
|
| 471 |
+
|
| 472 |
+
base_prompt = f"""你会接收到一个采购清单列表,请你提取以下字段并重新输出为一个结构化的 JSON 格式。
|
| 473 |
+
有时候第一行是表头,有时候是数据行,只输入数据行。请注意,输出的 JSON 需要符合以下格式要求:
|
| 474 |
+
|
| 475 |
+
# 输出格式要求:
|
| 476 |
+
每个条目输出以下字段:
|
| 477 |
+
- 序号
|
| 478 |
+
- 名称:只填中文
|
| 479 |
+
- 名称(英文):只填英文
|
| 480 |
+
- 品牌
|
| 481 |
+
- 规格
|
| 482 |
+
- 所属机型
|
| 483 |
+
- 采购数量
|
| 484 |
+
- 单位
|
| 485 |
+
- 单价: 只填数字
|
| 486 |
+
- 总价: 只填数字
|
| 487 |
+
- 几郎单价: 只填数字
|
| 488 |
+
- 几郎总额: 只填数字
|
| 489 |
+
- 备注
|
| 490 |
+
- 计划来源
|
| 491 |
+
- 其他:如果有以上以外的字段就以list的形式写在其他里 ("其他": "key1": "value1", "key2":"value2"),如果没有就给一个空的list
|
| 492 |
+
|
| 493 |
+
请确保输出的 JSON 是有效的,且字段名称与输入的字段名称一致。请注意,字段名称可能会有不同的拼写方式,请根据上下文进行判断。
|
| 494 |
+
请确保输出的条目数量与输入的列表数量一致。
|
| 495 |
+
|
| 496 |
+
# 原始价格表:
|
| 497 |
+
{price_list}"""
|
| 498 |
+
|
| 499 |
+
messages = [{"role": "user", "content": base_prompt}]
|
| 500 |
|
| 501 |
+
client = OpenAI(
|
| 502 |
+
base_url="https://router.huggingface.co/novita",
|
| 503 |
+
api_key=HF_API_KEY,
|
| 504 |
+
)
|
| 505 |
|
| 506 |
+
for attempt in range(3):
|
| 507 |
+
print(f"🔁 Attempt {attempt + 1} to extract and validate Price List")
|
| 508 |
|
| 509 |
+
try:
|
| 510 |
+
response = client.chat.completions.create(
|
| 511 |
+
model="deepseek/deepseek-r1-distill-qwen-14b",
|
| 512 |
+
messages=messages,
|
| 513 |
+
)
|
| 514 |
+
raw = response.choices[0].message.content
|
| 515 |
|
| 516 |
+
# Strip out LLM artifacts
|
| 517 |
+
raw = re.sub(r"<think>.*?</think>\s*", "", raw, flags=re.DOTALL)
|
| 518 |
+
raw = re.sub(r"^```json\n|```$", "", raw.strip(), flags=re.DOTALL)
|
| 519 |
|
| 520 |
+
# Wrap the raw JSON in a proper structure if it's a list
|
| 521 |
+
if raw.strip().startswith('['):
|
| 522 |
+
raw = '{"items": ' + raw + '}'
|
| 523 |
+
|
| 524 |
+
validated = PriceListModel.model_validate_json(raw)
|
| 525 |
+
price_list_json = validated.model_dump(by_alias=True)["items"]
|
| 526 |
+
|
| 527 |
+
if save_json:
|
| 528 |
+
with open(json_name, "w", encoding="utf-8") as f:
|
| 529 |
+
json.dump(price_list_json, f, ensure_ascii=False, indent=4)
|
| 530 |
+
print(f"✅ Saved to {json_name}")
|
| 531 |
+
|
| 532 |
+
return price_list_json
|
| 533 |
+
|
| 534 |
+
except ValidationError as ve:
|
| 535 |
+
error_msg = f"Pydantic validation error: {ve}"
|
| 536 |
+
except Exception as e:
|
| 537 |
+
error_msg = f"Unexpected error: {e}"
|
| 538 |
+
|
| 539 |
+
print(f"❌ {error_msg}")
|
| 540 |
+
messages.append({
|
| 541 |
"role": "user",
|
| 542 |
+
"content": f"Your previous attempt gave this error: {error_msg}. Please try again ensuring your response is valid JSON with correct format."
|
| 543 |
+
})
|
|
|
|
| 544 |
|
| 545 |
+
print("⚠️ Failed after 3 attempts.")
|
| 546 |
+
return raw
|
|
|
|
|
|
|
| 547 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 548 |
|
|
|
|
| 549 |
def json_to_excel(contract_summary, json_data, excel_path):
|
| 550 |
"""Converts extracted JSON tables to an Excel file."""
|
| 551 |
|
|
|
|
| 579 |
# Step 1: Extract XML content from DOCX
|
| 580 |
print("Extracting Docs data to XML...")
|
| 581 |
xml_filename = os.path.splitext(os.path.basename(docx_path))[0] + "_document.xml"
|
| 582 |
+
xml_file = extract_docx_as_xml(docx_bytes, save_xml=False, xml_filename=xml_filename)
|
| 583 |
|
| 584 |
get_namespace(ET.fromstring(xml_file))
|
| 585 |
|
| 586 |
# Step 2: Extract tables from DOCX and save JSON
|
| 587 |
print("Extracting XML data to JSON...")
|
| 588 |
json_filename = os.path.splitext(os.path.basename(docx_path))[0] + "_extracted_data.json"
|
| 589 |
+
extracted_data = xml_to_json(xml_file, save_json=False, json_filename=json_filename)
|
| 590 |
|
| 591 |
# Step 3: Process JSON with OpenAI to get structured output
|
| 592 |
+
print("Processing Contract Summary data with AI...")
|
| 593 |
contract_summary_filename = os.path.splitext(os.path.basename(docx_path))[0] + "_contract_summary.json"
|
| 594 |
+
contract_summary = deepseek_extract_contract_summary(extracted_data, save_json=False, json_filename=contract_summary_filename)
|
| 595 |
+
|
| 596 |
+
# Find the last long table (excluding summary tables)
|
| 597 |
+
print("Processing Price List data with AI...")
|
| 598 |
+
long_tables = [
|
| 599 |
+
table for key, table in json.loads(extracted_data).items()
|
| 600 |
+
if "long_table" in key and "summary" not in key
|
| 601 |
+
]
|
| 602 |
+
last_long_table = long_tables[-1] if long_tables else {}
|
| 603 |
+
|
| 604 |
+
# Generate the price list filename in the same folder as the document
|
| 605 |
+
price_list_filename = os.path.join(os.path.dirname(docx_path), os.path.splitext(os.path.basename(docx_path))[0] + "_price_list.json")
|
| 606 |
+
|
| 607 |
+
# Process the price list and save it to a JSON file
|
| 608 |
+
price_list = deepseek_extract_price_list(last_long_table, save_json=True, json_name=price_list_filename)
|
| 609 |
|
| 610 |
# Step 4: Combine contract summary and long table data into a single JSON object
|
| 611 |
print("Combining AI Generated JSON with Extracted Data...")
|
| 612 |
+
|
| 613 |
combined_data = {
|
| 614 |
"contract_summary": json.loads(json.loads(contract_summary)),
|
| 615 |
+
"price_list": price_list
|
|
|
|
|
|
|
|
|
|
| 616 |
}
|
| 617 |
|
| 618 |
# Logging
|
|
|
|
| 634 |
# extract_po("test-contract-converted.docx")
|
| 635 |
# extract_po("test-contract.docx")
|
| 636 |
|
| 637 |
+
# print(deepseek_extract_price_list([{'序号 No.': '1', '名称 Name': 'PE波纹管(双壁波纹管) PE corrugated pipe (double wall corrugated pipe)', '规格 Specification': '内径600mm,6米/根,SN8 Inner diameter 600mm, 6 meters per piece, SN8', '单位 Unit': '米m', '数量 Quantity': '180', '单价(元) Unit Price (CNY)': '106.00', '总额(元) Total Amount (CNY)': '1080.00', '几郎单价(元) Unit Price (GNF)': '16.21', '几郎总额(元) Total Amount (GNF)': '22118.38', '品牌 Brand': '鹏洲PZ', '计划来源 Planned Source': 'SMB268-GNHY-0021-WJ-20250108'}]))
|
| 638 |
+
|
| 639 |
# Gradio Interface ------------------------------
|
| 640 |
|
| 641 |
import gradio as gr
|
|
|
|
| 651 |
)
|
| 652 |
|
| 653 |
interface.launch()
|
| 654 |
+
|