| from pdf2image import convert_from_bytes | |
| import streamlit as st | |
| import os | |
| import tempfile | |
| def display_pdf(content: bytes): | |
| try: | |
| images = convert_from_bytes(content) | |
| for i, image in enumerate(images): | |
| st.image(image, caption=f"Page {i+1}", use_container_width=True) | |
| except Exception as e: | |
| st.error(f"Impossible d'afficher le PDF : {e}") | |
| st.download_button( | |
| label="📥 Télécharger le PDF", | |
| data=content, | |
| file_name="document.pdf", | |
| mime="application/pdf" | |
| ) | |
| def upload_pdf(client, content, filename): | |
| """ | |
| Uploads a PDF to Mistral's API and retrieves a signed URL for processing. | |
| Args: | |
| client (Mistral): Mistral API client instance. | |
| content (bytes): The content of the PDF file. | |
| filename (str): The name of the PDF file. | |
| Returns: | |
| str: Signed URL for the uploaded PDF. | |
| """ | |
| with tempfile.TemporaryDirectory() as temp_dir: | |
| temp_path = os.path.join(temp_dir, filename) | |
| with open(temp_path, "wb") as tmp: | |
| tmp.write(content) | |
| try: | |
| with open(temp_path, "rb") as file_obj: | |
| file_upload = client.files.upload( | |
| file={"file_name": filename, "content": file_obj}, | |
| purpose="ocr" | |
| ) | |
| signed_url = client.files.get_signed_url(file_id=file_upload.id) | |
| return signed_url.url | |
| finally: | |
| if os.path.exists(temp_path): | |
| os.remove(temp_path) |