# app.py —— 基于 HF issues 修复版(绕过 tokenizer NoneType bug)
import os
import gradio as gr
from transformers import GPT2LMHeadModel, GPT2Tokenizer, TextDataset, DataCollatorForLanguageModeling, Trainer, TrainingArguments
MODEL_DIR = "./fine_tuned_model"
BASE_MODEL = "uer/gpt2-chinese-cluecorpussmall"
def finetune_model():
if not os.path.exists("genealogy.txt"):
return "错误:genealogy.txt 缺失!\n1. Files 标签上传家谱\n2. 改名为 genealogy.txt\n3. Commit 后重试"
if os.path.exists(MODEL_DIR):
return "已微调完成!切聊天标签"
try:
with open("genealogy.txt", "r", encoding="utf-8") as f:
text = f.read()
examples = []
for block in text.split("——")[1:20]:
if "冯" not in block: continue
name = born = place = "未知"
lines = block.split("\n")
for line in lines:
line = line.strip()
if "冯" in line and len(line) < 25 and name == "未知":
name = line.split()[0]
…