Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import fitz
|
| 3 |
from transformers import pipeline
|
| 4 |
import os
|
| 5 |
import requests
|
|
@@ -10,48 +12,76 @@ API_URL = "https://api-inference.huggingface.co/models/runwayml/stable-diffusion
|
|
| 10 |
headers = {"Authorization": "Bearer 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"}
|
| 11 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 12 |
|
| 13 |
-
# Fonction pour extraire les longs paragraphes à partir du texte
|
| 14 |
-
def extraire_long_paragraphes(texte, longueur_maximale=999):
|
| 15 |
-
paragraphes = texte.split("\n\n") # Divise le texte en paragraphes en utilisant des doubles sauts de ligne
|
| 16 |
-
longs_paragraphes = [paragraphe.strip() for paragraphe in paragraphes if len(paragraphe) >= longueur_maximale]
|
| 17 |
-
return longs_paragraphes
|
| 18 |
-
|
| 19 |
def query(payload):
|
| 20 |
response = requests.post(API_URL, headers=headers, json=payload)
|
| 21 |
return response.content
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
# Lecture du contenu du fichier PDF
|
| 31 |
-
pdf_document = fitz.open(stream=uploaded_file.read(), filetype="pdf")
|
| 32 |
-
|
| 33 |
-
# Créez une liste pour stocker les paragraphes
|
| 34 |
-
paragraphs = []
|
| 35 |
|
| 36 |
-
|
| 37 |
-
for page_number in range(pdf_document.page_count):
|
| 38 |
-
page = pdf_document.load_page(page_number)
|
| 39 |
-
page_text = page.get_text()
|
| 40 |
-
page_paragraphs = page_text.split("\n\n") # Divisez en paragraphes
|
| 41 |
-
paragraphs.extend(page_paragraphs)
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
for
|
| 49 |
-
|
|
|
|
| 50 |
st.text(f"Paragraphe {i}: {summary[0]['summary_text']}") # Affiche le résumé du paragraphe
|
| 51 |
|
| 52 |
-
# Générer une image à partir du résumé
|
| 53 |
image_bytes = query({
|
| 54 |
-
"inputs": summary[0]['summary_text']
|
| 55 |
})
|
| 56 |
image = Image.open(io.BytesIO(image_bytes))
|
| 57 |
st.image(image)
|
|
|
|
|
|
|
|
|
| 1 |
+
from PyPDF2 import PdfReader
|
| 2 |
+
import re
|
| 3 |
import streamlit as st
|
| 4 |
+
import fitz
|
| 5 |
from transformers import pipeline
|
| 6 |
import os
|
| 7 |
import requests
|
|
|
|
| 12 |
headers = {"Authorization": "Bearer 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"}
|
| 13 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
def query(payload):
|
| 16 |
response = requests.post(API_URL, headers=headers, json=payload)
|
| 17 |
return response.content
|
| 18 |
|
| 19 |
+
def extract_paragraphs_by_vertical_spacing(pdf_data, spacing_threshold=10):
|
| 20 |
+
paragraphs = []
|
| 21 |
+
|
| 22 |
+
try:
|
| 23 |
+
pdf_stream = io.BytesIO(pdf_data)
|
| 24 |
+
pdf_document = fitz.open(stream=pdf_stream, filetype="pdf")
|
| 25 |
+
|
| 26 |
+
for page_number in range(pdf_document.page_count):
|
| 27 |
+
page = pdf_document.load_page(page_number)
|
| 28 |
+
blocks = page.get_text("blocks")
|
| 29 |
+
|
| 30 |
+
current_paragraph = ""
|
| 31 |
+
previous_bottom = None
|
| 32 |
+
|
| 33 |
+
for block in blocks:
|
| 34 |
+
x0, y0, x1, y1 = block[:4] # Coordonnées du bloc de texte
|
| 35 |
+
text = block[4] # Texte du bloc
|
| 36 |
+
|
| 37 |
+
# Mesurez l'espacement vertical entre les blocs de texte
|
| 38 |
+
if previous_bottom is not None:
|
| 39 |
+
vertical_spacing = y0 - previous_bottom
|
| 40 |
+
else:
|
| 41 |
+
vertical_spacing = 0
|
| 42 |
+
|
| 43 |
+
# Si l'espacement vertical dépasse le seuil, considérez-le comme un nouveau paragraphe
|
| 44 |
+
if vertical_spacing > spacing_threshold:
|
| 45 |
+
if current_paragraph:
|
| 46 |
+
paragraphs.append(current_paragraph.strip())
|
| 47 |
+
current_paragraph = text
|
| 48 |
+
else:
|
| 49 |
+
current_paragraph += " " + text # Ajoutez le texte au paragraphe actuel
|
| 50 |
+
|
| 51 |
+
previous_bottom = y1
|
| 52 |
+
|
| 53 |
+
# Ajoutez le dernier paragraphe de la page
|
| 54 |
+
if current_paragraph:
|
| 55 |
+
paragraphs.append(current_paragraph.strip())
|
| 56 |
+
|
| 57 |
+
pdf_document.close()
|
| 58 |
+
except Exception as e:
|
| 59 |
+
print(f"Erreur lors de l'extraction du PDF : {str(e)}")
|
| 60 |
+
|
| 61 |
+
return paragraphs
|
| 62 |
|
| 63 |
+
#def extract_paragraph(texte):
|
| 64 |
+
# paragraph = texte.split("\n\n")
|
| 65 |
+
# return paragraph
|
| 66 |
|
| 67 |
+
st.title("PDF2SLIDE")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
+
uploaded_file = st.file_uploader("Selectionnez un PDF", type=["pdf"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
+
if uploaded_file is not None:
|
| 72 |
+
pdf_data = uploaded_file.read()
|
| 73 |
|
| 74 |
+
paragraphs = extract_paragraphs_by_vertical_spacing(pdf_data)
|
| 75 |
+
i = 1
|
| 76 |
+
for paragraph in paragraphs:
|
| 77 |
+
|
| 78 |
+
summary = summarizer(paragraph, max_length=(len(paragraph)/2), min_length=10, do_sample=False)
|
| 79 |
st.text(f"Paragraphe {i}: {summary[0]['summary_text']}") # Affiche le résumé du paragraphe
|
| 80 |
|
|
|
|
| 81 |
image_bytes = query({
|
| 82 |
+
"inputs": summary[0]['summary_text'] # Utilisez le texte du résumé
|
| 83 |
})
|
| 84 |
image = Image.open(io.BytesIO(image_bytes))
|
| 85 |
st.image(image)
|
| 86 |
+
|
| 87 |
+
i = i + 1
|