Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -53,11 +53,38 @@ def process_pdf(pdf_path, session_id):
|
|
| 53 |
thumb_path = os.path.join(thumbs_folder, f"thumb_{page_num + 1}.png")
|
| 54 |
create_thumbnail(image_path, thumb_path)
|
| 55 |
|
| 56 |
-
# Add page
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
pages_info.append({
|
| 58 |
"src": os.path.join("output", session_id, f"page_{page_num + 1}.png"),
|
| 59 |
"thumb": os.path.join("thumbs", session_id, f"thumb_{page_num + 1}.png"),
|
| 60 |
-
"title": f"Page {page_num + 1}"
|
|
|
|
|
|
|
| 61 |
})
|
| 62 |
|
| 63 |
return pages_info
|
|
@@ -84,11 +111,50 @@ def process_images(image_paths, session_id):
|
|
| 84 |
thumb_path = os.path.join(thumbs_folder, f"thumb_{i + 1}.png")
|
| 85 |
create_thumbnail(img_path, thumb_path)
|
| 86 |
|
| 87 |
-
# Add
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
pages_info.append({
|
| 89 |
"src": os.path.join("output", session_id, f"image_{i + 1}.png"),
|
| 90 |
"thumb": os.path.join("thumbs", session_id, f"thumb_{i + 1}.png"),
|
| 91 |
-
"title": f"
|
|
|
|
|
|
|
| 92 |
})
|
| 93 |
except Exception as e:
|
| 94 |
print(f"Error processing image {img_path}: {e}")
|
|
@@ -102,12 +168,10 @@ def create_flipbook_from_pdf(pdf_file, view_mode="webgl", skin="light"):
|
|
| 102 |
pages_info = []
|
| 103 |
|
| 104 |
if pdf_file is not None:
|
| 105 |
-
#
|
| 106 |
-
pdf_path =
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
# Process PDF
|
| 111 |
pages_info = process_pdf(pdf_path, session_id)
|
| 112 |
else:
|
| 113 |
return """<div style="color: red; padding: 20px;">Please upload a PDF file.</div>"""
|
|
@@ -130,8 +194,9 @@ def create_flipbook_from_images(images, view_mode="webgl", skin="light"):
|
|
| 130 |
pages_info = []
|
| 131 |
|
| 132 |
if images is not None and len(images) > 0:
|
| 133 |
-
# Process images
|
| 134 |
-
|
|
|
|
| 135 |
else:
|
| 136 |
return """<div style="color: red; padding: 20px;">Please upload at least one image.</div>"""
|
| 137 |
|
|
@@ -148,6 +213,13 @@ def create_flipbook_from_images(images, view_mode="webgl", skin="light"):
|
|
| 148 |
|
| 149 |
def generate_flipbook_html(pages_info, session_id, view_mode, skin):
|
| 150 |
"""Generate HTML for the flipbook."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
# Convert pages_info to JSON for JavaScript
|
| 152 |
pages_json = json.dumps(pages_info)
|
| 153 |
|
|
@@ -198,12 +270,19 @@ def generate_flipbook_html(pages_info, session_id, view_mode, skin):
|
|
| 198 |
singlePageMode: false,
|
| 199 |
singlePageModeIfMobile: true,
|
| 200 |
pageFlipDuration: 1,
|
| 201 |
-
sound:
|
|
|
|
| 202 |
thumbnailsOnStart: true,
|
| 203 |
btnThumbs: {{ enabled: true }},
|
| 204 |
-
btnPrint: {{ enabled:
|
| 205 |
-
btnDownloadPages: {{ enabled:
|
| 206 |
-
btnDownloadPdf: {{ enabled:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
}};
|
| 208 |
|
| 209 |
const container = document.getElementById('{flipbook_id}');
|
|
@@ -215,10 +294,19 @@ def generate_flipbook_html(pages_info, session_id, view_mode, skin):
|
|
| 215 |
"""
|
| 216 |
return html
|
| 217 |
|
| 218 |
-
# Define the Gradio interface
|
| 219 |
with gr.Blocks(title="3D Flipbook Viewer") as demo:
|
| 220 |
gr.Markdown("# 3D Flipbook Viewer")
|
| 221 |
-
gr.Markdown("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
|
| 223 |
with gr.Tabs():
|
| 224 |
with gr.TabItem("PDF Upload"):
|
|
@@ -236,7 +324,7 @@ with gr.Blocks(title="3D Flipbook Viewer") as demo:
|
|
| 236 |
label="Skin"
|
| 237 |
)
|
| 238 |
|
| 239 |
-
pdf_create_btn = gr.Button("Create Flipbook from PDF")
|
| 240 |
pdf_output = gr.HTML(label="Flipbook Output")
|
| 241 |
|
| 242 |
# Set up PDF event handler
|
|
@@ -261,7 +349,7 @@ with gr.Blocks(title="3D Flipbook Viewer") as demo:
|
|
| 261 |
label="Skin"
|
| 262 |
)
|
| 263 |
|
| 264 |
-
img_create_btn = gr.Button("Create Flipbook from Images")
|
| 265 |
img_output = gr.HTML(label="Flipbook Output")
|
| 266 |
|
| 267 |
# Set up image event handler
|
|
@@ -271,6 +359,21 @@ with gr.Blocks(title="3D Flipbook Viewer") as demo:
|
|
| 271 |
outputs=img_output
|
| 272 |
)
|
| 273 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
# Launch the app
|
| 275 |
if __name__ == "__main__":
|
| 276 |
demo.launch()
|
|
|
|
| 53 |
thumb_path = os.path.join(thumbs_folder, f"thumb_{page_num + 1}.png")
|
| 54 |
create_thumbnail(image_path, thumb_path)
|
| 55 |
|
| 56 |
+
# Add interactive content to first page as an example
|
| 57 |
+
html_content = ""
|
| 58 |
+
items = []
|
| 59 |
+
|
| 60 |
+
if page_num == 0: # First page example
|
| 61 |
+
html_content = """
|
| 62 |
+
<div style="position: absolute; top: 50px; left: 50px; background-color: rgba(255,255,255,0.7); padding: 10px; border-radius: 5px;">
|
| 63 |
+
<h2 style="color: #333;">Interactive Flipbook Example</h2>
|
| 64 |
+
<p style="color: #666;">This page demonstrates interactive content capabilities.</p>
|
| 65 |
+
<a href="#" data-page="2" style="color: blue; text-decoration: underline;">Go to page 2</a>
|
| 66 |
+
</div>
|
| 67 |
+
"""
|
| 68 |
+
elif page_num == 1: # Second page example with items
|
| 69 |
+
items = [
|
| 70 |
+
{
|
| 71 |
+
"type": "link",
|
| 72 |
+
"x": 50,
|
| 73 |
+
"y": 50,
|
| 74 |
+
"width": 200,
|
| 75 |
+
"height": 50,
|
| 76 |
+
"page": 1,
|
| 77 |
+
"title": "Back to first page"
|
| 78 |
+
}
|
| 79 |
+
]
|
| 80 |
+
|
| 81 |
+
# Add page info with interactive content
|
| 82 |
pages_info.append({
|
| 83 |
"src": os.path.join("output", session_id, f"page_{page_num + 1}.png"),
|
| 84 |
"thumb": os.path.join("thumbs", session_id, f"thumb_{page_num + 1}.png"),
|
| 85 |
+
"title": f"Page {page_num + 1}",
|
| 86 |
+
"htmlContent": html_content if html_content else None,
|
| 87 |
+
"items": items if items else None
|
| 88 |
})
|
| 89 |
|
| 90 |
return pages_info
|
|
|
|
| 111 |
thumb_path = os.path.join(thumbs_folder, f"thumb_{i + 1}.png")
|
| 112 |
create_thumbnail(img_path, thumb_path)
|
| 113 |
|
| 114 |
+
# Add interactive content to specific pages as examples
|
| 115 |
+
html_content = ""
|
| 116 |
+
items = []
|
| 117 |
+
|
| 118 |
+
if i == 0: # First image example with HTML content
|
| 119 |
+
html_content = """
|
| 120 |
+
<div style="position: absolute; top: 50px; left: 50px; background-color: rgba(255,255,255,0.7); padding: 10px; border-radius: 5px;">
|
| 121 |
+
<h2 style="color: #333;">Image Gallery</h2>
|
| 122 |
+
<p style="color: #666;">This is the first image in your gallery.</p>
|
| 123 |
+
<a href="#" data-page="2" style="color: blue; text-decoration: underline;">Next Image</a>
|
| 124 |
+
</div>
|
| 125 |
+
"""
|
| 126 |
+
elif i == 1: # Second image with video example (if available)
|
| 127 |
+
items = [
|
| 128 |
+
{
|
| 129 |
+
"type": "link",
|
| 130 |
+
"x": 50,
|
| 131 |
+
"y": 50,
|
| 132 |
+
"width": 200,
|
| 133 |
+
"height": 50,
|
| 134 |
+
"page": 1,
|
| 135 |
+
"title": "Previous image"
|
| 136 |
+
}
|
| 137 |
+
]
|
| 138 |
+
|
| 139 |
+
# Example of adding a YouTube video if it's the second image
|
| 140 |
+
if len(image_paths) > 1:
|
| 141 |
+
html_content = """
|
| 142 |
+
<iframe class="flipbook-page-item"
|
| 143 |
+
src="https://www.youtube.com/embed/dQw4w9WgXcQ"
|
| 144 |
+
style="top:200px;left:50px;width:300px;height:200px;"
|
| 145 |
+
frameborder="0"
|
| 146 |
+
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
|
| 147 |
+
allowfullscreen="">
|
| 148 |
+
</iframe>
|
| 149 |
+
"""
|
| 150 |
+
|
| 151 |
+
# Add page info with interactive content
|
| 152 |
pages_info.append({
|
| 153 |
"src": os.path.join("output", session_id, f"image_{i + 1}.png"),
|
| 154 |
"thumb": os.path.join("thumbs", session_id, f"thumb_{i + 1}.png"),
|
| 155 |
+
"title": f"Image {i + 1}",
|
| 156 |
+
"htmlContent": html_content if html_content else None,
|
| 157 |
+
"items": items if items else None
|
| 158 |
})
|
| 159 |
except Exception as e:
|
| 160 |
print(f"Error processing image {img_path}: {e}")
|
|
|
|
| 168 |
pages_info = []
|
| 169 |
|
| 170 |
if pdf_file is not None:
|
| 171 |
+
# In Gradio, pdf_file is a file path string, not the actual content
|
| 172 |
+
pdf_path = pdf_file.name # Get the file path
|
| 173 |
+
|
| 174 |
+
# Process PDF using the file path directly
|
|
|
|
|
|
|
| 175 |
pages_info = process_pdf(pdf_path, session_id)
|
| 176 |
else:
|
| 177 |
return """<div style="color: red; padding: 20px;">Please upload a PDF file.</div>"""
|
|
|
|
| 194 |
pages_info = []
|
| 195 |
|
| 196 |
if images is not None and len(images) > 0:
|
| 197 |
+
# Process images using file paths
|
| 198 |
+
image_paths = [img.name for img in images]
|
| 199 |
+
pages_info = process_images(image_paths, session_id)
|
| 200 |
else:
|
| 201 |
return """<div style="color: red; padding: 20px;">Please upload at least one image.</div>"""
|
| 202 |
|
|
|
|
| 213 |
|
| 214 |
def generate_flipbook_html(pages_info, session_id, view_mode, skin):
|
| 215 |
"""Generate HTML for the flipbook."""
|
| 216 |
+
# Clean up pages_info to remove None values for JSON serialization
|
| 217 |
+
for page in pages_info:
|
| 218 |
+
if "htmlContent" in page and page["htmlContent"] is None:
|
| 219 |
+
del page["htmlContent"]
|
| 220 |
+
if "items" in page and page["items"] is None:
|
| 221 |
+
del page["items"]
|
| 222 |
+
|
| 223 |
# Convert pages_info to JSON for JavaScript
|
| 224 |
pages_json = json.dumps(pages_info)
|
| 225 |
|
|
|
|
| 270 |
singlePageMode: false,
|
| 271 |
singlePageModeIfMobile: true,
|
| 272 |
pageFlipDuration: 1,
|
| 273 |
+
sound: true,
|
| 274 |
+
backgroundMusic: false,
|
| 275 |
thumbnailsOnStart: true,
|
| 276 |
btnThumbs: {{ enabled: true }},
|
| 277 |
+
btnPrint: {{ enabled: true }},
|
| 278 |
+
btnDownloadPages: {{ enabled: true }},
|
| 279 |
+
btnDownloadPdf: {{ enabled: true }},
|
| 280 |
+
btnShare: {{ enabled: true }},
|
| 281 |
+
btnSound: {{ enabled: true }},
|
| 282 |
+
btnExpand: {{ enabled: true }},
|
| 283 |
+
rightToLeft: false,
|
| 284 |
+
autoplayOnStart: false,
|
| 285 |
+
autoplayInterval: 3000
|
| 286 |
}};
|
| 287 |
|
| 288 |
const container = document.getElementById('{flipbook_id}');
|
|
|
|
| 294 |
"""
|
| 295 |
return html
|
| 296 |
|
|
|
|
| 297 |
with gr.Blocks(title="3D Flipbook Viewer") as demo:
|
| 298 |
gr.Markdown("# 3D Flipbook Viewer")
|
| 299 |
+
gr.Markdown("""
|
| 300 |
+
## Create interactive 3D flipbooks from PDFs or images
|
| 301 |
+
|
| 302 |
+
Upload a PDF file or multiple images to generate an interactive flipbook with page-turning effects.
|
| 303 |
+
|
| 304 |
+
### Interactive Features:
|
| 305 |
+
- The created flipbook includes interactive elements on the first pages
|
| 306 |
+
- Navigate using the toolbar or by dragging page corners
|
| 307 |
+
- Use the thumbnails view for quick navigation
|
| 308 |
+
- Toggle fullscreen for better viewing experience
|
| 309 |
+
""")
|
| 310 |
|
| 311 |
with gr.Tabs():
|
| 312 |
with gr.TabItem("PDF Upload"):
|
|
|
|
| 324 |
label="Skin"
|
| 325 |
)
|
| 326 |
|
| 327 |
+
pdf_create_btn = gr.Button("Create Flipbook from PDF", variant="primary")
|
| 328 |
pdf_output = gr.HTML(label="Flipbook Output")
|
| 329 |
|
| 330 |
# Set up PDF event handler
|
|
|
|
| 349 |
label="Skin"
|
| 350 |
)
|
| 351 |
|
| 352 |
+
img_create_btn = gr.Button("Create Flipbook from Images", variant="primary")
|
| 353 |
img_output = gr.HTML(label="Flipbook Output")
|
| 354 |
|
| 355 |
# Set up image event handler
|
|
|
|
| 359 |
outputs=img_output
|
| 360 |
)
|
| 361 |
|
| 362 |
+
gr.Markdown("""
|
| 363 |
+
### Usage Instructions:
|
| 364 |
+
1. Select the tab for your content type (PDF or images)
|
| 365 |
+
2. Upload your file(s)
|
| 366 |
+
3. Adjust view mode and skin in Advanced Settings (optional)
|
| 367 |
+
4. Click the Create Flipbook button
|
| 368 |
+
5. Interact with your flipbook in the output area
|
| 369 |
+
|
| 370 |
+
### Notes:
|
| 371 |
+
- The first pages contain interactive elements and links as examples
|
| 372 |
+
- For best results, use PDFs with clear text and images
|
| 373 |
+
- Supported image formats: JPG, PNG, GIF, etc.
|
| 374 |
+
""")
|
| 375 |
+
|
| 376 |
+
|
| 377 |
# Launch the app
|
| 378 |
if __name__ == "__main__":
|
| 379 |
demo.launch()
|