Spaces:
Running
Running
Update parse.py
Browse files
parse.py
CHANGED
@@ -1,6 +1,13 @@
|
|
1 |
from langchain_core.prompts import ChatPromptTemplate
|
2 |
from langchain_openai import ChatOpenAI
|
|
|
3 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
# Load OpenRouter API Key
|
6 |
openrouter_api_key = "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"
|
@@ -14,18 +21,18 @@ model = ChatOpenAI(
|
|
14 |
# Create a chat prompt template
|
15 |
template = (
|
16 |
"You are tasked with extracting specific information from the following text content: {dom_content}. "
|
17 |
-
"Please follow these instructions carefully
|
18 |
-
"1. **Extract
|
19 |
-
"2. **
|
20 |
-
"3. **
|
21 |
-
"
|
22 |
-
"
|
23 |
-
"
|
24 |
-
"
|
25 |
-
"
|
26 |
-
"
|
27 |
-
"
|
28 |
-
"
|
29 |
)
|
30 |
|
31 |
# Function to parse and extract information from the chunks
|
@@ -45,3 +52,35 @@ def parse(dom_chunks, parse_description):
|
|
45 |
|
46 |
# Return the parsed results as a single string
|
47 |
return "\n".join(parsed_results)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from langchain_core.prompts import ChatPromptTemplate
|
2 |
from langchain_openai import ChatOpenAI
|
3 |
+
from langchain_core.messages import HumanMessage
|
4 |
import os
|
5 |
+
import pandas as pd
|
6 |
+
|
7 |
+
from huggingface_hub import login
|
8 |
+
|
9 |
+
login("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")
|
10 |
+
|
11 |
|
12 |
# Load OpenRouter API Key
|
13 |
openrouter_api_key = "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"
|
|
|
21 |
# Create a chat prompt template
|
22 |
template = (
|
23 |
"You are tasked with extracting specific information from the following text content: {dom_content}. "
|
24 |
+
"Please follow these instructions carefully:\n\n"
|
25 |
+
"1. **Task:** Extract data from the provided text that matches the description: {parse_description}.\n"
|
26 |
+
"2. **Output Format:** Return the extracted data ONLY as one or more Markdown tables. Each table MUST be correctly formatted.\n"
|
27 |
+
"3. **Markdown Table Format:** Each table must adhere to the following Markdown format:\n"
|
28 |
+
" - Start with a header row, clearly labeling each column, separated by pipes (|).\n"
|
29 |
+
" - Follow the header row with an alignment row, using hyphens (-) to indicate column alignment (e.g., --- for left alignment).\n"
|
30 |
+
" - Subsequent rows should contain the data, with cells aligned according to the alignment row.\n"
|
31 |
+
" - Use pipes (|) to separate columns in each data row.\n"
|
32 |
+
"4. **No Explanations:** Do not include any introductory or explanatory text before or after the table(s).\n"
|
33 |
+
"5. **Empty Response:** If no information matches the description, return an empty string ('').\n"
|
34 |
+
"6. **Multiple Tables:** If the text contains multiple tables matching the description, return each table separately, following the Markdown format for each.\n"
|
35 |
+
"7. **Accuracy:** The extracted data must be accurate and reflect the information in the provided text.\n"
|
36 |
)
|
37 |
|
38 |
# Function to parse and extract information from the chunks
|
|
|
52 |
|
53 |
# Return the parsed results as a single string
|
54 |
return "\n".join(parsed_results)
|
55 |
+
|
56 |
+
def merge_tables_with_llm(tables, parse_description):
|
57 |
+
"""Merges a list of Pandas DataFrames into a single Markdown table using LLM."""
|
58 |
+
from langchain_core.prompts import ChatPromptTemplate
|
59 |
+
from langchain_openai import ChatOpenAI
|
60 |
+
# Convert DataFrames to Markdown strings
|
61 |
+
table_strings = [table.to_markdown(index=False) for table in tables]
|
62 |
+
|
63 |
+
# Create a prompt for the LLM
|
64 |
+
merge_prompt = (
|
65 |
+
"You are tasked with merging the following Markdown tables into a single, comprehensive Markdown table.\n"
|
66 |
+
"The tables contain information related to: {parse_description}.\n"
|
67 |
+
"Please follow these instructions carefully:\n\n"
|
68 |
+
"1. **Task:** Merge the data from the following tables into a single table that matches the description: {parse_description}.\n"
|
69 |
+
"2. **Output Format:** Return the merged data ONLY as a single Markdown table. The table MUST be correctly formatted.\n"
|
70 |
+
"3. **Markdown Table Format:** The table must adhere to the following Markdown format:\n"
|
71 |
+
" - Start with a header row, clearly labeling each column, separated by pipes (|).\n"
|
72 |
+
" - Follow the header row with an alignment row, using hyphens (-) to indicate column alignment (e.g., --- for left alignment).\n"
|
73 |
+
" - Subsequent rows should contain the data, with cells aligned according to the alignment row.\n"
|
74 |
+
" - Use pipes (|) to separate columns in each data row.\n"
|
75 |
+
"4. **No Explanations:** Do not include any introductory or explanatory text before or after the table.\n"
|
76 |
+
"5. **Empty Response:** If no information matches the description, return an empty string ('') if no data can be merged.\n"
|
77 |
+
"6. **Duplicate Columns:** If there are duplicate columns, rename them to be unique.\n"
|
78 |
+
"7. **Missing Values:** If there are missing values, fill them with 'N/A'.\n\n"
|
79 |
+
"Here are the tables:\n\n" + "\n\n".join(table_strings) +
|
80 |
+
"\n\nReturn the merged table in Markdown format:"
|
81 |
+
)
|
82 |
+
|
83 |
+
# Invoke the LLM
|
84 |
+
message = HumanMessage(content=merge_prompt)
|
85 |
+
response = model.invoke([message])
|
86 |
+
return response.content
|