import os import toml #from dotenv import load_dotenv #load_dotenv() # Load secrets from TOML file #os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = ".streamlit/secrets.toml" """ google_creds = os.getenv('GOOGLE_APPLICATION_CREDENTIALS_JSON') if google_creds: with open('credentials.json', 'w') as f: f.write(google_creds) os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "credentials.json" """ import streamlit as st from datetime import datetime import pandas as pd from lime.lime_text import LimeTextExplainer from test import predict_hoax, predict_proba_for_lime import streamlit.components.v1 as components from load_model import load_model from st_aggrid import AgGrid, GridOptionsBuilder, GridUpdateMode from styles import COMMON_CSS from google.cloud import storage from io import StringIO import pytz def save_corrections_to_gcs(bucket_name, file_name, correction_data): try: client = storage.Client() # Uses the credentials set by the environment variable bucket = client.bucket("dashboardindohoax-bucket") blob = bucket.blob("koreksi_pengguna.csv") # Check if the blob (file) exists if blob.exists(): # Download existing CSV from GCS existing_data = blob.download_as_string().decode('utf-8') existing_df = pd.read_csv(StringIO(existing_data)) else: # Create a new DataFrame if the file does not exist existing_df = pd.DataFrame(columns=['Timestamp', 'Title', 'Content', 'Prediction', 'Correction']) # Append the new data to the existing data new_data_df = pd.DataFrame(correction_data) updated_df = pd.concat([existing_df, new_data_df], ignore_index=True) # Convert the DataFrame back to CSV and upload updated_csv_data = updated_df.to_csv(index=False) blob.upload_from_string(updated_csv_data, content_type='text/csv') except Exception as e: # If GCS fails, save locally as fallback local_file = "koreksi_pengguna.csv" if os.path.exists(local_file): existing_df = pd.read_csv(local_file) else: existing_df = pd.DataFrame(columns=['Timestamp', 'Title', 'Content', 'Prediction', 'Correction']) new_data_df = pd.DataFrame(correction_data) updated_df = pd.concat([existing_df, new_data_df], ignore_index=True) updated_df.to_csv(local_file, index=False) st.warning(f"GCS save failed ({e}), saved locally to {local_file}.") def show_deteksi_konten(): st.markdown(COMMON_CSS, unsafe_allow_html=True) if 'correction' not in st.session_state: st.session_state.correction = None if 'detection_result' not in st.session_state: st.session_state.detection_result = None if 'lime_explanation' not in st.session_state: st.session_state.lime_explanation = None if 'headline' not in st.session_state: st.session_state.headline = "" if 'content' not in st.session_state: st.session_state.content = "" if 'is_correct' not in st.session_state: st.session_state.is_correct = None # Dropdown for selecting a model st.markdown("
Title : {st.session_state.headline}
Content : {st.session_state.content}
Prediction : {st.session_state.detection_result}
Correction : {st.session_state.correction}