Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,7 @@ import shutil
|
|
| 4 |
import tempfile
|
| 5 |
import base64
|
| 6 |
from pptx import Presentation
|
| 7 |
-
from pptx.util import Inches, Pt
|
| 8 |
from pptx.enum.text import PP_ALIGN
|
| 9 |
import streamlit as st
|
| 10 |
import fitz
|
|
@@ -24,28 +24,28 @@ def query(payload):
|
|
| 24 |
|
| 25 |
def extract_paragraphs_by_vertical_spacing(pdf_data, spacing_threshold=10):
|
| 26 |
paragraphs = []
|
| 27 |
-
|
| 28 |
try:
|
| 29 |
pdf_stream = io.BytesIO(pdf_data)
|
| 30 |
pdf_document = fitz.open(stream=pdf_stream, filetype="pdf")
|
| 31 |
-
|
| 32 |
for page_number in range(pdf_document.page_count):
|
| 33 |
page = pdf_document.load_page(page_number)
|
| 34 |
blocks = page.get_text("blocks")
|
| 35 |
-
|
| 36 |
current_paragraph = ""
|
| 37 |
previous_bottom = None
|
| 38 |
-
|
| 39 |
for block in blocks:
|
| 40 |
x0, y0, x1, y1 = block[:4] # Coordonnées du bloc de texte
|
| 41 |
text = block[4] # Texte du bloc
|
| 42 |
-
|
| 43 |
# Mesurez l'espacement vertical entre les blocs de texte
|
| 44 |
if previous_bottom is not None:
|
| 45 |
vertical_spacing = y0 - previous_bottom
|
| 46 |
else:
|
| 47 |
vertical_spacing = 0
|
| 48 |
-
|
| 49 |
# Si l'espacement vertical dépasse le seuil, considérez-le comme un nouveau paragraphe
|
| 50 |
if vertical_spacing > spacing_threshold:
|
| 51 |
if current_paragraph:
|
|
@@ -53,17 +53,17 @@ def extract_paragraphs_by_vertical_spacing(pdf_data, spacing_threshold=10):
|
|
| 53 |
current_paragraph = text
|
| 54 |
else:
|
| 55 |
current_paragraph += " " + text # Ajoutez le texte au paragraphe actuel
|
| 56 |
-
|
| 57 |
previous_bottom = y1
|
| 58 |
-
|
| 59 |
# Ajoutez le dernier paragraphe de la page
|
| 60 |
if current_paragraph:
|
| 61 |
paragraphs.append(current_paragraph.strip())
|
| 62 |
-
|
| 63 |
pdf_document.close()
|
| 64 |
except Exception as e:
|
| 65 |
print(f"Erreur lors de l'extraction du PDF : {str(e)}")
|
| 66 |
-
|
| 67 |
return paragraphs
|
| 68 |
|
| 69 |
st.title("PDF2SLIDE")
|
|
@@ -74,11 +74,11 @@ if uploaded_file is not None:
|
|
| 74 |
pdf_data = uploaded_file.read()
|
| 75 |
|
| 76 |
paragraphs = extract_paragraphs_by_vertical_spacing(pdf_data)
|
| 77 |
-
i = 1
|
| 78 |
prs = Presentation()
|
| 79 |
-
|
| 80 |
for paragraph in paragraphs:
|
| 81 |
-
summary = summarizer(paragraph, max_length=(len(paragraph)//4), min_length=10, do_sample=False)
|
| 82 |
|
| 83 |
slide_layout = prs.slide_layouts[5] # Utilisez le modèle de diapositive approprié (index 5 pour une diapositive de titre et de contenu)
|
| 84 |
slide = prs.slides.add_slide(slide_layout)
|
|
@@ -94,7 +94,7 @@ if uploaded_file is not None:
|
|
| 94 |
image_height = slide_height * 0.7 # L'image occupe 70% de la hauteur de la slide
|
| 95 |
left_img = (slide_width - image_width) / 2 # Centrez horizontalement
|
| 96 |
top_img = (slide_height - image_height) * 0.6 # Occupe 15% de la hauteur en haut de la slide
|
| 97 |
-
|
| 98 |
image_bytes = query({
|
| 99 |
"inputs": 'A picture about :' + summary[0]['summary_text'] # Utilisez le texte du résumé
|
| 100 |
})
|
|
@@ -114,14 +114,18 @@ if uploaded_file is not None:
|
|
| 114 |
top_text = slide_height * 0.85 # Occupe 85% de la hauteur en bas de la slide
|
| 115 |
txBox = slide.shapes.add_textbox(left_text, top_text, text_width, text_height)
|
| 116 |
tf = txBox.text_frame
|
| 117 |
-
|
| 118 |
p = tf.add_paragraph()
|
| 119 |
p.text = summary[0]['summary_text']
|
| 120 |
|
| 121 |
-
p.space_after = Pt(
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
p.space_before = Pt(12) # Espace avant le paragraphe (12 points)
|
| 127 |
p.space_after = Pt(12) # Espace après le paragraphe (12 points)
|
|
@@ -129,7 +133,7 @@ if uploaded_file is not None:
|
|
| 129 |
|
| 130 |
text_frame_width = Pt(text_width - 2) # Réduisez la largeur de 0.1 pouce de chaque côté
|
| 131 |
txBox.text_frame.width = text_frame_width
|
| 132 |
-
|
| 133 |
i = i + 1
|
| 134 |
|
| 135 |
pptx_stream = io.BytesIO()
|
|
|
|
| 4 |
import tempfile
|
| 5 |
import base64
|
| 6 |
from pptx import Presentation
|
| 7 |
+
from pptx.util import Inches, Pt
|
| 8 |
from pptx.enum.text import PP_ALIGN
|
| 9 |
import streamlit as st
|
| 10 |
import fitz
|
|
|
|
| 24 |
|
| 25 |
def extract_paragraphs_by_vertical_spacing(pdf_data, spacing_threshold=10):
|
| 26 |
paragraphs = []
|
| 27 |
+
|
| 28 |
try:
|
| 29 |
pdf_stream = io.BytesIO(pdf_data)
|
| 30 |
pdf_document = fitz.open(stream=pdf_stream, filetype="pdf")
|
| 31 |
+
|
| 32 |
for page_number in range(pdf_document.page_count):
|
| 33 |
page = pdf_document.load_page(page_number)
|
| 34 |
blocks = page.get_text("blocks")
|
| 35 |
+
|
| 36 |
current_paragraph = ""
|
| 37 |
previous_bottom = None
|
| 38 |
+
|
| 39 |
for block in blocks:
|
| 40 |
x0, y0, x1, y1 = block[:4] # Coordonnées du bloc de texte
|
| 41 |
text = block[4] # Texte du bloc
|
| 42 |
+
|
| 43 |
# Mesurez l'espacement vertical entre les blocs de texte
|
| 44 |
if previous_bottom is not None:
|
| 45 |
vertical_spacing = y0 - previous_bottom
|
| 46 |
else:
|
| 47 |
vertical_spacing = 0
|
| 48 |
+
|
| 49 |
# Si l'espacement vertical dépasse le seuil, considérez-le comme un nouveau paragraphe
|
| 50 |
if vertical_spacing > spacing_threshold:
|
| 51 |
if current_paragraph:
|
|
|
|
| 53 |
current_paragraph = text
|
| 54 |
else:
|
| 55 |
current_paragraph += " " + text # Ajoutez le texte au paragraphe actuel
|
| 56 |
+
|
| 57 |
previous_bottom = y1
|
| 58 |
+
|
| 59 |
# Ajoutez le dernier paragraphe de la page
|
| 60 |
if current_paragraph:
|
| 61 |
paragraphs.append(current_paragraph.strip())
|
| 62 |
+
|
| 63 |
pdf_document.close()
|
| 64 |
except Exception as e:
|
| 65 |
print(f"Erreur lors de l'extraction du PDF : {str(e)}")
|
| 66 |
+
|
| 67 |
return paragraphs
|
| 68 |
|
| 69 |
st.title("PDF2SLIDE")
|
|
|
|
| 74 |
pdf_data = uploaded_file.read()
|
| 75 |
|
| 76 |
paragraphs = extract_paragraphs_by_vertical_spacing(pdf_data)
|
| 77 |
+
i = 1
|
| 78 |
prs = Presentation()
|
| 79 |
+
|
| 80 |
for paragraph in paragraphs:
|
| 81 |
+
summary = summarizer(paragraph, max_length=(len(paragraph) // 4), min_length=10, do_sample=False)
|
| 82 |
|
| 83 |
slide_layout = prs.slide_layouts[5] # Utilisez le modèle de diapositive approprié (index 5 pour une diapositive de titre et de contenu)
|
| 84 |
slide = prs.slides.add_slide(slide_layout)
|
|
|
|
| 94 |
image_height = slide_height * 0.7 # L'image occupe 70% de la hauteur de la slide
|
| 95 |
left_img = (slide_width - image_width) / 2 # Centrez horizontalement
|
| 96 |
top_img = (slide_height - image_height) * 0.6 # Occupe 15% de la hauteur en haut de la slide
|
| 97 |
+
|
| 98 |
image_bytes = query({
|
| 99 |
"inputs": 'A picture about :' + summary[0]['summary_text'] # Utilisez le texte du résumé
|
| 100 |
})
|
|
|
|
| 114 |
top_text = slide_height * 0.85 # Occupe 85% de la hauteur en bas de la slide
|
| 115 |
txBox = slide.shapes.add_textbox(left_text, top_text, text_width, text_height)
|
| 116 |
tf = txBox.text_frame
|
| 117 |
+
|
| 118 |
p = tf.add_paragraph()
|
| 119 |
p.text = summary[0]['summary_text']
|
| 120 |
|
| 121 |
+
p.space_after = Pt(0) # Initialize space_after to 0 points
|
| 122 |
+
|
| 123 |
+
while p.space_after != Pt(12):
|
| 124 |
+
p.space_after = Pt(12) # Set space_after to 12 points again
|
| 125 |
+
p.space_after = Pt(0) # Initialize space_after to 0 points
|
| 126 |
+
p.space_after = Pt(12) # Set space_after to 12 points again
|
| 127 |
+
p.space_after = Pt(0) # Initialize space_after to 0 points
|
| 128 |
+
p.font.size -= Pt(1) # Reduce the font size by 1 point
|
| 129 |
|
| 130 |
p.space_before = Pt(12) # Espace avant le paragraphe (12 points)
|
| 131 |
p.space_after = Pt(12) # Espace après le paragraphe (12 points)
|
|
|
|
| 133 |
|
| 134 |
text_frame_width = Pt(text_width - 2) # Réduisez la largeur de 0.1 pouce de chaque côté
|
| 135 |
txBox.text_frame.width = text_frame_width
|
| 136 |
+
|
| 137 |
i = i + 1
|
| 138 |
|
| 139 |
pptx_stream = io.BytesIO()
|