Spaces:
Sleeping
Sleeping
Update gemini_agent.py
Browse files- gemini_agent.py +17 -1
gemini_agent.py
CHANGED
|
@@ -3,6 +3,8 @@ import google.generativeai as genai
|
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
from excel_parser import ExcelParser
|
| 5 |
import re
|
|
|
|
|
|
|
| 6 |
|
| 7 |
load_dotenv()
|
| 8 |
|
|
@@ -15,6 +17,8 @@ class GeminiAgent:
|
|
| 15 |
genai.configure(api_key=api_key)
|
| 16 |
|
| 17 |
self.model = genai.GenerativeModel('gemini-2.0-flash-exp')
|
|
|
|
|
|
|
| 18 |
|
| 19 |
# Initialize parsers
|
| 20 |
self.excel_parser = ExcelParser()
|
|
@@ -61,6 +65,7 @@ class GeminiAgent:
|
|
| 61 |
Provide only the direct answer. If it's a quote, give just the quoted text. If it's a number, give just the number. If it's about bird species count, analyze carefully and give the exact count. If it's about dialogue, provide the exact words spoken."""
|
| 62 |
|
| 63 |
try:
|
|
|
|
| 64 |
response = self.model.generate_content(
|
| 65 |
video_prompt,
|
| 66 |
generation_config=genai.types.GenerationConfig(
|
|
@@ -128,6 +133,7 @@ Provide only the direct answer. If it's a quote, give just the quoted text. If i
|
|
| 128 |
If you don't have specific information about this Excel file, provide a reasonable estimate based on similar data."""
|
| 129 |
|
| 130 |
try:
|
|
|
|
| 131 |
response = self.model.generate_content(
|
| 132 |
excel_prompt,
|
| 133 |
generation_config=genai.types.GenerationConfig(
|
|
@@ -213,6 +219,7 @@ Answer:"""
|
|
| 213 |
|
| 214 |
# Use the constructed prompt for all cases
|
| 215 |
|
|
|
|
| 216 |
response = self.model.generate_content(
|
| 217 |
prompt,
|
| 218 |
generation_config=genai.types.GenerationConfig(
|
|
@@ -252,6 +259,7 @@ Answer:"""
|
|
| 252 |
Provide only the direct answer without explanation."""
|
| 253 |
|
| 254 |
try:
|
|
|
|
| 255 |
response = self.model.generate_content(
|
| 256 |
prompt,
|
| 257 |
generation_config=genai.types.GenerationConfig(
|
|
@@ -270,4 +278,12 @@ Answer:"""
|
|
| 270 |
|
| 271 |
except Exception as e:
|
| 272 |
print(f"Failed to generate video answer: {str(e)}")
|
| 273 |
-
return "Video analysis unavailable."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
from excel_parser import ExcelParser
|
| 5 |
import re
|
| 6 |
+
import time
|
| 7 |
+
import asyncio
|
| 8 |
|
| 9 |
load_dotenv()
|
| 10 |
|
|
|
|
| 17 |
genai.configure(api_key=api_key)
|
| 18 |
|
| 19 |
self.model = genai.GenerativeModel('gemini-2.0-flash-exp')
|
| 20 |
+
self.last_request_time = 0
|
| 21 |
+
self.min_request_interval = 1.0 # 1 second between requests
|
| 22 |
|
| 23 |
# Initialize parsers
|
| 24 |
self.excel_parser = ExcelParser()
|
|
|
|
| 65 |
Provide only the direct answer. If it's a quote, give just the quoted text. If it's a number, give just the number. If it's about bird species count, analyze carefully and give the exact count. If it's about dialogue, provide the exact words spoken."""
|
| 66 |
|
| 67 |
try:
|
| 68 |
+
await self._rate_limit()
|
| 69 |
response = self.model.generate_content(
|
| 70 |
video_prompt,
|
| 71 |
generation_config=genai.types.GenerationConfig(
|
|
|
|
| 133 |
If you don't have specific information about this Excel file, provide a reasonable estimate based on similar data."""
|
| 134 |
|
| 135 |
try:
|
| 136 |
+
await self._rate_limit()
|
| 137 |
response = self.model.generate_content(
|
| 138 |
excel_prompt,
|
| 139 |
generation_config=genai.types.GenerationConfig(
|
|
|
|
| 219 |
|
| 220 |
# Use the constructed prompt for all cases
|
| 221 |
|
| 222 |
+
await self._rate_limit()
|
| 223 |
response = self.model.generate_content(
|
| 224 |
prompt,
|
| 225 |
generation_config=genai.types.GenerationConfig(
|
|
|
|
| 259 |
Provide only the direct answer without explanation."""
|
| 260 |
|
| 261 |
try:
|
| 262 |
+
await self._rate_limit()
|
| 263 |
response = self.model.generate_content(
|
| 264 |
prompt,
|
| 265 |
generation_config=genai.types.GenerationConfig(
|
|
|
|
| 278 |
|
| 279 |
except Exception as e:
|
| 280 |
print(f"Failed to generate video answer: {str(e)}")
|
| 281 |
+
return "Video analysis unavailable."
|
| 282 |
+
|
| 283 |
+
async def _rate_limit(self):
|
| 284 |
+
"""Ensure minimum time between API requests"""
|
| 285 |
+
current_time = time.time()
|
| 286 |
+
time_since_last = current_time - self.last_request_time
|
| 287 |
+
if time_since_last < self.min_request_interval:
|
| 288 |
+
await asyncio.sleep(self.min_request_interval - time_since_last)
|
| 289 |
+
self.last_request_time = time.time()
|