Heyyaha commited on
Commit
6a0ce2e
·
verified ·
1 Parent(s): 0babd8c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -0
app.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from gradio_client import Client
3
+ import re
4
+ import json
5
+ from datetime import datetime
6
+
7
+ # 初始化任务生成客户端(腾讯混元 Space)
8
+ taskgen_client = Client("tencent/Hunyuan-Large")
9
+
10
+ # 构建 Gradio UI
11
+ def build_ui():
12
+ with gr.Blocks() as demo:
13
+ gr.Markdown("## 📝 JD 任务解析与解法选择 Demo")
14
+
15
+ jd_text = gr.Textbox(label="职位描述 (JD)", placeholder="请输入职位描述文本,如‘负责销售数据分析与报告撰写’")
16
+ task = gr.Textbox(label="提取的任务", interactive=False)
17
+ solution_options = gr.CheckboxGroup(label="任务解法选项", choices=["选项1", "选项2", "选项3"], type="index")
18
+ final_solution = gr.Textbox(label="您的最终解法", lines=3, placeholder="请提供您的解法")
19
+ comment = gr.Textbox(label="📝 选择理由", lines=3, placeholder="为什么选择这个解法")
20
+
21
+ generate_task = gr.Button("🔍 解析 JD 提取任务")
22
+ submit = gr.Button("✅ 提交选择与解法(保存为RLHF数据)")
23
+ feedback = gr.Textbox(label="系统反馈", interactive=False)
24
+
25
+ jd_state = gr.State("")
26
+ task_state = gr.State("")
27
+ solution_state = gr.State([])
28
+
29
+ def handle_jd_submit(jd_text):
30
+ # 从 JD 中提取任务描述
31
+ message = f"""你是一个任务解析助手,请根据以下 JD 提取出相关的任务:
32
+ JD: {jd_text}
33
+ 请提取一个任务描述并用简洁明了的语言表达。
34
+ """
35
+ response = taskgen_client.predict(message=message, api_name="/chat")
36
+ task_match = re.search(r"任务[::](.*)", response)
37
+
38
+ task = task_match.group(1).strip() if task_match else "任务解析失败"
39
+ # 生成解法选项
40
+ solution_options = [
41
+ f"解法1:基于数据分析工具,生成详细报告",
42
+ f"解法2:通过市场调研,提出优化建议",
43
+ f"解法3:结合用户需求,制定定制化销售策略"
44
+ ]
45
+
46
+ return task, solution_options, jd_text, task, solution_options
47
+
48
+ def handle_submit(selected_option, comment_text, final_solution_text):
49
+ record = {
50
+ "task": task_state,
51
+ "solution_options": solution_state,
52
+ "selected_option": selected_option,
53
+ "comment": comment_text,
54
+ "final_solution": final_solution_text,
55
+ "timestamp": datetime.now().isoformat()
56
+ }
57
+ try:
58
+ with open("rlhf_data.jsonl", "a", encoding="utf-8") as f:
59
+ json.dump(record, f, ensure_ascii=False)
60
+ f.write("\n")
61
+ feedback_text = f"✅ RLHF 数据已保存,选择了解法 {selected_option}"
62
+ except Exception as e:
63
+ feedback_text = f"❌ 保存失败:{str(e)}"
64
+ return feedback_text
65
+
66
+ generate_task.click(fn=handle_jd_submit, inputs=[jd_text], outputs=[task, solution_options, jd_state, task_state, solution_state])
67
+ submit.click(fn=handle_submit, inputs=[solution_options, comment, final_solution], outputs=[feedback])
68
+
69
+ return demo
70
+
71
+ if __name__ == "__main__":
72
+ demo = build_ui()
73
+ demo.launch()