piranaware_version3 / src /storage_local.py
ashandilgith's picture
pushing tested revised files for HF compatibility
ab65fad
#this was the original storage py file that worked well locally. revert to this if needed. the new file renamed storage py was revised for Hugging Face
import os
import json
import streamlit as st
from google.cloud import storage
from google.oauth2 import service_account
# ✅ UPDATED WITH YOUR BUCKET NAME
BUCKET_NAME = "piranaware20251227841ph"
def get_storage_client():
"""
Authenticates with Google Cloud.
CHECKS LOCAL FILE FIRST (to prevent crashes in Codespaces),
then checks Secrets (for Hugging Face).
"""
# 1. Local Dev: Check for local file FIRST
if os.path.exists("gcp_key.json"):
return storage.Client.from_service_account_json("gcp_key.json")
# 2. Production: Check Streamlit Secrets
try:
if "gcp_service_account" in st.secrets:
creds_dict = dict(st.secrets["gcp_service_account"])
creds = service_account.Credentials.from_service_account_info(creds_dict)
return storage.Client(credentials=creds)
except Exception:
# If secrets don't exist, we just move on
pass
# 3. If neither works
st.error("⚠️ No Google Cloud credentials found. Cannot save models.")
return None
def upload_file(local_path, boat_id, filename):
client = get_storage_client()
if not client: return False
bucket = client.bucket(BUCKET_NAME)
# Creates folder structure: boat_id/filename
blob_name = f"{boat_id}/{filename}"
blob = bucket.blob(blob_name)
blob.upload_from_filename(local_path)
return True
def download_file(boat_id, filename, local_dest):
client = get_storage_client()
if not client: return False
bucket = client.bucket(BUCKET_NAME)
blob_name = f"{boat_id}/{filename}"
blob = bucket.blob(blob_name)
if not blob.exists():
return False
blob.download_to_filename(local_dest)
return True