Spaces:
Sleeping
Sleeping
Update app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576import osfrom io import BytesIOimport gradio as grimport numpy as npimport cv2from PIL import Imagefrom dotenv import load_dotenvimport google.generativeai as genaifrom model import classify_image # Assuming this is a custom model for classification# Load environment variables for Google Gemini API keyload_dotenv()genai.configure(api_key=os.getenv("Google_Api_Key"))# Input prompt for Gemini modelinput_prompt = """"You are an expert in computer vision and agriculture who can easily predict the disease of the plant. ""Analyze the following image and provide 7 short outputs in a structured format: ""1. Crop: , ""2. Infected or Healthy: , ""3. Type of disease (if any): , ""4. Confidence out of 100% whether image is healthy or infected: , ""5. Reason for the disease such as whether it is happening due to fungus, bacteria, insect bite, poor nutrition, etc.: , ""6. Plant Growth Stage: , ""7. Pest Life Stage: .""""# Function to get a response from the Google Gemini APIdef get_gemini_response(image):model = genai.GenerativeModel('gemini-1.5-pro')# Convert PIL image to bytesbytes_io = BytesIO()image.save(bytes_io, format='PNG')bytes_data = bytes_io.getvalue()# Send image and prompt to Gemini APIresponse = model.generate_content([input_prompt, image])# Extract the relevant response (mock response logic for now)return response.text# Image classification using custom model (e.g., for detecting specific plant diseases)def classify_crop_image(img):img = cv2.resize(img, (224, 224))img = img / 255.0img = np.expand_dims(img, axis=0)# Use the custom classification model to predict the diseaselabel, accuracy = classify_image(img) # Assuming 'classify_image' returns label and accuracyreturn {label: accuracy}# Function to handle the uploaded image, predict crop health, and provide a structured outputdef predict_crop_health(uploaded_image):# Pass the image to the custom classifier for plant disease predictionclassification_result = classify_crop_image(np.array(uploaded_image))# Pass the image to the Gemini API for detailed disease analysisgemini_response = get_gemini_response(uploaded_image)# Combine results from both models (custom and Gemini)return f"Classification Result: {classification_result}\n\nGemini Response: {gemini_response}"# Define the Gradio interface: Inputs and Outputsinputs = gr.Image(type="pil", label="Upload Crop Image")outputs = gr.Textbox(label="Crop Disease Predictor", lines=10)# Launch the Gradio interfacegr.Interface(fn=predict_crop_health,inputs=inputs,outputs=outputs,title="Crop Disease Prediction App",description="Upload an image of a crop to predict its disease and get treatment suggestions.",live=False).launch()1234567891011121314151617181920import gradio as grimport numpy as npimport cv2from model import classify_imagedef main(img):img = cv2.resize(img, (224,224))img = img/255.0img = np.expand_dims(img, axis=0)label, accuracy = classify_image(img)print(label)out = {label: accuracy}return outdemo = gr.Interface(fn=main,inputs=gr.Image(),outputs=gr.Label(num_top_classes=1), allow_flagging='never')if __name__ == "__main__":demo.launch(share=True)"
app.py+13-69