pxGenius commited on
Commit
bc7a65f
·
verified ·
1 Parent(s): f7d9770

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +31 -27
app.py CHANGED
@@ -11,39 +11,43 @@ from PIL import Image
11
 
12
 
13
  def convert(image_path, lang):
14
- from px_image2pptx import image_to_pptx
 
 
15
 
16
- # Convert WebP to PNG if needed (PaddleOCR doesn't support WebP)
17
- img = Image.open(image_path)
18
- if img.format == "WEBP" or image_path.lower().endswith(".webp"):
19
- png_path = image_path.rsplit(".", 1)[0] + ".png"
20
- img.save(png_path)
21
- image_path = png_path
22
 
23
- tmpdir = tempfile.mkdtemp()
24
- out_pptx = os.path.join(tmpdir, "output.pptx")
25
- work_dir = os.path.join(tmpdir, "work")
26
 
27
- report = image_to_pptx(
28
- image_path,
29
- out_pptx,
30
- lang=lang,
31
- work_dir=work_dir,
32
- )
33
 
34
- # Load the inpainted background for preview
35
- bg_path = os.path.join(work_dir, "background.png")
36
- bg_preview = Image.open(bg_path) if os.path.exists(bg_path) else None
37
 
38
- summary = (
39
- f"**Text boxes:** {report['text_boxes']} \n"
40
- f"**OCR regions:** {report['ocr_regions']} \n"
41
- f"**Slide size:** {report['slide_size']['width_inches']}x"
42
- f"{report['slide_size']['height_inches']}\" \n"
43
- f"**Timings:** {report.get('timings', {})}"
44
- )
45
 
46
- return bg_preview, out_pptx, summary
 
 
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(