Nakhwa commited on
Commit
d4ac1ba
1 Parent(s): ae3b8d0

Update deteksi_content.py

Browse files
Files changed (1) hide show
  1. deteksi_content.py +149 -149
deteksi_content.py CHANGED
@@ -1,149 +1,149 @@
1
- import streamlit as st
2
- from datetime import datetime
3
- import pandas as pd
4
- from lime.lime_text import LimeTextExplainer
5
- from test import predict_hoax, predict_proba_for_lime
6
- import streamlit.components.v1 as components
7
- from load_model import load_model
8
- from st_aggrid import AgGrid, GridOptionsBuilder, GridUpdateMode
9
- from styles import COMMON_CSS
10
- from google.cloud import storage
11
- import os
12
- from io import StringIO
13
-
14
- # Set environment variable for Google Cloud credentials
15
- os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "D:\DashboardHoax\inbound-source-431806-g7-e49e388ce0be.json"
16
-
17
- def save_corrections_to_gcs(bucket_name, file_name, correction_data):
18
- client = storage.Client() # Uses the credentials set by the environment variable
19
- bucket = client.bucket("dashboardhoax-bucket")
20
- blob = bucket.blob("koreksi_pengguna_content.csv")
21
-
22
- # Check if the blob (file) exists
23
- if blob.exists():
24
- # Download existing CSV from GCS
25
- existing_data = blob.download_as_string().decode('utf-8')
26
- existing_df = pd.read_csv(StringIO(existing_data))
27
- else:
28
- # Create a new DataFrame if the file does not exist
29
- existing_df = pd.DataFrame(columns=['Timestamp', 'Title', 'Content', 'Prediction', 'Correction'])
30
-
31
- # Append the new data to the existing data
32
- new_data_df = pd.DataFrame(correction_data)
33
- updated_df = pd.concat([existing_df, new_data_df], ignore_index=True)
34
-
35
- # Convert the DataFrame back to CSV and upload
36
- updated_csv_data = updated_df.to_csv(index=False)
37
- blob.upload_from_string(updated_csv_data, content_type='text/csv')
38
-
39
- def show_deteksi_kontengcs():
40
- st.markdown(COMMON_CSS, unsafe_allow_html=True)
41
-
42
- if 'correction' not in st.session_state:
43
- st.session_state.correction = None
44
- if 'detection_result' not in st.session_state:
45
- st.session_state.detection_result = None
46
- if 'lime_explanation' not in st.session_state:
47
- st.session_state.lime_explanation = None
48
- if 'headline' not in st.session_state:
49
- st.session_state.headline = ""
50
- if 'content' not in st.session_state:
51
- st.session_state.content = ""
52
- if 'is_correct' not in st.session_state:
53
- st.session_state.is_correct = None
54
-
55
- # Dropdown for selecting a model
56
- st.markdown("<h6 style='font-size: 14px; margin-bottom: 0;'>Pilih Model</h6>", unsafe_allow_html=True)
57
- selected_model = st.selectbox(
58
- "",
59
- [
60
- "cahya/bert-base-indonesian-522M",
61
- "indobenchmark/indobert-base-p2",
62
- "indolem/indobert-base-uncased",
63
- "mdhugol/indonesia-bert-sentiment-classification"
64
- ],
65
- key="model_selector_content"
66
- )
67
-
68
- # Load the selected model
69
- tokenizer, model = load_model(selected_model)
70
-
71
- st.markdown("<h6 style='font-size: 14px; margin-bottom: 0;'>Masukkan Judul Berita :</h6>", unsafe_allow_html=True)
72
- st.session_state.headline = st.text_input("", value=st.session_state.headline)
73
-
74
- st.markdown("<h6 style='font-size: 14px; margin-bottom: 0;'>Masukkan Konten Berita :</h6>", unsafe_allow_html=True)
75
- st.session_state.content = st.text_area("", value=st.session_state.content)
76
-
77
- # Detection button
78
- if st.button("Deteksi", key="detect_content"):
79
- st.session_state.detection_result = predict_hoax(st.session_state.headline, st.session_state.content)
80
- st.success(f"Prediksi: {st.session_state.detection_result}")
81
-
82
- # Prepare the text for LIME
83
- lime_texts = [f"{st.session_state.headline} [SEP] {st.session_state.content}"]
84
-
85
- # Add a spinner and progress bar to indicate processing
86
- with st.spinner("Sedang memproses LIME, harap tunggu..."):
87
- # Explain the prediction
88
- explainer = LimeTextExplainer(class_names=['NON-HOAX', 'HOAX'])
89
- explanation = explainer.explain_instance(lime_texts[0], predict_proba_for_lime, num_features=5, num_samples=1000)
90
-
91
- # Save the LIME explanation in session state
92
- st.session_state.lime_explanation = explanation.as_html()
93
-
94
- # Display the detection result and LIME explanation if available
95
- if st.session_state.lime_explanation:
96
- lime_html = st.session_state.lime_explanation
97
-
98
- # Inject CSS for font size adjustment
99
- lime_html = f"""
100
- <style>
101
- .lime-text-explanation, .lime-highlight, .lime-classification,
102
- .lime-text-explanation * {{
103
- font-size: 14px !important;
104
- }}
105
- </style>
106
- <div class="lime-text-explanation">
107
- {lime_html}
108
- </div>
109
- """
110
- components.html(lime_html, height=200, scrolling=True)
111
-
112
- # Display a radio button asking if the detection result is correct
113
- if st.session_state.detection_result is not None:
114
- st.markdown("<h6 style='font-size: 16px; margin-bottom: -150px;'>Apakah hasil deteksi sudah benar?</h6>", unsafe_allow_html=True)
115
- st.session_state.is_correct = st.radio("", ("Ya", "Tidak"))
116
-
117
- if st.session_state.is_correct == "Ya":
118
- st.success("Deteksi sudah benar.")
119
- else:
120
- # Determine the correction based on the prediction
121
- st.session_state.correction = "HOAX" if st.session_state.detection_result == "NON-HOAX" else "NON-HOAX"
122
-
123
- # Display the correction DataFrame
124
- correction_data = [{
125
- 'Title': st.session_state.headline,
126
- 'Content': st.session_state.content,
127
- 'Prediction': st.session_state.detection_result,
128
- 'Correction': st.session_state.correction,
129
- 'Timestamp': datetime.now().strftime("%Y-%m-%d %H:%M:%S")
130
- }]
131
-
132
- # Save button
133
- if st.button("Simpan"):
134
- # Save the correction data to GCS
135
- save_corrections_to_gcs("your-bucket-name", "koreksi_pengguna.csv", correction_data)
136
-
137
- # Create a formatted string with CSS for alignment and multi-line content handling
138
- formatted_text = f"""
139
- <div style='font-size: 14px;'>
140
- <p style='margin: 0;'><span style='display: inline-block; width: 120px; font-weight: bold;'>Title</span> : <span style='white-space: pre-wrap;'>{st.session_state.headline}</span></p>
141
- <p style='margin: 0;'><span style='display: inline-block; width: 120px; font-weight: bold;'>Content</span> : <span style='white-space: pre-wrap;'>{st.session_state.content}</span></p>
142
- <p style='margin: 0;'><span style='display: inline-block; width: 120px; font-weight: bold;'>Prediction</span> : {st.session_state.detection_result}</p>
143
- <p style='margin: 0;'><span style='display: inline-block; width: 120px; font-weight: bold;'>Correction</span> : {st.session_state.correction}</p>
144
- </div>
145
- """
146
-
147
- # Display the correction as text
148
- st.markdown(formatted_text, unsafe_allow_html=True)
149
- st.success("Koreksi telah disimpan.")
 
