import os import openai import streamlit as st from PIL import Image from tensorflow.keras.models import load_model from tensorflow.keras.preprocessing import image import numpy as np # Configuration UPLOAD_FOLDER = 'static/uploads' ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'} TARGET_SIZE = (256, 256) # OpenAI API Key (make sure to keep this key secure and not expose it in public repositories) OPENAI_API_KEY = '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' openai.api_key = OPENAI_API_KEY # Ensure the upload folder exists os.makedirs(UPLOAD_FOLDER, exist_ok=True) # Load trained model MODEL_PATH = 'desnet2.h5' model = load_model(MODEL_PATH) classes = [ 'American Bollworm on Cotton', 'Anthracnose on Cotton', 'Aphids', 'Army worm', 'Carpetweeds', 'Crabgrass', 'Eclipta', 'Flag Smut', 'Goosegrass', 'Healthy', 'Leaf Curl', 'Leaf smut', 'Morningglory', 'Mosaic sugarcane', 'Nutsedge', 'PalmerAmaranth', 'Powdery_Mildew', 'Prickly Sida', 'Purslane', 'Ragweed', 'RedRot sugarcane', 'RedRust sugarcane', 'Rice Blast', 'Sicklepod', 'SpottedSpurge', 'SpurredAnoda', 'Sugarcane Healthy', 'Swinecress', 'Target_spot', 'Tungro', 'Waterhemp', 'Wheat Brown leaf Rust', 'Wheat Stem fly', 'Wheat aphid', 'Wheat black rust', 'Wheat leaf blight', 'Wheat mite', 'Wheat powdery mildew', 'Wheat scab', 'Wheat___Yellow_Rust', 'Wilt', 'Yellow Rust Sugarcane', 'bacterial_blight in Cotton', 'bollrot on Cotton', 'bollworm on Cotton', 'cotton mealy bug', 'cotton whitefly', 'curl_virus', 'fussarium_wilt', 'maize ear rot', 'maize fall armyworm', 'maize stem borer', 'pink bollworm in cotton', 'red cotton bug', 'thirps on cotton' ] def allowed_file(filename): return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS def predict_disease(image_path, model): test_image = image.load_img(image_path, target_size=TARGET_SIZE) test_image = image.img_to_array(test_image) test_image = test_image / 255 test_image = np.expand_dims(test_image, axis=0) result = model.predict(test_image) result = result.ravel() index = np.argmax(result) pred = str(classes[index]) return pred def get_disease_info(disease_name): response = openai.Completion.create( engine="text-davinci-003", prompt=f"Explain the cause and solution for the following cotton plant disease: {disease_name}", max_tokens=150 ) return response.choices[0].text.strip() # Streamlit app st.title("Cotton Disease Detection") st.write("Upload an image to detect the disease.") uploaded_file = st.file_uploader("Choose an image...", type=["png", "jpg", "jpeg"]) if uploaded_file is not None: if allowed_file(uploaded_file.name): # Save the uploaded file to the upload folder image_path = os.path.join(UPLOAD_FOLDER, uploaded_file.name) with open(image_path, "wb") as f: f.write(uploaded_file.getbuffer()) # Resize the image img = Image.open(image_path) img = img.resize(TARGET_SIZE) img.save(image_path) # Predict the disease prediction = predict_disease(image_path, model) # Display the uploaded image st.image(image_path, caption='Uploaded Image.', use_column_width=True) st.write(f"Prediction: {prediction}") # Get disease information from OpenAI GPT disease_info = get_disease_info(prediction) st.write("Disease Information:") st.write(disease_info) else: st.write("Please upload an image file (png, jpg, jpeg).") else: st.write("No file uploaded.")