Spaces:
Runtime error
Runtime error
Update app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707128 unmodified lines1001011021031041051064 unmodified lines111112113114115116117from PyPDF2 import PdfReaderimport reimport streamlit as stimport fitzfrom transformers import pipelineimport osimport requestsimport iofrom PIL import Imagefrom pptx import Presentationfrom pptx.util import Inchesimport tempfileAPI_URL = "https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5"headers = {"Authorization": "Bearer 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"}summarizer = pipeline("summarization", model="facebook/bart-large-cnn")def query(payload):response = requests.post(API_URL, headers=headers, json=payload)return response.contentdef extract_paragraphs_by_vertical_spacing(pdf_data, spacing_threshold=10):paragraphs = []try:pdf_stream = io.BytesIO(pdf_data)pdf_document = fitz.open(stream=pdf_stream, filetype="pdf")for page_number in range(pdf_document.page_count):page = pdf_document.load_page(page_number)blocks = page.get_text("blocks")current_paragraph = ""previous_bottom = Nonefor block in blocks:x0, y0, x1, y1 = block[:4] # Coordonnées du bloc de textetext = block[4] # Texte du bloc# Mesurez l'espacement vertical entre les blocs de texteif previous_bottom is not None:vertical_spacing = y0 - previous_bottomelse:vertical_spacing = 0# Si l'espacement vertical dépasse le seuil, considérez-le comme un nouveau paragrapheif vertical_spacing > spacing_threshold:if current_paragraph:paragraphs.append(current_paragraph.strip())current_paragraph = textelse:current_paragraph += " " + text # Ajoutez le texte au paragraphe actuelprevious_bottom = y1# Ajoutez le dernier paragraphe de la pageif current_paragraph:paragraphs.append(current_paragraph.strip())pdf_document.close()except Exception as e:print(f"Erreur lors de l'extraction du PDF : {str(e)}")return paragraphsst.title("PDF2SLIDE")uploaded_file = st.file_uploader("Selectionnez un PDF", type=["pdf"])if uploaded_file is not None:pdf_data = uploaded_file.read()28 unmodified linesimage = Image.open(io.BytesIO(image_bytes))temp_img_path = tempfile.NamedTemporaryFile(delete=False, suffix=".png").nameimage.save(temp_img_path)slide.shapes.add_picture(temp_img_path, Inches(1), Inches(1.5), Inches(5), Inches(3))i += 14 unmodified lines# Display a download button for the PowerPoint filest.download_button(label="Download PowerPoint Presentation",data=presentation_path,key="download_ppt",file_name="PDF2SLIDE_Presentation.pptx",)12345678910111213141528 unmodified lines444546474849505152534 unmodified lines58596061626364import streamlit as stfrom pptx import Presentationfrom pptx.util import Inchesimport tempfilefrom PIL import Imageimport io# ... (previous code)st.title("PDF2SLIDE")uploaded_file = st.file_uploader("Select a PDF", type=["pdf"])if uploaded_file is not None:pdf_data = uploaded_file.read()28 unmodified linesimage = Image.open(io.BytesIO(image_bytes))temp_img_path = tempfile.NamedTemporaryFile(delete=False, suffix=".png").nameimage.save(temp_img_path)left = Inches(1)top = Inches(2)pic = slide.shapes.add_picture(temp_img_path, left, top, width, height)i += 14 unmodified lines# Display a download button for the PowerPoint filest.download_button(label="Download PowerPoint Presentation",data=open(presentation_path, "rb"),key="download_ppt",file_name="PDF2SLIDE_Presentation.pptx",)
app.py+10-63