Aggiorna app.py

This commit is contained in:
Nicolò Tonello 2025-08-02 10:06:41 +00:00
parent 46053885ba
commit 6a392827c1

214
app.py
View file

@ -1,106 +1,108 @@
# app.py # app.py
import streamlit as st import streamlit as st
from PIL import Image from PIL import Image
from llama_cpp import Llama from llama_cpp import Llama
from image_generation import generate_image from image_generation import generate_image
st.set_page_config(page_title="GreenThumber", layout="centered") st.set_page_config(page_title="GreenThumber", layout="centered")
st.title("🌱 GreenThumber") st.title("🌱 GreenThumber")
@st.cache_resource(hash_funcs={Llama: lambda _: None}) @st.cache_resource(hash_funcs={Llama: lambda _: None})
def load_mistral_model(): def load_mistral_model():
llm = Llama( llm = Llama(
model_path="./models/mistral-7b-instruct-v0.1.Q4_K_M.gguf", model_path="./models/mistral-7b-instruct-v0.1.Q4_K_M.gguf",
n_ctx=2048, n_ctx=2048,
n_threads=4, n_threads=4,
n_batch=512, n_batch=512,
verbose=False verbose=False
) )
return llm return llm
llm = load_mistral_model() llm = load_mistral_model()
# Generate a description using the Mistral model # Generate a description using the Mistral model
def generate_growth_description(plant_type, plant_age, 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): fertilizer_type, temperature, humidity, days, additional_info):
prompt = ( prompt = (
f" Instruction:\n" f" Instruction:\n"
f"You are a botanical expert. Describe how a {plant_type} plant will likely look in {days} days " 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"based on these conditions:\n"
f"Important environment conditions: {additional_info}\n" f"Important environment conditions: {additional_info}\n"
f"- Plant age: {plant_age}\n" f"- Plant age: {plant_age}\n"
f"- Soil Type: {soil_type}\n" f"- Soil Type: {soil_type}\n"
f"- Sunlight: {sunlight_hours} hours per day\n" f"- Sunlight: {sunlight_hours} hours per day\n"
f"- Water Frequency: {water_frequency} times per week\n" f"- Water Frequency: {water_frequency} times per week\n"
f"- Fertilizer Type: {fertilizer_type}\n" f"- Fertilizer Type: {fertilizer_type}\n"
f"- Temperature: {temperature}°C\n" f"- Temperature: {temperature}°C\n"
f"- Humidity: {humidity}%\n" f"- Humidity: {humidity}%\n"
f"### Response:\n" f"### Response:\n"
) )
output = llm(prompt, max_tokens=100, stop=["###"]) output = llm(prompt, max_tokens=100, stop=["###"])
return output["choices"][0]["text"].strip() return output["choices"][0]["text"].strip()
st.header("Plant Info") st.header("Plant Info")
plant_input_mode = st.radio("How would you like to provide plant info?", ("Type name", "Upload image")) plant_input_mode = st.radio("How would you like to provide plant info?", ("Type name", "Upload image"))
plant_type = None plant_type = None
uploaded_image = None uploaded_image = None
if plant_input_mode == "Type name": if plant_input_mode == "Type name":
plant_type = st.selectbox("Select Plant Type", ["Basil", "Tomato", "Lettuce"]) 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) plant_age = st.number_input("Enter Plant Age (in days)", min_value=1, max_value=365, value=25)
elif plant_input_mode == "Upload image": elif plant_input_mode == "Upload image":
plant_type = st.selectbox("Select Plant Type", ["Basil", "Tomato", "Lettuce"]) 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) 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"]) image_file = st.file_uploader("Upload an image of your plant", type=["jpg", "jpeg", "png"])
if image_file: if image_file:
uploaded_image = Image.open(image_file) uploaded_image = Image.open(image_file)
st.image(uploaded_image, caption="Uploaded Plant Image", use_container_width =True) st.image(uploaded_image, caption="Uploaded Plant Image", use_container_width =True)
col1, col2 = st.columns(2) col1, col2 = st.columns(2)
with col1: with col1:
st.header("Environmental Parameters") st.markdown("<h3 style='text-align: center;'>Environmental Parameters</h3>", unsafe_allow_html=True)
soil_options = ["Sandy", "Clay", "Loamy", "Peaty", "Chalky", "Silty"] soil_options = ["Sandy", "Clay", "Loamy", "Peaty", "Chalky", "Silty"]
soil_type = st.selectbox("Soil Type", soil_options) soil_type = st.selectbox("Soil Type", soil_options)
sunlight_hours = st.slider("Sunlight Hours per day", 0, 24, 6) sunlight_hours = st.slider("Sunlight Hours per day", 0, 24, 6)
water_frequency = st.slider("Water Frequency (times per week)", 0, 14, 3) water_frequency = st.slider("Water Frequency (times per week)", 0, 14, 3)
with col2: with col2:
fertilizer_options = ["Organic", "Chemical", "None"] st.markdown("---")
fertilizer_options = ["Organic", "Chemical", "None"]
fertilizer_type = st.selectbox("Fertilizer Type", fertilizer_options)
temperature = st.slider("Temperature (°C)", -10, 50, 22) fertilizer_type = st.selectbox("Fertilizer Type", fertilizer_options)
humidity = st.slider("Humidity (%)", 0, 100, 60) temperature = st.slider("Temperature (°C)", -10, 50, 22)
days = st.slider("Prediction Interval (in days)", min_value=1, max_value=30, value=7) humidity = st.slider("Humidity (%)", 0, 100, 60)
additional_info = st.text_area("Feel free to include any additional detail", "- e.g. 'I'm adding compost to the soil.'", height=100) 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..."): #if st.button("Start Prediction"):
description = generate_growth_description( if plant_type and plant_type.strip() != "":
plant_type, plant_age, soil_type, sunlight_hours, water_frequency, with st.spinner("Analyzing data and generating description..."):
fertilizer_type, temperature, humidity, days, additional_info description = generate_growth_description(
) plant_type, plant_age, soil_type, sunlight_hours, water_frequency,
st.subheader(f"📝 Predicted Plant Condition in {days} Days:") fertilizer_type, temperature, humidity, days, additional_info
st.write(description) )
st.subheader(f"📝 Predicted Plant Condition in {days} Days:")
manipulated_img = generate_image(plant_type, description, plant_age) st.write(description)
st.image(manipulated_img, caption="Predicted Plant Condition Image")
manipulated_img = generate_image(plant_type, description, plant_age)
# else: st.image(manipulated_img, caption="Predicted Plant Condition Image")
# st.warning("Please select or enter a plant type.")
# else:
# st.warning("Please select or enter a plant type.")
st.markdown("---")
st.caption("Made with ❤️ by Sandwich Craftz.")
st.markdown("---")
st.caption("Made with ❤️ by Sandwich Craftz.")