Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,52 +4,59 @@ from PIL import Image, ImageDraw, ImageFont
|
|
| 4 |
import io
|
| 5 |
|
| 6 |
def main():
|
| 7 |
-
# 1.
|
| 8 |
-
st.title("AI Energy Score Label Generator")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# 2. Read Data from CSV
|
| 11 |
-
# Using pandas to read the 'data.csv' file
|
| 12 |
-
# Make sure 'data.csv' is in the same folder as 'app.py'
|
| 13 |
try:
|
| 14 |
data_df = pd.read_csv("data.csv")
|
| 15 |
except FileNotFoundError:
|
| 16 |
-
st.error("Could not find 'data.csv'! Please make sure it's present.")
|
| 17 |
return
|
| 18 |
|
| 19 |
-
# 3. Ensure the CSV has
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
# 4. Create a dropdown list based on unique values in the '
|
| 25 |
-
model_options = data_df["
|
| 26 |
-
selected_model = st.selectbox("Select a Model:", model_options)
|
| 27 |
|
| 28 |
# 5. Filter the data for the selected model
|
| 29 |
-
model_data = data_df[data_df["
|
| 30 |
|
| 31 |
-
# 6.
|
| 32 |
-
# Make sure 'background.png' is in the same folder as 'app.py'
|
| 33 |
try:
|
| 34 |
-
|
|
|
|
|
|
|
| 35 |
except FileNotFoundError:
|
| 36 |
-
st.error("Could not find '
|
|
|
|
|
|
|
|
|
|
| 37 |
return
|
| 38 |
|
| 39 |
# 7. Overlay the data on the image
|
| 40 |
-
# We'll create a function to do this cleanly.
|
| 41 |
generated_label = create_label(background, model_data)
|
| 42 |
|
| 43 |
-
# 8. Display the generated label in the
|
| 44 |
st.image(generated_label, caption="Generated Label Preview")
|
| 45 |
|
| 46 |
-
# 9. Provide a download button
|
| 47 |
-
# We'll create an in-memory file to let user download the image.
|
| 48 |
img_buffer = io.BytesIO()
|
| 49 |
generated_label.save(img_buffer, format="PNG")
|
| 50 |
img_buffer.seek(0)
|
| 51 |
|
| 52 |
-
st.download_button(
|
| 53 |
label="Download Label as PNG",
|
| 54 |
data=img_buffer,
|
| 55 |
file_name="AIEnergyScore.png",
|
|
@@ -61,49 +68,40 @@ def create_label(background_image, model_data):
|
|
| 61 |
This function takes a background image and a row (model_data) from the CSV,
|
| 62 |
then draws text on the image. Finally, returns the modified image object.
|
| 63 |
"""
|
| 64 |
-
# Convert background to a format that can be edited (RGBA mode).
|
| 65 |
label_img = background_image.convert("RGBA")
|
| 66 |
-
|
| 67 |
-
# Create a Drawing context
|
| 68 |
draw = ImageDraw.Draw(label_img)
|
| 69 |
|
| 70 |
-
#
|
| 71 |
-
# If you don't have a custom font file, you can use a PIL built-in font.
|
| 72 |
try:
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
| 75 |
except Exception as e:
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
#
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
text_lines = [model_name, task, date, energy]
|
| 102 |
-
|
| 103 |
-
# Draw each line on the image
|
| 104 |
-
for line in text_lines:
|
| 105 |
-
draw.text((x_position, y_position), line, fill="black", font=font)
|
| 106 |
-
y_position += line_spacing
|
| 107 |
|
| 108 |
return label_img
|
| 109 |
|
|
|
|
| 4 |
import io
|
| 5 |
|
| 6 |
def main():
|
| 7 |
+
# 1. Sidebar for Dropdown, Buttons, and Instructions
|
| 8 |
+
st.sidebar.title("AI Energy Score Label Generator")
|
| 9 |
+
st.sidebar.write("### Instructions:")
|
| 10 |
+
st.sidebar.write("1. Select a model from the dropdown.")
|
| 11 |
+
st.sidebar.write("2. Review the label preview.")
|
| 12 |
+
st.sidebar.write("3. Download the label as a PNG.")
|
| 13 |
+
st.sidebar.markdown("[Learn more about AI Energy Scores](https://example.com)")
|
| 14 |
|
| 15 |
# 2. Read Data from CSV
|
|
|
|
|
|
|
| 16 |
try:
|
| 17 |
data_df = pd.read_csv("data.csv")
|
| 18 |
except FileNotFoundError:
|
| 19 |
+
st.sidebar.error("Could not find 'data.csv'! Please make sure it's present.")
|
| 20 |
return
|
| 21 |
|
| 22 |
+
# 3. Ensure the CSV has required columns
|
| 23 |
+
required_columns = ["model", "provider", "date", "task", "hardware", "energy", "score"]
|
| 24 |
+
for col in required_columns:
|
| 25 |
+
if col not in data_df.columns:
|
| 26 |
+
st.sidebar.error(f"The CSV file must contain a column named '{col}'.")
|
| 27 |
+
return
|
| 28 |
|
| 29 |
+
# 4. Create a dropdown list based on unique values in the 'model' column
|
| 30 |
+
model_options = data_df["model"].unique().tolist()
|
| 31 |
+
selected_model = st.sidebar.selectbox("Select a Model:", model_options)
|
| 32 |
|
| 33 |
# 5. Filter the data for the selected model
|
| 34 |
+
model_data = data_df[data_df["model"] == selected_model].iloc[0]
|
| 35 |
|
| 36 |
+
# 6. Dynamically select the background image based on the score
|
|
|
|
| 37 |
try:
|
| 38 |
+
score = int(model_data["score"]) # Convert to int to avoid issues
|
| 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 |
# 7. Overlay the data on the image
|
|
|
|
| 49 |
generated_label = create_label(background, model_data)
|
| 50 |
|
| 51 |
+
# 8. Display the generated label in the main area
|
| 52 |
st.image(generated_label, caption="Generated Label Preview")
|
| 53 |
|
| 54 |
+
# 9. Provide a download button in the sidebar
|
|
|
|
| 55 |
img_buffer = io.BytesIO()
|
| 56 |
generated_label.save(img_buffer, format="PNG")
|
| 57 |
img_buffer.seek(0)
|
| 58 |
|
| 59 |
+
st.sidebar.download_button(
|
| 60 |
label="Download Label as PNG",
|
| 61 |
data=img_buffer,
|
| 62 |
file_name="AIEnergyScore.png",
|
|
|
|
| 68 |
This function takes a background image and a row (model_data) from the CSV,
|
| 69 |
then draws text on the image. Finally, returns the modified image object.
|
| 70 |
"""
|
|
|
|
| 71 |
label_img = background_image.convert("RGBA")
|
|
|
|
|
|
|
| 72 |
draw = ImageDraw.Draw(label_img)
|
| 73 |
|
| 74 |
+
# Load the Inter variable font
|
|
|
|
| 75 |
try:
|
| 76 |
+
inter_font_path = "Inter-VariableFont_opsz,wght.ttf"
|
| 77 |
+
title_font = ImageFont.truetype(inter_font_path, 16, layout_engine=ImageFont.LAYOUT_RAQM)
|
| 78 |
+
details_font = ImageFont.truetype(inter_font_path, 12, layout_engine=ImageFont.LAYOUT_RAQM)
|
| 79 |
+
energy_font = ImageFont.truetype(inter_font_path, 14, layout_engine=ImageFont.LAYOUT_RAQM)
|
| 80 |
except Exception as e:
|
| 81 |
+
st.error(f"Font loading failed: {e}")
|
| 82 |
+
return label_img
|
| 83 |
+
|
| 84 |
+
# Define positions for the text groups (easy to tweak!)
|
| 85 |
+
title_x, title_y = 20, 20 # Position for title group
|
| 86 |
+
details_x, details_y = 300, 20 # Position for details group
|
| 87 |
+
energy_x, energy_y = 150, 400 # Position for energy group
|
| 88 |
+
|
| 89 |
+
# Group 1: Title (Left-Justified)
|
| 90 |
+
draw.text((title_x, title_y), f"Model: {model_data['model']}", font=title_font, fill="black")
|
| 91 |
+
draw.text((title_x, title_y + 25), f"Provider: {model_data['provider']}", font=title_font, fill="black")
|
| 92 |
+
|
| 93 |
+
# Group 2: Details (Right-Justified)
|
| 94 |
+
details_lines = [
|
| 95 |
+
f"Date: {model_data['date']}",
|
| 96 |
+
f"Task: {model_data['task']}",
|
| 97 |
+
f"Hardware: {model_data['hardware']}"
|
| 98 |
+
]
|
| 99 |
+
for i, line in enumerate(details_lines):
|
| 100 |
+
draw.text((details_x, details_y + i * 20), line, font=details_font, fill="black", anchor="ra")
|
| 101 |
+
|
| 102 |
+
# Group 3: Energy (Bottom-Center)
|
| 103 |
+
energy_text = f"Energy: {model_data['energy']}"
|
| 104 |
+
draw.text((energy_x, energy_y), energy_text, font=energy_font, fill="black")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
return label_img
|
| 107 |
|