Spaces:
Runtime error
Runtime error
Musharraf11
commited on
Commit
•
14cb32e
1
Parent(s):
7dc8be5
Update app.py
Browse files
app.py
CHANGED
@@ -1,96 +1,100 @@
|
|
1 |
-
### Invoice extractor (modified for pet image analysis)
|
2 |
-
|
3 |
-
## Importing All the modules
|
4 |
-
import streamlit as st
|
5 |
-
import os
|
6 |
-
from PIL import Image
|
7 |
-
import google.generativeai as genai
|
8 |
-
from dotenv import load_dotenv
|
9 |
-
|
10 |
-
|
11 |
-
# Load all environment Variables
|
12 |
-
load_dotenv()
|
13 |
-
|
14 |
-
## Configuring the api key
|
15 |
-
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
16 |
-
|
17 |
-
## Function to load Gemini Vison Pro Vision Model and Get response
|
18 |
-
|
19 |
-
def get_gemini_response(image, prompt):
|
20 |
-
|
21 |
-
## Loading the desired Model
|
22 |
-
model= genai.GenerativeModel("gemini-pro-vision")
|
23 |
-
response=model.generate_content([image[0],prompt])
|
24 |
-
return response.text
|
25 |
-
|
26 |
-
## Function to extract data from Image Uploaded
|
27 |
-
|
28 |
-
def input_image_setup(uploaded_file):
|
29 |
-
# Check if a file has been uploaded
|
30 |
-
if uploaded_file is not None:
|
31 |
-
# Read the file into bytes
|
32 |
-
bytes_data = uploaded_file.getvalue()
|
33 |
-
|
34 |
-
image_parts = [
|
35 |
-
{
|
36 |
-
"mime_type": uploaded_file.type, # Get the mime type of the uploaded file
|
37 |
-
"data": bytes_data
|
38 |
-
}
|
39 |
-
]
|
40 |
-
return image_parts
|
41 |
-
else:
|
42 |
-
raise FileNotFoundError("No file uploaded")
|
43 |
-
|
44 |
-
# Initializing our Streamlit Prompt
|
45 |
-
|
46 |
-
st.set_page_config(page_title="Pet Image Analyzer")
|
47 |
-
|
48 |
-
st.header("Analyze Your Pet's Image")
|
49 |
-
|
50 |
-
## Removed user input for text prompt
|
51 |
-
|
52 |
-
uploaded_file = st.file_uploader("Choose a pet image...", type=["jpg", "jpeg", "png","webp"])
|
53 |
-
image = None
|
54 |
-
|
55 |
-
if uploaded_file is not None:
|
56 |
-
image = Image.open(uploaded_file)
|
57 |
-
st.image(image, caption="Uploaded Image.", use_column_width=True)
|
58 |
-
|
59 |
-
## Defining a System Prompt (pre-defined)
|
60 |
-
input_prompt = f"""Image: (content of the uploaded image)
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
*
|
68 |
-
*
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
st.
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
|
|
|
|
|
|
|
|
|
1 |
+
### Invoice extractor (modified for pet image analysis)
|
2 |
+
|
3 |
+
## Importing All the modules
|
4 |
+
import streamlit as st
|
5 |
+
import os
|
6 |
+
from PIL import Image
|
7 |
+
import google.generativeai as genai
|
8 |
+
from dotenv import load_dotenv
|
9 |
+
|
10 |
+
|
11 |
+
# Load all environment Variables
|
12 |
+
load_dotenv()
|
13 |
+
|
14 |
+
## Configuring the api key
|
15 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
16 |
+
|
17 |
+
## Function to load Gemini Vison Pro Vision Model and Get response
|
18 |
+
|
19 |
+
def get_gemini_response(image, prompt):
|
20 |
+
|
21 |
+
## Loading the desired Model
|
22 |
+
model= genai.GenerativeModel("gemini-pro-vision")
|
23 |
+
response=model.generate_content([image[0],prompt])
|
24 |
+
return response.text
|
25 |
+
|
26 |
+
## Function to extract data from Image Uploaded
|
27 |
+
|
28 |
+
def input_image_setup(uploaded_file):
|
29 |
+
# Check if a file has been uploaded
|
30 |
+
if uploaded_file is not None:
|
31 |
+
# Read the file into bytes
|
32 |
+
bytes_data = uploaded_file.getvalue()
|
33 |
+
|
34 |
+
image_parts = [
|
35 |
+
{
|
36 |
+
"mime_type": uploaded_file.type, # Get the mime type of the uploaded file
|
37 |
+
"data": bytes_data
|
38 |
+
}
|
39 |
+
]
|
40 |
+
return image_parts
|
41 |
+
else:
|
42 |
+
raise FileNotFoundError("No file uploaded")
|
43 |
+
|
44 |
+
# Initializing our Streamlit Prompt
|
45 |
+
|
46 |
+
st.set_page_config(page_title="Pet Image Analyzer")
|
47 |
+
|
48 |
+
st.header("Analyze Your Pet's Image")
|
49 |
+
|
50 |
+
## Removed user input for text prompt
|
51 |
+
|
52 |
+
uploaded_file = st.file_uploader("Choose a pet image...", type=["jpg", "jpeg", "png","webp"])
|
53 |
+
image = None
|
54 |
+
|
55 |
+
if uploaded_file is not None:
|
56 |
+
image = Image.open(uploaded_file)
|
57 |
+
st.image(image, caption="Uploaded Image.", use_column_width=True)
|
58 |
+
|
59 |
+
## Defining a System Prompt (pre-defined)
|
60 |
+
input_prompt = f"""Image: (content of the uploaded image)
|
61 |
+
|
62 |
+
|
63 |
+
Text: Analyze the image and provide the following information:
|
64 |
+
|
65 |
+
If the uploaded image is not related to Pets then give them a Applogise message but dont give any improper respones.
|
66 |
+
|
67 |
+
* Breed: Identify the breed of the animal in the image (if possible).
|
68 |
+
* Disease Detection: If the image shows a diseased area, identify the specific disease (if possible).
|
69 |
+
* Severity: If a disease is detected, assess the severity of the disease.
|
70 |
+
* Symptoms: Describe the common symptoms associated with the detected disease.
|
71 |
+
* Precautions: Recommend preventative measures to avoid the disease.
|
72 |
+
|
73 |
+
Give response with headings,
|
74 |
+
Give the response in proper format with the headings.
|
75 |
+
Inform the user if the image is not related to pet care.
|
76 |
+
"""
|
77 |
+
|
78 |
+
submit = st.button("Analyze Image")
|
79 |
+
Disclaimer ="""**Disclaimer:**
|
80 |
+
|
81 |
+
This application uses image analysis to provide potential information about your pet's health. The results are for informational purposes only and should not be considered a replacement for professional veterinary diagnosis.
|
82 |
+
|
83 |
+
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."""
|
84 |
+
|
85 |
+
|
86 |
+
if submit:
|
87 |
+
if image:
|
88 |
+
image_data = input_image_setup(uploaded_file)
|
89 |
+
response = get_gemini_response(image_data, input_prompt)
|
90 |
+
|
91 |
+
st.subheader("Analysis Result:")
|
92 |
+
st.write(response)
|
93 |
+
st.warning(Disclaimer)
|
94 |
+
else:
|
95 |
+
st.error("Please upload an image to proceed.")
|
96 |
+
|
97 |
+
else:
|
98 |
+
st.info("Upload an image of your pet to get started!")
|
99 |
+
|
100 |
+
# st.balloons()
|