Heyyaha commited on
Commit
7683d22
·
verified ·
1 Parent(s): 5b0d392

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -21
app.py CHANGED
@@ -1,15 +1,16 @@
1
- import os
2
  import gradio as gr
 
 
3
  from gradio_client import Client
 
 
4
  import json
5
- from datetime import datetime
6
 
7
- # 使用 Hugging Face 提供的 Qwen 模型 API 端点
8
- client = Client("https://qwen-qwen-72b-chat-demo.hf.space/--replicas/ypz66/")
9
 
10
- # 输出文件夹
11
  OUTPUT_DIR = "outputs"
12
- os.makedirs(OUTPUT_DIR, exist_ok=True) # 创建输出目录
13
 
14
  # 拆解 JD 成任务
15
  def extract_task_from_jd(jd):
@@ -18,8 +19,8 @@ def extract_task_from_jd(jd):
18
  任务:(任务描述)
19
 
20
  JD: {jd}"""
21
- response = client.predict([message], api_name="/model_chat")
22
- return response[0].strip()
23
 
24
  # 基于任务生成三个解决方案
25
  def generate_solutions_from_task(task):
@@ -30,10 +31,11 @@ def generate_solutions_from_task(task):
30
  方案3:(内容)
31
 
32
  任务: {task}"""
33
- response = client.predict([message], api_name="/model_chat")
34
- solutions = response[0].split("\n")
 
35
  if len(solutions) < 3:
36
- return response[0], "(解析失败,显示原始回复)", ""
37
  s1 = solutions[0].strip()
38
  s2 = solutions[1].strip()
39
  s3 = solutions[2].strip()
@@ -44,39 +46,35 @@ def build_ui():
44
  with gr.Blocks() as demo:
45
  gr.Markdown("## 📌 JD 任务拆解 + 解决方案选择 Demo")
46
 
47
- # 输入 JD
48
  jd_input = gr.Textbox(label="输入 JD", placeholder="请输入岗位描述 JD")
49
  task_output = gr.Textbox(label="拆解出的测试任务", lines=2, interactive=False)
50
  generate_task_btn = gr.Button("🧠 拆解 JD 成任务")
51
 
52
- # 生成解决方案
53
  generate_solutions_btn = gr.Button("🚀 基于任务生成三个方案")
54
- sol1 = gr.Textbox(label="方案1", lines=10, interactive=False)
55
  sol2 = gr.Textbox(label="方案2", lines=10, interactive=False)
56
  sol3 = gr.Textbox(label="方案3", lines=10, interactive=False)
57
 
58
  select_radio = gr.Radio(choices=["1", "2", "3"], label="请选择你最满意的解决方案编号")
 
 
59
 
60
- # 用户输入方案的文本框
61
  user_solution = gr.Textbox(lines=6, label="📄 填写你自己的解决方案(可选)")
62
 
63
- # 提交按钮
64
  submit = gr.Button("✅ 提交 RLHF 数据")
65
  feedback = gr.Textbox(label="系统反馈", interactive=False)
66
 
67
  task_state = gr.State()
68
 
69
- # 处理 JD 拆解成任务
70
  def handle_task_gen(jd_text):
71
  task = extract_task_from_jd(jd_text)
72
  return task, task
73
 
74
- # 生成解决方案
75
  def handle_solutions_gen(task_text):
76
  s1, s2, s3 = generate_solutions_from_task(task_text)
77
  return s1, s2, s3
78
 
79
- # 提交反馈
80
  def handle_submit(selected_idx, user_input_text, comment_text, task_text):
