Spaces:
Running
Running
Update app.py
Browse files
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 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
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 =
|
85 |
-
details_x, details_y =
|
86 |
-
energy_x, energy_y =
|
87 |
|
88 |
-
# Group 1: Title (Left-Justified)
|
89 |
-
draw.text((title_x, title_y),
|
90 |
-
draw.text((title_x, title_y + 25),
|
91 |
|
92 |
-
# Group 2: Details (Right-Justified)
|
93 |
details_lines = [
|
94 |
-
|
95 |
-
|
96 |
-
|
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]
|
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 =
|
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")
|