123156283104512131467816171819202122910112412131427282915161718192031323334353637383940414243444546474849505152535455565721596061222363646566676869707172737475767778798081828384858687882425269091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414527282930313233343536373839404142147148149150151152434445461551561574748495051159160525354555657585960616263646566676869707172737475767778798081828384858616316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424587888990919293949524724824925025125225396979899100255256257258259260261262263264265266101102103269270271272273274275276277278279280281104283284105106107108109110286287111112113289290291114115116117118119120121122294295296297298299300301302303304305306307123124309310311312313314315316317318319320321322323125126127128129130131132133134135136137138139140141142143144145325326327146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192330331332333334335336337193194195196197198199200201341202203204205206207208"""图表问答数据集审核系统 - Gradio 5.x 应用"""import gradio as grfrom data_manager import DataManager, data_managerfrom typing import Dict, List, Optional, Tuple, Anyimport jsonimport osimport base64import tempfilefrom data_manager import data_manager
# ============== 全局状态 ==============
class AppState:# ============== 状态管理 (封装逻辑) ==============class ReviewState: def __init__(self): self.current_source: str = "" self.current_chart_type: str = "" self.current_chart_id: str = "" self.current_model: str = "" self.all_paths: List[Dict] = [] self.current_index: int = -1 self.refresh_paths() self.all_paths = [] self.current_idx = -1 def refresh_paths(self): def sync_paths(self): self.all_paths = data_manager.get_all_chart_paths() def get_current_path(self) -> Optional[Dict]: if 0 <= self.current_index < len(self.all_paths): return self.all_paths[self.current_index] def get_nav_target(self, direction): new_idx = self.current_idx + direction if 0 <= new_idx < len(self.all_paths): self.current_idx = new_idx return self.all_paths[new_idx] return None def set_position(self, source: str, chart_type: str, chart_id: str, model: str): self.current_source = source self.current_chart_type = chart_type self.current_chart_id = chart_id self.current_model = model for i, path in enumerate(self.all_paths): if (path['source'] == source and path['chart_type'] == chart_type and path['chart_id'] == chart_id and path['model'] == model): self.current_index = i break def navigate(self, direction: int) -> bool: new_index = self.current_index + direction if 0 <= new_index < len(self.all_paths): self.current_index = new_index path = self.all_paths[new_index] self.current_source = path['source'] self.current_chart_type = path['chart_type'] self.current_chart_id = path['chart_id'] self.current_model = path['model'] return True return False
state = AppState()
# 当前 QA 数据缓存(使用简单全局变量,避免 gr.State 的问题)_current_qa_list: List = []_current_reviews: Dict = {}nav_state = ReviewState()
# ============== UI 更新函数 ==============
def init_dataset(): structure = data_manager.get_dataset_structure() sources = list(structure.get('sources', {}).keys()) return gr.Dropdown(choices=sources, value=sources[0] if sources else None)
def update_chart_type_dropdown(source: str): state.current_source = source structure = data_manager.get_dataset_structure() chart_types = list(structure.get('sources', {}).get(source, {}).get('chart_types', {}).keys()) return gr.Dropdown(choices=chart_types, value=chart_types[0] if chart_types else None)
def update_chart_dropdown(source: str, chart_type: str): state.current_source = source state.current_chart_type = chart_type charts = data_manager.get_chart_list(source, chart_type) structure = data_manager.get_dataset_structure() ct_data = structure.get('sources', {}).get(source, {}).get('chart_types', {}).get(chart_type, {}) models = ct_data.get('models', []) return ( gr.Dropdown(choices=charts, value=charts[0] if charts else None), gr.Dropdown(choices=models, value=models[0] if models else None) )
def create_embedded_html(html_content: str, chart_id: str = "") -> str:# ============== 工具函数 ==============def to_html_frame(html_content): if not html_content: return f'''<div style="display:flex;flex-direction:column;min-height:750px;align-items:center;justify-content:center;background:#fafafa;border:2px dashed #ddd;border-radius:12px;"> <div style="font-size:48px;margin-bottom:16px;">📭</div> <div style="font-size:18px;color:#666;">暂无图表内容</div> <div style="font-size:14px;color:#999;margin-top:8px;">ID: {chart_id}</div> </div>''' html_base64 = base64.b64encode(html_content.encode('utf-8')).decode('utf-8') return f'<iframe src="data:text/html;base64,{html_base64}" style="width:100%;height:750px;border:1px solid #e0e0e0;border-radius:8px;background:#fff;" sandbox="allow-scripts allow-same-origin"></iframe>'
def load_chart_data(source: str, chart_type: str, chart_id: str, model: str): global _current_qa_list, _current_reviews if not all([source, chart_type, chart_id, model]): _current_qa_list = [] _current_reviews = {} return ( create_embedded_html(""), "### 请选择图表", "等待加载...", "请选择图表", gr.Radio(choices=[], value=None), "" ) state.set_position(source, chart_type, chart_id, model) chart_data = data_manager.get_chart_data(source, chart_type, chart_id) html_content = chart_data.get('html_content', '') label_info = chart_data.get('label_info', {}) embedded_html = create_embedded_html(html_content, chart_id) # 格式化标签 if label_info: label_lines = ["### 📝 图表信息", ""] for k, v in label_info.items(): label_lines.append(f"**{k}**: {v}") label_text = "\n".join(label_lines) else: label_text = "### ⚠️ 暂无标签信息" # 获取 QA 数据 qa_list = data_manager.get_qa_list(source, chart_type, model, chart_id) _current_qa_list = [{"id": qa.id, "question": qa.question, "answer": qa.answer} for qa in qa_list] # 获取审核记录 reviews = data_manager.get_reviews_by_chart(chart_id, model) _current_reviews = {r['qa_id']: r for r in reviews} # 统计 stats = data_manager.get_review_stats() status_msg = f"已审核: {stats['total']} | ✅正确: {stats['correct']} | ❌错误: {stats['incorrect']} | ✏️需修改: {stats['needs_modification']}" progress_msg = f"进度: {state.current_index + 1} / {len(state.all_paths)}" qa_choices = [f"Q{i+1}: {qa.question[:40]}..." for i, qa in enumerate(qa_list)] debug_info = f"📁 {source}/{chart_type}/{chart_id} | HTML: {len(html_content)} 字符" return '<div style="padding:20px;text-align:center;">请选择数据进行加载</div>' b64_content = base64.b64encode(html_content.encode('utf-8')).decode('utf-8') return f'<iframe src="data:text/html;base64,{b64_content}" style="width:100%;height:600px;border:none;"></iframe>'
# ============== 交互逻辑 ==============def handle_source_change(source): struct = data_manager.get_dataset_structure() types = list(struct.get('sources', {}).get(source, {}).get('chart_types', {}).keys()) # 默认选中第一个 return gr.update(choices=types, value=types[0] if types else None)
def handle_type_change(source, c_type): charts = data_manager.get_chart_list(source, c_type) struct = data_manager.get_dataset_structure() models = struct.get('sources', {}).get(source, {}).get('chart_types', {}).get(c_type, {}).get('models', []) return ( embedded_html, label_text, status_msg, progress_msg, gr.Radio(choices=qa_choices, value=qa_choices[0] if qa_choices else None), debug_info gr.update(choices=charts, value=charts[0] if charts else None), gr.update(choices=models, value=models[0] if models else None) )
def on_qa_select(qa_index_str: str): """选择 QA 后更新审核面板""" global _current_qa_list, _current_reviewsdef handle_load(source, c_type, c_id, model): if not all([source, c_type, c_id, model]): # 必须返回 8 个 update 对象以匹配 outputs return [gr.update()] * 8 if not qa_index_str or not _current_qa_list: return ("", "", "", "correct", "", "", "", "") # 获取数据 chart_data = data_manager.get_chart_data(source, c_type, c_id) qa_list = data_manager.get_qa_list(source, c_type, model, c_id) stats = data_manager.get_review_stats() # 同步索引位置 nav_state.sync_paths() for i, p in enumerate(nav_state.all_paths): if p['chart_id'] == c_id and p['model'] == model: nav_state.current_idx = i break
# 准备 UI 数据 html_code = to_html_frame(chart_data.get('html_content', '')) meta_md = "\n".join([f"- **{k}**: {v}" for k, v in chart_data.get('label_info', {}).items()]) qa_json = json.dumps([{"id": q.id, "q": q.question, "a": q.answer} for q in qa_list]) stats_str = f"✅{stats['correct']} | ❌{stats['incorrect']} | 总{stats['total']}" prog_str = f"{nav_state.current_idx + 1} / {len(nav_state.all_paths)}" radio_choices = [f"Q{i+1}: {q.question[:20]}..." for i, q in enumerate(qa_list)] return [ html_code, meta_md, qa_json, stats_str, prog_str, f"{source}/{c_type}/{c_id}", gr.update(choices=radio_choices, value=radio_choices[0] if radio_choices else None), json.dumps({}) # 占位 review_store ]
def handle_qa_select(selection, qa_json): if not selection or not qa_json: return [""] * 8 try: idx = int(qa_index_str.split(":")[0].replace("Q", "")) - 1 qa = _current_qa_list[idx] review = _current_reviews.get(qa['id'], {}) return ( qa['id'], qa['question'], qa['answer'], review.get('status', 'correct'), review.get('issue_type', ''), review.get('modified_question', ''), review.get('modified_answer', ''), review.get('comment', '') ) except Exception as e: print(f"Error: {e}") return ("", "", "", "correct", "", "", "", "")
def navigate_prev(): if state.navigate(-1): path = state.get_current_path() if path: return ( gr.Dropdown(value=path['source']), gr.Dropdown(value=path['chart_type']), gr.Dropdown(value=path['chart_id']), gr.Dropdown(value=path['model']) ) return (gr.Dropdown(), gr.Dropdown(), gr.Dropdown(), gr.Dropdown())
def navigate_next(): if state.navigate(1): path = state.get_current_path() if path: return ( gr.Dropdown(value=path['source']), gr.Dropdown(value=path['chart_type']), gr.Dropdown(value=path['chart_id']), gr.Dropdown(value=path['model']) ) return (gr.Dropdown(), gr.Dropdown(), gr.Dropdown(), gr.Dropdown())
def save_review_handler(qa_id, chart_id, source, chart_type, model, orig_q, orig_a, status, mod_q, mod_a, issue_type, comment, reviewer): if not qa_id: return "❌ 请先选择问答对" data_manager.save_review({ "qa_id": qa_id, "chart_id": chart_id, "source": source, "chart_type": chart_type, "model": model, "original_question": orig_q, "original_answer": orig_a, "status": status, "modified_question": mod_q, "modified_answer": mod_a, "issue_type": issue_type, "comment": comment, "reviewer": reviewer }) # 更新缓存 global _current_reviews _current_reviews[qa_id] = { "qa_id": qa_id, "status": status, "issue_type": issue_type, "modified_question": mod_q, "modified_answer": mod_a, "comment": comment } stats = data_manager.get_review_stats() return f"✅ 已保存! 总计: {stats['total']} | ✅正确: {stats['correct']} | ❌错误: {stats['incorrect']}"
def export_reviews_handler(): reviews = data_manager.get_all_reviews() if not reviews: return None temp_path = os.path.join(tempfile.gettempdir(), "reviews_export.json") with open(temp_path, 'w', encoding='utf-8') as f: json.dump(reviews, f, ensure_ascii=False, indent=2) return temp_path
# ============== 创建 Gradio 界面 ==============
qas = json.loads(qa_json) idx = int(selection.split(":")[0][1:]) - 1 curr = qas[idx] return [curr['id'], curr['q'], curr['a'], "correct", "无", "", "", ""] except: return [""] * 8
# ============== UI 布局 ==============def create_ui(): custom_css = """ .chart-container { min-height: 780px; } .control-panel { background: #f8f9fa; padding: 12px; border-radius: 8px; margin-bottom: 8px; } """ with gr.Blocks(title="图表问答数据集审核系统", theme=gr.themes.Soft(), css=custom_css) as app: gr.Markdown("# 📊 图表问答数据集审核系统\n\n审核图表问答对,使用 ← → 按钮切换图表") with gr.Blocks(title="审核系统 V2", theme=gr.themes.Soft()) as demo: # 显式初始化 State,避免布尔值陷阱 qa_store = gr.State(value="[]") review_store = gr.State(value="{}") # 顶部状态 with gr.Row(): status_text = gr.Textbox(label="统计", interactive=False, scale=2) progress_text = gr.Textbox(label="进度", interactive=False, scale=1) # 导航栏 with gr.Row(): source_drop = gr.Dropdown(label="数据来源", choices=[], interactive=True, scale=1) type_drop = gr.Dropdown(label="图表类型", choices=[], interactive=True, scale=1) id_drop = gr.Dropdown(label="图表ID", choices=[], interactive=True, scale=1) model_drop = gr.Dropdown(label="模型", choices=[], interactive=True, scale=1) reviewer = gr.Textbox(label="审核人", value="admin", interactive=True, scale=1) gr.Markdown("## 📑 图表问答数据集审核终端") with gr.Row(): prev_btn = gr.Button("⬅️ 上一个", size="sm") next_btn = gr.Button("➡️ 下一个", size="sm") export_btn = gr.Button("📦 导出记录", variant="secondary", size="sm") download_file = gr.File(label="下载", visible=False) # 主内容 with gr.Row(): # 左侧:图表 with gr.Column(scale=6): html_display = gr.HTML(elem_classes=["chart-container"]) debug_info = gr.Textbox(label="调试", interactive=False, show_label=False) # 右侧:审核 with gr.Column(scale=4): with gr.Accordion("📝 图表元数据", open=False): label_display = gr.Markdown() with gr.Row(): src_dd = gr.Dropdown(label="数据源", choices=["None"]) typ_dd = gr.Dropdown(label="图表类型") id_dd = gr.Dropdown(label="图表 ID") mdl_dd = gr.Dropdown(label="评估模型") gr.Markdown("### ❓ 问答审核") qa_selector = gr.Radio(label="选择问答对", choices=[], interactive=True) chart_view = gr.HTML(value='<div style="height:500px; background:#f0f0f0;"></div>') path_info = gr.Text(label="当前路径", interactive=False) current_qa_id = gr.Textbox(visible=False, value="") orig_q = gr.Textbox(label="原始问题", interactive=False, lines=2) orig_a = gr.Textbox(label="原始答案", interactive=False) with gr.Column(scale=2): with gr.Group(): stats_txt = gr.Text(label="统计信息", interactive=False) prog_txt = gr.Text(label="审核进度", interactive=False) with gr.Accordion("元数据解析", open=False): meta_md = gr.Markdown()
gr.Markdown("---") status_radio = gr.Radio( label="判定结果", choices=[("✅ 正确", "correct"), ("❌ 错误", "incorrect"), ("✏️ 需修改", "needs_modification")], value="correct", interactive=True ) issue_type = gr.Dropdown( label="错误类型", choices=["答案错误", "问题模糊", "图文不符", "其他"], interactive=True ) mod_q = gr.Textbox(label="修改后问题", lines=2, interactive=True) mod_a = gr.Textbox(label="修改后答案", interactive=True) comment = gr.Textbox(label="备注", lines=2, interactive=True) qa_radio = gr.Radio(label="题目列表", choices=[]) save_btn = gr.Button("💾 保存审核", variant="primary") save_msg = gr.Textbox(label="", visible=False) # ===== 事件绑定 ===== app.load(fn=init_dataset, outputs=[source_drop]) source_drop.change(fn=update_chart_type_dropdown, inputs=[source_drop], outputs=[type_drop]) type_drop.change(fn=update_chart_dropdown, inputs=[source_drop, type_drop], outputs=[id_drop, model_drop]) load_outputs = [html_display, label_display, status_text, progress_text, qa_selector, debug_info] id_drop.change(fn=load_chart_data, inputs=[source_drop, type_drop, id_drop, model_drop], outputs=load_outputs) model_drop.change(fn=load_chart_data, inputs=[source_drop, type_drop, id_drop, model_drop], outputs=load_outputs) qa_outputs = [current_qa_id, orig_q, orig_a, status_radio, issue_type, mod_q, mod_a, comment] qa_selector.change(fn=on_qa_select, inputs=[qa_selector], outputs=qa_outputs) with gr.Group(): curr_qid = gr.Text(visible=False) q_disp = gr.Text(label="问题内容", lines=2) a_disp = gr.Text(label="标准答案") status_opt = gr.Radio( label="审核结论", choices=[("正确", "correct"), ("错误", "incorrect"), ("优化", "modified")], value="correct" ) err_type = gr.Dropdown(label="错误分类", choices=["无", "事实错误", "逻辑错误", "图表无法读取"]) comment = gr.Text(label="审核备注") save_btn = gr.Button("💾 提交单条审核", variant="primary") with gr.Row(): prev_btn = gr.Button("⬅️ 上一个图表") next_btn = gr.Button("➡️ 下一个图表")
# --- 事件绑定 --- nav_outputs = [source_drop, type_drop, id_drop, model_drop] prev_btn.click(fn=navigate_prev, outputs=nav_outputs) next_btn.click(fn=navigate_next, outputs=nav_outputs) # 初始化第一级 demo.load( fn=lambda: gr.update(choices=list(data_manager.get_dataset_structure().get('sources', {}).keys())), outputs=[src_dd] )
# 联动逻辑 src_dd.change(handle_source_change, inputs=[src_dd], outputs=[typ_dd]) typ_dd.change(handle_type_change, inputs=[src_dd, typ_dd], outputs=[id_dd, mdl_dd]) # 加载数据 (严格匹配 8 个 output) load_event_outputs = [chart_view, meta_md, qa_store, stats_txt, prog_txt, path_info, qa_radio, review_store] id_dd.change(handle_load, inputs=[src_dd, typ_dd, id_dd, mdl_dd], outputs=load_event_outputs) mdl_dd.change(handle_load, inputs=[src_dd, typ_dd, id_dd, mdl_dd], outputs=load_event_outputs)
# 题目切换 qa_radio.change( handle_qa_select, inputs=[qa_radio, qa_store], outputs=[curr_qid, q_disp, a_disp, status_opt, err_type, gr.State(), gr.State(), comment] )
# 导航逻辑 def navigate(direction): target = nav_state.get_nav_target(direction) if target: return [ gr.update(value=target['source']), gr.update(value=target['chart_type']), gr.update(value=target['chart_id']), gr.update(value=target['model']) ] return [gr.update()] * 4
prev_btn.click(lambda: navigate(-1), outputs=[src_dd, typ_dd, id_dd, mdl_dd]) next_btn.click(lambda: navigate(1), outputs=[src_dd, typ_dd, id_dd, mdl_dd])
# 保存逻辑 def quick_save(qid, cid, src, status, cmt): if not qid: return "无效操作" data_manager.save_review({ "qa_id": qid, "chart_id": cid, "source": src, "status": status, "comment": cmt }) return "已保存"
save_btn.click( fn=save_review_handler, inputs=[current_qa_id, id_drop, source_drop, type_drop, model_drop, orig_q, orig_a, status_radio, mod_q, mod_a, issue_type, comment, reviewer], outputs=[save_msg] ).then(fn=lambda: gr.Textbox(visible=True), outputs=[save_msg]) export_btn.click(fn=export_reviews_handler, outputs=[download_file]).then(fn=lambda: gr.File(visible=True), outputs=[download_file]) return app quick_save, inputs=[curr_qid, id_dd, src_dd, status_opt, comment], outputs=[gr.Text(visible=False)] )
return demo
if __name__ == "__main__": app = create_ui() app.launch(server_name="0.0.0.0", server_port=7860, show_api=False) # 强制禁用所有 API 预览功能 app.launch( server_name="0.0.0.0", server_port=7860, show_api=False, max_threads=10 )app.py+174-307