Musharraf11 commited on
Commit
52c3014
·
verified ·
1 Parent(s): 3d2e539

Update model.py

Browse files
Files changed (1) hide show
  1. model.py +101 -96
model.py CHANGED
@@ -1,96 +1,101 @@
1
- def model():
2
- # Importing All the modules
3
- import streamlit as st
4
- import os
5
- from PIL import Image
6
- import google.generativeai as genai
7
- from dotenv import load_dotenv
8
-
9
- # Load all environment Variables
10
- load_dotenv()
11
-
12
- # Configuring the api key
13
- genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
14
-
15
- # Function to load Gemini Vison Pro Vision Model and Get response
16
- def get_gemini_response(image, prompt):
17
- # Loading the desired Model
18
- model = genai.GenerativeModel("gemini-pro-vision")
19
- response = model.generate_content([image[0], prompt])
20
- return response.text
21
-
22
- # Function to extract data from Image Uploaded
23
- def input_image_setup(uploaded_file):
24
- # Check if a file has been uploaded
25
- if uploaded_file is not None:
26
- # Read the file into bytes
27
- bytes_data = uploaded_file.getvalue()
28
- image_parts = [
29
- {
30
- "mime_type": uploaded_file.type, # Get the mime type of the uploaded file
31
- "data": bytes_data
32
- }
33
- ]
34
- return image_parts
35
- else:
36
- raise FileNotFoundError("No file uploaded")
37
-
38
- # Initializing our Streamlit Prompt
39
- st.title("Pet Image Analyzer")
40
- st.write(
41
- """
42
- Welcome to the Pet Image Analyzer! This tool uses advanced AI technology to analyze images of your pets
43
- and provide insights into their breed, potential health issues, and more. Please upload an image of your pet
44
- to get started.
45
- """
46
- )
47
-
48
- # File uploader for image input
49
- uploaded_file = st.file_uploader("Choose a pet image...", type=["jpg", "jpeg", "png", "webp"])
50
- image = None
51
-
52
- if uploaded_file is not None:
53
- image = Image.open(uploaded_file)
54
- st.image(image, caption="Uploaded Image.", use_column_width=True)
55
-
56
- # Defining a System Prompt (pre-defined)
57
- input_prompt = f"""Image: (content of the uploaded image)
58
-
59
- Text: Analyze the image and provide the following information:
60
-
61
- * Breed: Identify the breed of the animal in the image (if possible).
62
- * Disease Detection: If the image shows a diseased area, identify the specific disease (if possible).
63
- * Severity: If a disease is detected, assess the severity of the disease.
64
- * Symptoms: Describe the common symptoms associated with the detected disease.
65
- * Precautions: Recommend preventative measures to avoid the disease.
66
-
67
- Give response with headings,
68
- Inform the user if the image is not related to pet care.
69
- """
70
-
71
- submit = st.button("Analyze Image")
72
- Disclaimer = (
73
- "**Disclaimer:** This application uses image analysis to provide potential information about your pet's health. "
74
- "The results are for informational purposes only and should not be considered a replacement for professional veterinary diagnosis. "
75
- "For any concerns about your pet's health, please consult a licensed veterinarian. They can conduct a thorough examination and provide personalized recommendations for your pet's well-being."
76
- )
77
-
78
- if submit:
79
- if image:
80
- with st.spinner("Analyzing Image..."):
81
- image_data = input_image_setup(uploaded_file)
82
- response = get_gemini_response(image_data, input_prompt)
83
- st.subheader("Analysis Result:")
84
- st.write(response)
85
- st.warning(Disclaimer)
86
- st.balloons()
87
- else:
88
- st.error("Please upload an image to proceed.")
89
- else:
90
- st.info("Upload an image of your pet to get started!")
91
- st.write(
92
- """
93
- To analyze your pet's image, click on the 'Choose a pet image...' button above and select an image file from your device.
94
- Once the image is uploaded, click on 'Analyze Image' to receive detailed information about your pet.
95
- """
96
- )
 
 
 
 
 
 
1
+ def model():
2
+ # Importing All the modules
3
+ import streamlit as st
4
+ import os
5
+ from PIL import Image
6
+ import google.generativeai as genai
7
+ from dotenv import load_dotenv
8
+ import faiss # Assuming faiss library is used for the index
9
+
10
+ # Load all environment Variables
11
+ load_dotenv()
12
+
13
+ # Configuring the api key
14
+ genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
15
+
16
+ # Function to load Gemini Vison Pro Vision Model and Get response
17
+ def get_gemini_response(image, prompt):
18
+ # Loading the desired Model
19
+ model = genai.GenerativeModel("gemini-pro-vision")
20
+ response = model.generate_content([image[0], prompt])
21
+ return response.text
22
+
23
+ # Function to extract data from Image Uploaded
24
+ def input_image_setup(uploaded_file):
25
+ # Check if a file has been uploaded
26
+ if uploaded_file is not None:
27
+ # Read the file into bytes
28
+ bytes_data = uploaded_file.getvalue()
29
+ image_parts = [
30
+ {
31
+ "mime_type": uploaded_file.type, # Get the mime type of the uploaded file
32
+ "data": bytes_data
33
+ }
34
+ ]
35
+ return image_parts
36
+ else:
37
+ raise FileNotFoundError("No file uploaded")
38
+
39
+ # Initializing our Streamlit Prompt
40
+ st.title("Pet Image Analyzer")
41
+ st.write(
42
+ """
43
+ Welcome to the Pet Image Analyzer! This tool uses advanced AI technology to analyze images of your pets
44
+ and provide insights into their breed, potential health issues, and more. Please upload an image of your pet
45
+ to get started.
46
+ """
47
+ )
48
+
49
+ # File uploader for image input
50
+ uploaded_file = st.file_uploader("Choose a pet image...", type=["jpg", "jpeg", "png", "webp"])
51
+ image = None
52
+
53
+ if uploaded_file is not None:
54
+ image = Image.open(uploaded_file)
55
+ st.image(image, caption="Uploaded Image.", use_column_width=True)
56
+
57
+ # Defining a System Prompt (pre-defined)
58
+ input_prompt = f"""Image: (content of the uploaded image)
59
+
60
+ Text: Analyze the image and provide the following information:
61
+
62
+ * Breed: Identify the breed of the animal in the image (if possible).
63
+ * Disease Detection: If the image shows a diseased area, identify the specific disease (if possible).
64
+ * Severity: If a disease is detected, assess the severity of the disease.
65
+ * Symptoms: Describe the common symptoms associated with the detected disease.
66
+ * Precautions: Recommend preventative measures to avoid the disease.
67
+
68
+ Give response with headings,
69
+ Inform the user if the image is not related to pet care.
70
+ """
71
+
72
+ submit = st.button("Analyze Image")
73
+ Disclaimer = (
74
+ "**Disclaimer:** This application uses image analysis to provide potential information about your pet's health. "
75
+ "The results are for informational purposes only and should not be considered a replacement for professional veterinary diagnosis. "
76
+ "For any concerns about your pet's health, please consult a licensed veterinarian. They can conduct a thorough examination and provide personalized recommendations for your pet's well-being."
77
+ )
78
+
79
+ if submit:
80
+ if image:
81
+ with st.spinner("Analyzing Image..."):
82
+ image_data = input_image_setup(uploaded_file)
83
+ response = get_gemini_response(image_data, input_prompt)
84
+ st.subheader("Analysis Result:")
85
+ st.write(response)
86
+ st.warning(Disclaimer)
87
+ st.balloons()
88
+ else:
89
+ st.error("Please upload an image to proceed.")
90
+ else:
91
+ st.info("Upload an image of your pet to get started!")
92
+ st.write(
93
+ """
94
+ To analyze your pet's image, click on the 'Choose a pet image...' button above and select an image file from your device.
95
+ Once the image is uploaded, click on 'Analyze Image' to receive detailed information about your pet.
96
+ """
97
+ )
98
+
99
+ # Example usage if running as a script
100
+ if __name__ == "__main__":
101
+ model()