bgamazay commited on
Commit
1cdf5d6
·
verified ·
1 Parent(s): 25ce5b4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -24
app.py CHANGED
@@ -34,16 +34,22 @@ def main():
34
  model_data = data_df[data_df["model"] == selected_model].iloc[0]
35
 
36
  # Dynamically select the background image based on the score
37
- try:
38
- score = int(model_data["score"]) # Convert to int
39
- background_path = f"{score}.png" # E.g., "1.png", "2.png"
40
- background = Image.open(background_path).convert("RGBA")
41
- except FileNotFoundError:
42
- st.sidebar.error(f"Could not find background image '{score}.png'. Using default background.")
43
- background = Image.open("default_background.png").convert("RGBA")
44
- except ValueError:
45
- st.sidebar.error(f"Invalid score '{model_data['score']}'. Score must be an integer.")
46
- return
 
 
 
 
 
 
47
 
48
  # Generate the label with text
49
  generated_label = create_label(background, model_data)
@@ -81,28 +87,27 @@ def create_label(background_image, model_data):
81
  return label_img
82
 
83
  # Define positions for each text group
84
- title_x, title_y = 20, 20 # Top-left corner for title
85
- details_x, details_y = label_img.width - 20, 20 # Top-right corner for details
86
- energy_x, energy_y = label_img.width // 2, label_img.height - 50 # Center-bottom for energy
87
 
88
- # Group 1: Title (Left-Justified)
89
- draw.text((title_x, title_y), f"Model: {model_data['model']}", font=title_font, fill="black")
90
- draw.text((title_x, title_y + 25), f"Provider: {model_data['provider']}", font=title_font, fill="black")
91
 
92
- # Group 2: Details (Right-Justified)
93
  details_lines = [
94
- f"Date: {model_data['date']}",
95
- f"Task: {model_data['task']}",
96
- f"Hardware: {model_data['hardware']}"
97
  ]
98
  for i, line in enumerate(details_lines):
99
- # Use textbbox to calculate text width
100
  bbox = draw.textbbox((0, 0), line, font=details_font)
101
- text_width = bbox[2] - bbox[0] # Right - Left
102
  draw.text((details_x - text_width, details_y + i * 20), line, font=details_font, fill="black")
103
 
104
- # Group 3: Energy (Bottom-Center)
105
- energy_text = f"Energy: {model_data['energy']}"
106
  bbox = draw.textbbox((0, 0), energy_text, font=energy_font)
107
  energy_text_width = bbox[2] - bbox[0]
108
  draw.text((energy_x - energy_text_width // 2, energy_y), energy_text, font=energy_font, fill="black")
 
34
  model_data = data_df[data_df["model"] == selected_model].iloc[0]
35
 
36
  # Dynamically select the background image based on the score
37
+ try:
38
+ score = int(model_data["score"]) # Convert to int
39
+ background_path = f"{score}.png" # E.g., "1.png", "2.png"
40
+ background = Image.open(background_path).convert("RGBA")
41
+
42
+ # Proportional scaling to fit within the target size
43
+ target_size = (800, 600) # Maximum width and height
44
+ background.thumbnail(target_size, Image.ANTIALIAS)
45
+
46
+ except FileNotFoundError:
47
+ st.sidebar.error(f"Could not find background image '{score}.png'. Using default background.")
48
+ background = Image.open("default_background.png").convert("RGBA")
49
+ background.thumbnail(target_size, Image.ANTIALIAS) # Resize default image proportionally
50
+ except ValueError:
51
+ st.sidebar.error(f"Invalid score '{model_data['score']}'. Score must be an integer.")
52
+ return
53
 
54
  # Generate the label with text
55
  generated_label = create_label(background, model_data)
 
87
  return label_img
88
 
89
  # Define positions for each text group
90
+ title_x, title_y = 100, 120
91
+ details_x, details_y = 200, 340
92
+ energy_x, energy_y = 600, 600
93
 
94
+ # Group 1: Title (Left-Justified, no prefixes)
95
+ draw.text((title_x, title_y), model_data['model'], font=title_font, fill="black")
96
+ draw.text((title_x, title_y + 25), model_data['provider'], font=title_font, fill="black")
97
 
98
+ # Group 2: Details (Right-Justified, no prefixes)
99
  details_lines = [
100
+ model_data['date'],
101
+ model_data['task'],
102
+ model_data['hardware']
103
  ]
104
  for i, line in enumerate(details_lines):
 
105
  bbox = draw.textbbox((0, 0), line, font=details_font)
106
+ text_width = bbox[2] - bbox[0]
107
  draw.text((details_x - text_width, details_y + i * 20), line, font=details_font, fill="black")
108
 
109
+ # Group 3: Energy (Bottom-Center, no prefixes)
110
+ energy_text = model_data['energy']
111
  bbox = draw.textbbox((0, 0), energy_text, font=energy_font)
112
  energy_text_width = bbox[2] - bbox[0]
113
  draw.text((energy_x - energy_text_width // 2, energy_y), energy_text, font=energy_font, fill="black")