1
+ import streamlit as st
2
+ from datetime import datetime
3
+ import pandas as pd
4
+ from lime.lime_text import LimeTextExplainer
5
+ from test import predict_hoax, predict_proba_for_lime
6
+ import streamlit.components.v1 as components
7
+ from load_model import load_model
8
+ from st_aggrid import AgGrid, GridOptionsBuilder, GridUpdateMode
9
+ from styles import COMMON_CSS
10
+ from google.cloud import storage
11
+ import os
12
+ from io import StringIO
13
+
14
+ # Set environment variable for Google Cloud credentials
15
+ os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "D:\DashboardHoax\inbound-source-431806-g7-e49e388ce0be.json"
16
+
17
+ def save_corrections_to_gcs(bucket_name, file_name, correction_data):
18
+ client = storage.Client() # Uses the credentials set by the environment variable
19
+ bucket = client.bucket("dashboardhoax-bucket")
20
+ blob = bucket.blob("koreksi_pengguna_content.csv")
21
+
22
+ # Check if the blob (file) exists
23
+ if blob.exists():
24
+ # Download existing CSV from GCS
25
+ existing_data = blob.download_as_string().decode('utf-8')
26
+ existing_df = pd.read_csv(StringIO(existing_data))
27
+ else:
28
+ # Create a new DataFrame if the file does not exist
29
+ existing_df = pd.DataFrame(columns=['Timestamp', 'Title', 'Content', 'Prediction', 'Correction'])
30
+
31
+ # Append the new data to the existing data
32
+ new_data_df = pd.DataFrame(correction_data)
33
+ updated_df = pd.concat([existing_df, new_data_df], ignore_index=True)
34
+
35
+ # Convert the DataFrame back to CSV and upload
36
+ updated_csv_data = updated_df.to_csv(index=False)
37
+ blob.upload_from_string(updated_csv_data, content_type='text/csv')
38
+
39
+ def show_deteksi_konten():
40
+ st.markdown(COMMON_CSS, unsafe_allow_html=True)
41
+
42
+ if 'correction' not in st.session_state:
43
+ st.session_state.correction = None
44
+ if 'detection_result' not in st.session_state:
45
+ st.session_state.detection_result = None
46
+ if 'lime_explanation' not in st.session_state:
47
+ st.session_state.lime_explanation = None
48
+ if 'headline' not in st.session_state:
49
+ st.session_state.headline = ""
50
+ if 'content' not in st.session_state:
51
+ st.session_state.content = ""
52
+ if 'is_correct' not in st.session_state:
53
+ st.session_state.is_correct = None
54
+
55
+ # Dropdown for selecting a model
56
+ st.markdown("<h6 style='font-size: 14px; margin-bottom: 0;'>Pilih Model</h6>", unsafe_allow_html=True)
57
+ selected_model = st.selectbox(
58
+ "",
59
+ [
60
+ "cahya/bert-base-indonesian-522M",
61
+ "indobenchmark/indobert-base-p2",
62
+ "indolem/indobert-base-uncased",
63
+ "mdhugol/indonesia-bert-sentiment-classification"
64
+ ],
65
+ key="model_selector_content"
66
+ )
67
+
68
+ # Load the selected model
69
+ tokenizer, model = load_model(selected_model)
70
+
71
+ st.markdown("<h6 style='font-size: 14px; margin-bottom: 0;'>Masukkan Judul Berita :</h6>", unsafe_allow_html=True)
72
+ st.session_state.headline = st.text_input("", value=st.session_state.headline)
73
+
74
+ st.markdown("<h6 style='font-size: 14px; margin-bottom: 0;'>Masukkan Konten Berita :</h6>", unsafe_allow_html=True)
75
+ st.session_state.content = st.text_area("", value=st.session_state.content)
76
+
77
+ # Detection button
78
+ if st.button("Deteksi", key="detect_content"):
79
+ st.session_state.detection_result = predict_hoax(st.session_state.headline, st.session_state.content)
80
+ st.success(f"Prediksi: {st.session_state.detection_result}")
81
+
82
+ # Prepare the text for LIME
83
+ lime_texts = [f"{st.session_state.headline} [SEP] {st.session_state.content}"]
84
+
85
+ # Add a spinner and progress bar to indicate processing
86
+ with st.spinner("Sedang memproses LIME, harap tunggu..."):
87
+ # Explain the prediction
88
+ explainer = LimeTextExplainer(class_names=['NON-HOAX', 'HOAX'])
89
+ explanation = explainer.explain_instance(lime_texts[0], predict_proba_for_lime, num_features=5, num_samples=1000)
90
+
91
+ # Save the LIME explanation in session state
92
+ st.session_state.lime_explanation = explanation.as_html()
93
+
94
+ # Display the detection result and LIME explanation if available
95
+ if st.session_state.lime_explanation:
96
+ lime_html = st.session_state.lime_explanation
97
+
98
+ # Inject CSS for font size adjustment
99
+ lime_html = f"""
100
+ <style>
101
+ .lime-text-explanation, .lime-highlight, .lime-classification,
102
+ .lime-text-explanation * {{
103
+ font-size: 14px !important;
104
+ }}
105
+ </style>
106
+ <div class="lime-text-explanation">
107
+ {lime_html}
108
+ </div>
109
+ """
110
+ components.html(lime_html, height=200, scrolling=True)
111
+
112
+ # Display a radio button asking if the detection result is correct
113
+ if st.session_state.detection_result is not None:
114
+ st.markdown("<h6 style='font-size: 16px; margin-bottom: -150px;'>Apakah hasil deteksi sudah benar?</h6>", unsafe_allow_html=True)
115
+ st.session_state.is_correct = st.radio("", ("Ya", "Tidak"))
116
+
117
+ if st.session_state.is_correct == "Ya":
118
+ st.success("Deteksi sudah benar.")
119
+ else:
120
+ # Determine the correction based on the prediction
121
+ st.session_state.correction = "HOAX" if st.session_state.detection_result == "NON-HOAX" else "NON-HOAX"
122
+
123
+ # Display the correction DataFrame
124
+ correction_data = [{
125
+ 'Title': st.session_state.headline,
126
+ 'Content': st.session_state.content,
127
+ 'Prediction': st.session_state.detection_result,
128
+ 'Correction': st.session_state.correction,
129
+ 'Timestamp': datetime.now().strftime("%Y-%m-%d %H:%M:%S")
130
+ }]
131
+
132
+ # Save button
133
+ if st.button("Simpan"):
134
+ # Save the correction data to GCS
135
+ save_corrections_to_gcs("your-bucket-name", "koreksi_pengguna.csv", correction_data)
136
+
137
+ # Create a formatted string with CSS for alignment and multi-line content handling
138
+ formatted_text = f"""
139
+ <div style='font-size: 14px;'>
140
+ <p style='margin: 0;'><span style='display: inline-block; width: 120px; font-weight: bold;'>Title</span> : <span style='white-space: pre-wrap;'>{st.session_state.headline}</span></p>
141
+ <p style='margin: 0;'><span style='display: inline-block; width: 120px; font-weight: bold;'>Content</span> : <span style='white-space: pre-wrap;'>{st.session_state.content}</span></p>
142
+ <p style='margin: 0;'><span style='display: inline-block; width: 120px; font-weight: bold;'>Prediction</span> : {st.session_state.detection_result}</p>
143
+ <p style='margin: 0;'><span style='display: inline-block; width: 120px; font-weight: bold;'>Correction</span> : {st.session_state.correction}</p>
144
+ </div>
145
+ """
146
+
147
+ # Display the correction as text
148
+ st.markdown(formatted_text, unsafe_allow_html=True)
149
+ st.success("Koreksi telah disimpan.")