mominah commited on
Commit
dad5e8d
Β·
verified Β·
1 Parent(s): 147ad6b

Update video_rag_routes.py

Browse files
Files changed (1) hide show
  1. video_rag_routes.py +9 -8
video_rag_routes.py CHANGED
@@ -116,15 +116,15 @@ def process_transcription(text: str) -> str:
116
  # β€”β€”β€” Retry Decorator for Rate-Limits β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
117
 
118
  def retry_on_rate_limit(fn):
119
- async def wrapper(*args, **kwargs):
120
  last_exc = None
121
  for _ in range(len(_api_keys)):
122
- client = init_google_client()
123
  try:
124
- return await fn(client, *args, **kwargs)
125
  except ResourceExhausted as e:
126
  last_exc = e
127
  logger.warning("Gemini key rate-limited, rotating…")
 
128
  continue
129
  logger.error("All Gemini API keys are rate-limited")
130
  raise HTTPException(
@@ -142,7 +142,8 @@ class URLIn(BaseModel):
142
 
143
  @validator("youtube_url")
144
  def validate_youtube_url(cls, v):
145
- if "youtube.com" not in v.host and "youtu.be" not in v.host:
 
146
  raise ValueError("Only YouTube URLs are allowed")
147
  return v
148
 
@@ -154,7 +155,7 @@ async def transcribe_url(client: genai.Client, body: URLIn):
154
  Accepts only valid YouTube URLs.
155
  """
156
  try:
157
- resp = client.models.generate_content(
158
  model="models/gemini-2.0-flash",
159
  contents=types.Content(parts=[
160
  types.Part(text="Transcribe the video"),
@@ -170,7 +171,7 @@ async def transcribe_url(client: genai.Client, body: URLIn):
170
 
171
  @router.post("/upload_video")
172
  @retry_on_rate_limit
173
- async def upload_file(
174
  client: genai.Client,
175
  file: UploadFile = File(..., description="Upload a video file"),
176
  prompt: str = "Transcribe the video",
@@ -181,7 +182,7 @@ async def upload_file(
181
  """
182
  data = await file.read()
183
  try:
184
- resp = client.models.generate_content(
185
  model="models/gemini-2.0-flash",
186
  contents=types.Content(parts=[
187
  types.Part(text=prompt),
@@ -200,7 +201,7 @@ class QueryIn(BaseModel):
200
  query: str
201
 
202
  @router.post("/vid_query")
203
- async def query_rag(body: QueryIn):
204
  """
205
  Query the RAG chain for a previous transcription session.
206
  """
 
116
  # β€”β€”β€” Retry Decorator for Rate-Limits β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
117
 
118
  def retry_on_rate_limit(fn):
119
+ async def wrapper(client: genai.Client, *unused_args, **unused_kwargs):
120
  last_exc = None
121
  for _ in range(len(_api_keys)):
 
122
  try:
123
+ return await fn(client, *unused_args, **unused_kwargs)
124
  except ResourceExhausted as e:
125
  last_exc = e
126
  logger.warning("Gemini key rate-limited, rotating…")
127
+ client = init_google_client()
128
  continue
129
  logger.error("All Gemini API keys are rate-limited")
130
  raise HTTPException(
 
142
 
143
  @validator("youtube_url")
144
  def validate_youtube_url(cls, v):
145
+ host = v.host.lower()
146
+ if "youtube.com" not in host and "youtu.be" not in host:
147
  raise ValueError("Only YouTube URLs are allowed")
148
  return v
149
 
 
155
  Accepts only valid YouTube URLs.
156
  """
157
  try:
158
+ resp = await client.models.generate_content(
159
  model="models/gemini-2.0-flash",
160
  contents=types.Content(parts=[
161
  types.Part(text="Transcribe the video"),
 
171
 
172
  @router.post("/upload_video")
173
  @retry_on_rate_limit
174
+ async def upload_video_file(
175
  client: genai.Client,
176
  file: UploadFile = File(..., description="Upload a video file"),
177
  prompt: str = "Transcribe the video",
 
182
  """
183
  data = await file.read()
184
  try:
185
+ resp = await client.models.generate_content(
186
  model="models/gemini-2.0-flash",
187
  contents=types.Content(parts=[
188
  types.Part(text=prompt),
 
201
  query: str
202
 
203
  @router.post("/vid_query")
204
+ async def vid_query(body: QueryIn):
205
  """
206
  Query the RAG chain for a previous transcription session.
207
  """