Hi, I’m having problems processing audio files. It doesn’t always, but randomly returns error 500. I’m attaching log lines and part of the app code.
What could be happening?
LOG:
2024-08-13 18:06:54,869 - uvicorn.access - INFO - xxx.xxx.xxx.xxx:35136 - “POST /ai_api_audio_file/ HTTP/1.1” 200
2024-08-13 18:09:48,882 - google.auth.compute_engine._metadata - WARNING - Compute Engine Metadata server unavailable on attempt 1 of 3. Reason: timed out
2024-08-13 18:09:52,864 - google.auth.compute_engine._metadata - WARNING - Compute Engine Metadata server unavailable on attempt 2 of 3. Reason: timed out
2024-08-13 18:09:57,942 - google.auth.compute_engine._metadata - WARNING - Compute Engine Metadata server unavailable on attempt 3 of 3. Reason: timed out
2024-08-13 18:09:57,942 - google.auth._default - WARNING - Authentication failed using Compute Engine authentication due to unavailable metadata server.
2024-08-13 18:09:57,942 - gemini_api - ERROR - Error processing file:
No API_KEY or ADC found. Please either:
- Set the GOOGLE_API_KEY
environment variable.
- Manually pass the key with genai.configure(api_key=my_api_key)
.
- Or set up Application Default Credentials, see Authentication with OAuth quickstart | Gemini API | Google AI for Developers for more information.
2024-08-13 18:09:57,943 - gemini_api - ERROR - Error processing file: 500: Internal Server Error
2024-08-13 18:09:57,943 - uvicorn.access - INFO - xxx.xxx.xxx.xxx:33504 - “POST /ai_api_audio_file/ HTTP/1.1” 500
CODE:
@app.post(“/ai_api_audio_file/”)
async def ai_api_audio_file(file: UploadFile = File(…), seconds: int = Form(…), username: str = Depends(get_current_username)):
file_extension = file.filename.split(".")[-1]
file_path = f'temp_file_{uuid.uuid4()}.{file_extension}'
try:
async with aiofiles.open(file_path, 'wb') as f:
content = await file.read()
await f.write(content)
prompt= "Make a transcription of the attached audio."
result = await process_file(file_path, prompt)
except Exception as e:
logger.error(f"Error processing file: {e}")
raise HTTPException(status_code=500, detail="Internal Server Error")
finally:
# Eliminar el archivo temporal
if os.path.exists(file_path):
os.remove(file_path)
return result
async def process_file(file_path: str, prompt: str):
try:
uploaded_file = genai.upload_file(path=file_path)
is_valid = await validate_limits()
if not is_valid:
return JSONResponse(
status_code=200,
content={"text": "Limite RPD"}
)
genai.configure(api_key=GOOGLE_API_KEY)
model = genai.GenerativeModel('models/gemini-1.5-flash')
response = model.generate_content(
[prompt, uploaded_file],
generation_config=generation_config,
safety_settings=safety_settings,
request_options={"timeout": 120},
stream=False
)
total_token_count = response.usage_metadata.total_token_count
api_call_registry.add_call(total_token_count)
return handle_response(response)
except Exception as e:
logger.error(f"Error processing file: {e}")
raise HTTPException(status_code=500, detail="Internal Server Error")