Update gemini_vision.py
Browse files- gemini_vision.py +15 -22
gemini_vision.py
CHANGED
@@ -15,26 +15,6 @@ genai.configure()
|
|
15 |
# Initialize the Gemini Vision Pro model
|
16 |
model = genai.GenerativeModel('gemini-1.5-flash')
|
17 |
|
18 |
-
|
19 |
-
import time
|
20 |
-
import google.api_core.exceptions # Import this to handle specific exceptions
|
21 |
-
|
22 |
-
|
23 |
-
def retry_with_backoff(func, retries=3, initial_wait=2):
|
24 |
-
for i in range(retries):
|
25 |
-
try:
|
26 |
-
return func() # Call the function that uses the API
|
27 |
-
except google.api_core.exceptions.ResourceExhausted as e:
|
28 |
-
if i < retries - 1:
|
29 |
-
wait_time = initial_wait * (2 ** i) # Exponential backoff
|
30 |
-
print(f"API quota exhausted. Retrying in {wait_time} seconds...")
|
31 |
-
time.sleep(wait_time)
|
32 |
-
else:
|
33 |
-
raise e # Raise error after last retry
|
34 |
-
except Exception as e:
|
35 |
-
print(f"An error occurred: {e}")
|
36 |
-
break # Break on non-recoverable errors
|
37 |
-
|
38 |
def process_frame_with_gemini(frame, mode="image"):
|
39 |
if mode == "image":
|
40 |
# Convert OpenCV frame to PIL Image
|
@@ -44,8 +24,21 @@ def process_frame_with_gemini(frame, mode="image"):
|
|
44 |
image = Image.new('RGB', (1, 1), color='white')
|
45 |
audio_path = frame # In this case, 'frame' is actually the audio file path
|
46 |
|
47 |
-
#
|
48 |
if mode == "image":
|
49 |
-
|
50 |
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
|
|
15 |
# Initialize the Gemini Vision Pro model
|
16 |
model = genai.GenerativeModel('gemini-1.5-flash')
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
def process_frame_with_gemini(frame, mode="image"):
|
19 |
if mode == "image":
|
20 |
# Convert OpenCV frame to PIL Image
|
|
|
24 |
image = Image.new('RGB', (1, 1), color='white')
|
25 |
audio_path = frame # In this case, 'frame' is actually the audio file path
|
26 |
|
27 |
+
# Process the image or audio with Gemini Vision Pro
|
28 |
if mode == "image":
|
29 |
+
response = model.generate_content(["Describe the content of this frame", image])
|
30 |
else:
|
31 |
+
response = model.generate_content(["Transcribe the audio file at this path", audio_path, image])
|
32 |
+
|
33 |
+
return response.text
|
34 |
+
|
35 |
+
def summarize_with_gemini(text, max_words):
|
36 |
+
text_model = genai.GenerativeModel('gemini-pro')
|
37 |
+
response = text_model.generate_content(f"Summarize the following text in about {max_words} words: {text}")
|
38 |
+
return response.text
|
39 |
+
|
40 |
+
def extract_code_with_gemini(text):
|
41 |
+
text_model = genai.GenerativeModel('gemini-pro')
|
42 |
+
response = text_model.generate_content(f"Extract and format any code snippets from the following text: {text}")
|
43 |
+
return response.text
|
44 |
|