From 46053885ba36512a0b80bd8bbee9874f7ce4b8fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2?= Date: Sat, 2 Aug 2025 11:41:53 +0200 Subject: [PATCH 1/2] init --- app.py | 59 +++++++++++++++++++++------------------------------------- 1 file changed, 21 insertions(+), 38 deletions(-) diff --git a/app.py b/app.py index fd133803d..cc1f393cd 100644 --- a/app.py +++ b/app.py @@ -1,29 +1,14 @@ -import os +# app.py import streamlit as st -import pandas as pd -import joblib from PIL import Image -import torch -from torchvision import models from llama_cpp import Llama -from diffusers import DiffusionPipeline, StableDiffusionPipeline, DPMSolverMultistepScheduler from image_generation import generate_image st.set_page_config(page_title="GreenThumber", layout="centered") st.title("🌱 GreenThumber") -@st.cache_resource -def load_imagenet_labels(): - import urllib - url = "https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt" - response = urllib.request.urlopen(url) - labels = [line.strip() for line in response.read().decode("utf-8").split("\n")] - return labels -labels = load_imagenet_labels() - -# Load Mistral LLM via llama-cpp-python with custom hash to avoid Streamlit caching issues @st.cache_resource(hash_funcs={Llama: lambda _: None}) def load_mistral_model(): llm = Llama( @@ -39,13 +24,14 @@ llm = load_mistral_model() # Generate a description using the Mistral model -def generate_growth_description(plant_type, soil_type, sunlight_hours, water_frequency, +def generate_growth_description(plant_type, plant_age, soil_type, sunlight_hours, water_frequency, fertilizer_type, temperature, humidity, days, additional_info): prompt = ( f" Instruction:\n" f"You are a botanical expert. Describe how a {plant_type} plant will likely look in {days} days " f"based on these conditions:\n" - f"Important additional conditions: {additional_info}\n" + f"Important environment conditions: {additional_info}\n" + f"- Plant age: {plant_age}\n" f"- Soil Type: {soil_type}\n" f"- Sunlight: {sunlight_hours} hours per day\n" f"- Water Frequency: {water_frequency} times per week\n" @@ -66,10 +52,11 @@ uploaded_image = None if plant_input_mode == "Type name": plant_type = st.selectbox("Select Plant Type", ["Basil", "Tomato", "Lettuce"]) plant_age = st.number_input("Enter Plant Age (in days)", min_value=1, max_value=365, value=25) + elif plant_input_mode == "Upload image": plant_type = st.selectbox("Select Plant Type", ["Basil", "Tomato", "Lettuce"]) - plant_age = st.number_input("Enter Plant Age (in days)", min_value=1, max_value=365, value=30) + plant_age = st.number_input("Enter Plant Age (in days)", min_value=1, max_value=365, value=25) image_file = st.file_uploader("Upload an image of your plant", type=["jpg", "jpeg", "png"]) if image_file: uploaded_image = Image.open(image_file) @@ -86,7 +73,6 @@ with col1: sunlight_hours = st.slider("Sunlight Hours per day", 0, 24, 6) water_frequency = st.slider("Water Frequency (times per week)", 0, 14, 3) -# --- Column 2: Environmental Parameters with col2: fertilizer_options = ["Organic", "Chemical", "None"] @@ -95,27 +81,24 @@ with col2: humidity = st.slider("Humidity (%)", 0, 100, 60) days = st.slider("Prediction Interval (in days)", min_value=1, max_value=30, value=7) -additional_info = st.text_area("Feel free to include any additional detail") +additional_info = st.text_area("Feel free to include any additional detail", "- e.g. 'I'm adding compost to the soil.'", height=100) -# Prediction + Description + Image Generation -if st.button("Start Prediction"): - if plant_type and plant_type.strip() != "": - if plant_input_mode == "Upload image" and uploaded_image is None: - st.warning("Please upload a plant image to proceed.") - else: - with st.spinner("Analyzing data and generating description..."): - description = generate_growth_description( - plant_type, soil_type, sunlight_hours, water_frequency, - fertilizer_type, temperature, humidity, days, additional_info - ) - st.subheader(f"📝 Predicted Plant Condition in {days} Days:") - st.write(description) - manipulated_img = generate_image(plant_type, description, plant_age) - st.image(manipulated_img, caption="Predicted Plant Condition Image") +#if st.button("Start Prediction"): +if plant_type and plant_type.strip() != "": + with st.spinner("Analyzing data and generating description..."): + description = generate_growth_description( + plant_type, plant_age, soil_type, sunlight_hours, water_frequency, + fertilizer_type, temperature, humidity, days, additional_info + ) + st.subheader(f"📝 Predicted Plant Condition in {days} Days:") + st.write(description) - else: - st.warning("Please select or enter a plant type.") + manipulated_img = generate_image(plant_type, description, plant_age) + st.image(manipulated_img, caption="Predicted Plant Condition Image") + +# else: +# st.warning("Please select or enter a plant type.") From 6a392827c1e39e97a55af152a9400c1f5151959b Mon Sep 17 00:00:00 2001 From: nicolo-tonello Date: Sat, 2 Aug 2025 10:06:41 +0000 Subject: [PATCH 2/2] Aggiorna app.py --- app.py | 214 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 108 insertions(+), 106 deletions(-) diff --git a/app.py b/app.py index cc1f393cd..66adbd230 100644 --- a/app.py +++ b/app.py @@ -1,106 +1,108 @@ -# app.py -import streamlit as st -from PIL import Image -from llama_cpp import Llama -from image_generation import generate_image - -st.set_page_config(page_title="GreenThumber", layout="centered") -st.title("🌱 GreenThumber") - - - -@st.cache_resource(hash_funcs={Llama: lambda _: None}) -def load_mistral_model(): - llm = Llama( - model_path="./models/mistral-7b-instruct-v0.1.Q4_K_M.gguf", - n_ctx=2048, - n_threads=4, - n_batch=512, - verbose=False - ) - return llm - -llm = load_mistral_model() - - -# Generate a description using the Mistral model -def generate_growth_description(plant_type, plant_age, soil_type, sunlight_hours, water_frequency, - fertilizer_type, temperature, humidity, days, additional_info): - prompt = ( - f" Instruction:\n" - f"You are a botanical expert. Describe how a {plant_type} plant will likely look in {days} days " - f"based on these conditions:\n" - f"Important environment conditions: {additional_info}\n" - f"- Plant age: {plant_age}\n" - f"- Soil Type: {soil_type}\n" - f"- Sunlight: {sunlight_hours} hours per day\n" - f"- Water Frequency: {water_frequency} times per week\n" - f"- Fertilizer Type: {fertilizer_type}\n" - f"- Temperature: {temperature}°C\n" - f"- Humidity: {humidity}%\n" - f"### Response:\n" - ) - output = llm(prompt, max_tokens=100, stop=["###"]) - return output["choices"][0]["text"].strip() - - -st.header("Plant Info") -plant_input_mode = st.radio("How would you like to provide plant info?", ("Type name", "Upload image")) -plant_type = None -uploaded_image = None - -if plant_input_mode == "Type name": - plant_type = st.selectbox("Select Plant Type", ["Basil", "Tomato", "Lettuce"]) - plant_age = st.number_input("Enter Plant Age (in days)", min_value=1, max_value=365, value=25) - - -elif plant_input_mode == "Upload image": - plant_type = st.selectbox("Select Plant Type", ["Basil", "Tomato", "Lettuce"]) - plant_age = st.number_input("Enter Plant Age (in days)", min_value=1, max_value=365, value=25) - image_file = st.file_uploader("Upload an image of your plant", type=["jpg", "jpeg", "png"]) - if image_file: - uploaded_image = Image.open(image_file) - st.image(uploaded_image, caption="Uploaded Plant Image", use_container_width =True) - - -col1, col2 = st.columns(2) - -with col1: - st.header("Environmental Parameters") - soil_options = ["Sandy", "Clay", "Loamy", "Peaty", "Chalky", "Silty"] - - soil_type = st.selectbox("Soil Type", soil_options) - sunlight_hours = st.slider("Sunlight Hours per day", 0, 24, 6) - water_frequency = st.slider("Water Frequency (times per week)", 0, 14, 3) - -with col2: - fertilizer_options = ["Organic", "Chemical", "None"] - - fertilizer_type = st.selectbox("Fertilizer Type", fertilizer_options) - temperature = st.slider("Temperature (°C)", -10, 50, 22) - humidity = st.slider("Humidity (%)", 0, 100, 60) - days = st.slider("Prediction Interval (in days)", min_value=1, max_value=30, value=7) - -additional_info = st.text_area("Feel free to include any additional detail", "- e.g. 'I'm adding compost to the soil.'", height=100) - - -#if st.button("Start Prediction"): -if plant_type and plant_type.strip() != "": - with st.spinner("Analyzing data and generating description..."): - description = generate_growth_description( - plant_type, plant_age, soil_type, sunlight_hours, water_frequency, - fertilizer_type, temperature, humidity, days, additional_info - ) - st.subheader(f"📝 Predicted Plant Condition in {days} Days:") - st.write(description) - - manipulated_img = generate_image(plant_type, description, plant_age) - st.image(manipulated_img, caption="Predicted Plant Condition Image") - -# else: -# st.warning("Please select or enter a plant type.") - - - -st.markdown("---") -st.caption("Made with ❤️ by Sandwich Craftz.") +# app.py +import streamlit as st +from PIL import Image +from llama_cpp import Llama +from image_generation import generate_image + +st.set_page_config(page_title="GreenThumber", layout="centered") +st.title("🌱 GreenThumber") + + + +@st.cache_resource(hash_funcs={Llama: lambda _: None}) +def load_mistral_model(): + llm = Llama( + model_path="./models/mistral-7b-instruct-v0.1.Q4_K_M.gguf", + n_ctx=2048, + n_threads=4, + n_batch=512, + verbose=False + ) + return llm + +llm = load_mistral_model() + + +# Generate a description using the Mistral model +def generate_growth_description(plant_type, plant_age, soil_type, sunlight_hours, water_frequency, + fertilizer_type, temperature, humidity, days, additional_info): + prompt = ( + f" Instruction:\n" + f"You are a botanical expert. Describe how a {plant_type} plant will likely look in {days} days " + f"based on these conditions:\n" + f"Important environment conditions: {additional_info}\n" + f"- Plant age: {plant_age}\n" + f"- Soil Type: {soil_type}\n" + f"- Sunlight: {sunlight_hours} hours per day\n" + f"- Water Frequency: {water_frequency} times per week\n" + f"- Fertilizer Type: {fertilizer_type}\n" + f"- Temperature: {temperature}°C\n" + f"- Humidity: {humidity}%\n" + f"### Response:\n" + ) + output = llm(prompt, max_tokens=100, stop=["###"]) + return output["choices"][0]["text"].strip() + + +st.header("Plant Info") +plant_input_mode = st.radio("How would you like to provide plant info?", ("Type name", "Upload image")) +plant_type = None +uploaded_image = None + +if plant_input_mode == "Type name": + plant_type = st.selectbox("Select Plant Type", ["Basil", "Tomato", "Lettuce"]) + plant_age = st.number_input("Enter Plant Age (in days)", min_value=1, max_value=365, value=25) + + +elif plant_input_mode == "Upload image": + plant_type = st.selectbox("Select Plant Type", ["Basil", "Tomato", "Lettuce"]) + plant_age = st.number_input("Enter Plant Age (in days)", min_value=1, max_value=365, value=25) + image_file = st.file_uploader("Upload an image of your plant", type=["jpg", "jpeg", "png"]) + if image_file: + uploaded_image = Image.open(image_file) + st.image(uploaded_image, caption="Uploaded Plant Image", use_container_width =True) + + +col1, col2 = st.columns(2) + +with col1: + st.markdown("

