Spaces:
Sleeping
Sleeping
Commit ·
a227197
1
Parent(s): d5bf1bd
Add support for GCP_CREDENTIALS_JSON env variable for Hugging Face Spaces
Browse files41 unmodified lines424344454647484950515253545556575859606162636465666768697071721046 unmodified lines111911201121109710981099112211231124112511261127112841 unmodified linesexcept Exception: _speech_available = False
# GCP Credentials - Support both file and environment variable (for Hugging Face Spaces)_gcp_credentials = Noneif _speech_available: # Try environment variable first (Hugging Face Spaces secret) gcp_json_str = os.environ.get('GCP_CREDENTIALS_JSON') if gcp_json_str: try: import json as json_module gcp_info = json_module.loads(gcp_json_str) _gcp_credentials = service_account.Credentials.from_service_account_info(gcp_info) logging.info("GCP credentials loaded from GCP_CREDENTIALS_JSON environment variable") except Exception as e: logging.warning(f"Failed to parse GCP_CREDENTIALS_JSON: {e}") # Fallback to file if env var not available elif GCP_SERVICE_ACCOUNT_FILE and os.path.exists(GCP_SERVICE_ACCOUNT_FILE): try: _gcp_credentials = service_account.Credentials.from_service_account_file(GCP_SERVICE_ACCOUNT_FILE) logging.info(f"GCP credentials loaded from file: {GCP_SERVICE_ACCOUNT_FILE}") except Exception as e: logging.warning(f"Failed to load GCP credentials from file: {e}") if _gcp_credentials is None: _speech_available = False logging.warning("Speech-to-Text: No GCP credentials available")
# Configure logginglogging.basicConfig(level=logging.INFO)logger = logging.getLogger(__name__)1046 unmodified lines
logger.info(f"Received audio file: {len(content)} bytes")
# Create Speech client credentials = service_account.Credentials.from_service_account_file(GCP_SERVICE_ACCOUNT_FILE) speech_client = speech.SpeechClient(credentials=credentials) # Create Speech client using pre-loaded credentials if _gcp_credentials is None: return jsonify({'error': 'GCP credentials not configured'}), 500 speech_client = speech.SpeechClient(credentials=_gcp_credentials)
# Configure audio for LINEAR16 format (raw PCM) audio = speech.RecognitionAudio(content=content)app.py+29-3