Spaces:
Running
Running
File size: 6,179 Bytes
1ef4e10 0fb23b8 bc1cd44 e1df50c bc1cd44 0fb23b8 1ef4e10 bc1cd44 1ef4e10 e1df50c 1ef4e10 e1df50c 0fb23b8 bc1cd44 e1df50c bc1cd44 e1df50c f7ef7d3 e1df50c f7ef7d3 bc1cd44 e1df50c bc1cd44 f7ef7d3 bc1cd44 e1df50c bc1cd44 f7ef7d3 e1df50c f7ef7d3 e1df50c bc1cd44 e1df50c f7ef7d3 bc1cd44 e1df50c 1ef4e10 e1df50c f7ef7d3 1ef4e10 e1df50c 1ef4e10 bc1cd44 1ef4e10 0fb23b8 1ef4e10 c8c252f 1ef4e10 |
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 |
import gradio as gr
from utils.logger import Logger
from utils.database import get_db
from data.repository.annotator_repo import AnnotatorRepo
from data.repository.annotator_workload_repo import AnnotatorWorkloadRepo
from utils.security import verify_password
log = Logger()
class AuthService:
"""
Authenticate users against DB and drive Gradio UI states.
"""
# ───────────── LOGIN ───────────── #
@staticmethod
def login(username: str, password: str, session: dict):
"""
خروجیها (به همین ترتیب در LoginPage ثبت شده است):
0) message (Markdown داخل فرم لاگین)
1) login_container (پنهان/نمایان شدن فرم لاگین)
2) dashboard_container (نمایش داشبورد)
3) header_welcome (پیام خوشآمد در هدر)
4) items_state (لیست رکوردها)
5) idx_state (اندیس فعلی)
6-11) شش فیلد نمایشی (id, filename, sentence, ann_sentence, ann_at, validated)
"""
# ---------- اعتبارسنجی ---------- #
log.info(f"Login attempt: username={username}")
with get_db() as db:
repo = AnnotatorRepo(db)
annotator = repo.get_annotator_by_name(username)
# ⬇️ توابع کمکی برای تولید خروجی خالی (درصورت خطا)
def empty_dashboard_outputs_for_ui(): # Renamed and adjusted for UI outputs
return (
[], # items_state
0, # idx_state
"", # tts_id
"", # filename
"", # sentence
"", # ann_sentence
)
# --- کاربر موجود نیست / غیر فعال
if annotator is None or not annotator.is_active:
log.warning("Failed login (not found / inactive)")
return (
"❌ Wrong username or password!", # message
gr.update(), # login_container (no change)
gr.update(visible=False), # dashboard_container
gr.update(value=""), # header_welcome
*empty_dashboard_outputs_for_ui(), # items_state, idx_state, and 4 UI textboxes
)
# --- رمز عبور اشتباه
if not verify_password(password, annotator.password):
log.warning("Failed login (bad password)")
return (
"❌ Wrong username or password!", # message
gr.update(), # login_container (no change)
gr.update(visible=False), # dashboard_container
gr.update(value=""), # header_welcome
*empty_dashboard_outputs_for_ui(), # items_state, idx_state, and 4 UI textboxes
)
# ---------- ورود موفق ---------- #
session["user_id"] = annotator.id
session["username"] = annotator.name
# بارگذاری دادههای داشبورد
workload_repo = AnnotatorWorkloadRepo(db)
raw_items = workload_repo.get_tts_data_with_annotations(username)
dashboard_items = [
{
"id": row["tts_data"].id,
"filename": row["tts_data"].filename,
"sentence": row["tts_data"].sentence,
"annotated_sentence": (
row["annotation"].annotated_sentence
if row["annotation"]
else ""
),
"annotated_at": (
row["annotation"].annotated_at.isoformat()
if row["annotation"] and row["annotation"].annotated_at
else ""
),
"validated": (
row["annotation"].validated
if row["annotation"] is not None
else False
),
}
for row in raw_items
]
session["dashboard_items"] = dashboard_items
# مقداردهی فیلدهای رکورد اول (یا مقادیر تهی)
if dashboard_items:
first = dashboard_items[0]
# Only take the first 4 values needed for the 4 textboxes
# tts_id, filename, sentence, ann_sentence
first_vals_for_ui = (
first["id"],
first["filename"],
first["sentence"],
first["annotated_sentence"],
)
else:
first_vals_for_ui = ("", "", "", "")
log.info(f"User '{username}' logged in successfully.")
# ---------- خروجی نهایی برای Gradio ---------- #
return (
None, # 0: پیام خطا وجود ندارد
gr.update(visible=False), # 1: فرم لاگین را مخفی کن
gr.update(visible=True), # 2: داشبورد را نشان بده
gr.update(value=f"👋 Welcome, {annotator.name}!"), # 3
dashboard_items, # 4: items_state
0, # 5: idx_state
*first_vals_for_ui, # 6-9: چهار فیلد نخست برای UI
)
# ───────────── LOGOUT ───────────── #
@staticmethod
def logout(session: dict):
username = session.get("username", "unknown")
session.clear()
log.info(f"User '{username}' logged out.")
return (
gr.update(visible=True), # 1 → login_page.container
gr.update(visible=False), # 2 → dashboard_page.container
gr.update(value=""), # 3 → self.welcome
gr.update(value=""), # 4 → login_page.message
)
|