bgamazay commited on
Commit
0687bfd
·
verified ·
1 Parent(s): 76364d7

Upload 4 files

Browse files
Files changed (4) hide show
  1. app.py +110 -0
  2. background.png +0 -0
  3. data.csv +4 -0
  4. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ from PIL import Image, ImageDraw, ImageFont
4
+ import io
5
+
6
+ def main():
7
+ # 1. Page Title
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 a "Model" column
20
+ if "Model" not in data_df.columns:
21
+ st.error("The CSV file must contain a column named 'Model'.")
22
+ return
23
+
24
+ # 4. Create a dropdown list based on unique values in the 'Model' column
25
+ model_options = data_df["Model"].unique().tolist()
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["Model"] == selected_model].iloc[0]
30
+
31
+ # 6. Load the background image for the label
32
+ # Make sure 'background.png' is in the same folder as 'app.py'
33
+ try:
34
+ background = Image.open("background.png")
35
+ except FileNotFoundError:
36
+ st.error("Could not find 'background.png'! Please make sure it's present.")
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 Streamlit app
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",
56
+ mime="image/png"
57
+ )
58
+
59
+ def create_label(background_image, model_data):
60
+ """
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
+ # Choose a font and size. Change path/size as needed.
71
+ # If you don't have a custom font file, you can use a PIL built-in font.
72
+ try:
73
+ font = ImageFont.truetype("Roboto-Regular.ttf", 20) # or "Arial.ttf", etc.
74
+ except:
75
+ # Fallback to a default PIL font if 'arial.ttf' is not found
76
+ font = ImageFont.load_default()
77
+
78
+ # You can customize the positions, colors, etc. For instance:
79
+ # We'll just place the text in a simple stacked format at a fixed position.
80
+ # Position variables (x, y) - Adjust as needed.
81
+ x_position = 50
82
+ y_position = 50
83
+ line_spacing = 30
84
+
85
+ # Extracting data from the row. Customize these lines based on your CSV columns.
86
+ model_name = f"Model: {model_data['Model']}"
87
+ if 'Task' in model_data:
88
+ task = f"Task: {model_data['Task']}"
89
+ else:
90
+ task = "Task: N/A"
91
+ if 'date' in model_data:
92
+ date = f"Date: {model_data['Date']}"
93
+ else:
94
+ date = "Date: N/A"
95
+ if 'Energy' in model_data:
96
+ energy = f"Energy: {model_data['Energy']}"
97
+ else:
98
+ energy = "Energy: N/A"
99
+
100
+ text_lines = [model_name, task, date, energy]
101
+
102
+ # Draw each line on the image
103
+ for line in text_lines:
104
+ draw.text((x_position, y_position), line, fill="black", font=font)
105
+ y_position += line_spacing
106
+
107
+ return label_img
108
+
109
+ if __name__ == "__main__":
110
+ main()
background.png ADDED
data.csv ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ Model,Task,Date,Energy
2
+ GPT2,text generation,Feb-25,122
3
+ SmolLm,text generation,Feb-25,1.23
4
+ Llama3.2,text generation,Feb-25,13213
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ streamlit
2
+ pandas
3
+ Pillow