File size: 1,453 Bytes
fbdb6be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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)