Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,88 +1,89 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
from gradio_client import Client
|
3 |
import re
|
4 |
-
|
5 |
import json
|
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("##
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
25 |
feedback = gr.Textbox(label="系统反馈", interactive=False)
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
JD: {jd_text}
|
35 |
-
请提取出以下内容:
|
36 |
-
1. 任务描述:可以通过具体操作测试的任务
|
37 |
-
2. 硬技能:为测试该任务所需的硬技能(例如编程语言、工具、技术等)"""
|
38 |
-
|
39 |
-
response = taskgen_client.predict(message=message, api_name="/chat")
|
40 |
-
|
41 |
-
# 任务描述和可测试硬技能的提取
|
42 |
-
task_match = re.search(r"任务描述[::](.*?)(、|。)", response)
|
43 |
-
skills_match = re.search(r"硬技能[::](.*?)(、|。)", response)
|
44 |
-
|
45 |
-
task = task_match.group(1).strip() if task_match else "任务解析失败"
|
46 |
-
testable_skills = skills_match.group(1).strip() if skills_match else "无硬技能信息"
|
47 |
-
|
48 |
-
return task, testable_skills, jd_text, task, testable_skills
|
49 |
-
|
50 |
-
def handle_generate_solution(task):
|
51 |
-
# 步骤 2:生成解法选项
|
52 |
-
solution_message = f"""根据以下任务描述,生成三个解决方案选项:
|
53 |
-
任务描述:{task}
|
54 |
-
请为该任务生成三个具体的解决方案,并列出其优缺点。"""
|
55 |
-
|
56 |
-
solution_response = taskgen_client.predict(message=solution_message, api_name="/chat")
|
57 |
-
|
58 |
-
# 解析解法选项(假设模型返回每个选项单独一行)
|
59 |
-
solution_options = solution_response.split("\n") # 假设解法选项按行分开
|
60 |
-
return solution_options
|
61 |
-
|
62 |
-
def handle_submit(selected_option, comment_text, final_solution_text):
|
63 |
record = {
|
64 |
-
"task":
|
65 |
-
"
|
66 |
-
"
|
67 |
"comment": comment_text,
|
68 |
-
"final_solution": final_solution_text,
|
69 |
"timestamp": datetime.now().isoformat()
|
70 |
}
|
71 |
try:
|
72 |
-
with open("
|
73 |
json.dump(record, f, ensure_ascii=False)
|
74 |
f.write("\n")
|
75 |
-
|
76 |
except Exception as e:
|
77 |
-
|
78 |
-
return feedback_text
|
79 |
|
80 |
-
|
81 |
-
|
82 |
-
submit.click(fn=handle_submit, inputs=[solution_options, comment, final_solution], outputs=[feedback])
|
83 |
|
84 |
return demo
|
85 |
|
86 |
if __name__ == "__main__":
|
87 |
demo = build_ui()
|
88 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import os
|
3 |
+
from datetime import datetime
|
4 |
from gradio_client import Client
|
5 |
import re
|
6 |
+
import uuid
|
7 |
import json
|
8 |
|
9 |
# 初始化任务生成客户端(腾讯混元 Space)
|
10 |
taskgen_client = Client("tencent/Hunyuan-Large")
|
11 |
|
12 |
+
OUTPUT_DIR = "outputs"
|
13 |
+
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
14 |
+
|
15 |
+
# 拆解 JD 成任务,并生成三个解决方案
|
16 |
+
def generate_solutions_from_jd(jd):
|
17 |
+
message = f"""你是一个岗位分析助手,请根据以下JD内容:\n
|
18 |
+
1. 首先识别一个可以用来测试岗位候选人核心能力的具体任务;
|
19 |
+
2. 然后为这个任务生成三个不同的解决方案建议。
|
20 |
+
|
21 |
+
请严格以如下格式输出:
|
22 |
+
任务:(任务描述)\n方案1:(内容)\n方案2:(内容)\n方案3:(内容)\n
|
23 |
+
JD: {jd}
|
24 |
+
"""
|
25 |
+
response = taskgen_client.predict(message=message, api_name="/chat")
|
26 |
+
|
27 |
+
task_match = re.search(r"任务[::](.*)", response)
|
28 |
+
solution1 = re.search(r"方案1[::](.*)", response)
|
29 |
+
solution2 = re.search(r"方案2[::](.*)", response)
|
30 |
+
solution3 = re.search(r"方案3[::](.*)", response)
|
31 |
+
|
32 |
+
task = task_match.group(1).strip() if task_match else "任务解析失败"
|
33 |
+
s1 = solution1.group(1).strip() if solution1 else "方案1解析失败"
|
34 |
+
s2 = solution2.group(1).strip() if solution2 else "方案2解析失败"
|
35 |
+
s3 = solution3.group(1).strip() if solution3 else "方案3解析失败"
|
36 |
+
|
37 |
+
return task, s1, s2, s3
|
38 |
+
|
39 |
# 构建 Gradio UI
|
40 |
def build_ui():
|
41 |
with gr.Blocks() as demo:
|
42 |
+
gr.Markdown("## 📌 JD 任务拆解 + 解决方案选择 Demo")
|
43 |
+
|
44 |
+
jd_input = gr.Textbox(label="输入 JD", placeholder="请输入岗位描述 JD")
|
45 |
+
task_output = gr.Textbox(label="拆解出的测试任务", interactive=False)
|
46 |
+
|
47 |
+
generate_button = gr.Button("🚀 拆解任务并生成方案")
|
48 |
+
|
49 |
+
sol1 = gr.Textbox(label="方案1", interactive=False)
|
50 |
+
sol2 = gr.Textbox(label="方案2", interactive=False)
|
51 |
+
sol3 = gr.Textbox(label="方案3", interactive=False)
|
52 |
+
|
53 |
+
select_radio = gr.Radio(choices=["1", "2", "3"], label="请选择你最满意的解决方案编号")
|
54 |
+
user_solution = gr.Textbox(lines=4, label="📄 或者填写你自己的解决方案(可选)")
|
55 |
+
comment = gr.Textbox(lines=3, label="📝 请选择的理由或批注")
|
56 |
+
|
57 |
+
submit = gr.Button("✅ 提交 RLHF 数据")
|
58 |
feedback = gr.Textbox(label="系统反馈", interactive=False)
|
59 |
|
60 |
+
task_state = gr.State()
|
61 |
+
|
62 |
+
def handle_jd_input(jd_text):
|
63 |
+
task, s1, s2, s3 = generate_solutions_from_jd(jd_text)
|
64 |
+
return task, s1, s2, s3, task
|
65 |
+
|
66 |
+
def handle_submit(selected_idx, user_input_text, comment_text, task_text):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
record = {
|
68 |
+
"task": task_text,
|
69 |
+
"selected_index": selected_idx,
|
70 |
+
"user_solution": user_input_text,
|
71 |
"comment": comment_text,
|
|
|
72 |
"timestamp": datetime.now().isoformat()
|
73 |
}
|
74 |
try:
|
75 |
+
with open("rlhf_jd_data.jsonl", "a", encoding="utf-8") as f:
|
76 |
json.dump(record, f, ensure_ascii=False)
|
77 |
f.write("\n")
|
78 |
+
return f"✅ 数据已保存,选择方案 {selected_idx}"
|
79 |
except Exception as e:
|
80 |
+
return f"❌ 保存失败:{str(e)}"
|
|
|
81 |
|
82 |
+
generate_button.click(fn=handle_jd_input, inputs=[jd_input], outputs=[task_output, sol1, sol2, sol3, task_state])
|
83 |
+
submit.click(fn=handle_submit, inputs=[select_radio, user_solution, comment, task_state], outputs=[feedback])
|
|
|
84 |
|
85 |
return demo
|
86 |
|
87 |
if __name__ == "__main__":
|
88 |
demo = build_ui()
|
89 |
+
demo.launch()
|