Update app.py
Browse files
app.py
CHANGED
|
@@ -7,15 +7,12 @@ from pptx.util import Inches, Pt
|
|
| 7 |
from pptx.dml.color import RGBColor
|
| 8 |
from pptx.enum.text import PP_ALIGN
|
| 9 |
from PIL import Image, ImageOps
|
| 10 |
-
from functools import lru_cache
|
| 11 |
-
|
| 12 |
from cropper import crop_with_yolo, predict_infection_score
|
| 13 |
|
| 14 |
|
| 15 |
# βββββββββββββββββββββββββββββ
|
| 16 |
# Streamlit UI
|
| 17 |
# βββββββββββββββββββββββββββββ
|
| 18 |
-
st.set_page_config(layout="wide")
|
| 19 |
st.title("π Treatments β PowerPoint Generator with Infection Scores")
|
| 20 |
|
| 21 |
main_option = st.selectbox("Select Main Option", ["O", "V"])
|
|
@@ -48,39 +45,37 @@ for treatment in treatment_list:
|
|
| 48 |
SUPPORTED_BY_PPTX = {"BMP", "GIF", "JPEG", "PNG", "TIFF", "WMF"}
|
| 49 |
|
| 50 |
|
| 51 |
-
|
| 52 |
# βββββββββββββββββββββββββββββ
|
| 53 |
-
#
|
| 54 |
# βββββββββββββββββββββββββββββ
|
| 55 |
-
|
| 56 |
def normalize_image_for_ppt(path: str) -> str | None:
|
| 57 |
-
"""
|
| 58 |
-
KEEP ORIGINAL RESOLUTION.
|
| 59 |
-
KEEP ORIGINAL FORMAT IF PPT-SUPPORTED.
|
| 60 |
-
ONLY fix EXIF rotation.
|
| 61 |
-
NEVER convert PNG/JPG, no re-encoding.
|
| 62 |
-
"""
|
| 63 |
try:
|
| 64 |
with Image.open(path) as im:
|
| 65 |
fmt = (im.format or "").upper()
|
| 66 |
im = ImageOps.exif_transpose(im)
|
| 67 |
-
|
| 68 |
-
# if format not supported, convert ONCE to PNG (lossless)
|
| 69 |
if fmt not in SUPPORTED_BY_PPTX:
|
| 70 |
-
|
|
|
|
| 71 |
im.save(out_path, "PNG")
|
| 72 |
return out_path
|
| 73 |
-
|
| 74 |
return path
|
| 75 |
-
|
| 76 |
except Exception as e:
|
| 77 |
st.error(f"β Could not normalize image '{path}': {e}")
|
| 78 |
return None
|
| 79 |
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
def compute_grid(n: int) -> tuple[int, int]:
|
| 83 |
-
"""Decide columns/rows so n images fit nicely."""
|
| 84 |
cols = math.ceil(math.sqrt(n))
|
| 85 |
rows = math.ceil(n / cols)
|
| 86 |
return cols, rows
|
|
@@ -113,27 +108,9 @@ def get_font_size(num_images: int) -> int:
|
|
| 113 |
return 7
|
| 114 |
|
| 115 |
|
| 116 |
-
|
| 117 |
# βββββββββββββββββββββββββββββ
|
| 118 |
-
#
|
| 119 |
-
# (Original image resolution is not changed)
|
| 120 |
# βββββββββββββββββββββββββββββ
|
| 121 |
-
|
| 122 |
-
def get_fixed_display_size(cell_w, img_area_h):
|
| 123 |
-
"""
|
| 124 |
-
All images appear SAME SIZE inside PPT.
|
| 125 |
-
Resolution preserved, only display size controlled.
|
| 126 |
-
"""
|
| 127 |
-
img_w = cell_w * 0.90
|
| 128 |
-
img_h = img_area_h * 0.90
|
| 129 |
-
return img_w, img_h
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
# βββββββββββββββββββββββββββββ
|
| 134 |
-
# MAIN PPT GENERATION
|
| 135 |
-
# βββββββββββββββββββββββββββββ
|
| 136 |
-
|
| 137 |
if st.button("π Generate PowerPoint"):
|
| 138 |
prs = Presentation()
|
| 139 |
EMU_PER_INCH = 914400
|
|
@@ -153,7 +130,6 @@ if st.button("π Generate PowerPoint"):
|
|
| 153 |
for day in day_list:
|
| 154 |
st.write(f"πΌοΈ Adding images for {day}...")
|
| 155 |
|
| 156 |
-
# collect files
|
| 157 |
day_files = []
|
| 158 |
for treatment in treatment_list:
|
| 159 |
files = uploaded_files[treatment][day]
|
|
@@ -165,15 +141,13 @@ if st.button("π Generate PowerPoint"):
|
|
| 165 |
if total_imgs == 0:
|
| 166 |
continue
|
| 167 |
|
| 168 |
-
# grid
|
| 169 |
cols, rows = compute_grid(max_imgs_per_slide)
|
| 170 |
cell_w = usable_w_in / cols
|
| 171 |
cell_h = usable_h_in / rows
|
| 172 |
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
fixed_img_w, fixed_img_h = get_fixed_display_size(cell_w, img_area_h)
|
| 177 |
|
| 178 |
idx = 0
|
| 179 |
slide_index_for_day = 0
|
|
@@ -198,22 +172,22 @@ if st.button("π Generate PowerPoint"):
|
|
| 198 |
tmp.write(file.read())
|
| 199 |
raw_path = tmp.name
|
| 200 |
|
| 201 |
-
cropped_path =
|
| 202 |
safe_path = normalize_image_for_ppt(cropped_path)
|
| 203 |
if not safe_path:
|
| 204 |
continue
|
| 205 |
|
| 206 |
try:
|
| 207 |
-
#
|
| 208 |
-
|
| 209 |
-
|
|
|
|
| 210 |
|
| 211 |
-
slide.shapes.add_picture(
|
| 212 |
safe_path,
|
| 213 |
left=Inches(img_left),
|
| 214 |
top=Inches(img_top),
|
| 215 |
-
width=Inches(
|
| 216 |
-
height=Inches(fixed_img_h),
|
| 217 |
)
|
| 218 |
|
| 219 |
# Caption
|
|
@@ -228,9 +202,9 @@ if st.button("π Generate PowerPoint"):
|
|
| 228 |
)
|
| 229 |
p = tx.text_frame.paragraphs[0]
|
| 230 |
p.text = caption_text
|
|
|
|
| 231 |
p.font.size = Pt(base_font_size)
|
| 232 |
p.font.bold = True
|
| 233 |
-
p.alignment = PP_ALIGN.CENTER
|
| 234 |
|
| 235 |
except Exception as e:
|
| 236 |
st.error(f"β Failed to add image '{safe_path}': {e}")
|
|
@@ -247,6 +221,7 @@ if st.button("π Generate PowerPoint"):
|
|
| 247 |
)
|
| 248 |
|
| 249 |
|
|
|
|
| 250 |
# #First deployed
|
| 251 |
# import os
|
| 252 |
# import tempfile
|
|
|
|
| 7 |
from pptx.dml.color import RGBColor
|
| 8 |
from pptx.enum.text import PP_ALIGN
|
| 9 |
from PIL import Image, ImageOps
|
|
|
|
|
|
|
| 10 |
from cropper import crop_with_yolo, predict_infection_score
|
| 11 |
|
| 12 |
|
| 13 |
# βββββββββββββββββββββββββββββ
|
| 14 |
# Streamlit UI
|
| 15 |
# βββββββββββββββββββββββββββββ
|
|
|
|
| 16 |
st.title("π Treatments β PowerPoint Generator with Infection Scores")
|
| 17 |
|
| 18 |
main_option = st.selectbox("Select Main Option", ["O", "V"])
|
|
|
|
| 45 |
SUPPORTED_BY_PPTX = {"BMP", "GIF", "JPEG", "PNG", "TIFF", "WMF"}
|
| 46 |
|
| 47 |
|
|
|
|
| 48 |
# βββββββββββββββββββββββββββββ
|
| 49 |
+
# Helpers
|
| 50 |
# βββββββββββββββββββββββββββββ
|
|
|
|
| 51 |
def normalize_image_for_ppt(path: str) -> str | None:
|
| 52 |
+
"""Convert unsupported formats to PNG + fix EXIF rotation."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
try:
|
| 54 |
with Image.open(path) as im:
|
| 55 |
fmt = (im.format or "").upper()
|
| 56 |
im = ImageOps.exif_transpose(im)
|
|
|
|
|
|
|
| 57 |
if fmt not in SUPPORTED_BY_PPTX:
|
| 58 |
+
im = im.convert("RGB")
|
| 59 |
+
out_path = os.path.splitext(path)[0] + "_clean.png"
|
| 60 |
im.save(out_path, "PNG")
|
| 61 |
return out_path
|
|
|
|
| 62 |
return path
|
|
|
|
| 63 |
except Exception as e:
|
| 64 |
st.error(f"β Could not normalize image '{path}': {e}")
|
| 65 |
return None
|
| 66 |
|
| 67 |
|
| 68 |
+
def crop_with_margin(image_path: str, mode: str) -> str:
|
| 69 |
+
"""Crop using YOLO with your logic."""
|
| 70 |
+
try:
|
| 71 |
+
cropped = crop_with_yolo(image_path, mode)
|
| 72 |
+
return cropped if cropped and os.path.exists(cropped) else image_path
|
| 73 |
+
except Exception as e:
|
| 74 |
+
st.error(f"β YOLO crop failed for {image_path}: {e}")
|
| 75 |
+
return image_path
|
| 76 |
+
|
| 77 |
|
| 78 |
def compute_grid(n: int) -> tuple[int, int]:
|
|
|
|
| 79 |
cols = math.ceil(math.sqrt(n))
|
| 80 |
rows = math.ceil(n / cols)
|
| 81 |
return cols, rows
|
|
|
|
| 108 |
return 7
|
| 109 |
|
| 110 |
|
|
|
|
| 111 |
# βββββββββββββββββββββββββββββ
|
| 112 |
+
# PowerPoint Generation
|
|
|
|
| 113 |
# βββββββββββββββββββββββββββββ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
if st.button("π Generate PowerPoint"):
|
| 115 |
prs = Presentation()
|
| 116 |
EMU_PER_INCH = 914400
|
|
|
|
| 130 |
for day in day_list:
|
| 131 |
st.write(f"πΌοΈ Adding images for {day}...")
|
| 132 |
|
|
|
|
| 133 |
day_files = []
|
| 134 |
for treatment in treatment_list:
|
| 135 |
files = uploaded_files[treatment][day]
|
|
|
|
| 141 |
if total_imgs == 0:
|
| 142 |
continue
|
| 143 |
|
|
|
|
| 144 |
cols, rows = compute_grid(max_imgs_per_slide)
|
| 145 |
cell_w = usable_w_in / cols
|
| 146 |
cell_h = usable_h_in / rows
|
| 147 |
|
| 148 |
+
image_h_ratio = 0.75
|
| 149 |
+
img_area_h = cell_h * image_h_ratio
|
| 150 |
+
caption_h = cell_h * (1 - image_h_ratio)
|
|
|
|
| 151 |
|
| 152 |
idx = 0
|
| 153 |
slide_index_for_day = 0
|
|
|
|
| 172 |
tmp.write(file.read())
|
| 173 |
raw_path = tmp.name
|
| 174 |
|
| 175 |
+
cropped_path = crop_with_margin(raw_path, mode)
|
| 176 |
safe_path = normalize_image_for_ppt(cropped_path)
|
| 177 |
if not safe_path:
|
| 178 |
continue
|
| 179 |
|
| 180 |
try:
|
| 181 |
+
# ALWAYS preserve aspect ratio β remove height
|
| 182 |
+
img_w = cell_w * 0.9 # consistent WIDTH
|
| 183 |
+
img_left = cell_left + (cell_w - img_w) / 2
|
| 184 |
+
img_top = cell_top + (img_area_h - img_area_h * 0.9) / 2
|
| 185 |
|
| 186 |
+
pic = slide.shapes.add_picture(
|
| 187 |
safe_path,
|
| 188 |
left=Inches(img_left),
|
| 189 |
top=Inches(img_top),
|
| 190 |
+
width=Inches(img_w) # Height auto-calculated = no stretching
|
|
|
|
| 191 |
)
|
| 192 |
|
| 193 |
# Caption
|
|
|
|
| 202 |
)
|
| 203 |
p = tx.text_frame.paragraphs[0]
|
| 204 |
p.text = caption_text
|
| 205 |
+
p.alignment = PP_ALIGN.CENTER
|
| 206 |
p.font.size = Pt(base_font_size)
|
| 207 |
p.font.bold = True
|
|
|
|
| 208 |
|
| 209 |
except Exception as e:
|
| 210 |
st.error(f"β Failed to add image '{safe_path}': {e}")
|
|
|
|
| 221 |
)
|
| 222 |
|
| 223 |
|
| 224 |
+
|
| 225 |
# #First deployed
|
| 226 |
# import os
|
| 227 |
# import tempfile
|