1 line
2.8 KiB
JSON
1 line
2.8 KiB
JSON
{"repo": "abdkhan-git/stockinsightstracker", "files": {"pdf_extractor.py": {"d1a6d5a1a2ff1c8a2bf0158090aacc8c2b1b55ad": {"t": 1785674520.288727, "n": 2525, "h": "e064d20b03347152", "c": "import requests\nimport json\nimport os\n\ndef extract_pdf_content(pdf_url, api_key):\n \"\"\"\n Extracts content from a PDF using the OpenRouter API.\n\n Args:\n pdf_url (str): The URL of the PDF file.\n api_key (str): The API key for OpenRouter.\n\n Returns:\n str: The extracted content from the PDF, or an error message if extraction fails.\n \"\"\"\n headers = {\n \"Authorization\": f\"Bearer {api_key}\",\n \"Content-Type\": \"application/json\"\n }\n\n messages = [\n {\n \"role\": \"user\",\n \"content\": [\n {\n \"type\": \"text\",\n \"text\": (\n \"Please extract all the stock trades in this PDF and put them in a table. Use MarkDown formatting. Do not add additional comments or notes. Do not add markdown begin/end tags. Just the table.\\n\"\n \"Format: Owner | Asset | Type | Transaction Date | Notification Date | Amount\"\n )\n },\n {\n \"type\": \"file\",\n \"file\": {\n \"filename\": \"ptr.pdf\",\n \"file_data\": pdf_url\n }\n }\n ]\n }\n ]\n\n plugins = [\n {\n \"id\": \"file-parser\",\n \"pdf\": {\n \"engine\": \"pdf-text\" # Use free, fast text parser\n }\n }\n ]\n\n payload = {\n \"model\": os.getenv(\"AI_MODEL\", \"mistralai/mistral-small-3.2-24b-instruct:free\"),\n \"messages\": messages,\n \"plugins\": plugins\n }\n\n try:\n response = requests.post(\"https://openrouter.ai/api/v1/chat/completions\", headers=headers, json=payload)\n response.raise_for_status() # Raise HTTPError for bad responses (4xx or 5xx)\n result = response.json()\n return result[\"choices\"][0][\"message\"][\"content\"]\n except requests.exceptions.RequestException as e:\n return f\"Error: Failed to fetch PDF or API request: {e}\"\n except (KeyError, ValueError) as e:\n return f\"Error: Failed to parse API response: {e}\"\n except Exception as e:\n return f\"An unexpected error occurred: {e}\"\n\n\nif __name__ == '__main__':\n # Example usage:\n pdf_url = \"https://disclosures-clerk.house.gov/public_disc/ptr-pdfs/2025/20030630.pdf\" # Replace with a valid PDF URL\n api_key = \"sk-or-v1-\" # Replace with your actual API key\n extracted_content = extract_pdf_content(pdf_url, api_key)\n print(extracted_content)\n"}}}} |