lodhrangpt commited on
Commit
60a1c01
·
verified ·
1 Parent(s): e39e5cb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -19
app.py CHANGED
@@ -1,20 +1,60 @@
 
 
1
  import gradio as gr
2
- import numpy as np
3
- import cv2
4
- from model import classify_image
5
-
6
- def main(img):
7
- img = cv2.resize(img, (224,224))
8
- img = img/255.0
9
- img = np.expand_dims(img, axis=0)
10
- label, accuracy = classify_image(img)
11
- print(label)
12
- out = {label: accuracy}
13
- return out
14
-
15
- demo = gr.Interface(fn=main,
16
- inputs=gr.Image(),
17
- outputs=gr.Label(num_top_classes=1), allow_flagging='never')
18
-
19
- if __name__ == "__main__":
20
- demo.launch(share=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from io import BytesIO
3
  import gradio as gr
4
+ import google.generativeai as genai
5
+ from PIL import Image
6
+ from dotenv import load_dotenv
7
+
8
+ # Load environment variables
9
+ load_dotenv()
10
+ genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
11
+ input_prompt="""
12
+ "You are an expert in computer vision and agriculture who can easily predict the disease of the plant. "
13
+ "Analyze the following image and provide 7 short outputs in a structured format: "
14
+ "1. Crop : , "
15
+ "2. Infected or Healthy : , "
16
+ "3. Type of disease (if any), "
17
+ "4. How confident out of 100% whether image is healthy or infected "
18
+ "5. Reason for the disease such as whether it is happening due to fungus, bacteria, insect bite, poor nutrition, etc., "
19
+ "6. Plant Growth Stage. "
20
+ "7. Pest Life Stage."
21
+ """
22
+
23
+
24
+ # Function to get a response from the Google Gemini Vision API
25
+ def get_gemini_response(image):
26
+ model = genai.GenerativeModel('gemini-1.5-pro')
27
+
28
+ # Convert PIL image to bytes
29
+ bytes_io = BytesIO()
30
+ image.save(bytes_io, format='PNG')
31
+ bytes_data = bytes_io.getvalue()
32
+ response=model.generate_content([input_prompt,image])
33
+ # Placeholder logic for now: replace with actual API usage
34
+ # You would pass the image bytes data to the API
35
+ # Currently this is a mock response
36
+ crop_name = "Wheat" # Example: Replace this with the actual API result
37
+ disease_type = "Wheat Septoria" # Example: Replace this with the actual API result
38
+
39
+ # Return disease info
40
+ #return get_disease_info(crop_name, disease_type)
41
+ return response.text
42
+
43
+ # Function to handle the uploaded image and predict crop health
44
+ def predict_crop_health(uploaded_image):
45
+ # Pass the image to the Gemini API to get prediction
46
+ return get_gemini_response(uploaded_image)
47
+
48
+ # Define the Gradio interface: Inputs and Outputs
49
+ inputs = gr.Image(type="pil", label="Upload Crop Image")
50
+ outputs = gr.Markdown(label="Prediction Results")
51
+
52
+ # Launch the Gradio interface
53
+ gr.Interface(
54
+ fn=predict_crop_health,
55
+ inputs=inputs,
56
+ outputs=gr.Textbox(label="Crop Disease Predictor"),
57
+ title="Crop Disease Prediction App",
58
+ description="Upload an image of a crop to predict its disease and get treatment suggestions.",
59
+ live=False
60
+ ).launch()