Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,128 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
import os
|
| 4 |
-
from translator_utils import
|
| 5 |
|
| 6 |
def load_data():
|
| 7 |
-
"""Load the dataset
|
| 8 |
if not os.path.exists(LOCAL_FILENAME):
|
| 9 |
try:
|
| 10 |
download_dataset()
|
| 11 |
except:
|
| 12 |
-
return pd.DataFrame(
|
| 13 |
-
|
| 14 |
try:
|
| 15 |
df = pd.read_csv(LOCAL_FILENAME)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
return df
|
| 17 |
-
except
|
| 18 |
-
return pd.DataFrame(
|
| 19 |
|
| 20 |
-
def
|
| 21 |
-
"""
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
translate_btn = gr.Button("Translate & Save", variant="primary")
|
| 36 |
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
|
|
|
|
|
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
translate_btn.click(
|
| 42 |
-
fn=
|
| 43 |
-
inputs=[hf_token],
|
| 44 |
-
outputs=[
|
| 45 |
)
|
|
|
|
|
|
|
| 46 |
|
| 47 |
if __name__ == "__main__":
|
| 48 |
app.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
import os
|
| 4 |
+
from translator_utils import process_rows, download_dataset, LOCAL_FILENAME
|
| 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 |
+
# Ensure translation columns exist
|
| 16 |
+
if 'Judul_Inggris' not in df.columns:
|
| 17 |
+
df['Judul_Inggris'] = ""
|
| 18 |
+
if 'Isi_Inggris' not in df.columns:
|
| 19 |
+
df['Isi_Inggris'] = ""
|
| 20 |
+
df = df.fillna("")
|
| 21 |
return df
|
| 22 |
+
except:
|
| 23 |
+
return pd.DataFrame()
|
| 24 |
|
| 25 |
+
def get_pending_data(full_df):
|
| 26 |
+
"""Filter for rows that are NOT fully translated."""
|
| 27 |
+
if full_df.empty:
|
| 28 |
+
return full_df
|
| 29 |
+
# A row is pending if either Judul_Inggris or Isi_Inggris is empty
|
| 30 |
+
mask = (full_df['Judul_Inggris'].str.strip() == "") | (full_df['Isi_Inggris'].str.strip() == "")
|
| 31 |
+
# Add a 'Select' column for checkbox, initialized to False
|
| 32 |
+
df_view = full_df[mask].copy()
|
| 33 |
+
df_view.insert(0, "Select", False)
|
| 34 |
+
return df_view
|
| 35 |
|
| 36 |
+
def get_history_data(full_df, date_filter=""):
|
| 37 |
+
"""Filter for rows that ARE fully translated."""
|
| 38 |
+
if full_df.empty:
|
| 39 |
+
return full_df
|
| 40 |
+
mask = (full_df['Judul_Inggris'].str.strip() != "") & (full_df['Isi_Inggris'].str.strip() != "")
|
| 41 |
+
df_view = full_df[mask].copy()
|
| 42 |
+
|
| 43 |
+
if date_filter:
|
| 44 |
+
df_view = df_view[df_view['Tanggal'].str.contains(date_filter, case=False, na=False)]
|
| 45 |
+
|
| 46 |
+
return df_view
|
| 47 |
+
|
| 48 |
+
def on_translate_selected(df_data, token, progress=gr.Progress()):
|
| 49 |
+
"""
|
| 50 |
+
Callback when user clicks 'Translate Selected'.
|
| 51 |
+
df_data is the dataframe from the component (with checkboxes).
|
| 52 |
+
"""
|
| 53 |
+
# Filter rows where 'Select' is True
|
| 54 |
+
# Gradio Dataframe sends back a pandas DataFrame
|
| 55 |
+
selected_rows = df_data[df_data["Select"] == True]
|
| 56 |
|
| 57 |
+
if selected_rows.empty:
|
| 58 |
+
return gr.DataFrame(), gr.DataFrame(), "No rows selected!"
|
|
|
|
| 59 |
|
| 60 |
+
# We need the ORIGINAL indices.
|
| 61 |
+
# Since we filtered the view, the index of the dataframe should still be the original index
|
| 62 |
+
# IF we didn't reset_index.
|
| 63 |
+
# Let's check. Gradio might reset it or treat it as data.
|
| 64 |
+
# To be safe, we rely on the fact that we passed the dataframe with original index?
|
| 65 |
+
# Actually Gradio usually converts index to 0..n.
|
| 66 |
+
# We should have preserved the original index as a column or rely on matching content?
|
| 67 |
+
# Better approach: We passed the DF. Gradio returns the modified DF.
|
| 68 |
+
# If the index is preserved, we are good.
|
| 69 |
+
# Let's try to trust the index.
|
| 70 |
+
|
| 71 |
+
indices = selected_rows.index.tolist()
|
| 72 |
+
|
| 73 |
+
# Call backend
|
| 74 |
+
updated_df, msg = process_rows(indices, token=token, progress=progress)
|
| 75 |
|
| 76 |
+
# Refresh views
|
| 77 |
+
pending = get_pending_data(updated_df)
|
| 78 |
+
history = get_history_data(updated_df)
|
| 79 |
|
| 80 |
+
return pending, history, msg
|
| 81 |
+
|
| 82 |
+
def filter_history(date_input):
|
| 83 |
+
df = load_data()
|
| 84 |
+
return get_history_data(df, date_input)
|
| 85 |
+
|
| 86 |
+
with gr.Blocks(title="Auto AI News Translator") as app:
|
| 87 |
+
gr.Markdown("# Auto AI News Translator")
|
| 88 |
+
gr.Markdown("Translate news from Indonesian to English (with Chunking). Select rows to translate.")
|
| 89 |
+
|
| 90 |
+
# Store full data state if needed, but we read from disk to be single-source-of-truth safe
|
| 91 |
+
# token input
|
| 92 |
+
hf_token = gr.Textbox(label="HF Token (Optional for Persistence)", placeholder="hf_...", type="password")
|
| 93 |
+
status_output = gr.Textbox(label="Status", value="Ready", interactive=False)
|
| 94 |
+
|
| 95 |
+
with gr.Tabs():
|
| 96 |
+
with gr.TabItem("Translate Pending"):
|
| 97 |
+
gr.Markdown("Select rows to translate and click Submit.")
|
| 98 |
+
# We use wrap=True for long text
|
| 99 |
+
# We use interactive=True to allow checking the 'Select' box
|
| 100 |
+
pending_df_comp = gr.DataFrame(
|
| 101 |
+
value=get_pending_data(load_data()),
|
| 102 |
+
label="Pending Translations",
|
| 103 |
+
interactive=True,
|
| 104 |
+
wrap=True,
|
| 105 |
+
datatype=["bool"] + ["str"] * 6 # approximate types
|
| 106 |
+
)
|
| 107 |
+
translate_btn = gr.Button("Translate Selected Rows", variant="primary")
|
| 108 |
+
|
| 109 |
+
with gr.TabItem("History"):
|
| 110 |
+
date_filter = gr.Textbox(label="Filter by Date (YYYY-MM-DD)")
|
| 111 |
+
history_df_comp = gr.DataFrame(
|
| 112 |
+
value=get_history_data(load_data()),
|
| 113 |
+
label="Translation History",
|
| 114 |
+
interactive=False,
|
| 115 |
+
wrap=True
|
| 116 |
+
)
|
| 117 |
+
date_filter.change(fn=filter_history, inputs=[date_filter], outputs=[history_df_comp])
|
| 118 |
+
|
| 119 |
translate_btn.click(
|
| 120 |
+
fn=on_translate_selected,
|
| 121 |
+
inputs=[pending_df_comp, hf_token],
|
| 122 |
+
outputs=[pending_df_comp, history_df_comp, status_output]
|
| 123 |
)
|
| 124 |
+
|
| 125 |
+
# Auto refresh on load? (Handled by initial value)
|
| 126 |
|
| 127 |
if __name__ == "__main__":
|
| 128 |
app.launch()
|