Spaces:
Running
Running
Final Secret Bypass Fix
Browse files- src/streamlit_app.py +37 -30
src/streamlit_app.py
CHANGED
|
@@ -57,42 +57,49 @@ st.markdown("""
|
|
| 57 |
""", unsafe_allow_html=True)
|
| 58 |
|
| 59 |
# --- CLOUD DATA LOGGING (GOOGLE SHEETS) ---
|
| 60 |
-
#
|
| 61 |
-
|
| 62 |
-
sheet_url = st.secrets.get("GSHEETS_URL")
|
| 63 |
-
|
| 64 |
-
if gsheets_json and sheet_url:
|
| 65 |
try:
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
except Exception as e:
|
| 69 |
-
st.error(f"
|
| 70 |
st.stop()
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
st.info("If you just added them, please wait 30 seconds and refresh.")
|
| 75 |
-
st.stop()
|
| 76 |
|
| 77 |
def save_to_cloud(text, ai_label, ai_score, corrected_label=None):
|
| 78 |
try:
|
| 79 |
-
#
|
| 80 |
-
existing_data = conn.read(spreadsheet=
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
|
| 97 |
# --- MODEL ENGINE ---
|
| 98 |
MODEL_PATH = "SumedhGajbhiye/Sentiment-Analyzer"
|
|
|
|
| 57 |
""", unsafe_allow_html=True)
|
| 58 |
|
| 59 |
# --- CLOUD DATA LOGGING (GOOGLE SHEETS) ---
|
| 60 |
+
# This manual approach bypasses the buggy Streamlit "auto-discovery"
|
| 61 |
+
def get_connection():
|
|
|
|
|
|
|
|
|
|
| 62 |
try:
|
| 63 |
+
# 1. Pull the raw strings from the Hugging Face vault
|
| 64 |
+
json_secrets = st.secrets.get("GSHEETS_JSON")
|
| 65 |
+
sheet_url = st.secrets.get("GSHEETS_URL")
|
| 66 |
+
|
| 67 |
+
if not json_secrets or not sheet_url:
|
| 68 |
+
st.warning("🚀 System Initializing... Waiting for Cloud Vault Access.")
|
| 69 |
+
st.info("Check your HF Settings: Ensure secrets are named GSHEETS_JSON and GSHEETS_URL.")
|
| 70 |
+
st.stop()
|
| 71 |
+
|
| 72 |
+
# 2. Convert string to a real Python dictionary
|
| 73 |
+
creds_dict = json.loads(json_secrets)
|
| 74 |
+
|
| 75 |
+
# 3. Create the connection manually
|
| 76 |
+
return st.connection("gsheets", type=GSheetsConnection, credentials=creds_dict), sheet_url
|
| 77 |
except Exception as e:
|
| 78 |
+
st.error(f"Vault Connection Error: {e}")
|
| 79 |
st.stop()
|
| 80 |
+
|
| 81 |
+
# Initialize connection and get URL
|
| 82 |
+
conn, GSHEETS_URL = get_connection()
|
|
|
|
|
|
|
| 83 |
|
| 84 |
def save_to_cloud(text, ai_label, ai_score, corrected_label=None):
|
| 85 |
try:
|
| 86 |
+
# Read the current sheet (Worksheet name MUST be "Sheet1")
|
| 87 |
+
existing_data = conn.read(spreadsheet=GSHEETS_URL, worksheet="Sheet1", ttl=0)
|
| 88 |
+
|
| 89 |
+
new_entry = pd.DataFrame([{
|
| 90 |
+
"Timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
| 91 |
+
"Text": text,
|
| 92 |
+
"AI_Label": ai_label,
|
| 93 |
+
"Confidence": f"{ai_score:.2%}",
|
| 94 |
+
"Correction": corrected_label if corrected_label else "N/A"
|
| 95 |
+
}])
|
| 96 |
+
|
| 97 |
+
updated_df = pd.concat([existing_data, new_entry], ignore_index=True)
|
| 98 |
+
conn.update(spreadsheet=GSHEETS_URL, worksheet="Sheet1", data=updated_df)
|
| 99 |
+
return True
|
| 100 |
+
except Exception as e:
|
| 101 |
+
st.error(f"Cloud Save Failed: {e}")
|
| 102 |
+
return False
|
| 103 |
|
| 104 |
# --- MODEL ENGINE ---
|
| 105 |
MODEL_PATH = "SumedhGajbhiye/Sentiment-Analyzer"
|