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,12 @@ 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 |
-
|
|
|
|
| 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)
|
|
@@ -95,10 +96,22 @@ if uploaded_file is not None:
|
|
| 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 |
})
|
| 101 |
image = Image.open(io.BytesIO(image_bytes))
|
|
|
|
| 102 |
pic = slide.shapes.add_picture(io.BytesIO(image_bytes), left_img, top_img, image_width, image_height)
|
| 103 |
|
| 104 |
title_width = slide_width * 0.7 # Le titre occupe 70% de la largeur de la slide
|
|
@@ -108,39 +121,47 @@ if uploaded_file is not None:
|
|
| 108 |
title = slide.shapes.add_textbox(left_title, top_title, title_width, title_height)
|
| 109 |
title_frame = title.text_frame
|
| 110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
text_width = slide_width * 0.7 # Le texte occupe 70% de la largeur de la slide
|
| 112 |
text_height = slide_height * 0.15 # Le texte occupe 15% de la hauteur de la slide
|
| 113 |
left_text = (slide_width - text_width) / 2 # Centrez horizontalement
|
| 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 |
-
|
| 122 |
-
|
| 123 |
-
|
| 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)
|
| 132 |
p.alignment = PP_ALIGN.CENTER # Centrez le texte horizontalement
|
| 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()
|
| 140 |
prs.save(pptx_stream)
|
| 141 |
pptx_stream.seek(0)
|
| 142 |
|
| 143 |
st.markdown(
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
|
| 82 |
+
summary = summarizer(paragraph, max_length=(len(paragraph)/4), min_length=10, do_sample=False)
|
| 83 |
|
| 84 |
slide_layout = prs.slide_layouts[5] # Utilisez le modèle de diapositive approprié (index 5 pour une diapositive de titre et de contenu)
|
| 85 |
slide = prs.slides.add_slide(slide_layout)
|
|
|
|
| 96 |
left_img = (slide_width - image_width) / 2 # Centrez horizontalement
|
| 97 |
top_img = (slide_height - image_height) * 0.6 # Occupe 15% de la hauteur en haut de la slide
|
| 98 |
|
| 99 |
+
#left = prs.slide_width * 0.1
|
| 100 |
+
#top = prs.slide_height * 0.6
|
| 101 |
+
#width = prs.slide_width * 0.8
|
| 102 |
+
#height = prs.slide_height * 0.3
|
| 103 |
+
#txBox = slide.shapes.add_textbox(left, top, width, height)
|
| 104 |
+
#tf = txBox.text_frame
|
| 105 |
+
#p = tf.add_paragraph()
|
| 106 |
+
#p.text = summary[0]['summary_text']
|
| 107 |
+
|
| 108 |
+
#st.text(f"Paragraphe {i}: {summary[0]['summary_text']}") # Affiche le résumé du paragraphe
|
| 109 |
+
|
| 110 |
image_bytes = query({
|
| 111 |
"inputs": 'A picture about :' + summary[0]['summary_text'] # Utilisez le texte du résumé
|
| 112 |
})
|
| 113 |
image = Image.open(io.BytesIO(image_bytes))
|
| 114 |
+
#left = top = prs.slide_width * 0.1
|
| 115 |
pic = slide.shapes.add_picture(io.BytesIO(image_bytes), left_img, top_img, image_width, image_height)
|
| 116 |
|
| 117 |
title_width = slide_width * 0.7 # Le titre occupe 70% de la largeur de la slide
|
|
|
|
| 121 |
title = slide.shapes.add_textbox(left_title, top_title, title_width, title_height)
|
| 122 |
title_frame = title.text_frame
|
| 123 |
|
| 124 |
+
#title_p = title_frame.add_paragraph()
|
| 125 |
+
#title_p.text = "Paragraphe {i}: "
|
| 126 |
+
#title_p.alignment = PP_ALIGN.CENTER # Centrez le texte horizontalement
|
| 127 |
+
|
| 128 |
text_width = slide_width * 0.7 # Le texte occupe 70% de la largeur de la slide
|
| 129 |
text_height = slide_height * 0.15 # Le texte occupe 15% de la hauteur de la slide
|
| 130 |
left_text = (slide_width - text_width) / 2 # Centrez horizontalement
|
| 131 |
top_text = slide_height * 0.85 # Occupe 85% de la hauteur en bas de la slide
|
| 132 |
txBox = slide.shapes.add_textbox(left_text, top_text, text_width, text_height)
|
| 133 |
tf = txBox.text_frame
|
| 134 |
+
|
| 135 |
p = tf.add_paragraph()
|
| 136 |
p.text = summary[0]['summary_text']
|
| 137 |
|
| 138 |
+
# Ajustez la taille de police pour le texte afin qu'il rentre dans le cadre de texte
|
| 139 |
+
while p.space_after > Pt(0):
|
| 140 |
+
p.font.size -= Pt(1) # Réduisez la taille de police de 1 point
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
p.space_before = Pt(12) # Espace avant le paragraphe (12 points)
|
| 143 |
p.space_after = Pt(12) # Espace après le paragraphe (12 points)
|
| 144 |
p.alignment = PP_ALIGN.CENTER # Centrez le texte horizontalement
|
| 145 |
|
| 146 |
+
# Ajustez la largeur du cadre de texte du texte pour éviter le dépassement
|
| 147 |
text_frame_width = Pt(text_width - 2) # Réduisez la largeur de 0.1 pouce de chaque côté
|
| 148 |
+
#txBox.width = text_frame_width
|
| 149 |
txBox.text_frame.width = text_frame_width
|
| 150 |
+
|
| 151 |
+
# st.image(image)
|
| 152 |
|
| 153 |
i = i + 1
|
| 154 |
|
| 155 |
+
#tempfile_name = tempfile.NamedTemporaryFile(delete=False, suffix=".pptx").name
|
| 156 |
pptx_stream = io.BytesIO()
|
| 157 |
prs.save(pptx_stream)
|
| 158 |
pptx_stream.seek(0)
|
| 159 |
|
| 160 |
st.markdown(
|
| 161 |
+
f'<a href="data:application/vnd.openxmlformats-officedocument.presentationml.presentation;base64,{base64.b64encode(pptx_stream.read()).decode()}" download="presentation.pptx">Télécharger la présentation</a>',
|
| 162 |
+
unsafe_allow_html=True,
|
| 163 |
+
)
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
#st.download_button("Télécharger la présentation", "output_path", key="download_pptx", mime="application/vnd.openxmlformats-officedocument.presentationml.presentation")
|