Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -399,7 +399,7 @@ def markdown_to_html(markdown_text: str) -> str:
|
|
399 |
# On rend le prompt pour get_image_description paramétrable
|
400 |
async def get_image_description(base64_image: str, prompt: str) -> str:
|
401 |
try:
|
402 |
-
#
|
403 |
content = f"{prompt}\n\n"
|
404 |
logging.debug(f"Contenu envoyé à l'API OpenAI : {content}")
|
405 |
|
@@ -414,6 +414,10 @@ async def get_image_description(base64_image: str, prompt: str) -> str:
|
|
414 |
)
|
415 |
logging.debug(f"Réponse de l'API OpenAI : {response}")
|
416 |
|
|
|
|
|
|
|
|
|
417 |
description = response.choices[0].message.content.strip()
|
418 |
logging.debug(f"Description obtenue : {description}")
|
419 |
return description
|
@@ -422,6 +426,7 @@ async def get_image_description(base64_image: str, prompt: str) -> str:
|
|
422 |
return "Description indisponible."
|
423 |
|
424 |
|
|
|
425 |
# MODIFICATIONS END
|
426 |
|
427 |
async def rewrite_html_accessible(html_content: str) -> str:
|
@@ -756,10 +761,10 @@ def extract_images_from_pdf(input_filename: str) -> List[bytes]:
|
|
756 |
def extract_text_with_image_markers(input_filename: str) -> Tuple[str, List[Tuple[int, bytes]]]:
|
757 |
"""
|
758 |
Extrait le texte d'un PDF en insérant des marqueurs pour les images.
|
759 |
-
|
760 |
Args:
|
761 |
input_filename (str): Chemin vers le fichier PDF.
|
762 |
-
|
763 |
Returns:
|
764 |
Tuple[str, List[Tuple[int, bytes]]]: Le texte extrait avec des marqueurs et une liste d'images extraites.
|
765 |
"""
|
@@ -767,38 +772,33 @@ def extract_text_with_image_markers(input_filename: str) -> Tuple[str, List[Tupl
|
|
767 |
images = []
|
768 |
with fitz.open(input_filename) as doc:
|
769 |
for page_num, page in enumerate(doc, start=1):
|
770 |
-
|
771 |
-
|
772 |
-
|
773 |
-
|
774 |
-
|
775 |
-
|
776 |
-
|
777 |
-
|
778 |
-
|
779 |
-
|
780 |
-
|
781 |
-
|
782 |
-
|
783 |
-
|
784 |
-
text += '\n' # Saut de ligne après chaque ligne de texte
|
785 |
-
elif block['type'] == 1: # Image
|
786 |
-
# Insérer un marqueur unique pour l'image
|
787 |
img_num = len(images) + 1
|
788 |
marker = f"[IMG_{img_num}]"
|
789 |
-
text += marker + '\n'
|
790 |
-
|
791 |
-
|
792 |
-
|
793 |
-
|
794 |
-
|
795 |
-
|
796 |
-
|
797 |
-
|
798 |
-
|
799 |
-
|
800 |
-
logging.debug(f"Total text length: {len(text)} characters.")
|
801 |
-
logging.debug(f"Total images extracted: {len(images)}.")
|
802 |
return text, images
|
803 |
|
804 |
|
@@ -929,7 +929,6 @@ async def convert_file_to_txt(
|
|
929 |
description_text = f"Image {img_num}: {desc}"
|
930 |
text = text.replace(marker, description_text)
|
931 |
logging.debug("Remplacement des marqueurs d'images par les descriptions terminé.")
|
932 |
-
|
933 |
else:
|
934 |
logging.debug("Aucune image trouvée. Aucun remplacement de marqueur effectué.")
|
935 |
|
|
|
399 |
# On rend le prompt pour get_image_description paramétrable
|
400 |
async def get_image_description(base64_image: str, prompt: str) -> str:
|
401 |
try:
|
402 |
+
# Préparer le contenu avec le prompt et l'image en markdown
|
403 |
content = f"{prompt}\n\n"
|
404 |
logging.debug(f"Contenu envoyé à l'API OpenAI : {content}")
|
405 |
|
|
|
414 |
)
|
415 |
logging.debug(f"Réponse de l'API OpenAI : {response}")
|
416 |
|
417 |
+
if not response.choices:
|
418 |
+
logging.error("Aucune réponse reçue de l'API OpenAI.")
|
419 |
+
return "Description indisponible."
|
420 |
+
|
421 |
description = response.choices[0].message.content.strip()
|
422 |
logging.debug(f"Description obtenue : {description}")
|
423 |
return description
|
|
|
426 |
return "Description indisponible."
|
427 |
|
428 |
|
429 |
+
|
430 |
# MODIFICATIONS END
|
431 |
|
432 |
async def rewrite_html_accessible(html_content: str) -> str:
|
|
|
761 |
def extract_text_with_image_markers(input_filename: str) -> Tuple[str, List[Tuple[int, bytes]]]:
|
762 |
"""
|
763 |
Extrait le texte d'un PDF en insérant des marqueurs pour les images.
|
764 |
+
|
765 |
Args:
|
766 |
input_filename (str): Chemin vers le fichier PDF.
|
767 |
+
|
768 |
Returns:
|
769 |
Tuple[str, List[Tuple[int, bytes]]]: Le texte extrait avec des marqueurs et une liste d'images extraites.
|
770 |
"""
|
|
|
772 |
images = []
|
773 |
with fitz.open(input_filename) as doc:
|
774 |
for page_num, page in enumerate(doc, start=1):
|
775 |
+
text += f"<!--PAGE_{page_num}-->\n"
|
776 |
+
|
777 |
+
# Extraction du texte
|
778 |
+
page_text = page.get_text("text")
|
779 |
+
text += page_text + '\n'
|
780 |
+
|
781 |
+
# Extraction des images
|
782 |
+
image_list = page.get_images(full=True)
|
783 |
+
for img in image_list:
|
784 |
+
xref = img[0]
|
785 |
+
try:
|
786 |
+
base_image = doc.extract_image(xref)
|
787 |
+
image_bytes = base_image["image"]
|
788 |
+
|
|
|
|
|
|
|
789 |
img_num = len(images) + 1
|
790 |
marker = f"[IMG_{img_num}]"
|
791 |
+
text += marker + '\n'
|
792 |
+
|
793 |
+
images.append((img_num, image_bytes))
|
794 |
+
logging.debug(f"Image {img_num} extraite de la page {page_num}.")
|
795 |
+
except Exception as e:
|
796 |
+
logging.error(f"Erreur lors de l'extraction de l'image xref={xref} sur la page {page_num} : {str(e)}")
|
797 |
+
|
798 |
+
logging.debug(f"Page {page_num}: {len(images)} images extraites jusqu'à présent.")
|
799 |
+
|
800 |
+
logging.debug(f"Total text length: {len(text)} caractères.")
|
801 |
+
logging.debug(f"Total images extraites: {len(images)}.")
|
|
|
|
|
802 |
return text, images
|
803 |
|
804 |
|
|
|
929 |
description_text = f"Image {img_num}: {desc}"
|
930 |
text = text.replace(marker, description_text)
|
931 |
logging.debug("Remplacement des marqueurs d'images par les descriptions terminé.")
|
|
|
932 |
else:
|
933 |
logging.debug("Aucune image trouvée. Aucun remplacement de marqueur effectué.")
|
934 |
|