| | import streamlit as st |
| | import pandas as pd |
| | import altair as alt |
| | import base64 |
| | import pdfkit |
| | import io |
| | from comparateur import * |
| |
|
| | def load_svg_as_base64(svg_file_path): |
| | with open(svg_file_path, "rb") as svg_file: |
| | return base64.b64encode(svg_file.read()).decode() |
| |
|
| | def save_pdf(html_content): |
| | pdf = pdfkit.from_string(html_content, False) |
| | return pdf |
| |
|
| | def display_comparaison_html(value_init, ratio_equivalent, icon, unit): |
| | link_url = f"https://impactco2.fr/outils/comparateur?value={value_init}&comparisons=tgv,eauenbouteille,voiturethermique" |
| | html = f""" |
| | <div style='text-align: center;'> |
| | <a href='{link_url}' target='_blank'><img src='{icon}' alt='{unit}' width='50'></a> |
| | </div> |
| | <div style='text-align: center;'> |
| | <b>{compare(value_init, ratio_equivalent):.2f}</b> {unit} |
| | </div> |
| | """ |
| | return html |
| |
|
| | def display_cf_comparison(): |
| | svg_file_path = "feuille.svg" |
| | svg_base64 = load_svg_as_base64(svg_file_path) |
| | |
| | html_content = f""" |
| | <div style='display: flex; align-items: center;'> |
| | <h4 style='margin: 0;'>Votre consommation Carbone</h4> |
| | <img src='data:image/svg+xml;base64,{svg_base64}' alt='svg' width='15' height='15' style='margin-left: 10px;'> |
| | </div> |
| | <br> |
| | """ |
| | |
| | serveur_emission = st.session_state['emission'].stop() |
| | emission_api = sum([value["el"] for value in st.session_state["partial_emissions"].values()]) |
| |
|
| | total_emission = serveur_emission + emission_api |
| |
|
| | pourcentage_api = emission_api / total_emission |
| | pourcentage_serveur = serveur_emission / total_emission |
| |
|
| | html_content += f"<div style='text-align: center; margin-bottom: 10px;'><b>{total_emission*1000:.3f}</b> g eq. CO2</div>" |
| | html_content += f"<p>Dont :</p>" |
| | html_content += f"<p>- Empreinte serveur (via CodeCarbon) : <b>{serveur_emission*1000:.3f}</b> g eq. CO2 ({pourcentage_serveur:.2%})</p>" |
| | html_content += f"<p>- Empreinte IA (via EcoLogits) : <b>{emission_api*1000:.3f}</b> g eq. CO2 ({pourcentage_api:.2%})</p>" |
| |
|
| | html_content += "<h4>Votre équivalence</h4>" |
| | html_content += """ |
| | <div style='display: flex; justify-content: space-around;'> |
| | """ |
| |
|
| | html_content += f""" |
| | <div> |
| | {display_comparaison_html(total_emission, dict_comparaison_1kgCO2["eau en litre"][0]*1000, dict_comparaison_1kgCO2["eau en litre"][1], "ml")} |
| | </div> |
| | <div> |
| | {display_comparaison_html(total_emission, dict_comparaison_1kgCO2["tgv en km"][0], dict_comparaison_1kgCO2["tgv en km"][1], "km")} |
| | </div> |
| | <div> |
| | {display_comparaison_html(total_emission, dict_comparaison_1kgCO2["voiture en km"][0]*1000, dict_comparaison_1kgCO2["voiture en km"][1], "m")} |
| | </div> |
| | """ |
| |
|
| | html_content += "</div><br>" |
| |
|
| |
|
| | html_content += f""" |
| | <br> |
| | <div style='display: flex; align-items: center;'> |
| | <p>Powered by <b>ADEME</b></p> |
| | <a href='https://www.ademe.fr' target='_blank'><img src='https://www.ademe.fr/wp-content/uploads/2022/11/ademe-logo-2022-1.svg' alt='svg' width='30' height='30' style='margin-left: 10px;'></a> |
| | </div> |
| | <br> |
| | """ |
| | |
| | |
| | return html_content |
| |
|
| | def color_scale(val): |
| | if val == '-': |
| | return 'background-color: white' |
| | elif val <= 1: |
| | return 'background-color: rgba(0,100,0,0.5)' |
| | elif val <= 10: |
| | return 'background-color: rgba(0,128,0,0.5)' |
| | elif val <= 50: |
| | return 'background-color: rgba(255,255,0,0.5)' |
| | elif val <= 100: |
| | return 'background-color: rgba(255,165,0,0.5)' |
| | else: |
| | return 'background-color: rgba(255,0,0,0.5)' |
| |
|
| |
|
| | def get_carbon_footprint_html(): |
| |
|
| | html_content = "<h2>EMPREINTE ÉNERGÉTIQUE DE L'APPLICATION IA CARTO RSE</h2>" |
| | html_content += display_cf_comparison() |
| | |
| | table = get_table_empreintes_detailed() |
| | table.replace({0.00: '-'}, inplace=True) |
| | styled_df = table[['Consommation Totale']].rename(columns={'Consommation Totale': 'Consommation totale (g eqCo2)'}) |
| | styled_df = styled_df.style.applymap(color_scale, subset=['Consommation totale (g eqCo2)']) |
| | |
| | html_content += """ |
| | <style> |
| | .centered-table { |
| | margin-left: auto; |
| | margin-right: auto; |
| | border-collapse: collapse; |
| | width: 80%; |
| | } |
| | .centered-table th, .centered-table td { |
| | border: 1px solid #ddd; |
| | padding: 8px; |
| | text-align: left; |
| | } |
| | .centered-table th { |
| | background-color: #f2f2f2; |
| | } |
| | </style> |
| | """ |
| |
|
| | html_content += """ |
| | <h2>DÉTAIL PAR TÂCHE</h2> |
| | <div style="overflow-x:auto;"> |
| | """ |
| |
|
| | html_content += styled_df.set_table_attributes('class="centered-table"').to_html() |
| |
|
| | html_content += """ |
| | </div> |
| | """ |
| |
|
| | serveur_emission = st.session_state['emission'].stop() |
| | emission_api = sum([value["el"] for value in st.session_state["partial_emissions"].values()]) |
| |
|
| | total_emission = serveur_emission + emission_api |
| |
|
| | pourcentage_api = emission_api / total_emission |
| | pourcentage_serveur = serveur_emission / total_emission |
| |
|
| | df = pd.DataFrame({"Catégorie": ["Identification + dessin", "IA (extraction pp + dialogue)"], "valeur": [pourcentage_serveur, pourcentage_api]}) |
| | color_scale_alt = alt.Scale(domain=['Identification + dessin', 'IA (extraction pp + dialogue)'], range=['#011166', '#63abdf']) |
| |
|
| | base = alt.Chart(df).encode( |
| | theta=alt.Theta(field="valeur", type="quantitative", stack=True), |
| | color=alt.Color(field="Catégorie", type="nominal",scale=color_scale_alt) |
| | ) |
| |
|
| | pie = base.mark_arc(outerRadius=100) |
| | text = base.mark_text(radius=150, fill="black",align='center', baseline='middle',fontSize=14).encode(alt.Text(field="valeur", type="quantitative", format=".2%")) |
| |
|
| | chart = alt.layer(pie, text, data=df).resolve_scale(theta="independent") |
| |
|
| | html_content += """ |
| | <h2>SYNTHESE (Dialogue IA et non IA)</h2> |
| | """ |
| | chart.save("chart.png") |
| | with open("chart.png", "rb") as image_file: |
| | encoded_image = base64.b64encode(image_file.read()).decode() |
| |
|
| | html_content += f'<div style="text-align:center;"><img src="data:image/png;base64,{encoded_image}" alt="Pie chart"></div>' |
| | |
| | return html_content |
| |
|
| |
|