Sumedhzz commited on
Commit
efd5730
·
1 Parent(s): d125af0

Final Secret Bypass Fix

Browse files
Files changed (1) hide show
  1. 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
- # Use .get() to prevent the "StreamlitSecretNotFoundError" crash
61
- gsheets_json = st.secrets.get("GSHEETS_JSON")
62
- sheet_url = st.secrets.get("GSHEETS_URL")
63
-
64
- if gsheets_json and sheet_url:
65
  try:
66
- creds = json.loads(gsheets_json)
67
- conn = st.connection("gsheets", type=GSheetsConnection, credentials=creds)
 
 
 
 
 
 
 
 
 
 
 
 
68
  except Exception as e:
69
- st.error(f"Configuration Error: {e}")
70
  st.stop()
71
- else:
72
- # This replaces the red crash box with a clean instructions message
73
- st.warning("🚀 System Initializing... Please ensure GSHEETS_JSON and GSHEETS_URL are set in Hugging Face Settings.")
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
- # Pull fresh data to append to
80
- existing_data = conn.read(spreadsheet=sheet_url, worksheet="Sheet1", ttl=0)
81
- except:
82
- existing_data = pd.DataFrame()
83
-
84
- needs_review = "YES" if 0.33 <= ai_score <= 0.65 else "NO"
85
- new_entry = pd.DataFrame([{
86
- "Timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
87
- "Text": text,
88
- "AI_Label": ai_label,
89
- "Confidence": f"{ai_score:.2%}",
90
- "Needs_Review": needs_review,
91
- "Correction": corrected_label if corrected_label else "N/A"
92
- }])
93
-
94
- updated_df = pd.concat([existing_data, new_entry], ignore_index=True)
95
- conn.update(spreadsheet=sheet_url, worksheet="Sheet1", data=updated_df)
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"