File size: 28,432 Bytes
37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 5eec335 08ec439 37c00da 0327394 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 5eec335 08ec439 5eec335 08ec439 5eec335 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 08ec439 37c00da 5eec335 08ec439 5eec335 37c00da 08ec439 37c00da 08ec439 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 |
# import libraries
import pandas as pd
import numpy as np
import os
import time
import math
import streamlit as st
# from streamlit_chat import message
from streamlit_extras.colored_header import colored_header
from streamlit_extras.add_vertical_space import add_vertical_space
from PIL import Image
# import modules
from src.backend.chatbot import *
from src.backend.optimization_algo import *
from src.frontend.visualizations import *
# import compatibilities matrix
# make plant_compatibility.csv into a matrix. it currently has indexes as rows and columns for plant names and then compatibility values as the values
st.session_state.raw_plant_compatibility = pd.read_csv(
"src/data/plant_compatibility.csv", index_col=0
)
# fill NaN values with 0
st.session_state.raw_plant_compatibility = (
st.session_state.raw_plant_compatibility.fillna(0)
)
# get list of plants
st.session_state.plant_list = st.session_state.raw_plant_compatibility.index.tolist()
# set version
st.session_state.demo_lite = False
# set default model
st.session_state.model = "Llama2-7b_CPP"
# setup keys and api info
# OPENAI_API_KEY = st.secrets["OPENAI_API_KEY"]
# os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
# chat = ChatOpenAI()
# UI page config
st.set_page_config(
# page_title="GRDN.AI",
page_icon="🌱",
layout="wide",
initial_sidebar_state="expanded",
)
# Function to display chat message with an icon
def chat_message(message, is_user=False):
if is_user:
icon = Image.open("src/assets/cool.png")
side = "left"
else:
icon = Image.open("src/assets/bot.png")
side = "right"
chat_container = st.container()
with chat_container:
col1, col2, col3, col4 = st.columns([1, 7, 7, 1])
with col1:
if is_user == True:
st.image(icon, width=50)
with col2:
if is_user == True:
st.markdown(
f'<div style="text-align: {side};">{message}</div>',
unsafe_allow_html=True,
)
with col3:
if is_user == False:
st.markdown(
f'<div style="text-align: {side};">{message}</div>',
unsafe_allow_html=True,
)
with col4:
if is_user == False:
st.image(icon, width=50)
st.image(
"src/assets/logo_title_transparent.png",
caption=None,
use_column_width=None,
clamp=False,
channels="RGB",
output_format="auto",
)
st.write("AI and optimization powered companion gardening")
colored_header(label="", description="", color_name="green-30")
# Sidebar
# st.sidebar.title("Navigation")
# page = st.sidebar.radio("Select a page:", ("Home", "Companion Gardening", "Optimization", "About"))
# add vertical space
with st.sidebar:
add_vertical_space(2)
# Sidebar
st.sidebar.title("Navigation")
# Define the page options
pages = ["Garden Optimization", "About"]
# Render the selected page content
page = st.sidebar.selectbox("Select a page:", pages)
if page == "Garden Optimization":
st.sidebar.subheader("Companion Gardening")
st.write(
"GRDN is a companion gardening app that helps you plan your garden and maximize your harvest. It uses optimization and AI to predict the best plants to grow together and optimization algorithms to optimize how you build your garden."
)
st.write("This app is currently in beta. Please report any bugs.")
companion_planting_info = """
Key Benefits
- **Pest control:**
- **Improved pollination:**
- **Maximized space:**
- **Nutrient enhancement:**
- **Complementary growth:**
"""
st.sidebar.markdown(companion_planting_info)
# dropdown with advanced algorithm options- LLM agent base model and optimization algorithm type (constrained genetic algorithm or constrained community detection mixed integer programming model)
# with st.sidebar.expander("Advanced Options: LLM, optimization algorithm"):
# select LLM agent base model
st.sidebar.write("\n\n\n")
st.sidebar.write("\n\n\n")
st.sidebar.subheader("LLM agent base model")
# radio buttons for LLM used throughout the app ('openai', 'Llama2-7b_CPP', 'deci-7b-CPP')
st.session_state.model = st.sidebar.radio(
"Select an open-source LLM :",
(
#'openai-gpt35turbo',
"Llama2-7b_CPP",
"deci-7b_CPP",
"lite_demo (no LLM)",
),
)
# # radio buttons for optimization algorithm used throughout the app ('constrained_genetic_algorithm', 'constrained_community_detection_mip')
# st.session_state.optimization_algo = st.radio("Select an optimization algorithm :", (
# 'constrained_genetic_algorithm',
# 'constrained_community_detection_mip'))
# override model if lite demo is selected
if (
st.session_state.model == "lite_demo (no LLM)"
or st.session_state.model == "deci-7b_CPP"
):
st.session_state.demo_lite = True
# Set the initial value of user_name
if "user_name" not in st.session_state:
st.session_state.user_name = ""
# add in some vertical space
add_vertical_space(3)
# Display the welcome message
st.title("Let's get started! Decide on your garden parameters")
# add in some vertical space
add_vertical_space(2)
# make a container for this section
container1 = st.container(border=True)
with container1:
# Modify the user_name variable based on user input
if st.session_state["user_name"] == "":
col1, col2, col3 = st.columns([1, 2, 1])
with col1:
st.session_state["user_name_input"] = st.text_input(
"Enter your name", st.session_state.user_name
)
if "user_name_input" in st.session_state:
st.session_state.user_name = st.session_state.user_name_input
if st.session_state.user_name != "":
st.write(
"Hello "
+ st.session_state["user_name"]
+ "! Let's optimize your garden. "
)
# # add in some vertical space
add_vertical_space(2)
print("")
print("____________________")
print("start of session")
col1a, col2a = st.columns([1, 2])
enable_max_species = False
enable_min_species = False
# make a form to get the plant list from the user
with col1a:
with st.form(key="plant_list_form"):
input_plants_raw = st.multiselect(
"plants", st.session_state.plant_list
)
submit_button = st.form_submit_button(label="Submit Plant List")
if submit_button:
st.session_state["input_plants_raw"] = input_plants_raw
st.session_state.submitted_plant_list = True
# add in some vertical space
add_vertical_space(1)
with col2a:
col1, col2, col3 = st.columns([1, 1, 1])
if "input_plants_raw" in st.session_state:
print("BP1")
# first question is what plants would you like to plant
plants_response = st.session_state.input_plants_raw
# Initialize session state variables if they don't exist
if "n_plant_beds" not in st.session_state:
st.session_state["n_plant_beds"] = 1
if "min_species" not in st.session_state:
st.session_state["min_species"] = 1
if "max_species" not in st.session_state:
st.session_state["max_species"] = 2
# Number of plant beds input
with col1:
n_plant_beds = st.number_input(
"Number of plant beds \n",
min_value=1,
max_value=20,
value=st.session_state.n_plant_beds,
step=1,
)
st.session_state.n_plant_beds = n_plant_beds
with col2:
# Minimum species per plant bed input
min_species = st.number_input(
"Minimum number of species per plant bed",
min_value=1,
max_value=len(st.session_state.input_plants_raw),
value=st.session_state.min_species,
step=1,
)
st.session_state.min_species = min_species
# Maximum species per plant bed input
# It will be enabled only if min_species is set
enable_max_species = st.session_state.min_species > 0
with col3:
max_species = st.number_input(
"Maximum number of species per plant bed",
min_value=st.session_state.min_species,
max_value=len(st.session_state.input_plants_raw),
value=max(
st.session_state.min_species,
st.session_state.max_species,
),
step=1,
disabled=not enable_max_species,
)
if enable_max_species:
st.session_state.max_species = max_species
# extract the compatibility matrix from the user's input
if "extracted_mat" not in st.session_state:
valid = False
if (
"submitted_plant_list" in st.session_state
and st.session_state.submitted_plant_list
):
# check if the user's input is valid
# min species per bed must be less than or equal to max species per bed
if (
(
st.session_state.min_species
<= st.session_state.max_species
)
and (
# max species per bed must be less than or equal to the number of plants
st.session_state.max_species
<= len(st.session_state.input_plants_raw)
)
and (
# max species per bed must be greater than or equal to the min species per bed
st.session_state.max_species
>= st.session_state.min_species
)
and (
# min species per bed must be less than or equal to the number of plants
st.session_state.min_species
<= len(st.session_state.input_plants_raw)
)
and (
# number of plant beds multiplied by min species per bed must be less than or equal to the number of plants
len(st.session_state.input_plants_raw)
>= st.session_state.n_plant_beds
* st.session_state.min_species
)
and (
# number of plant beds multiplied by max species per bed must be greater than or equal to the number of plants
len(st.session_state.input_plants_raw)
<= st.session_state.n_plant_beds
* st.session_state.max_species
)
):
valid = True
else:
# add a warning message
st.warning(
"Please enter valid parameters. The minimum number of species per plant bed must be less than or equal to the maximum number of species per plant bed. The maximum number of species per plant bed must be less than or equal to the number of plants. The maximum number of species per plant bed must be greater than or equal to the minimum number of species per plant bed. The minimum number of species per plant bed must be less than or equal to the number of plants. The number of plant beds multiplied by the minimum number of species per plant bed must be less than or equal to the number of plants. The number of plant beds multiplied by the maximum number of species per plant bed must be greater than or equal to the number of plants."
)
if valid:
# add in some vertical space
add_vertical_space(2)
if st.button(
"Generate Companion Plant Compatibility Matrix"
):
with st.spinner(
"generating companion plant compatibility matrix..."
):
st.session_state["generating_mat"] = True
# now get compatibility matrix for companion planting
time.sleep(1)
(
extracted_mat,
full_mat,
plant_index_mapping,
) = get_compatibility_matrix_2(
st.session_state.input_plants_raw
)
print(extracted_mat)
st.session_state.extracted_mat = extracted_mat
st.session_state.full_mat = full_mat
st.session_state.plant_index_mapping = (
plant_index_mapping
)
# add in some vertical space
add_vertical_space(4)
# display the companion plant compatibility matrix
if "extracted_mat" in st.session_state:
# add a title for the next section- companion plant compatibility matrix based on user input
st.title("Your companion plant compatibility matrix")
# make a container for this section
container2 = st.container(border=True)
with container2:
col1, col2 = st.columns([8, 4])
# display the companion plant compatibility matrix
with col2:
st.write("Here is your companion plant compatibility matrix:")
with st.expander("Show ugly compatibility matrix of 1's 0's and -1's"):
st.write(st.session_state.extracted_mat)
with col1:
st.write(
"Here is a network visualization of your companion plant compatibility matrix. It is color coded to show which plants are companions (green), antagonists (violetred), or neutral (grey)."
)
plot_compatibility_with_agraph(
st.session_state.input_plants_raw, st.session_state.full_mat
)
st.session_state["got_mat"] = True
if "got_mat" in st.session_state:
# add in some vertical space
add_vertical_space(4)
# make a container for this section
container3 = st.container(border=True)
with container3:
st.title(
"Optimizing companion planting with the genetic algorithm and AI"
)
st.write(
"Now that we have your companion plant compatibility matrix, we can use optimization to maximize your harvest. We will use a genetic algorithm to determine the best way to plant your garden. The genetic algorithm will determine the best way to plant your garden by maximizing the number of companion plants and minimizing the number of antagonists."
)
st.write(
"Set the parameters for the genetic algorithm. Here is more info for your reference:"
)
with st.form(key="genetic_algorithm_form"):
col1, col2 = st.columns([1, 1])
with col2:
with st.expander(
"Show more information about the genetic algorithm parameters"
):
st.subheader("Plant Optimization Heuristic Performance")
st.write(
"The genetic algorithm parameters impact the performance of the plant optimization heuristic in the following ways:"
)
st.markdown(
"- **Population Size**: A larger population size allows for a more diverse exploration of the solution space. However, it also increases computational complexity."
)
st.markdown(
"- **Number of Generations**: Increasing the number of generations provides more opportunities for the algorithm to converge towards an optimal solution."
)
st.markdown(
"- **Tournament Size**: A larger tournament size promotes stronger selection pressure and can lead to faster convergence, but it may also increase the risk of premature convergence."
)
st.markdown(
"- **Crossover Rate**: A higher crossover rate increases the exploration capability by creating diverse offspring, potentially improving the algorithm's ability to escape local optima."
)
st.markdown(
"- **Mutation Rate**: Mutation introduces random changes in individuals, helping to maintain diversity in the population and preventing premature convergence."
)
# seed population rate
st.markdown(
"- **Seed Population Rate**: The seed population rate is the percentage of the population that is generated based on the LLM's interpretation of compatibility. The remaining percentage of the population is generated randomly. A higher seed population rate increases the likelihood that the genetic algorithm will converge towards a solution that is compatible."
)
# Run the Genetic Algorithm
with col1:
st.subheader("Genetic Algorithm Parameters")
st.write(
"These parameters control the behavior of the genetic algorithm."
)
# Genetic Algorithm parameters
st.session_state.population_size = st.slider(
"Population Size",
min_value=100,
max_value=1000,
value=500,
help="The number of individuals in each generation of the genetic algorithm.",
)
st.session_state.num_generations = st.slider(
"Number of Generations",
min_value=100,
max_value=1000,
value=450,
help="The total number of generations to evolve through.",
)
st.session_state.tournament_size = st.slider(
"Tournament Size",
min_value=5,
max_value=20,
value=10,
help="The number of individuals competing in each tournament selection round.",
)
st.session_state.crossover_rate = st.slider(
"Crossover Rate",
min_value=0.1,
max_value=1.0,
step=0.1,
value=0.8,
help="The probability of two individuals undergoing crossover to create offspring.",
)
st.session_state.mutation_rate = st.slider(
"Mutation Rate",
min_value=0.01,
max_value=0.9,
step=0.01,
value=0.3,
help="The probability of an individual undergoing mutation.",
)
st.session_state.seed_population_rate = st.slider(
"Seed Population Rate",
min_value=0.0,
max_value=0.02,
step=0.001,
value=0.08,
help="The percentage of the population that is generated based on the LLM's interpretation of compatibility. The remaining percentage of the population is generated randomly.",
)
#
# Run the genetic algorithm
if st.form_submit_button(label="Run Genetic Algorithm"):
with st.spinner(
"running genetic algorithm... this may take a minute"
):
grouping = genetic_algorithm_plants(
st.session_state.model, st.session_state.demo_lite
)
st.session_state.grouping = grouping
# visualize the groupings
# add in some vertical space
add_vertical_space(4)
# make a container for this section
st.title(st.session_state.user_name + "'s optimized garden")
st.header("Here are the optimized groupings of plants for your garden")
container4 = st.container(border=True)
with container4:
if "grouping" in st.session_state:
visualize_groupings()
if "best_fitness" in st.session_state:
# embed score.png
col1b, col2b = st.columns([2, 11])
with col1b:
st.image(
"src/assets/score.png",
caption=None,
width=160,
use_column_width=None,
clamp=False,
channels="RGB",
output_format="auto",
)
with col2b:
# st.write("\n")
st.header("| " + str(st.session_state.best_fitness))
st.write(
"The genetic algorithm converged towards a solution with a fitness score of "
+ str(st.session_state.best_fitness)
+ "."
)
# Add vertical space
add_vertical_space(4)
# show plant care tips
st.header("Plant care tips")
with st.spinner("generating plant care tips..."):
st.write(
"Here are some plant care tips for your plants. Good luck!"
)
if st.session_state.demo_lite:
st.session_state.plant_care_tips = "Plant care tips are not available in this LLM or lite demo version but will be available in the future! Select the Llama 2 LLM for full functionality."
else:
# if 'plant_care_tips' not in st.session_state:
st.session_state.plant_care_tips = get_plant_care_tips(
st.session_state.input_plants_raw,
st.session_state.model,
st.session_state.demo_lite,
)
styled_text = f'<div style="background-color: #2d5a59; color: white; padding: 10px; border-radius: 5px;">{st.session_state.plant_care_tips}</div>'
st.write(styled_text, unsafe_allow_html=True)
if page == "About":
st.sidebar.subheader("About")
st.sidebar.write(
"GRDN is a companion gardening app that helps you plan your garden and maximize your harvest. It uses AI to predict the best plants to grow together and optimization algorithms to optimize how you build your garden."
)
st.sidebar.write(
"Companion gardening is the practice of planting different plants together to maximize their growth. Companion gardening can help to increase the yield of your garden, improve the health of your plants, and reduce the need for pesticides."
)
st.write("This app is currently in beta. Please report any bugs to the team.")
add_vertical_space(1)
st.subheader("Tech Stack Diagram")
st.image(
"src/assets/GRDN_AI_techstack_.png",
caption=None,
use_column_width=None,
clamp=False,
channels="RGB",
output_format="auto",
)
add_vertical_space(4)
col1, col2 = st.columns([1, 1])
with col1:
st.subheader("Contact Information")
st.write("Author: Danielle Heymann")
st.write("Email: dheymann314@gmail.com")
st.write("LinkedIn: https://www.linkedin.com/in/danielle-heymann/")
with col2:
st.subheader("Software, data, and libraries used")
st.write("Libraries and Software")
st.markdown(
"""
- Python
- streamlit
- openai
- plotly
- pandas
- numpy
- PIL
- langchain
- streamlit_chat
- github copilot
- Llama2
- Deci AI
- HuggingFace
- LlamaIndex
- chatGPT
- GPT family of models
- DALL·E 3 (in preprocessing script for image generation)
"""
)
st.write(
"Data sources in addition to what LLMs were trained on: \n https://waldenlabs.com/the-ultimate-companion-planting-guide-chart/ "
)
# st.write("avatars from: https://www.flaticon.com/free-icons/bot")
|