|
import gradio as gr |
|
import pandas as pd |
|
from datasets import load_dataset |
|
from deep_translator import GoogleTranslator |
|
from huggingface_hub import login |
|
import os |
|
|
|
|
|
|
|
HF_TOKEN = os.environ.get("HF_TOKEN") |
|
|
|
|
|
|
|
dataset = None |
|
df = None |
|
category_counts = None |
|
|
|
|
|
def load_and_process_data(): |
|
"""データセットをロードし、グローバル変数を初期化する""" |
|
global dataset, df, category_counts |
|
if dataset is not None: |
|
return |
|
|
|
try: |
|
|
|
if HF_TOKEN: |
|
login(token=HF_TOKEN) |
|
dataset = load_dataset("cais/hle", split="test") |
|
|
|
df = dataset.remove_columns(['image_preview', 'rationale_image']).to_pandas() |
|
category_counts = df['category'].value_counts() |
|
print("データセットのロードと前処理が完了しました。") |
|
except Exception as e: |
|
print(f"データセットのロードエラー: {e}") |
|
|
|
df = pd.DataFrame(columns=['id', 'question', 'category']) |
|
category_counts = pd.Series() |
|
|
|
|
|
def translate_text(text, dest_lang='ja'): |
|
"""テキストを翻訳します""" |
|
if not text or not isinstance(text, str): |
|
return "" |
|
try: |
|
|
|
translator = GoogleTranslator(source='auto', target=dest_lang) |
|
return translator.translate(text) |
|
except Exception as e: |
|
print(f"翻訳エラー: {e}") |
|
return f"翻訳エラー: {str(e)}" |
|
|
|
|
|
def on_category_change(selected_category): |
|
"""カテゴリが変更されたときに、問題のドロップダウンを更新する""" |
|
if selected_category == "全カテゴリ": |
|
filtered_indices = df.index |
|
else: |
|
filtered_indices = df[df['category'] == selected_category].index |
|
|
|
|
|
question_choices = [ |
|
(f"{df.loc[idx, 'question'][:80]}...", idx) for idx in filtered_indices |
|
] |
|
|
|
if not question_choices: |
|
|
|
return gr.Dropdown(choices=[], label="問題 (該当なし)", interactive=False, value=None) |
|
else: |
|
return gr.Dropdown(choices=question_choices, label="問題を選択", interactive=True, value=question_choices[0][1]) |
|
|
|
def on_question_change(selected_index): |
|
"""問題が選択されたときに、すべての詳細表示を更新する""" |
|
if selected_index is None or pd.isna(selected_index): |
|
|
|
empty_outputs = [gr.Markdown(visible=False)] * 6 + [gr.Image(visible=False)] * 2 |
|
return tuple(empty_outputs) |
|
|
|
|
|
entry = dataset[int(selected_index)] |
|
|
|
|
|
q_trans = translate_text(entry['question']) |
|
a_trans = translate_text(entry['answer']) |
|
r_trans = translate_text(entry.get('rationale', '')) |
|
|
|
|
|
outputs = { |
|
"question_md": gr.Markdown(f"### 質問\n---\n**原文:**\n{entry['question']}\n\n**日本語訳:**\n{q_trans}", visible=True), |
|
"question_img": gr.Image(entry.get('image_preview'), label="質問画像", visible=bool(entry.get('image_preview'))), |
|
"answer_md": gr.Markdown(f"### 回答\n---\n**原文:**\n{entry['answer']}\n\n**日本語訳:**\n{a_trans}", visible=True), |
|
"rationale_md": gr.Markdown(f"### 解説\n---\n**原文:**\n{entry.get('rationale', 'N/A')}\n\n**日本語訳:**\n{r_trans}", visible=bool(entry.get('rationale'))), |
|
"rationale_img": gr.Image(entry.get('rationale_image'), label="解説画像", visible=bool(entry.get('rationale_image'))), |
|
"metadata_md": gr.Markdown(f"**ID:** `{entry['id']}`<br>**分野:** `{entry['raw_subject']}`<br>**回答タイプ:** `{entry['answer_type']}`", visible=True), |
|
"json_output": gr.JSON({k: str(v) for k, v in entry.items()}, label="元のデータ", visible=True) |
|
} |
|
|
|
|
|
return ( |
|
outputs["question_md"], |
|
outputs["question_img"], |
|
outputs["answer_md"], |
|
outputs["rationale_md"], |
|
outputs["rationale_img"], |
|
outputs["metadata_md"], |
|
outputs["json_output"] |
|
) |
|
|
|
|
|
def create_demo(): |
|
|
|
load_and_process_data() |
|
|
|
with gr.Blocks(theme=gr.themes.Soft(), title="HLE Dataset Viewer") as demo: |
|
gr.Markdown("# Humanity's Last Exam (HLE) Dataset Viewer") |
|
gr.Markdown("Hugging Face `cais/hle`データセットを探索し、日本語訳を確認できます。") |
|
|
|
with gr.Row(): |
|
with gr.Column(scale=1, min_width=350): |
|
gr.Markdown("## 操作パネル") |
|
category_dd = gr.Dropdown( |
|
choices=["全カテゴリ"] + sorted(category_counts.index.tolist()), |
|
value="全カテゴリ", |
|
label="1. カテゴリを選択" |
|
) |
|
question_dd = gr.Dropdown(label="2. 問題を選択", interactive=False) |
|
|
|
gr.Markdown("### カテゴリ別問題数") |
|
gr.Dataframe(value=pd.DataFrame(category_counts).reset_index(), headers=['カテゴリ', '問題数'], interactive=False) |
|
|
|
with gr.Column(scale=3): |
|
|
|
metadata_md = gr.Markdown(visible=False) |
|
question_md = gr.Markdown(visible=False) |
|
question_img = gr.Image(label="質問画像", visible=False) |
|
answer_md = gr.Markdown(visible=False) |
|
rationale_md = gr.Markdown(visible=False) |
|
rationale_img = gr.Image(label="解説画像", visible=False) |
|
json_output = gr.JSON(label="元のデータ", visible=False) |
|
|
|
|
|
category_dd.change(fn=on_category_change, inputs=category_dd, outputs=question_dd) |
|
question_dd.change(fn=on_question_change, inputs=question_dd, outputs=[ |
|
question_md, question_img, answer_md, rationale_md, rationale_img, metadata_md, json_output |
|
]) |
|
|
|
|
|
demo.load(fn=on_category_change, inputs=category_dd, outputs=question_dd) |
|
|
|
return demo |
|
|
|
|
|
if __name__ == "__main__": |
|
app = create_demo() |
|
app.launch() |