Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,37 +1,67 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
#
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from dotenv import load_dotenv
|
2 |
+
|
3 |
+
load_dotenv() ## load all the environment variables
|
4 |
+
|
5 |
+
import streamlit as st
|
6 |
+
import os
|
7 |
+
import google.generativeai as genai
|
8 |
+
from PIL import Image
|
9 |
+
|
10 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
11 |
+
|
12 |
+
## Function to load Google Gemini Pro Vision API And get response
|
13 |
+
|
14 |
+
def get_gemini_repsonse(input,image,prompt):
|
15 |
+
model=genai.GenerativeModel('gemini-1.5-flash')
|
16 |
+
response=model.generate_content([input,image[0],prompt])
|
17 |
+
return response.text
|
18 |
+
|
19 |
+
def input_image_setup(uploaded_file):
|
20 |
+
# Check if a file has been uploaded
|
21 |
+
if uploaded_file is not None:
|
22 |
+
# Read the file into bytes
|
23 |
+
bytes_data = uploaded_file.getvalue()
|
24 |
+
|
25 |
+
image_parts = [
|
26 |
+
{
|
27 |
+
"mime_type": uploaded_file.type, # Get the mime type of the uploaded file
|
28 |
+
"data": bytes_data
|
29 |
+
}
|
30 |
+
]
|
31 |
+
return image_parts
|
32 |
+
else:
|
33 |
+
raise FileNotFoundError("No file uploaded")
|
34 |
+
|
35 |
+
##initialize our streamlit app
|
36 |
+
|
37 |
+
st.set_page_config(page_title="Crop Disease Detection App")
|
38 |
+
|
39 |
+
st.header("Gemini Crop Disease App")
|
40 |
+
input=st.text_input("Input Prompt: ",key="input")
|
41 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
42 |
+
image=""
|
43 |
+
if uploaded_file is not None:
|
44 |
+
image = Image.open(uploaded_file)
|
45 |
+
st.image(image, caption="Uploaded Image.", use_column_width=True)
|
46 |
+
|
47 |
+
|
48 |
+
submit=st.button("Predict Crop/Plant Health")
|
49 |
+
|
50 |
+
input_prompt="""
|
51 |
+
"You are an expert in computer vision and agriculture who can easily predict the disease of the plant. "
|
52 |
+
"Analyze the following image and provide 6 outputs in a structured table format: "
|
53 |
+
"1. Crop in the image, "
|
54 |
+
"2. Whether it is infected or healthy, "
|
55 |
+
"3. Type of disease (if any), "
|
56 |
+
"4. How confident out of 100% whether image is healthy or infected "
|
57 |
+
"5. Reason for the disease such as whether it is happening due to fungus, bacteria, insect bite, poor nutrition, etc., "
|
58 |
+
"6. Precautions for it."
|
59 |
+
"""
|
60 |
+
|
61 |
+
## If submit button is clicked
|
62 |
+
|
63 |
+
if submit:
|
64 |
+
image_data=input_image_setup(uploaded_file)
|
65 |
+
response=get_gemini_repsonse(input_prompt,image_data,input)
|
66 |
+
st.subheader("The Response is")
|
67 |
+
st.write(response)
|