Spaces:
Runtime error
Runtime error
Update update_docx_with_pdf.py
Browse files1234567891011121314151617181910 unmodified lines30313233343536373839404142434445466 unmodified lines53545556import openaiimport json# Set your OpenAI API key hereOPENAI_API_KEY = "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"# Load PDF textWORD_JSON_FILE = "word_red_data.json"PDF_TEXT_FILE = "pdf_all_text_full.txt"OUTPUT_FILE = "updated_word_data1.json"# --- Load files ---with open(WORD_JSON_FILE, "r", encoding="utf-8") as f:word_json = f.read()with open(PDF_TEXT_FILE, "r", encoding="utf-8") as f:pdf_txt = f.read()# --- Build prompt ---user_prompt = f"""10 unmodified lines- Make sure the JSON is valid and ready to use."""# --- Call OpenAI API (no env var needed) ---client = openai.OpenAI(api_key=OPENAI_API_KEY)response = client.chat.completions.create(model="gpt-4o",messages=[{"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."},{"role": "user", "content": user_prompt}],max_tokens=4096,temperature=0)updated_json_str = response.choices[0].message.content.strip()6 unmodified linesexcept Exception as e:print("⚠️ Model did not return valid JSON. Raw output below:\n")print(updated_json_str)print("\n❌ Failed to parse updated JSON:", e)123456789101112131410 unmodified lines25262728293031323334353637383940414243446 unmodified lines5152535455565758596061import openaiimport jsonimport osdef update_json_with_pdf(word_json_file, pdf_txt_file, output_file):# --- Load files ---with open(word_json_file, "r", encoding="utf-8") as f:word_json = f.read()with open(pdf_txt_file, "r", encoding="utf-8") as f:pdf_txt = f.read()# --- Build prompt ---user_prompt = f"""10 unmodified lines- Make sure the JSON is valid and ready to use."""# --- Call OpenAI API ---api_key = os.environ.get("OPENAI_API_KEY")if not api_key:raise RuntimeError("OPENAI_API_KEY not found in environment variables!")client = openai.OpenAI(api_key=api_key)response = client.chat.completions.create(model="gpt-4o",messages=[{"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."},{"role": "user", "content": user_prompt}],max_tokens=4096,temperature=0)updated_json_str = response.choices[0].message.content.strip()6 unmodified linesexcept Exception as e:print("⚠️ Model did not return valid JSON. Raw output below:\n")print(updated_json_str)print("\n❌ Failed to parse updated JSON:", e)if __name__ == "__main__":import sysif len(sys.argv) != 4:print("Usage: python update_docx_with_pdf.py <word_json_file> <pdf_txt_file> <output_json_file>")exit(1)update_json_with_pdf(sys.argv[1], sys.argv[2], sys.argv[3])
update_docx_with_pdf.py+29-24