bgamazay commited on
Commit
fe4fae8
·
verified ·
1 Parent(s): 82bca2e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -19
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 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,33 +97,31 @@ 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=(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,7 +133,7 @@ def create_label_single_pass(background_image, model_data, final_size=(520, 728)
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'])
 
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 260x364
81
+ final_size = (260, 364)
82
  generated_label = create_label_single_pass(background, model_data, final_size)
83
 
84
+ st.image(generated_label, caption="Generated Label Preview", width=final_size[0])
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=(260, 364)):
101
  """
102
+ Resizes the background to 260×364, 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 260×364 label
109
  try:
110
+ title_font = ImageFont.truetype("Inter_24pt-Bold.ttf", size=12)
111
+ details_font = ImageFont.truetype("Inter_18pt-Regular.ttf", size=10)
112
+ energy_font = ImageFont.truetype("Inter_18pt-Medium.ttf", size=12)
113
  except Exception as e:
114
  st.error(f"Font loading failed: {e}")
115
  return bg_resized
116
 
117
+ # 3. Place your text.
118
+ title_x, title_y = 16, 75
119
+ details_x, details_y = 245, 130
120
+ energy_x, energy_y = 245, 250
 
 
121
 
122
  # Text 1 (title)
123
  draw.text((title_x, title_y), str(model_data['model']), font=title_font, fill="black")
124
+ draw.text((title_x, title_y + 20), str(model_data['provider']), font=title_font, fill="black")
125
 
126
  # Text 2 (details)
127
  details_lines = [
 
133
  bbox = draw.textbbox((0, 0), line, font=details_font)
134
  text_width = bbox[2] - bbox[0]
135
  # Right-justify the details text at details_x
136
+ draw.text((details_x - text_width, details_y + i * 25), line, font=details_font, fill="black")
137
 
138
  # Text 3 (energy)
139
  energy_text = str(model_data['energy'])