Spaces:
Sleeping
Sleeping
Update vision.py
Browse files
vision.py
CHANGED
|
@@ -1,61 +1,89 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import time
|
| 3 |
-
from PIL import Image
|
| 4 |
-
import streamlit as st
|
| 5 |
-
import google.generativeai as genai
|
| 6 |
-
|
| 7 |
-
# Load environment variables
|
| 8 |
-
from dotenv import load_dotenv
|
| 9 |
-
load_dotenv()
|
| 10 |
-
|
| 11 |
-
# Configure the Google AI Python SDK
|
| 12 |
-
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
| 13 |
-
|
| 14 |
-
def upload_to_gemini(path, mime_type=None):
|
| 15 |
-
"""Uploads the given file to Gemini."""
|
| 16 |
-
file = genai.upload_file(path, mime_type=mime_type)
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
if
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import time
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import streamlit as st
|
| 5 |
+
import google.generativeai as genai
|
| 6 |
+
|
| 7 |
+
# Load environment variables
|
| 8 |
+
from dotenv import load_dotenv
|
| 9 |
+
load_dotenv()
|
| 10 |
+
|
| 11 |
+
# Configure the Google AI Python SDK
|
| 12 |
+
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
| 13 |
+
|
| 14 |
+
def upload_to_gemini(path, mime_type=None):
|
| 15 |
+
"""Uploads the given file to Gemini."""
|
| 16 |
+
file = genai.upload_file(path, mime_type=mime_type)
|
| 17 |
+
return file
|
| 18 |
+
|
| 19 |
+
def wait_for_files_active(files):
|
| 20 |
+
"""Waits for the given files to be active."""
|
| 21 |
+
for name in (file.name for file in files):
|
| 22 |
+
file = genai.get_file(name)
|
| 23 |
+
while file.state.name == "PROCESSING":
|
| 24 |
+
print(".", end="", flush=True)
|
| 25 |
+
time.sleep(10)
|
| 26 |
+
file = genai.get_file(name)
|
| 27 |
+
if file.state.name != "ACTIVE":
|
| 28 |
+
raise Exception(f"File {file.name} failed to process")
|
| 29 |
+
|
| 30 |
+
def get_gemini_response(input, images):
|
| 31 |
+
context = """Generates a response based on the images and input prompt."""
|
| 32 |
+
model = genai.GenerativeModel('gemini-pro-vision')
|
| 33 |
+
responses = []
|
| 34 |
+
for image in images:
|
| 35 |
+
if input != "":
|
| 36 |
+
input += context
|
| 37 |
+
response = model.generate_content([input, image])
|
| 38 |
+
else:
|
| 39 |
+
response = model.generate_content(image)
|
| 40 |
+
|
| 41 |
+
# Use result.parts to access the response parts
|
| 42 |
+
for part in response.parts:
|
| 43 |
+
if part.text:
|
| 44 |
+
responses.append(part.text)
|
| 45 |
+
return responses
|
| 46 |
+
|
| 47 |
+
def visoto():
|
| 48 |
+
"""Main function to run the Streamlit app."""
|
| 49 |
+
st.title= "Gemini Image Demo"
|
| 50 |
+
st.header= "Image Chat Assistant"
|
| 51 |
+
|
| 52 |
+
input = st.text_input("Input Prompt: ", key="input")
|
| 53 |
+
|
| 54 |
+
# State variable to control camera input visibility
|
| 55 |
+
if 'camera_open' not in st.session_state:
|
| 56 |
+
st.session_state.camera_open = False
|
| 57 |
+
|
| 58 |
+
if st.button("Open Camera"):
|
| 59 |
+
st.session_state.camera_open = True
|
| 60 |
+
|
| 61 |
+
if st.button("Close Camera"):
|
| 62 |
+
st.session_state.camera_open = False
|
| 63 |
+
|
| 64 |
+
camera_image = None
|
| 65 |
+
if st.session_state.camera_open:
|
| 66 |
+
camera_image = st.camera_input("Capture an image")
|
| 67 |
+
|
| 68 |
+
uploaded_files = st.file_uploader("Choose images...", type=["jpg", "jpeg", "png"], accept_multiple_files=True)
|
| 69 |
+
images = []
|
| 70 |
+
|
| 71 |
+
if camera_image is not None:
|
| 72 |
+
images.append(Image.open(camera_image))
|
| 73 |
+
st.image(images[-1], caption="Captured Image.", use_column_width=True)
|
| 74 |
+
|
| 75 |
+
if uploaded_files is not None:
|
| 76 |
+
for uploaded_file in uploaded_files:
|
| 77 |
+
image = Image.open(uploaded_file)
|
| 78 |
+
images.append(image)
|
| 79 |
+
st.image(image, caption="Uploaded Image.", use_column_width=True)
|
| 80 |
+
|
| 81 |
+
submit = st.button("Tell me about the images")
|
| 82 |
+
if submit and images:
|
| 83 |
+
responses = get_gemini_response(input, images)
|
| 84 |
+
st.subheader("The Responses are")
|
| 85 |
+
for response in responses:
|
| 86 |
+
st.write(response)
|
| 87 |
+
|
| 88 |
+
if __name__ == "__main__":
|
| 89 |
+
visoto()
|