Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,115 +1,15 @@
|
|
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 |
-
#
|
10 |
client = Client("https://qwen-qwen-72b-chat-demo.hf.space/--replicas/ypz66/")
|
11 |
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
-
#
|
16 |
-
|
17 |
-
message = f"""你是一个岗位分析助手,请根据以下JD内容提取一个可以用来测试候选人核心能力的具体任务。
|
18 |
-
请以如下格式回复:
|
19 |
-
任务:(任务描述)
|
20 |
-
|
21 |
-
JD: {jd}"""
|
22 |
-
result = client.predict(
|
23 |
-
message, # 输入 JD 描述
|
24 |
-
[["Hello!", None]], # Chatbot 输入
|
25 |
-
"Hello!!", # 作为参数 9 的文本
|
26 |
-
api_name="/model_chat" # API 名称
|
27 |
-
)
|
28 |
-
return result.strip() # 直接返回原始响应内容,便于调试
|
29 |
-
|
30 |
-
# 基于任务生成三个解决方案
|
31 |
-
def generate_solutions_from_task(task):
|
32 |
-
message = f"""你是一个解决方案生成助手,请根据以下任务设计三种不同的实现思路。
|
33 |
-
请严格按如下格式输出:
|
34 |
-
方案1:(内容)
|
35 |
-
方案2:(内容)
|
36 |
-
方案3:(内容)
|
37 |
-
|
38 |
-
任务: {task}"""
|
39 |
-
result = client.predict(
|
40 |
-
message, # 输入任务描述
|
41 |
-
[["Hello!", None]], # Chatbot 输入
|
42 |
-
"Hello!!", # 作为参数 9 的文本
|
43 |
-
api_name="/model_chat" # API 名称
|
44 |
-
)
|
45 |
-
|
46 |
-
# 使用正则表达式提取方案
|
47 |
-
solutions = re.findall(r"方案[123][::]\s*(.*)", result)
|
48 |
-
|
49 |
-
if len(solutions) < 3:
|
50 |
-
return result.strip(), "(解析失败,显示原始回复)", ""
|
51 |
-
|
52 |
-
s1 = solutions[0].strip()
|
53 |
-
s2 = solutions[1].strip()
|
54 |
-
s3 = solutions[2].strip()
|
55 |
-
return s1, s2, s3
|
56 |
-
|
57 |
-
# 构建 Gradio UI
|
58 |
-
def build_ui():
|
59 |
-
with gr.Blocks() as demo:
|
60 |
-
gr.Markdown("## 📌 JD 任务拆解 + 解决方案选择 Demo")
|
61 |
-
|
62 |
-
jd_input = gr.Textbox(label="输入 JD", placeholder="请输入岗位描述 JD")
|
63 |
-
task_output = gr.Textbox(label="拆解出的测试任务", lines=2, interactive=False)
|
64 |
-
generate_task_btn = gr.Button("🧠 拆解 JD 成任务")
|
65 |
-
|
66 |
-
generate_solutions_btn = gr.Button("🚀 基于任务生成三个方案")
|
67 |
-
sol1 = gr.Textbox(label="方案1 / 或原始回复", lines=10, interactive=False)
|
68 |
-
sol2 = gr.Textbox(label="方案2", lines=10, interactive=False)
|
69 |
-
sol3 = gr.Textbox(label="方案3", lines=10, interactive=False)
|
70 |
-
|
71 |
-
select_radio = gr.Radio(choices=["1", "2", "3"], label="请选择你最满意的解决方案编号")
|
72 |
-
|
73 |
-
comment = gr.Textbox(lines=4, label="📝 请对选择的方案填写选择理由或批注该方案的优缺点")
|
74 |
-
|
75 |
-
user_solution = gr.Textbox(lines=6, label="📄 填写你自己的解决方案(可选)")
|
76 |
-
|
77 |
-
|
78 |
-
submit = gr.Button("✅ 提交 RLHF 数据")
|
79 |
-
feedback = gr.Textbox(label="系统反馈", interactive=False)
|
80 |
-
|
81 |
-
task_state = gr.State()
|
82 |
-
|
83 |
-
def handle_task_gen(jd_text):
|
84 |
-
task = extract_task_from_jd(jd_text)
|
85 |
-
return task, task
|
86 |
-
|
87 |
-
def handle_solutions_gen(task_text):
|
88 |
-
s1, s2, s3 = generate_solutions_from_task(task_text)
|
89 |
-
return s1, s2, s3
|
90 |
-
|
91 |
-
def handle_submit(selected_idx, user_input_text, comment_text, task_text):
|
92 |
-
record = {
|
93 |
-
"task": task_text,
|
94 |
-
"selected_index": selected_idx,
|
95 |
-
"user_solution": user_input_text,
|
96 |
-
"comment": comment_text,
|
97 |
-
"timestamp": datetime.now().isoformat()
|
98 |
-
}
|
99 |
-
try:
|
100 |
-
with open("rlhf_jd_data.jsonl", "a", encoding="utf-8") as f:
|
101 |
-
json.dump(record, f, ensure_ascii=False)
|
102 |
-
f.write("\n")
|
103 |
-
return f"✅ 数据已保存,选择方案 {selected_idx}"
|
104 |
-
except Exception as e:
|
105 |
-
return f"❌ 保存失败:{str(e)}"
|
106 |
-
|
107 |
-
generate_task_btn.click(fn=handle_task_gen, inputs=[jd_input], outputs=[task_output, task_state])
|
108 |
-
generate_solutions_btn.click(fn=handle_solutions_gen, inputs=[task_state], outputs=[sol1, sol2, sol3])
|
109 |
-
submit.click(fn=handle_submit, inputs=[select_radio, user_solution, comment, task_state], outputs=[feedback])
|
110 |
-
|
111 |
-
return demo
|
112 |
-
|
113 |
-
if __name__ == "__main__":
|
114 |
-
demo = build_ui()
|
115 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
1 |
from gradio_client import Client
|
|
|
|
|
|
|
2 |
|
3 |
+
# 初始化客户端连接到 Qwen API
|
4 |
client = Client("https://qwen-qwen-72b-chat-demo.hf.space/--replicas/ypz66/")
|
5 |
|
6 |
+
# 发送请求并获取结果
|
7 |
+
result = client.predict(
|
8 |
+
"Hello!!", # 输入 'Input' Textbox 组件中的内容
|
9 |
+
[["Hello!", None]], # Chatbot 输入的内容(Tuple 格式)
|
10 |
+
"Hello!!", # 作为 'parameter_9' 组件的文本内容
|
11 |
+
api_name="/model_chat" # 指定 API 名称
|
12 |
+
)
|
13 |
|
14 |
+
# 打印返回的结果
|
15 |
+
print(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|