TheoLvs commited on
Commit
ff1c689
·
1 Parent(s): f474c7c

FIrst commit

Browse files
Files changed (3) hide show
  1. .gitignore +2 -0
  2. app.py +74 -0
  3. sandbox.ipynb +37 -0
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+
2
+ .ipynb_checkpoints/sandbox-checkpoint.ipynb
app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from codecarbon import EmissionsTracker
3
+
4
+ # Import necessary libraries
5
+ from sklearn.model_selection import train_test_split
6
+ from sklearn.ensemble import RandomForestClassifier
7
+ from sklearn.metrics import classification_report, accuracy_score
8
+ import pandas as pd
9
+ import numpy as np
10
+
11
+ # Let's create a sample dataset (you can replace this with your own data)
12
+ def create_sample_data():
13
+ np.random.seed(42)
14
+ n_samples = 10000
15
+
16
+ # Create features (X)
17
+ X = np.random.randn(n_samples, 4) # 4 features
18
+
19
+ # Create target (y) - binary classification
20
+ y = (X[:, 0] + X[:, 1] + X[:, 2] > 0).astype(int)
21
+
22
+ return X, y
23
+
24
+ # Get data (replace this with your data loading code)
25
+ X, y = create_sample_data()
26
+ tracker = EmissionsTracker()
27
+
28
+ def submit(username):
29
+
30
+ tracker.start()
31
+
32
+ tracker.start_task("train_model")
33
+ # Split the data into training and testing sets
34
+ X_train, X_test, y_train, y_test = train_test_split(
35
+ X, y, test_size=0.2, random_state=42
36
+ )
37
+
38
+ # Initialize the model
39
+ rf_model = RandomForestClassifier(
40
+ n_estimators=1000,
41
+ max_depth=5,
42
+ random_state=42
43
+ )
44
+
45
+ # Train the model
46
+ print("Training the model...")
47
+ rf_model.fit(X_train, y_train)
48
+
49
+ training_emissions = tracker.stop_task()
50
+
51
+ tracker.start_task("inference")
52
+ rf_model.predict(X_test)
53
+ inference_emissions = tracker.stop_task()
54
+
55
+ emissions = inference_emissions.emissions
56
+ energy = inference_emissions.energy_consumed
57
+
58
+ return [emissions, energy]
59
+
60
+ # Update the interface configuration
61
+ demo = gr.Interface(
62
+ fn=submit,
63
+ inputs=gr.Textbox(label="Username"),
64
+ outputs=[
65
+ gr.Number(label="Emissions (kgCO2eq)", precision=6),
66
+ gr.Number(label="Energy Consumed (kWh)", precision=6)
67
+ ],
68
+ title="Carbon Emissions Tracker",
69
+ description="Track the carbon emissions and energy consumption of model training and inference."
70
+ )
71
+
72
+ # Launch the Gradio interface
73
+ if __name__ == "__main__":
74
+ demo.launch()
sandbox.ipynb ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": null,
6
+ "id": "6941ccb0-6ed0-45c1-9460-8f8c0bbfc288",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "from codecarbon import EmissionsTracker\n",
11
+ "tracker = EmissionsTracker()\n",
12
+ "tracker.start()"
13
+ ]
14
+ }
15
+ ],
16
+ "metadata": {
17
+ "kernelspec": {
18
+ "display_name": "Python 3 (ipykernel)",
19
+ "language": "python",
20
+ "name": "python3"
21
+ },
22
+ "language_info": {
23
+ "codemirror_mode": {
24
+ "name": "ipython",
25
+ "version": 3
26
+ },
27
+ "file_extension": ".py",
28
+ "mimetype": "text/x-python",
29
+ "name": "python",
30
+ "nbconvert_exporter": "python",
31
+ "pygments_lexer": "ipython3",
32
+ "version": "3.12.4"
33
+ }
34
+ },
35
+ "nbformat": 4,
36
+ "nbformat_minor": 5
37
+ }