Environmental Parameters

", unsafe_allow_html=True) + soil_options = ["Sandy", "Clay", "Loamy", "Peaty", "Chalky", "Silty"] + + soil_type = st.selectbox("Soil Type", soil_options) + sunlight_hours = st.slider("Sunlight Hours per day", 0, 24, 6) + water_frequency = st.slider("Water Frequency (times per week)", 0, 14, 3) + +with col2: + st.markdown("---") + fertilizer_options = ["Organic", "Chemical", "None"] + + fertilizer_type = st.selectbox("Fertilizer Type", fertilizer_options) + temperature = st.slider("Temperature (°C)", -10, 50, 22) + humidity = st.slider("Humidity (%)", 0, 100, 60) + +days = st.slider("Prediction Interval (in days)", min_value=1, max_value=30, value=7) + +additional_info = st.text_area("Feel free to include any additional detail", "- e.g. 'I'm adding compost to the soil.'", height=100) + + +#if st.button("Start Prediction"): +if plant_type and plant_type.strip() != "": + with st.spinner("Analyzing data and generating description..."): + description = generate_growth_description( + plant_type, plant_age, soil_type, sunlight_hours, water_frequency, + fertilizer_type, temperature, humidity, days, additional_info + ) + st.subheader(f"📝 Predicted Plant Condition in {days} Days:") + st.write(description) + + manipulated_img = generate_image(plant_type, description, plant_age) + st.image(manipulated_img, caption="Predicted Plant Condition Image") + +# else: +# st.warning("Please select or enter a plant type.") + + + +st.markdown("---") +st.caption("Made with ❤️ by Sandwich Craftz.")