Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,12 +7,12 @@ def main():
|
|
| 7 |
# Sidebar logo and title
|
| 8 |
with st.sidebar:
|
| 9 |
col1, col2 = st.columns([1, 5]) # Shrink the logo column and expand the text column
|
| 10 |
-
|
| 11 |
with col1:
|
| 12 |
logo = Image.open("logo.png")
|
| 13 |
resized_logo = logo.resize((40, 40)) # Resize the logo
|
| 14 |
st.image(resized_logo)
|
| 15 |
-
|
| 16 |
with col2:
|
| 17 |
st.markdown(
|
| 18 |
"""
|
|
@@ -77,11 +77,11 @@ def main():
|
|
| 77 |
st.sidebar.error(f"Invalid score '{model_data['score']}'. Score must be an integer.")
|
| 78 |
return
|
| 79 |
|
| 80 |
-
# Keep the final label size at
|
| 81 |
-
final_size = (
|
| 82 |
generated_label = create_label_single_pass(background, model_data, final_size)
|
| 83 |
|
| 84 |
-
st.image(generated_label, caption="Generated Label Preview", width=
|
| 85 |
|
| 86 |
img_buffer = io.BytesIO()
|
| 87 |
generated_label.save(img_buffer, format="PNG")
|
|
@@ -97,37 +97,33 @@ def main():
|
|
| 97 |
st.sidebar.write("#### 3. Share your label in technical reports, announcements, etc.")
|
| 98 |
|
| 99 |
|
| 100 |
-
def create_label_single_pass(background_image, model_data, final_size=(
|
| 101 |
"""
|
| 102 |
-
Resizes the background to
|
| 103 |
"""
|
| 104 |
-
#
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
# Step 1: Resize background to high-resolution canvas
|
| 109 |
-
high_res_image = background_image.resize(high_res_size, Image.Resampling.LANCZOS)
|
| 110 |
|
| 111 |
-
#
|
| 112 |
-
draw = ImageDraw.Draw(high_res_image)
|
| 113 |
-
|
| 114 |
-
# Load fonts with scaled sizes
|
| 115 |
try:
|
| 116 |
-
title_font = ImageFont.truetype("Inter_24pt-Bold.ttf", size=
|
| 117 |
-
details_font = ImageFont.truetype("Inter_18pt-Regular.ttf", size=
|
| 118 |
-
energy_font = ImageFont.truetype("Inter_18pt-Medium.ttf", size=
|
| 119 |
except Exception as e:
|
| 120 |
st.error(f"Font loading failed: {e}")
|
| 121 |
-
return
|
| 122 |
|
| 123 |
-
#
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
|
|
|
|
|
|
| 127 |
|
| 128 |
# Text 1 (title)
|
| 129 |
draw.text((title_x, title_y), str(model_data['model']), font=title_font, fill="black")
|
| 130 |
-
draw.text((title_x, title_y +
|
| 131 |
|
| 132 |
# Text 2 (details)
|
| 133 |
details_lines = [
|
|
@@ -139,7 +135,7 @@ def create_label_single_pass(background_image, model_data, final_size=(260, 364)
|
|
| 139 |
bbox = draw.textbbox((0, 0), line, font=details_font)
|
| 140 |
text_width = bbox[2] - bbox[0]
|
| 141 |
# Right-justify the details text at details_x
|
| 142 |
-
draw.text((details_x - text_width, details_y + i
|
| 143 |
|
| 144 |
# Text 3 (energy)
|
| 145 |
energy_text = str(model_data['energy'])
|
|
@@ -148,9 +144,8 @@ def create_label_single_pass(background_image, model_data, final_size=(260, 364)
|
|
| 148 |
# Right-align the energy text at energy_x
|
| 149 |
draw.text((energy_x - energy_text_width, energy_y), energy_text, font=energy_font, fill="black")
|
| 150 |
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
return final_image
|
| 154 |
|
| 155 |
if __name__ == "__main__":
|
| 156 |
main()
|
|
|
|
| 7 |
# Sidebar logo and title
|
| 8 |
with st.sidebar:
|
| 9 |
col1, col2 = st.columns([1, 5]) # Shrink the logo column and expand the text column
|
| 10 |
+
|
| 11 |
with col1:
|
| 12 |
logo = Image.open("logo.png")
|
| 13 |
resized_logo = logo.resize((40, 40)) # Resize the logo
|
| 14 |
st.image(resized_logo)
|
| 15 |
+
|
| 16 |
with col2:
|
| 17 |
st.markdown(
|
| 18 |
"""
|
|
|
|
| 77 |
st.sidebar.error(f"Invalid score '{model_data['score']}'. Score must be an integer.")
|
| 78 |
return
|
| 79 |
|
| 80 |
+
# Keep the final label size at 520×728
|
| 81 |
+
final_size = (520, 728)
|
| 82 |
generated_label = create_label_single_pass(background, model_data, final_size)
|
| 83 |
|
| 84 |
+
st.image(generated_label, caption="Generated Label Preview", width=520)
|
| 85 |
|
| 86 |
img_buffer = io.BytesIO()
|
| 87 |
generated_label.save(img_buffer, format="PNG")
|
|
|
|
| 97 |
st.sidebar.write("#### 3. Share your label in technical reports, announcements, etc.")
|
| 98 |
|
| 99 |
|
| 100 |
+
def create_label_single_pass(background_image, model_data, final_size=(520, 728)):
|
| 101 |
"""
|
| 102 |
+
Resizes the background to 520×728, then draws text onto it.
|
| 103 |
"""
|
| 104 |
+
# 1. Resize background to final_size
|
| 105 |
+
bg_resized = background_image.resize(final_size, Image.Resampling.LANCZOS)
|
| 106 |
+
draw = ImageDraw.Draw(bg_resized)
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
+
# 2. Load fonts at sizes appropriate for a 520×728 label
|
|
|
|
|
|
|
|
|
|
| 109 |
try:
|
| 110 |
+
title_font = ImageFont.truetype("Inter_24pt-Bold.ttf", size=27)
|
| 111 |
+
details_font = ImageFont.truetype("Inter_18pt-Regular.ttf", size=23)
|
| 112 |
+
energy_font = ImageFont.truetype("Inter_18pt-Medium.ttf", size=24)
|
| 113 |
except Exception as e:
|
| 114 |
st.error(f"Font loading failed: {e}")
|
| 115 |
+
return bg_resized
|
| 116 |
|
| 117 |
+
# 3. Place your text.
|
| 118 |
+
# You may need to experiment with x/y coordinates or font sizes
|
| 119 |
+
# to make it look right in 520×728.
|
| 120 |
+
title_x, title_y = 33, 150
|
| 121 |
+
details_x, details_y = 480, 256
|
| 122 |
+
energy_x, energy_y = 480, 472
|
| 123 |
|
| 124 |
# Text 1 (title)
|
| 125 |
draw.text((title_x, title_y), str(model_data['model']), font=title_font, fill="black")
|
| 126 |
+
draw.text((title_x, title_y + 38), str(model_data['provider']), font=title_font, fill="black")
|
| 127 |
|
| 128 |
# Text 2 (details)
|
| 129 |
details_lines = [
|
|
|
|
| 135 |
bbox = draw.textbbox((0, 0), line, font=details_font)
|
| 136 |
text_width = bbox[2] - bbox[0]
|
| 137 |
# Right-justify the details text at details_x
|
| 138 |
+
draw.text((details_x - text_width, details_y + i*47), line, font=details_font, fill="black")
|
| 139 |
|
| 140 |
# Text 3 (energy)
|
| 141 |
energy_text = str(model_data['energy'])
|
|
|
|
| 144 |
# Right-align the energy text at energy_x
|
| 145 |
draw.text((energy_x - energy_text_width, energy_y), energy_text, font=energy_font, fill="black")
|
| 146 |
|
| 147 |
+
return bg_resized
|
| 148 |
+
|
|
|
|
| 149 |
|
| 150 |
if __name__ == "__main__":
|
| 151 |
main()
|