Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Update app.py
Browse files
app.py
CHANGED
@@ -95,14 +95,13 @@ def process_images(image_paths, session_id):
|
|
95 |
|
96 |
return pages_info
|
97 |
|
98 |
-
def
|
99 |
-
"""Create a flipbook from uploaded PDF
|
100 |
try:
|
101 |
session_id = str(uuid.uuid4())
|
102 |
pages_info = []
|
103 |
|
104 |
-
|
105 |
-
if upload_type == "pdf" and pdf_file is not None:
|
106 |
# Save PDF to temp directory
|
107 |
pdf_path = os.path.join(UPLOAD_DIR, f"{session_id}.pdf")
|
108 |
with open(pdf_path, "wb") as f:
|
@@ -110,21 +109,39 @@ def create_flipbook(upload_type, pdf_file=None, images=None, view_mode="webgl",
|
|
110 |
|
111 |
# Process PDF
|
112 |
pages_info = process_pdf(pdf_path, session_id)
|
113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
# Process images
|
115 |
pages_info = process_images(images, session_id)
|
116 |
else:
|
117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
|
119 |
if not pages_info:
|
120 |
-
return """<div style="color: red; padding: 20px;">Failed to process the uploaded
|
121 |
|
122 |
# Create and return HTML for the flipbook
|
123 |
flipbook_html = generate_flipbook_html(pages_info, session_id, view_mode, skin)
|
124 |
return flipbook_html
|
125 |
|
126 |
except Exception as e:
|
127 |
-
print(f"Error creating flipbook: {e}")
|
128 |
return f"""<div style="color: red; padding: 20px;">An error occurred: {str(e)}</div>"""
|
129 |
|
130 |
def generate_flipbook_html(pages_info, session_id, view_mode, skin):
|
@@ -204,38 +221,53 @@ with gr.Blocks(title="3D Flipbook Viewer") as demo:
|
|
204 |
with gr.Tabs():
|
205 |
with gr.TabItem("PDF Upload"):
|
206 |
pdf_file = gr.File(label="Upload PDF", file_types=[".pdf"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
207 |
pdf_create_btn = gr.Button("Create Flipbook from PDF")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
208 |
|
209 |
with gr.TabItem("Image Upload"):
|
210 |
images = gr.File(label="Upload Images", file_types=["image"], file_count="multiple")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
211 |
img_create_btn = gr.Button("Create Flipbook from Images")
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
choices=["light", "dark", "gradient"],
|
221 |
-
value="light",
|
222 |
-
label="Skin"
|
223 |
-
)
|
224 |
-
|
225 |
-
output = gr.HTML(label="Flipbook Output")
|
226 |
-
|
227 |
-
# Set up event handlers
|
228 |
-
pdf_create_btn.click(
|
229 |
-
fn=create_flipbook,
|
230 |
-
inputs=[gr.Textbox(value="pdf", visible=False), pdf_file, None, view_mode, skin],
|
231 |
-
outputs=output
|
232 |
-
)
|
233 |
-
|
234 |
-
img_create_btn.click(
|
235 |
-
fn=create_flipbook,
|
236 |
-
inputs=[gr.Textbox(value="images", visible=False), None, images, view_mode, skin],
|
237 |
-
outputs=output
|
238 |
-
)
|
239 |
|
240 |
# Launch the app
|
241 |
if __name__ == "__main__":
|
|
|
95 |
|
96 |
return pages_info
|
97 |
|
98 |
+
def create_flipbook_from_pdf(pdf_file, view_mode="webgl", skin="light"):
|
99 |
+
"""Create a flipbook from uploaded PDF."""
|
100 |
try:
|
101 |
session_id = str(uuid.uuid4())
|
102 |
pages_info = []
|
103 |
|
104 |
+
if pdf_file is not None:
|
|
|
105 |
# Save PDF to temp directory
|
106 |
pdf_path = os.path.join(UPLOAD_DIR, f"{session_id}.pdf")
|
107 |
with open(pdf_path, "wb") as f:
|
|
|
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>"""
|
114 |
+
|
115 |
+
def create_flipbook_from_images(images, view_mode="webgl", skin="light"):
|
116 |
+
"""Create a flipbook from uploaded images."""
|
117 |
+
try:
|
118 |
+
session_id = str(uuid.uuid4())
|
119 |
+
pages_info = []
|
120 |
+
|
121 |
+
if images is not None and len(images) > 0:
|
122 |
# Process images
|
123 |
pages_info = process_images(images, session_id)
|
124 |
else:
|
125 |
+
if not pages_info:
|
126 |
+
return """<div style="color: red; padding: 20px;">Failed to process the uploaded images. Please try again.</div>"""
|
127 |
+
|
128 |
+
# Create and return HTML for the flipbook
|
129 |
+
flipbook_html = generate_flipbook_html(pages_info, session_id, view_mode, skin)
|
130 |
+
return flipbook_html
|
131 |
+
|
132 |
+
except Exception as e:
|
133 |
+
print(f"Error creating flipbook from images: {e}")
|
134 |
+
return f"""<div style="color: red; padding: 20px;">An error occurred: {str(e)}</div>"""
|
135 |
|
136 |
if not pages_info:
|
137 |
+
return """<div style="color: red; padding: 20px;">Failed to process the uploaded PDF. Please try again.</div>"""
|
138 |
|
139 |
# Create and return HTML for the flipbook
|
140 |
flipbook_html = generate_flipbook_html(pages_info, session_id, view_mode, skin)
|
141 |
return flipbook_html
|
142 |
|
143 |
except Exception as e:
|
144 |
+
print(f"Error creating flipbook from PDF: {e}")
|
145 |
return f"""<div style="color: red; padding: 20px;">An error occurred: {str(e)}</div>"""
|
146 |
|
147 |
def generate_flipbook_html(pages_info, session_id, view_mode, skin):
|
|
|
221 |
with gr.Tabs():
|
222 |
with gr.TabItem("PDF Upload"):
|
223 |
pdf_file = gr.File(label="Upload PDF", file_types=[".pdf"])
|
224 |
+
|
225 |
+
with gr.Accordion("Advanced Settings", open=False):
|
226 |
+
pdf_view_mode = gr.Dropdown(
|
227 |
+
choices=["webgl", "3d", "2d", "swipe"],
|
228 |
+
value="webgl",
|
229 |
+
label="View Mode"
|
230 |
+
)
|
231 |
+
pdf_skin = gr.Dropdown(
|
232 |
+
choices=["light", "dark", "gradient"],
|
233 |
+
value="light",
|
234 |
+
label="Skin"
|
235 |
+
)
|
236 |
+
|
237 |
pdf_create_btn = gr.Button("Create Flipbook from PDF")
|
238 |
+
pdf_output = gr.HTML(label="Flipbook Output")
|
239 |
+
|
240 |
+
# Set up PDF event handler
|
241 |
+
pdf_create_btn.click(
|
242 |
+
fn=create_flipbook_from_pdf,
|
243 |
+
inputs=[pdf_file, pdf_view_mode, pdf_skin],
|
244 |
+
outputs=pdf_output
|
245 |
+
)
|
246 |
|
247 |
with gr.TabItem("Image Upload"):
|
248 |
images = gr.File(label="Upload Images", file_types=["image"], file_count="multiple")
|
249 |
+
|
250 |
+
with gr.Accordion("Advanced Settings", open=False):
|
251 |
+
img_view_mode = gr.Dropdown(
|
252 |
+
choices=["webgl", "3d", "2d", "swipe"],
|
253 |
+
value="webgl",
|
254 |
+
label="View Mode"
|
255 |
+
)
|
256 |
+
img_skin = gr.Dropdown(
|
257 |
+
choices=["light", "dark", "gradient"],
|
258 |
+
value="light",
|
259 |
+
label="Skin"
|
260 |
+
)
|
261 |
+
|
262 |
img_create_btn = gr.Button("Create Flipbook from Images")
|
263 |
+
img_output = gr.HTML(label="Flipbook Output")
|
264 |
+
|
265 |
+
# Set up image event handler
|
266 |
+
img_create_btn.click(
|
267 |
+
fn=create_flipbook_from_images,
|
268 |
+
inputs=[images, img_view_mode, img_skin],
|
269 |
+
outputs=img_output
|
270 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
271 |
|
272 |
# Launch the app
|
273 |
if __name__ == "__main__":
|