FlorianSC/agritech-interface
0
1import streamlit as st2import requests3import pandas as pd4 5# Configuration des URL6API_URL_PREDICT = "http://localhost:8000/predict"7API_URL_RECO = "http://localhost:8000/recommend"8 9st.set_page_config(page_title="Agritech Predictor", layout="wide")10 11st.title("🌾 Système de Recommandation Agricole")12 13# Dictionnaire des infos mape à afficher dans l'encart14infos_mape = {15 "Maize": "18,45%",16 "Potatoes": "13,77%",17 "Rice": "13,07%",18 "Wheat": "16,79%",19 "Sorghum": "22,50%",20 "Soybean": "20,23%",21 "Cassava": "16,35%",22 "Yams": "28,40%",23 "Sweet potatoes": "17,24%",24 "Plantains and others": "17,46%"25}26# Dictionnaire des infos de perte économqiue à afficher dans l'encart27infos_economic = {28 "Maize": "137.09 $",29 "Potatoes": "547.61 $",30 "Rice": "123.23 $",31 "Wheat": "71.89 $",32 "Sorghum": "71.33 $",33 "Soybean": "88.31 $",34 "Cassava": "316.57 $",35 "Yams": "1244.71 $",36 "Sweet potatoes": "278.97 $",37 "Plantains and others": "325.10 $"38}39 40# Utilisation d'onglets41tab1, tab2 = st.tabs(["🔮 Prédiction de Rendement", "💡 Recommandation de Culture"])42 43# --- ONGLET 1 : PRÉDICTION ---44with tab1:45 st.header("Estimer le rendement d'une culture précise")46 47 # 2 colonnes : gauche formulaire / droite encart48 left_col, right_col = st.columns([3, 1])49 50 with left_col:51 with st.form("form_prediction"):52 col1, col2 = st.columns(2)53 54 with col1:55 item = st.selectbox(56 "Culture",57 ['Maize', 'Potatoes', 'Rice', 'Wheat', 'Sorghum', 'Soybean',58 'Cassava', 'Yams', 'Sweet potatoes', 'Plantains and others'],59 key="item_p"60 )61 region = st.selectbox(62 "Région",63 ['Southern Asia', 'Southern Europe', 'Northern Africa', 'Polynesia',64 'Sub-Saharan Africa', 'Latin America and the Caribbean',65 'Western Asia', 'Australia and New Zealand', 'Western Europe',66 'Eastern Europe', 'Northern America', 'South-eastern Asia', 'Eastern Asia',67 'Northern Europe', 'Melanesia', 'Micronesia', 'Central Asia'],68 key="reg_p"69 )70 71 with col2:72 avg_temp = st.slider("Température Moyenne (°C)", -5.0, 45.0, 15.0, key="temp_p")73 rainfall = st.slider("Précipitations (mm)", min_value=0, value=3500, key="rain_p")74 pesticides = st.slider("Pesticides (tonnes)", min_value=0.0, value=1850000.0, key="pest_p")75 76 submit_p = st.form_submit_button("Lancer la prédiction")77 78 with right_col:79 st.markdown("### Taux d'erreur en %")80 st.info(infos_mape.get(st.session_state.get("item_p", "Maize"), "Aucune info disponible."))81 st.markdown("### Perte économique en dollars (par hectare)")82 st.info(infos_economic.get(st.session_state.get("item_p", "Maize"), "Aucune info disponible."))83 84 if submit_p:85 payload = {86 "region": region,87 "item": item,88 "avg_temp": avg_temp,89 "rainfall_mm": rainfall,90 "pesticides_tonnes": pesticides91 }92 try:93 with st.spinner("Calcul en cours..."):94 res = requests.post(API_URL_PREDICT, json=payload)95 res.raise_for_status()96 data = res.json()97 st.success(f"### Résultat : {data['prediction']:.2f} kg/ha")98 except Exception as e:99 st.error(f"Erreur : {e}")100 101# --- ONGLET 2 : RECOMMANDATION ---102with tab2:103 st.header("Quelle culture est la plus adaptée ?")104 st.info("Cette fonction testera toutes les cultures pour vos conditions climatiques.")105 with st.form("form_reco"):106 col1, col2 = st.columns(2)107 with col1:108 region_r = st.selectbox("Région", ['Southern Asia', 'Southern Europe', 'Northern Africa', 'Polynesia',109 'Sub-Saharan Africa', 'Latin America and the Caribbean',110 'Western Asia', 'Australia and New Zealand', 'Western Europe',111 'Eastern Europe', 'Northern America', 'South-eastern Asia','Eastern Asia',112 'Northern Europe', 'Melanesia', 'Micronesia','Central Asia'], key="reg_r")113 114 with col2:115 avg_temp_r = st.slider("Température Moyenne (°C)", -5.0, 45.0, 15.0, key="temp_r")116 rainfall_r = st.slider("Précipitations (mm)", min_value=0, value=3500, key="rain_r")117 pesticides_r = st.slider("Pesticides (tonnes)", min_value=0.0, value=1850000.0, key="pest_r")118 119 submit_r = st.form_submit_button("Trouver la meilleure culture")120 121 if submit_r:122 payload_r = {123 "region": region_r,124 "avg_temp": avg_temp_r, "rainfall_mm": rainfall_r, "pesticides_tonnes": pesticides_r125 }126 try:127 with st.spinner("Analyse des cultures..."):128 res = requests.post(API_URL_RECO, json=payload_r)129 res.raise_for_status()130 data = res.json()131 132 # Préparation des données pour le graphique133 df_reco = pd.DataFrame(data['ranking'])134 df_reco = df_reco.set_index('crop')135 df_reco = df_reco.sort_values(by='predicted_yield', ascending=False)136 137 st.success(f"🏆 La meilleure culture est : **{data['best_crop']}**")138 # Afficher le classement139 st.write("Classement complet :")140 st.table(data['ranking'])141 142 # Affichage du graphique 143 st.subheader("📊 Comparaison des rendements (kg/ha)")144 st.bar_chart(df_reco)145 146 except Exception as e:147 st.error(f"Erreur : {e}")148 149 