bgamazay commited on
Commit
bd369fc
·
verified ·
1 Parent(s): 4575207

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -19
app.py CHANGED
@@ -8,7 +8,27 @@ def main():
8
  st.sidebar.image("logo.png", use_container_width=True) # Display the logo at the top
9
  st.sidebar.title("Label Generator")
10
 
11
- # Instructions with integrated dropdown
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  st.sidebar.write("### Instructions:")
13
  st.sidebar.write("1. Select a model from the dropdown below:")
14
  model_options = data_df["model"].unique().tolist() # Get model options
@@ -18,9 +38,38 @@ def main():
18
 
19
  # Add step 2 instructions and move the Download button
20
  st.sidebar.write("2. Review the label preview and download your label below:")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  img_buffer = io.BytesIO()
22
  generated_label.save(img_buffer, format="PNG")
23
  img_buffer.seek(0)
 
24
  st.sidebar.download_button(
25
  label="Download Label as PNG",
26
  data=img_buffer,
@@ -32,24 +81,6 @@ def main():
32
  st.sidebar.write("3. Share your label in technical reports, announcements, etc.")
33
  st.sidebar.markdown("[AI Energy Score Leaderboard](https://huggingface.co/spaces/AIEnergyScore/Leaderboard)")
34
 
35
- # Read Data from CSV
36
- try:
37
- data_df = pd.read_csv("data.csv")
38
- except FileNotFoundError:
39
- st.sidebar.error("Could not find 'data.csv'! Please make sure it's present.")
40
- return
41
-
42
- # Ensure the CSV has required columns
43
- required_columns = ["model", "provider", "date", "task", "hardware", "energy", "score"]
44
- for col in required_columns:
45
- if col not in data_df.columns:
46
- st.sidebar.error(f"The CSV file must contain a column named '{col}'.")
47
- return
48
-
49
- # Dropdown for selecting a model
50
- model_options = data_df["model"].unique().tolist()
51
- selected_model = st.sidebar.selectbox("Select a Model:", model_options)
52
-
53
  # Filter the data for the selected model
54
  model_data = data_df[data_df["model"] == selected_model].iloc[0]
55
 
 
8
  st.sidebar.image("logo.png", use_container_width=True) # Display the logo at the top
9
  st.sidebar.title("Label Generator")
10
 
11
+ # Initialize data_df
12
+ data_df = None
13
+
14
+ # Read Data from CSV
15
+ try:
16
+ data_df = pd.read_csv("data.csv")
17
+ except FileNotFoundError:
18
+ st.sidebar.error("Could not find 'data.csv'! Please make sure it's present.")
19
+ return
20
+ except Exception as e:
21
+ st.sidebar.error(f"Error reading 'data.csv': {e}")
22
+ return
23
+
24
+ # Ensure the CSV has required columns
25
+ required_columns = ["model", "provider", "date", "task", "hardware", "energy", "score"]
26
+ for col in required_columns:
27
+ if col not in data_df.columns:
28
+ st.sidebar.error(f"The CSV file must contain a column named '{col}'.")
29
+ return
30
+
31
+ # Dropdown for selecting a model
32
  st.sidebar.write("### Instructions:")
33
  st.sidebar.write("1. Select a model from the dropdown below:")
34
  model_options = data_df["model"].unique().tolist() # Get model options
 
38
 
39
  # Add step 2 instructions and move the Download button
40
  st.sidebar.write("2. Review the label preview and download your label below:")
41
+
42
+ # Dynamically select the background image and generate label (this part assumes data_df is valid)
43
+ model_data = data_df[data_df["model"] == selected_model].iloc[0]
44
+
45
+ try:
46
+ score = int(model_data["score"]) # Convert to int
47
+ background_path = f"{score}.png" # E.g., "1.png", "2.png"
48
+ background = Image.open(background_path).convert("RGBA")
49
+
50
+ # Proportional scaling to fit within the target size
51
+ target_size = (800, 600) # Maximum width and height
52
+ background.thumbnail(target_size, Image.Resampling.LANCZOS)
53
+
54
+ except FileNotFoundError:
55
+ st.sidebar.error(f"Could not find background image '{score}.png'. Using default background.")
56
+ background = Image.open("default_background.png").convert("RGBA")
57
+ background.thumbnail(target_size, Image.Resampling.LANCZOS) # Resize default image proportionally
58
+ except ValueError:
59
+ st.sidebar.error(f"Invalid score '{model_data['score']}'. Score must be an integer.")
60
+ return
61
+
62
+ # Generate the label with text
63
+ generated_label = create_label(background, model_data)
64
+
65
+ # Display the label
66
+ st.image(generated_label, caption="Generated Label Preview")
67
+
68
+ # Download button for the label
69
  img_buffer = io.BytesIO()
70
  generated_label.save(img_buffer, format="PNG")
71
  img_buffer.seek(0)
72
+
73
  st.sidebar.download_button(
74
  label="Download Label as PNG",
75
  data=img_buffer,
 
81
  st.sidebar.write("3. Share your label in technical reports, announcements, etc.")
82
  st.sidebar.markdown("[AI Energy Score Leaderboard](https://huggingface.co/spaces/AIEnergyScore/Leaderboard)")
83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  # Filter the data for the selected model
85
  model_data = data_df[data_df["model"] == selected_model].iloc[0]
86