Spaces:
Running
Running
Update update_docx_with_pdf.py
Browse files- update_docx_with_pdf.py +29 -24
update_docx_with_pdf.py
CHANGED
|
@@ -1,19 +1,14 @@
|
|
| 1 |
import openai
|
| 2 |
import json
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
# Load PDF text
|
| 8 |
-
WORD_JSON_FILE = "word_red_data.json"
|
| 9 |
-
PDF_TEXT_FILE = "pdf_all_text_full.txt"
|
| 10 |
-
OUTPUT_FILE = "updated_word_data1.json"
|
| 11 |
-
|
| 12 |
-
# --- Load files ---
|
| 13 |
-
with open(WORD_JSON_FILE, "r", encoding="utf-8") as f:
|
| 14 |
-
word_json = f.read()
|
| 15 |
-
with open(PDF_TEXT_FILE, "r", encoding="utf-8") as f:
|
| 16 |
-
pdf_txt = f.read()
|
| 17 |
|
| 18 |
# --- Build prompt ---
|
| 19 |
user_prompt = f"""
|
|
@@ -30,17 +25,20 @@ Instructions:
|
|
| 30 |
- Make sure the JSON is valid and ready to use.
|
| 31 |
"""
|
| 32 |
|
| 33 |
-
# --- Call OpenAI API
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
updated_json_str = response.choices[0].message.content.strip()
|
| 46 |
|
|
@@ -53,4 +51,11 @@ try:
|
|
| 53 |
except Exception as e:
|
| 54 |
print("⚠️ Model did not return valid JSON. Raw output below:\n")
|
| 55 |
print(updated_json_str)
|
| 56 |
-
print("\n❌ Failed to parse updated JSON:", e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import openai
|
| 2 |
import json
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
+
def update_json_with_pdf(word_json_file, pdf_txt_file, output_file):
|
| 6 |
+
# --- Load files ---
|
| 7 |
+
with open(word_json_file, "r", encoding="utf-8") as f:
|
| 8 |
+
word_json = f.read()
|
| 9 |
+
with open(pdf_txt_file, "r", encoding="utf-8") as f:
|
| 10 |
+
pdf_txt = f.read()
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# --- Build prompt ---
|
| 14 |
user_prompt = f"""
|
|
|
|
| 25 |
- Make sure the JSON is valid and ready to use.
|
| 26 |
"""
|
| 27 |
|
| 28 |
+
# --- Call OpenAI API ---
|
| 29 |
+
api_key = os.environ.get("OPENAI_API_KEY")
|
| 30 |
+
if not api_key:
|
| 31 |
+
raise RuntimeError("OPENAI_API_KEY not found in environment variables!")
|
| 32 |
+
client = openai.OpenAI(api_key=api_key)
|
| 33 |
+
response = client.chat.completions.create(
|
| 34 |
+
model="gpt-4o",
|
| 35 |
+
messages=[
|
| 36 |
+
{"role": "system", "content": "You are a data extraction assistant. Only reply with valid JSON. Do not add any extra text or formatting. Do NOT use markdown/code blocks, just output JSON."},
|
| 37 |
+
{"role": "user", "content": user_prompt}
|
| 38 |
+
],
|
| 39 |
+
max_tokens=4096,
|
| 40 |
+
temperature=0
|
| 41 |
+
)
|
| 42 |
|
| 43 |
updated_json_str = response.choices[0].message.content.strip()
|
| 44 |
|
|
|
|
| 51 |
except Exception as e:
|
| 52 |
print("⚠️ Model did not return valid JSON. Raw output below:\n")
|
| 53 |
print(updated_json_str)
|
| 54 |
+
print("\n❌ Failed to parse updated JSON:", e)
|
| 55 |
+
|
| 56 |
+
if __name__ == "__main__":
|
| 57 |
+
import sys
|
| 58 |
+
if len(sys.argv) != 4:
|
| 59 |
+
print("Usage: python update_docx_with_pdf.py <word_json_file> <pdf_txt_file> <output_json_file>")
|
| 60 |
+
exit(1)
|
| 61 |
+
update_json_with_pdf(sys.argv[1], sys.argv[2], sys.argv[3])
|