Prajwal3009 commited on
Commit
5ed0494
·
verified ·
1 Parent(s): c876ba5

Update vision.py

Browse files
Files changed (1) hide show
  1. vision.py +89 -61
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
- # print(f"Uploaded file '{file.display_name}' as: {file.uri}")
18
- return file
19
-
20
- def wait_for_files_active(files):
21
- """Waits for the given files to be active."""
22
- # print("Waiting for file processing...")
23
- for name in (file.name for file in files):
24
- file = genai.get_file(name)
25
- while file.state.name == "PROCESSING":
26
- print(".", end="", flush=True)
27
- time.sleep(10)
28
- file = genai.get_file(name)
29
- if file.state.name != "ACTIVE":
30
- raise Exception(f"File {file.name} failed to process")
31
- # print("...all files ready")
32
- # print()
33
-
34
- def get_gemini_response(input, image):
35
- context = """Generates a response based on the image and input prompt."""
36
- model = genai.GenerativeModel('gemini-pro-vision')
37
- if input != "":
38
- input += context
39
- response = model.generate_content([input, image])
40
- else:
41
- response = model.generate_content(image)
42
- return response.text
43
-
44
- def visoto():
45
- """Main function to run the Streamlit app."""
46
- st.title = "Gemini Image Demo"
47
- st.header ="Image Chat Assistant"
48
- input = st.text_input("Input Prompt: ", key="input")
49
- uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
50
- image = ""
51
- if uploaded_file is not None:
52
- image = Image.open(uploaded_file)
53
- st.image(image, caption="Uploaded Image.", use_column_width=True)
54
- submit = st.button("Tell me about the image")
55
- if submit:
56
- response = get_gemini_response(input, image)
57
- st.subheader("The Response is")
58
- st.write(response)
59
-
60
- # if __name__ == "__main__":
61
- # main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()