PRIYANSHUDHAKED commited on
Commit
b979b93
·
verified ·
1 Parent(s): 09b2977

Update gemini_vision.py

Browse files
Files changed (1) hide show
  1. gemini_vision.py +37 -6
gemini_vision.py CHANGED
@@ -15,6 +15,26 @@ genai.configure()
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,20 +44,31 @@ def process_frame_with_gemini(frame, mode="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
 
 
 
 
 
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
  image = Image.new('RGB', (1, 1), color='white')
45
  audio_path = frame # In this case, 'frame' is actually the audio file path
46
 
47
+ # Define the prompt
48
  if mode == "image":
49
+ prompt = ["Describe the content of this frame", image]
50
  else:
51
+ prompt = ["Transcribe the audio file at this path", audio_path, image]
52
+
53
+ # Call the API using retry logic
54
+ response = retry_with_backoff(lambda: model.generate_content(prompt))
55
 
56
  return response.text
57
 
58
  def summarize_with_gemini(text, max_words):
59
  text_model = genai.GenerativeModel('gemini-pro')
60
+ prompt = f"Summarize the following text in about {max_words} words: {text}"
61
+
62
+ # Call the API using retry logic
63
+ response = retry_with_backoff(lambda: text_model.generate_content(prompt))
64
+
65
  return response.text
66
 
67
  def extract_code_with_gemini(text):
68
  text_model = genai.GenerativeModel('gemini-pro')
69
+ prompt = f"Extract and format any code snippets from the following text: {text}"
70
+
71
+ # Call the API using retry logic
72
+ response = retry_with_backoff(lambda: text_model.generate_content(prompt))
73
+
74
+ return response.text