81
  record = {
82
  "task": task_text,
@@ -93,14 +91,12 @@ def build_ui():
93
  except Exception as e:
94
  return f"❌ 保存失败:{str(e)}"
95
 
96
- # 绑定按钮事件
97
  generate_task_btn.click(fn=handle_task_gen, inputs=[jd_input], outputs=[task_output, task_state])
98
  generate_solutions_btn.click(fn=handle_solutions_gen, inputs=[task_state], outputs=[sol1, sol2, sol3])
99
  submit.click(fn=handle_submit, inputs=[select_radio, user_solution, comment, task_state], outputs=[feedback])
100
 
101
  return demo
102
 
103
- # 启动 Gradio 应用
104
  if __name__ == "__main__":
105
  demo = build_ui()
106
  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
+ # 初始化任务生成客户端(Qwen API
10
+ taskgen_client = Client("https://qwen-qwen-72b-chat-demo.hf.space/-replicas/ypz66/")
11
 
 
12
  OUTPUT_DIR = "outputs"
13
+ os.makedirs(OUTPUT_DIR, exist_ok=True)
14
 
15
  # 拆解 JD 成任务
16
  def extract_task_from_jd(jd):
 
19
  任务:(任务描述)
20
 
21
  JD: {jd}"""
22
+ response = taskgen_client.predict(message=message, api_name="/model_chat")
23
+ return response.strip() # 直接返回原始响应内容,便于调试
24
 
25
  # 基于任务生成三个解决方案
26
  def generate_solutions_from_task(task):
 
31
  方案3:(内容)
32
 
33
  任务: {task}"""
34
+ response = taskgen_client.predict(message=message, api_name="/model_chat")
35
+ # fallback:若正则提取失败,直接展示原始响应
36
+ solutions = re.findall(r"方案[123][::]\s*(.*)", response)
37
  if len(solutions) < 3:
38
+ return response.strip(), "(解析失败,显示原始回复)", ""
39
  s1 = solutions[0].strip()
40
  s2 = solutions[1].strip()
41
  s3 = solutions[2].strip()
 
46
  with gr.Blocks() as demo:
47
  gr.Markdown("## 📌 JD 任务拆解 + 解决方案选择 Demo")
48
 
 
49
  jd_input = gr.Textbox(label="输入 JD", placeholder="请输入岗位描述 JD")
50
  task_output = gr.Textbox(label="拆解出的测试任务", lines=2, interactive=False)
51
  generate_task_btn = gr.Button("🧠 拆解 JD 成任务")
52
 
 
53
  generate_solutions_btn = gr.Button("🚀 基于任务生成三个方案")
54
+ sol1 = gr.Textbox(label="方案1 / 或原始回复", lines=10, interactive=False)
55
  sol2 = gr.Textbox(label="方案2", lines=10, interactive=False)
56
  sol3 = gr.Textbox(label="方案3", lines=10, interactive=False)
57
 
58
  select_radio = gr.Radio(choices=["1", "2", "3"], label="请选择你最满意的解决方案编号")
59
+
60
+ comment = gr.Textbox(lines=4, label="📝 请对选择的方案填写选择理由或批注该方案的优缺点")
61
 
 
62
  user_solution = gr.Textbox(lines=6, label="📄 填写你自己的解决方案(可选)")
63
 
64
+
65
  submit = gr.Button("✅ 提交 RLHF 数据")
66
  feedback = gr.Textbox(label="系统反馈", interactive=False)
67
 
68
  task_state = gr.State()
69
 
 
70
  def handle_task_gen(jd_text):
71
  task = extract_task_from_jd(jd_text)
72
  return task, task
73
 
 
74
  def handle_solutions_gen(task_text):
75
  s1, s2, s3 = generate_solutions_from_task(task_text)
76
  return s1, s2, s3
77
 
 
78
  def handle_submit(selected_idx, user_input_text, comment_text, task_text):
79
  record = {
80
  "task": task_text,
 
91
  except Exception as e:
92
  return f"❌ 保存失败:{str(e)}"
93
 
 
94
  generate_task_btn.click(fn=handle_task_gen, inputs=[jd_input], outputs=[task_output, task_state])
95
  generate_solutions_btn.click(fn=handle_solutions_gen, inputs=[task_state], outputs=[sol1, sol2, sol3])
96
  submit.click(fn=handle_submit, inputs=[select_radio, user_solution, comment, task_state], outputs=[feedback])
97
 
98
  return demo
99
 
 
100
  if __name__ == "__main__":
101
  demo = build_ui()
102
  demo.launch()