Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,8 +8,7 @@ import requests
|
|
| 8 |
import io
|
| 9 |
from PIL import Image
|
| 10 |
from pptx import Presentation
|
| 11 |
-
from pptx.util import Inches
|
| 12 |
-
import tempfile
|
| 13 |
|
| 14 |
API_URL = "https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5"
|
| 15 |
headers = {"Authorization": "Bearer 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"}
|
|
@@ -20,8 +19,20 @@ def query(payload):
|
|
| 20 |
return response.content
|
| 21 |
|
| 22 |
def add_line_breaks_to_summary(summary_text, line_length):
|
| 23 |
-
# Split the summary text into lines
|
| 24 |
-
lines = [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
return "\n".join(lines)
|
| 26 |
|
| 27 |
def extract_paragraphs_by_vertical_spacing(pdf_data, spacing_threshold=10):
|
|
@@ -80,7 +91,7 @@ if uploaded_file is not None:
|
|
| 80 |
|
| 81 |
for paragraph in paragraphs:
|
| 82 |
summary = summarizer(paragraph, max_length=(len(paragraph) / 2), min_length=10, do_sample=False)
|
| 83 |
-
summary_text = add_line_breaks_to_summary(summary[0]['summary_text'],
|
| 84 |
|
| 85 |
# Generate and save the image to a temporary file
|
| 86 |
image_bytes = query({
|
|
@@ -93,23 +104,25 @@ if uploaded_file is not None:
|
|
| 93 |
# Create a slide
|
| 94 |
slide = prs.slides.add_slide(prs.slide_layouts[5])
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
# Add the paragraph to the slide at the top
|
| 97 |
-
left =
|
| 98 |
-
|
|
|
|
| 99 |
height = Inches(1.5)
|
| 100 |
txBox = slide.shapes.add_textbox(left, top, width, height)
|
| 101 |
tf = txBox.text_frame
|
| 102 |
p = tf.add_paragraph()
|
| 103 |
p.text = f"Paragraph {i}:"
|
| 104 |
-
p.space_after =
|
| 105 |
p = tf.add_paragraph()
|
| 106 |
p.text = summary_text
|
| 107 |
-
|
| 108 |
-
# Add the image to the slide at the bottom
|
| 109 |
-
left = Inches(1)
|
| 110 |
-
top = Inches(2)
|
| 111 |
-
width = height = Inches(5)
|
| 112 |
-
pic = slide.shapes.add_picture(temp_img_path, left, top, width, height)
|
| 113 |
|
| 114 |
i += 1
|
| 115 |
|
|
|
|
| 8 |
import io
|
| 9 |
from PIL import Image
|
| 10 |
from pptx import Presentation
|
| 11 |
+
from pptx.util import Inches, Pt
|
|
|
|
| 12 |
|
| 13 |
API_URL = "https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5"
|
| 14 |
headers = {"Authorization": "Bearer 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"}
|
|
|
|
| 19 |
return response.content
|
| 20 |
|
| 21 |
def add_line_breaks_to_summary(summary_text, line_length):
|
| 22 |
+
# Split the summary text into lines without breaking words
|
| 23 |
+
lines = []
|
| 24 |
+
words = summary_text.split()
|
| 25 |
+
current_line = ""
|
| 26 |
+
for word in words:
|
| 27 |
+
if len(current_line) + len(word) + 1 <= line_length: # Include space between words
|
| 28 |
+
if current_line:
|
| 29 |
+
current_line += " "
|
| 30 |
+
current_line += word
|
| 31 |
+
else:
|
| 32 |
+
lines.append(current_line)
|
| 33 |
+
current_line = word
|
| 34 |
+
if current_line:
|
| 35 |
+
lines.append(current_line)
|
| 36 |
return "\n".join(lines)
|
| 37 |
|
| 38 |
def extract_paragraphs_by_vertical_spacing(pdf_data, spacing_threshold=10):
|
|
|
|
| 91 |
|
| 92 |
for paragraph in paragraphs:
|
| 93 |
summary = summarizer(paragraph, max_length=(len(paragraph) / 2), min_length=10, do_sample=False)
|
| 94 |
+
summary_text = add_line_breaks_to_summary(summary[0]['summary_text'], 40)
|
| 95 |
|
| 96 |
# Generate and save the image to a temporary file
|
| 97 |
image_bytes = query({
|
|
|
|
| 104 |
# Create a slide
|
| 105 |
slide = prs.slides.add_slide(prs.slide_layouts[5])
|
| 106 |
|
| 107 |
+
# Add the image to the slide at the bottom (resized to 500x500 pixels)
|
| 108 |
+
left = Inches(1)
|
| 109 |
+
top = Inches(2)
|
| 110 |
+
width = height = Inches(5)
|
| 111 |
+
pic = slide.shapes.add_picture(temp_img_path, left, top, width, height)
|
| 112 |
+
|
| 113 |
# Add the paragraph to the slide at the top
|
| 114 |
+
left = Inches(1)
|
| 115 |
+
top = Inches(1)
|
| 116 |
+
width = Inches(8) # Adjust the width as needed
|
| 117 |
height = Inches(1.5)
|
| 118 |
txBox = slide.shapes.add_textbox(left, top, width, height)
|
| 119 |
tf = txBox.text_frame
|
| 120 |
p = tf.add_paragraph()
|
| 121 |
p.text = f"Paragraph {i}:"
|
| 122 |
+
p.space_after = Pt(8) # Adjust the spacing as needed
|
| 123 |
p = tf.add_paragraph()
|
| 124 |
p.text = summary_text
|
| 125 |
+
p.space_after = Pt(0) # Adjust the spacing as needed
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
i += 1
|
| 128 |
|