forked from baichuan-inc/Baichuan2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Similar_Question.py
62 lines (46 loc) · 1.54 KB
/
Similar_Question.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'''
File : Similar_Question.py
Description : 生成相似问
Date : 2023/12/19 15:48:59
Author : Feiyu Zhu
'''
import requests
import json
import pandas as pd
import re
# API 的 URL
def extract_questions_general(text):
lines = text.split('\n')
questions = []
for line in lines:
match = re.search(r'\d+\.\s(.+)', line)
if match:
questions.append(match.group(1))
return questions
url = "http://127.0.0.1:5000/generate"
excel_file = "/zhufeiyu/baichuan/Baichuan2/simary.xlsx"
df=pd.read_excel(excel_file)
for index,row in df.iterrows():
exceldata=row['Standard_Question']
# 要发送的数据
excel_question = f""
# 构建嵌套的 JSON 数据
data = {"input": excel_question}
# 发送请求
session = requests.Session()
response = session.post(url, json=data, headers={"Content-Type": "application/json", "Cache-Control": "no-cache"})
# 检查响应状态码
if response.status_code == 200:
# 解析响应内容
result = response.json()
response_string = result['response']
print(response_string)
try:
questions = extract_questions_general(response_string)
except:
questions = extract_questions_general(response_string)
print(questions)
df.at[index, "Similar_Question"] = str(questions)
else:
print("请求失败,状态码:", response.status_code)
df.to_excel('/home/zhengm/testcode/zhufeiyu/baichuan/Baichuan2/simarychange.xlsx',index=False)