| import base64 | |
| from mistralai import Mistral | |
| import os | |
| client = Mistral(api_key=os.environ["MISTRAL_API_KEY"]) | |
| print("MISTRAL_API_KEY =", os.getenv("MISTRAL_API_KEY")) | |
| def extract_text_from_pdf(pdf_path: str) -> str: | |
| with open(pdf_path, "rb") as f: | |
| pdf_bytes = f.read() | |
| b64 = base64.b64encode(pdf_bytes).decode("utf-8") | |
| resp = client.ocr.process( | |
| model="mistral-ocr-latest", | |
| document={ | |
| "type": "document_url", | |
| "document_url": f"data:application/pdf;base64,{b64}", | |
| }, | |
| include_image_base64=False, | |
| ) | |
| pages_text = [] | |
| for page in resp.pages: | |
| pages_text.append(page.markdown) | |
| return "\n\n".join(pages_text) | |