File size: 8,831 Bytes
1db7196 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | import gradio as gr
import json
import os
from datetime import datetime
def sanitize_username(username: str) -> str:
"""Make username safe for filesystem paths."""
if not username:
return ""
username = username.strip()
safe = "".join(ch for ch in username if ch.isalnum() or ch in ("_", "-"))
return safe
def get_user_session_file(username):
safe = sanitize_username(username)
return os.path.join(SAVE_DIR, f"ratings_{safe}.json")
language="Bengali"
if language=="Chinese":
language_code="ch"
elif language=="Hindi":
language_code="hi"
elif language=="Bengali":
language_code="be"
else:
assert False, "Unsupported language"
# Load translation dataset (EN -> BN fulltext/summary)
TRANSLATION_PATH = (
"/home/mshahidul/readctrl/data/translated_data/translation_wo_judge/"
"multiclinsum_gs_train_en2bn_gemma(0_200).json"
)
with open(TRANSLATION_PATH, "r", encoding="utf-8") as f:
translation_dataset = json.load(f)
dataset = [
{
"src_fulltext": item.get("fulltext", ""),
"translated_fulltext": item.get("translated_fulltext", ""),
"id": item.get("id"),
}
for item in translation_dataset
][:50]
# 2. Configuration for saving
SAVE_DIR = f"/home/mshahidul/readctrl/data/translated_data/rating_info_v2/{language_code}"
os.makedirs(SAVE_DIR, exist_ok=True)
SESSION_FILE = None # Will be set per user
RATING_OPTIONS = [
("1 - Poor (Incorrect/Nonsense)", 1),
("2 - Fair (Understandable but awkward)", 2),
("3 - Good (Accurate/Perfect)", 3)
]
custom_css = """
.small-header { font-size: 0.85rem !important; font-weight: 600; margin-bottom: -10px; color: #555; }
.nav-row { background-color: #f9f9f9; padding: 10px; border-radius: 8px; margin-bottom: 15px; }
"""
def save_rating_to_json(data_item, username):
session_file = get_user_session_file(username)
output_data = []
if os.path.exists(session_file):
with open(session_file, "r", encoding="utf-8") as f:
try:
output_data = json.load(f)
except json.JSONDecodeError:
output_data = []
# Backward/forward compatibility: support either list[record] or dict with "records".
if isinstance(output_data, dict):
records = output_data.get("records", [])
else:
records = output_data if isinstance(output_data, list) else []
# Keep a single record per index (update if it already exists).
new_index = data_item.get("index")
updated = False
for i, rec in enumerate(records):
if isinstance(rec, dict) and rec.get("index") == new_index:
records[i] = data_item
updated = True
break
if not updated:
records.append(data_item)
payload = {
"username": sanitize_username(username) or username,
"updated_at": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"records": records,
}
with open(session_file, "w", encoding="utf-8") as f:
json.dump(payload, f, ensure_ascii=False, indent=4)
def load_user_records(username):
session_file = get_user_session_file(username)
if not os.path.exists(session_file):
return []
try:
with open(session_file, "r", encoding="utf-8") as f:
data = json.load(f)
if isinstance(data, dict):
records = data.get("records", [])
else:
records = data
return records if isinstance(records, list) else []
except Exception:
return []
def load_example(index):
total = len(dataset)
index = max(0, min(index, total - 1))
item = dataset[index]
progress_pct = (index / total) * 100
progress_text = f"Sample {index + 1} of {total} ({progress_pct:.1f}%)"
src_fulltext = item["src_fulltext"]
translated_fulltext = item["translated_fulltext"]
return (
src_fulltext, # src_display
translated_fulltext, # eng_display
None, # rating_dropdown (clears selection)
index, # current_index
progress_text, # progress_display
progress_pct, # progress_bar
index + 1 # jump_input
)
def get_last_index_for_user(username):
if not username:
return 0
records = load_user_records(username)
done_indices = set()
for rec in records:
if isinstance(rec, dict) and isinstance(rec.get("index"), int):
done_indices.add(rec["index"])
# Resume means: first unannotated sample in order.
for i in range(len(dataset)):
if i not in done_indices:
return i
# Completed.
return len(dataset)
def load_example_or_done(index):
if index >= len(dataset):
total = len(dataset)
progress_text = f"✅ Completed all {total} samples"
return (
"✅ ALL DONE",
"✅ ALL DONE",
None,
total,
progress_text,
100,
total,
)
return load_example(index)
def next_item(index, rating, src_txt, eng_txt, username):
if rating is None:
raise gr.Error("Please select a rating before proceeding!")
if not username:
raise gr.Error("Please enter your username!")
safe_user = sanitize_username(username)
if not safe_user:
raise gr.Error("Username must contain letters/numbers (optionally _ or -).")
record = {
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"index": index,
"src_text": src_txt,
"translated_text": eng_txt,
"rating": rating,
"username": safe_user
}
save_rating_to_json(record, safe_user)
gr.Info(f"Saved record {index + 1} for {safe_user}.")
# After saving, resume at first unannotated index.
next_idx = get_last_index_for_user(safe_user)
return load_example_or_done(next_idx)
def jump_to_instance(target_index):
return load_example_or_done(target_index - 1)
with gr.Blocks(css=custom_css) as demo:
username_box = gr.Textbox(label="Enter your username", value="", interactive=True)
login_btn = gr.Button("Start/Resume Session", variant="primary")
current_index = gr.State(0)
total_count = len(dataset)
gr.Markdown("## Translation Quality Annotation")
gr.Markdown("Data generated by TranslateGemma.")
with gr.Row(elem_classes="nav-row"):
with gr.Column(scale=2):
progress_bar = gr.Slider(label="Progress", minimum=0, maximum=100, value=0, interactive=False)
progress_display = gr.Markdown(f"Sample 1 of {total_count} (0.0%)")
with gr.Column(scale=1):
jump_input = gr.Number(label="Jump to Sample #", value=1, precision=0)
jump_btn = gr.Button("Go", size="sm")
with gr.Row():
with gr.Column():
gr.Markdown("##### Source Fulltext (English)")
src_display = gr.Textbox(value=dataset[0]["src_fulltext"], interactive=False, lines=12, show_label=False)
with gr.Column():
gr.Markdown("##### Fulltext Translation (Bangla)")
eng_display = gr.Textbox(value=dataset[0]["translated_fulltext"], interactive=False, lines=12, show_label=False)
rating_dropdown = gr.Dropdown(choices=RATING_OPTIONS, label="Select Rating")
with gr.Row():
prev_btn = gr.Button("⬅ Previous (Review)", variant="secondary")
submit_btn = gr.Button("Save & Next ➡", variant="primary")
def login_user(username):
safe_user = sanitize_username(username)
if not safe_user:
raise gr.Error("Please enter a valid username (letters/numbers, _ or -).")
idx = get_last_index_for_user(safe_user)
return load_example_or_done(idx)
login_btn.click(
fn=login_user,
inputs=[username_box],
outputs=[src_display, eng_display, rating_dropdown, current_index, progress_display, progress_bar, jump_input]
)
submit_btn.click(
fn=next_item,
inputs=[current_index, rating_dropdown, src_display, eng_display, username_box],
outputs=[src_display, eng_display, rating_dropdown, current_index, progress_display, progress_bar, jump_input]
)
# 2. Update Prev Button: removed tr_display from outputs
prev_btn.click(
fn=lambda idx: load_example_or_done(idx - 1),
inputs=[current_index],
outputs=[src_display, eng_display, rating_dropdown, current_index, progress_display, progress_bar, jump_input]
)
# 3. Update Jump Button: removed tr_display from outputs
jump_btn.click(
fn=jump_to_instance,
inputs=[jump_input],
outputs=[src_display, eng_display, rating_dropdown, current_index, progress_display, progress_bar, jump_input]
)
if __name__ == "__main__":
demo.launch(share=True) |