Spaces:
Sleeping
Sleeping
Update model.py
Browse files
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 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
if
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
"
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
st.
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
image =
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
*
|
| 63 |
-
*
|
| 64 |
-
*
|
| 65 |
-
*
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
"
|
| 75 |
-
"
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
st.
|
| 85 |
-
st.
|
| 86 |
-
st.
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
st.
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 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()
|