aimaswx commited on
Commit
e28d550
·
verified ·
1 Parent(s): 73d37ca

Upload 4 files

Browse files
Files changed (4) hide show
  1. batch_query.py +38 -0
  2. sdoc_request.py +43 -0
  3. streamlit_sdoc_analysis.py +70 -0
  4. tianshu.py +64 -0
batch_query.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sdoc_request import get_tianshu_response
2
+ import pandas as pd
3
+ import time
4
+ import json
5
+
6
+ # 问题数据导入
7
+ sdoc_df = pd.read_excel("sdoc直播观众提问和回复.xlsx")
8
+ questions = sdoc_df["描述"].to_list()
9
+
10
+ # AI智能分析
11
+ categories = []
12
+ modules = []
13
+ summaries = []
14
+
15
+ for qs in questions:
16
+ if qs and len(qs)>0:
17
+ time.sleep(1)
18
+ params = {"HOA_USERINPUT": qs}
19
+ temp_result = get_tianshu_response(params)
20
+ try:
21
+ result = json.loads(temp_result)
22
+ except json.JSONDecodeError:
23
+ result = {"类别":'', "模块":'', "问题归类": ''}
24
+ print("not json!")
25
+ else:
26
+ result = {"类别":'', "模块":'', "问题归类": ''}
27
+ print("question:", qs)
28
+ print("answer:", result)
29
+ categories.append(result["类别"])
30
+ modules.append(result["模块"])
31
+ summaries.append(result["问题归类"])
32
+
33
+ # 结果数据整理
34
+ sdoc_df['类别'] = categories
35
+ sdoc_df['模块'] = modules
36
+ sdoc_df['问题归类'] = summaries
37
+
38
+ sdoc_df.to_excel("smart_analysis.xlsx", index=0)
sdoc_request.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from tianshu import AiHelperClient, generate_serial_number
2
+
3
+ def get_tianshu_response(params):
4
+ # 请求地址
5
+ base_url = "https://api-aihelper.sheincorp.cn"
6
+ # 流水号,非必填,请求幂等处理,方便后续请求跟踪
7
+ out_id = generate_serial_number()
8
+ # 应用秘钥,必填
9
+ app_secret = "120a6f8685f8652ace71f7a2b0f2d395"
10
+ # 应用id,必填
11
+ scene_id = 7180
12
+ # 业务编码,对应系统编码或工号,必填
13
+ biz_code = "10270178"
14
+ # 构建请求客户端
15
+ client = AiHelperClient(base_url, app_secret, scene_id)
16
+ # 构建请求参数
17
+ # params = {"HOA_USERINPUT": "在线表格可以 @人吗"}
18
+
19
+
20
+ response = client.post("/open/v1/chat", {
21
+ # "out_id": out_id,
22
+ "scene_id": scene_id,
23
+ "biz_code": biz_code,
24
+ "params": params
25
+ })
26
+ result = response["info"].get("info")
27
+
28
+ return result
29
+
30
+
31
+ # if response is not None:
32
+ # result = response["info"].get("info")
33
+ # try:
34
+ # json_result = json.loads(result)
35
+ # print(json_result["类别"])
36
+ # print(json_result["模块"])
37
+ # except json.JSONDecodeError:
38
+ # print("not json!")
39
+ # else:
40
+ # print("request error!")
41
+
42
+ params = {"question": "SDOC的表格,我已经建了个在线表格,我要插入一个本地EX表,从企微文档迁移过来是在的,但我在上面操作,上传不了新的"}
43
+ get_tianshu_response(params)
streamlit_sdoc_analysis.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from sdoc_request import get_tianshu_response
3
+ import pandas as pd
4
+ import time
5
+ import json
6
+
7
+ # 设置页面标题
8
+ st.title("SDoc 问题智能分析")
9
+
10
+ # 创建文件上传组件
11
+ st.text("1. 少量问题,请访问链接提问。")
12
+ st.markdown("https://aiapi.sheincorp.cn/llm/#/scene/general_app/7180")
13
+
14
+ uploaded_file = st.file_uploader("2. 大量问题,请上传 Excel 文件,必须含有【问题描述】字段。", type=["xlsx"])
15
+
16
+ if uploaded_file is not None:
17
+ # 读取上传的 Excel 文件
18
+ sdoc_df = pd.read_excel(uploaded_file)
19
+ questions = sdoc_df["问题描述"].to_list()
20
+
21
+ # AI 智能分析
22
+ categories = []
23
+ modules = []
24
+ summaries = []
25
+
26
+ # 创建进度条
27
+ progress_bar = st.progress(0)
28
+ total_questions = len(questions)
29
+
30
+ for i, qs in enumerate(questions):
31
+ if qs and len(qs) > 0:
32
+ time.sleep(1)
33
+ params = {"HOA_USERINPUT": qs}
34
+ temp_result = get_tianshu_response(params)
35
+ try:
36
+ result = json.loads(temp_result)
37
+ except json.JSONDecodeError:
38
+ result = {"类别": '', "模块": '', "问题归类": ''}
39
+ st.warning(f"问题 '{qs}' 的返回结果不是有效的 JSON 格式。")
40
+ else:
41
+ result = {"类别": '', "模块": '', "问题归类": ''}
42
+
43
+ st.write(f"问题{i+1}: {qs}")
44
+ st.write(f"答案: {result}")
45
+
46
+ categories.append(result.get("类别"))
47
+ modules.append(result.get("模块"))
48
+ summaries.append(result.get("问题归类"))
49
+
50
+ # 更新进度条
51
+ progress_bar.progress((i + 1) / total_questions)
52
+
53
+ # 结果数据整理
54
+ sdoc_df['类别'] = categories
55
+ sdoc_df['模块'] = modules
56
+ sdoc_df['问题归类'] = summaries
57
+
58
+ # 将结果保存为 Excel 文件
59
+ output_file = "smart_analysis.xlsx"
60
+ sdoc_df.to_excel(output_file, index=0)
61
+
62
+ # 创建下载链接
63
+ with open(output_file, "rb") as file:
64
+ btn = st.download_button(
65
+ label="下载分析结果",
66
+ data=file,
67
+ file_name="smart_analysis.xlsx",
68
+ mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
69
+ )
70
+
tianshu.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import hashlib
3
+ import hmac
4
+ import base64
5
+ import time
6
+ import uuid
7
+ import random
8
+
9
+ # 生成流水号
10
+ def generate_serial_number():
11
+ uid = uuid.uuid4()
12
+ serial_number = uid.hex[:32]
13
+ return serial_number
14
+
15
+ # 获取随机码
16
+ def generate_random_code(length=5):
17
+ characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
18
+ code = ""
19
+ for _ in range(length):
20
+ code += random.choice(characters)
21
+ return code
22
+
23
+ # 生成签名和时间戳
24
+ def generate_signature(scene_id, request_path, app_secret, random_key):
25
+ timestamp = str(int(time.time() * 1000))
26
+ sign_string = f"{scene_id}&{timestamp}&{request_path}"
27
+ secret_key = app_secret + random_key
28
+ hmac_sha256 = hmac.new(
29
+ secret_key.encode(),
30
+ sign_string.encode(),
31
+ hashlib.sha256
32
+ ).hexdigest().encode()
33
+ signature = random_key + base64.b64encode(hmac_sha256).decode()
34
+ return signature, timestamp
35
+
36
+ class AiHelperClient:
37
+ def __init__(self, base_url, app_secret, scene_id):
38
+ self.base_url = base_url
39
+ self.app_secret = app_secret
40
+ self.random_key = generate_random_code()
41
+ self.scene_id = scene_id
42
+ self.session = requests.Session()
43
+
44
+ def post(self, endpoint, data):
45
+ signature, timestamp = generate_signature(
46
+ self.scene_id,
47
+ endpoint,
48
+ self.app_secret,
49
+ self.random_key
50
+ )
51
+
52
+ url = f"{self.base_url}{endpoint}"
53
+ headers = {
54
+ 'Content-Type': 'application/json',
55
+ 'x-signature': signature,
56
+ 'x-timestamp': timestamp
57
+ }
58
+
59
+ try:
60
+ response = self.session.post(url, json=data, headers=headers)
61
+ response.raise_for_status()
62
+ return response.json()
63
+ except requests.RequestException as e:
64
+ return None