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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import random
|
| 3 |
+
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
from sqlalchemy import (
|
| 7 |
+
TIMESTAMP,
|
| 8 |
+
Boolean,
|
| 9 |
+
Column,
|
| 10 |
+
ForeignKey,
|
| 11 |
+
Integer,
|
| 12 |
+
String,
|
| 13 |
+
Text,
|
| 14 |
+
create_engine,
|
| 15 |
+
or_,
|
| 16 |
+
)
|
| 17 |
+
from sqlalchemy.ext.declarative import declarative_base
|
| 18 |
+
from sqlalchemy.orm import Mapped, relationship, sessionmaker
|
| 19 |
+
from sqlalchemy.sql import func
|
| 20 |
+
|
| 21 |
+
from datasets import load_dataset
|
| 22 |
+
|
| 23 |
+
ds = load_dataset("bilguun/flickr30k-mn")
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
load_dotenv()
|
| 27 |
+
|
| 28 |
+
DATABASE_URL = os.getenv("DATABASE_URL")
|
| 29 |
+
engine = create_engine(DATABASE_URL)
|
| 30 |
+
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
| 31 |
+
Base = declarative_base()
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
# images_captions model
|
| 35 |
+
class ImagesCaptions(Base):
|
| 36 |
+
__tablename__ = "images_captions"
|
| 37 |
+
id = Column(Integer, primary_key=True, index=True)
|
| 38 |
+
image_id = Column(Integer)
|
| 39 |
+
image_name = Column(String)
|
| 40 |
+
caption_num = Column(Integer)
|
| 41 |
+
caption = Column(Text)
|
| 42 |
+
caption_mn_v1 = Column(Text)
|
| 43 |
+
caption_mn_v2 = Column(Text)
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
# task model
|
| 47 |
+
class Task(Base):
|
| 48 |
+
__tablename__ = "task"
|
| 49 |
+
id = Column(Integer, primary_key=True, index=True)
|
| 50 |
+
image_caption_id = Column(Integer, ForeignKey("images_captions.id"))
|
| 51 |
+
caption_num = Column(Integer)
|
| 52 |
+
reverse_caption = Column(Boolean)
|
| 53 |
+
status = Column(String)
|
| 54 |
+
image_caption: Mapped[ImagesCaptions] = relationship("ImagesCaptions")
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
# task_submission model
|
| 58 |
+
class TaskSubmission(Base):
|
| 59 |
+
__tablename__ = "task_submission"
|
| 60 |
+
id = Column(Integer, primary_key=True, index=True)
|
| 61 |
+
task_id = Column(Integer, ForeignKey("task.id"))
|
| 62 |
+
choice = Column(Text)
|
| 63 |
+
created_by = Column(String)
|
| 64 |
+
created_at = Column(TIMESTAMP, server_default=func.now())
|
| 65 |
+
task: Mapped[Task] = relationship("Task")
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def get_random_task() -> Task | None:
|
| 69 |
+
"""Retrieves a random task from the top 100 pending or in_progress tasks."""
|
| 70 |
+
db = SessionLocal()
|
| 71 |
+
try:
|
| 72 |
+
tasks = (
|
| 73 |
+
db.query(Task)
|
| 74 |
+
.filter(or_(Task.status == "pending", Task.status == "in_progress"))
|
| 75 |
+
.where(Task.image_caption_id % random.randint(1, 5) == 0)
|
| 76 |
+
.order_by(Task.image_caption_id.asc())
|
| 77 |
+
.limit(500)
|
| 78 |
+
.all()
|
| 79 |
+
)
|
| 80 |
+
if tasks:
|
| 81 |
+
random_task = random.choice(tasks)
|
| 82 |
+
return random_task
|
| 83 |
+
else:
|
| 84 |
+
return None
|
| 85 |
+
finally:
|
| 86 |
+
db.close()
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def random_task():
|
| 90 |
+
task = get_random_task()
|
| 91 |
+
if task is None:
|
| 92 |
+
return None, None, None, None
|
| 93 |
+
with SessionLocal() as db:
|
| 94 |
+
task = db.query(Task).filter(Task.id == task.id).first()
|
| 95 |
+
if task is None:
|
| 96 |
+
return None, None, None, None
|
| 97 |
+
caption1 = str(task.image_caption.caption_mn_v1)
|
| 98 |
+
caption2 = str(task.image_caption.caption_mn_v2)
|
| 99 |
+
if task.reverse_caption:
|
| 100 |
+
caption1, caption2 = caption2, caption1
|
| 101 |
+
return (
|
| 102 |
+
ds["train"][task.image_caption.image_id]["image"],
|
| 103 |
+
# str(task.image_caption.caption),
|
| 104 |
+
caption1,
|
| 105 |
+
caption2,
|
| 106 |
+
int(task.id),
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
css = """
|
| 111 |
+
.caption-btn {
|
| 112 |
+
background: #fcdccc;
|
| 113 |
+
border: 2px solid #f09162;
|
| 114 |
+
}
|
| 115 |
+
.dark .caption-btn {
|
| 116 |
+
background: #26201f;
|
| 117 |
+
border: 2px solid #40271a;
|
| 118 |
+
}
|
| 119 |
+
"""
|
| 120 |
+
|
| 121 |
+
with gr.Blocks(css=css) as blind_test:
|
| 122 |
+
username = gr.Textbox(
|
| 123 |
+
label="Нэрээ оруулна уу", placeholder="Нэр", max_lines=1, max_length=40
|
| 124 |
+
)
|
| 125 |
+
local_storage = gr.BrowserState([""])
|
| 126 |
+
|
| 127 |
+
@blind_test.load(inputs=[local_storage], outputs=[username])
|
| 128 |
+
def load_from_local_storage(saved_values):
|
| 129 |
+
print("loading from local storage", saved_values)
|
| 130 |
+
return saved_values[0]
|
| 131 |
+
|
| 132 |
+
@gr.on([username.change], inputs=[username], outputs=[local_storage])
|
| 133 |
+
def save_to_local_storage(username):
|
| 134 |
+
return [username]
|
| 135 |
+
|
| 136 |
+
task_id = gr.State(None)
|
| 137 |
+
|
| 138 |
+
image, desc, choice1, choice2 = None, None, None, None
|
| 139 |
+
img_preview = gr.Image(
|
| 140 |
+
image, label="Зураг", show_label=True, show_download_button=False, height=400
|
| 141 |
+
)
|
| 142 |
+
|
| 143 |
+
md_desc = gr.Markdown(
|
| 144 |
+
"### Доорх хоёр тайлбараас зурагтай хамгийн сайн тохирч буйг сонгоно уу."
|
| 145 |
+
)
|
| 146 |
+
with gr.Row(equal_height=True, variant="panel"):
|
| 147 |
+
with gr.Column(scale=1):
|
| 148 |
+
caption_choice1_button = gr.Button(
|
| 149 |
+
choice1, variant="secondary", elem_classes="caption-btn"
|
| 150 |
+
)
|
| 151 |
+
with gr.Column(scale=1):
|
| 152 |
+
caption_choice2_button = gr.Button(
|
| 153 |
+
choice2, variant="secondary", elem_classes="caption-btn"
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
def on_submit(username: str, choice: int, task_id: int):
|
| 157 |
+
print("on_submit", username if username is not None else None, choice, task_id)
|
| 158 |
+
if username == "":
|
| 159 |
+
gr.Warning("Нэрээ оруулна уу!")
|
| 160 |
+
return (
|
| 161 |
+
gr.update(visible=True),
|
| 162 |
+
gr.update(visible=True),
|
| 163 |
+
gr.update(visible=True),
|
| 164 |
+
gr.update(visible=True),
|
| 165 |
+
)
|
| 166 |
+
if choice not in [1, 2]:
|
| 167 |
+
return (
|
| 168 |
+
gr.update(visible=True),
|
| 169 |
+
gr.update(visible=True),
|
| 170 |
+
gr.update(visible=True),
|
| 171 |
+
gr.update(visible=True),
|
| 172 |
+
)
|
| 173 |
+
|
| 174 |
+
with SessionLocal() as db:
|
| 175 |
+
task = db.query(Task).filter(Task.id == task_id).first()
|
| 176 |
+
if task is None:
|
| 177 |
+
return None, None, None, None
|
| 178 |
+
if task.reverse_caption:
|
| 179 |
+
choice = 2 if choice == 1 else 1
|
| 180 |
+
task_submission = TaskSubmission(
|
| 181 |
+
task_id=task.id, choice=choice, created_by=username
|
| 182 |
+
)
|
| 183 |
+
db.add(task_submission)
|
| 184 |
+
db.commit()
|
| 185 |
+
submission_count = (
|
| 186 |
+
db.query(TaskSubmission)
|
| 187 |
+
.filter(TaskSubmission.task_id == task.id)
|
| 188 |
+
.count()
|
| 189 |
+
)
|
| 190 |
+
if submission_count >= 0:
|
| 191 |
+
task.status = "in_progress"
|
| 192 |
+
elif submission_count >= 3:
|
| 193 |
+
task.status = "done"
|
| 194 |
+
db.commit()
|
| 195 |
+
|
| 196 |
+
image, choice1, choice2, task_id = random_task()
|
| 197 |
+
return image, choice1, choice2, task_id
|
| 198 |
+
|
| 199 |
+
@caption_choice1_button.click(
|
| 200 |
+
inputs=[username, task_id],
|
| 201 |
+
outputs=[img_preview, caption_choice1_button, caption_choice2_button, task_id],
|
| 202 |
+
)
|
| 203 |
+
def submit_choice1(username, task_id):
|
| 204 |
+
return on_submit(username, 1, task_id)
|
| 205 |
+
|
| 206 |
+
@caption_choice2_button.click(
|
| 207 |
+
inputs=[username, task_id],
|
| 208 |
+
outputs=[img_preview, caption_choice1_button, caption_choice2_button, task_id],
|
| 209 |
+
)
|
| 210 |
+
def submit_choice2(username, task_id):
|
| 211 |
+
return on_submit(username, 2, task_id)
|
| 212 |
+
|
| 213 |
+
blind_test.load(
|
| 214 |
+
fn=random_task,
|
| 215 |
+
outputs=[img_preview, caption_choice1_button, caption_choice2_button, task_id],
|
| 216 |
+
)
|
| 217 |
+
|
| 218 |
+
|
| 219 |
+
blind_test.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
datasets
|
| 2 |
+
gradio
|
| 3 |
+
numpy<2
|
| 4 |
+
psycopg2
|
| 5 |
+
python-dotenv
|
| 6 |
+
sqlalchemy
|