Spaces:
Running
Running
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -11,39 +11,43 @@ from PIL import Image
|
|
| 11 |
|
| 12 |
|
| 13 |
def convert(image_path, lang):
|
| 14 |
-
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
|
| 46 |
-
|
|
|
|
|
|
|
| 47 |
|
| 48 |
|
| 49 |
demo = gr.Interface(
|
|
|
|
| 11 |
|
| 12 |
|
| 13 |
def convert(image_path, lang):
|
| 14 |
+
import traceback
|
| 15 |
+
try:
|
| 16 |
+
from px_image2pptx import image_to_pptx
|
| 17 |
|
| 18 |
+
# Convert WebP to PNG if needed (PaddleOCR doesn't support WebP)
|
| 19 |
+
img = Image.open(image_path)
|
| 20 |
+
if img.format == "WEBP" or image_path.lower().endswith(".webp"):
|
| 21 |
+
png_path = image_path.rsplit(".", 1)[0] + ".png"
|
| 22 |
+
img.save(png_path)
|
| 23 |
+
image_path = png_path
|
| 24 |
|
| 25 |
+
tmpdir = tempfile.mkdtemp()
|
| 26 |
+
out_pptx = os.path.join(tmpdir, "output.pptx")
|
| 27 |
+
work_dir = os.path.join(tmpdir, "work")
|
| 28 |
|
| 29 |
+
report = image_to_pptx(
|
| 30 |
+
image_path,
|
| 31 |
+
out_pptx,
|
| 32 |
+
lang=lang,
|
| 33 |
+
work_dir=work_dir,
|
| 34 |
+
)
|
| 35 |
|
| 36 |
+
# Load the inpainted background for preview
|
| 37 |
+
bg_path = os.path.join(work_dir, "background.png")
|
| 38 |
+
bg_preview = Image.open(bg_path) if os.path.exists(bg_path) else None
|
| 39 |
|
| 40 |
+
summary = (
|
| 41 |
+
f"**Text boxes:** {report['text_boxes']} \n"
|
| 42 |
+
f"**OCR regions:** {report['ocr_regions']} \n"
|
| 43 |
+
f"**Slide size:** {report['slide_size']['width_inches']}x"
|
| 44 |
+
f"{report['slide_size']['height_inches']}\" \n"
|
| 45 |
+
f"**Timings:** {report.get('timings', {})}"
|
| 46 |
+
)
|
| 47 |
|
| 48 |
+
return bg_preview, out_pptx, summary
|
| 49 |
+
except Exception as e:
|
| 50 |
+
raise gr.Error(f"{type(e).__name__}: {e}\n\n{traceback.format_exc()}")
|
| 51 |
|
| 52 |
|
| 53 |
demo = gr.Interface(
|