Brian045 commited on
Commit
e7dbf80
·
verified ·
1 Parent(s): 422fccf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -4
app.py CHANGED
@@ -1,17 +1,21 @@
1
  import gradio as gr
2
  import pandas as pd
3
  import os
4
- from translator_utils import process_rows, revert_rows, download_dataset, LOCAL_FILENAME, ENV_HF_TOKEN
5
 
6
  def load_data():
7
- """Load the dataset safely."""
8
  if not os.path.exists(LOCAL_FILENAME):
9
  try:
10
  download_dataset()
11
  except:
12
  return pd.DataFrame()
 
 
 
 
 
13
  try:
14
- df = pd.read_csv(LOCAL_FILENAME)
15
  if 'Judul_Inggris' not in df.columns:
16
  df['Judul_Inggris'] = ""
17
  if 'Isi_Inggris' not in df.columns:
@@ -21,6 +25,14 @@ def load_data():
21
  except:
22
  return pd.DataFrame()
23
 
 
 
 
 
 
 
 
 
24
  def get_pending_data(full_df):
25
  """Filter for rows that are NOT fully translated."""
26
  if full_df.empty:
@@ -97,6 +109,12 @@ def filter_history(date_input):
97
  df = load_data()
98
  return get_history_data(df, date_input)
99
 
 
 
 
 
 
 
100
  # CSS to ensure columns are readable
101
  custom_css = """
102
  /* Fix minimum width for content columns */
@@ -121,7 +139,11 @@ with gr.Blocks(title="Auto AI News Translator") as app:
121
  type="password",
122
  value=ENV_HF_TOKEN
123
  )
124
- status_output = gr.Textbox(label="Status", value="Ready", interactive=False)
 
 
 
 
125
 
126
  with gr.Tabs():
127
  with gr.TabItem("Translate Pending"):
@@ -138,6 +160,7 @@ with gr.Blocks(title="Auto AI News Translator") as app:
138
  with gr.TabItem("History (Revert Supported)"):
139
  gr.Markdown("Select rows to delete translation and move back to Pending.")
140
  date_filter = gr.Textbox(label="Filter by Date (YYYY-MM-DD)")
 
141
  history_df_comp = gr.DataFrame(
142
  value=get_history_data(load_data()),
143
  label="Translation History",
@@ -160,6 +183,11 @@ with gr.Blocks(title="Auto AI News Translator") as app:
160
  inputs=[history_df_comp, hf_token],
161
  outputs=[pending_df_comp, history_df_comp, status_output]
162
  )
 
 
 
 
 
163
 
164
  if __name__ == "__main__":
165
  app.launch(css=custom_css)
 
1
  import gradio as gr
2
  import pandas as pd
3
  import os
4
+ from translator_utils import process_rows, revert_rows, download_dataset, LOCAL_FILENAME, ENV_HF_TOKEN, read_csv_robust
5
 
6
  def load_data():
7
+ """Load the dataset safely using robust reader."""
8
  if not os.path.exists(LOCAL_FILENAME):
9
  try:
10
  download_dataset()
11
  except:
12
  return pd.DataFrame()
13
+
14
+ df = read_csv_robust(LOCAL_FILENAME)
15
+ if df.empty:
16
+ return df
17
+
18
  try:
 
19
  if 'Judul_Inggris' not in df.columns:
20
  df['Judul_Inggris'] = ""
21
  if 'Isi_Inggris' not in df.columns:
 
25
  except:
26
  return pd.DataFrame()
27
 
28
+ def force_refresh_data():
29
+ """Forces a re-download and reload of the data."""
30
+ try:
31
+ download_dataset(force=True)
32
+ return load_data(), "Data refreshed successfully."
33
+ except Exception as e:
34
+ return pd.DataFrame(), f"Failed to refresh data: {e}"
35
+
36
  def get_pending_data(full_df):
37
  """Filter for rows that are NOT fully translated."""
38
  if full_df.empty:
 
109
  df = load_data()
110
  return get_history_data(df, date_input)
111
 
112
+ def refresh_views():
113
+ df, msg = force_refresh_data()
114
+ pending = get_pending_data(df)
115
+ history = get_history_data(df)
116
+ return pending, history, msg
117
+
118
  # CSS to ensure columns are readable
119
  custom_css = """
120
  /* Fix minimum width for content columns */
 
139
  type="password",
140
  value=ENV_HF_TOKEN
141
  )
142
+
143
+ with gr.Row():
144
+ status_output = gr.Textbox(label="Status", value="Ready", interactive=False, scale=4)
145
+ refresh_btn = gr.Button("🔄 Refresh Data (Force Redownload)", scale=1)
146
+
147
 
148
  with gr.Tabs():
149
  with gr.TabItem("Translate Pending"):
 
160
  with gr.TabItem("History (Revert Supported)"):
161
  gr.Markdown("Select rows to delete translation and move back to Pending.")
162
  date_filter = gr.Textbox(label="Filter by Date (YYYY-MM-DD)")
163
+ # History now has checkbox
164
  history_df_comp = gr.DataFrame(
165
  value=get_history_data(load_data()),
166
  label="Translation History",
 
183
  inputs=[history_df_comp, hf_token],
184
  outputs=[pending_df_comp, history_df_comp, status_output]
185
  )
186
+
187
+ refresh_btn.click(
188
+ fn=refresh_views,
189
+ outputs=[pending_df_comp, history_df_comp, status_output]
190
+ )
191
 
192
  if __name__ == "__main__":
193
  app.launch(css=custom_css)