Upload wiki.py with huggingface_hub
Browse files
wiki.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datasets import load_dataset
|
| 2 |
+
data = load_dataset("/map-vepfs/siwei/coig/hf/test_dataset")
|
| 3 |
+
|
| 4 |
+
print(data)
|
| 5 |
+
input()
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
import json
|
| 9 |
+
from tqdm import tqdm
|
| 10 |
+
import requests
|
| 11 |
+
|
| 12 |
+
api_key = "sk-lNZE6m8qs8dbhu6GDb5763Ea728041B08fB9D8EfB98fD57f"
|
| 13 |
+
api_url = "http://180.184.175.69:3000/v1/chat/completions"
|
| 14 |
+
|
| 15 |
+
import json
|
| 16 |
+
from tqdm import tqdm
|
| 17 |
+
import requests
|
| 18 |
+
|
| 19 |
+
def get_GPT_4_judgment(openai_api_url, openai_api_key , messages):
|
| 20 |
+
|
| 21 |
+
def single_turn_wrapper(text):
|
| 22 |
+
return [{"role": "user", "content": text}]
|
| 23 |
+
|
| 24 |
+
url = openai_api_url
|
| 25 |
+
key = openai_api_key
|
| 26 |
+
|
| 27 |
+
if isinstance(messages, str):
|
| 28 |
+
messages = single_turn_wrapper(messages)
|
| 29 |
+
payload = json.dumps({
|
| 30 |
+
"model": "gpt-4o",
|
| 31 |
+
# "model": "GPT-4-0613",
|
| 32 |
+
"messages": messages,
|
| 33 |
+
"temperature": 0,
|
| 34 |
+
})
|
| 35 |
+
headers = {
|
| 36 |
+
'Content-Type': 'application/json',
|
| 37 |
+
'Authorization': f'Bearer {key}'
|
| 38 |
+
}
|
| 39 |
+
response = requests.request("POST", url, headers=headers, data=payload)
|
| 40 |
+
return response.text
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def save_json(data, file_path, indent=4, ensure_ascii=False):
|
| 44 |
+
"""
|
| 45 |
+
保存数据为JSON文件。
|
| 46 |
+
|
| 47 |
+
:param data: 要保存的数据(通常是字典或列表)
|
| 48 |
+
:param file_path: JSON文件的路径
|
| 49 |
+
:param indent: 缩进级别,默认4(美化输出)
|
| 50 |
+
:param ensure_ascii: 是否保证ASCII编码,默认False(支持中文等非ASCII字符)
|
| 51 |
+
"""
|
| 52 |
+
try:
|
| 53 |
+
with open(file_path, 'w', encoding='utf-8') as f:
|
| 54 |
+
json.dump(data, f, indent=indent, ensure_ascii=ensure_ascii)
|
| 55 |
+
print(f"JSON文件已成功保存到 {file_path}")
|
| 56 |
+
except Exception as e:
|
| 57 |
+
print(f"保存JSON文件时出错: {e}")
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def load_json(file_path):
|
| 61 |
+
"""
|
| 62 |
+
读取 JSON 文件并返回解析后的数据。
|
| 63 |
+
:param file_path: str, JSON 文件的路径
|
| 64 |
+
:return: dict or list, 解析后的 JSON 数据
|
| 65 |
+
"""
|
| 66 |
+
try:
|
| 67 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
| 68 |
+
data = json.load(file)
|
| 69 |
+
return data
|
| 70 |
+
except FileNotFoundError:
|
| 71 |
+
print(f"Error: File '{file_path}' not found.")
|
| 72 |
+
return None
|
| 73 |
+
except json.JSONDecodeError:
|
| 74 |
+
print(f"Error: File '{file_path}' is not a valid JSON file.")
|
| 75 |
+
return None
|
| 76 |
+
except Exception as e:
|
| 77 |
+
print(f"Unexpected error: {e}")
|
| 78 |
+
return None
|
| 79 |
+
|
| 80 |
+
if __name__ == '__main__':
|
| 81 |
+
data = load_dataset('/map-vepfs/huggingface/datasets/OpenLLM-France/wikipedia', split="train")
|
| 82 |
+
select_data = []
|
| 83 |
+
for item in tqdm(data):
|
| 84 |
+
if len(item['text'].split(' ')) > 1000 and 'Calendar' not in item['text'] and item['text'].count('|') < 10:
|
| 85 |
+
|
| 86 |
+
select_data.append(item)
|
| 87 |
+
|
| 88 |
+
if len(select_data) > 10000:
|
| 89 |
+
break
|
| 90 |
+
# print(item.key())
|
| 91 |
+
select_data = select_data[:10000]
|
| 92 |
+
print('over')
|
| 93 |
+
input()
|
| 94 |
+
save_json(select_data, './wiki/wiki_data_10k.json', indent=4, ensure_ascii=False)
|
| 95 |
+
data = load_json('./wiki/wiki_data_10k.json')
|
| 96 |
+
new_data = []
|
| 97 |
+
for item in tqdm(data):
|
| 98 |
+
text = item['text']
|
| 99 |
+
if '###' in text:
|
| 100 |
+
text = text.replace('###', '---')
|
| 101 |
+
text_splits = text.split('##')
|
| 102 |
+
item['text_splits'] = text_splits
|
| 103 |
+
new_data.append(item)
|
| 104 |
+
save_json(new_data, './wiki/wiki_data_10k.json', indent=4, ensure_ascii=False)
|
| 105 |
+
|
| 106 |
+
# data = new_data
|
| 107 |
+
# new_data = []
|
| 108 |
+
# for item in tqdm(data):
|
| 109 |
+
# bullet_points = []
|
| 110 |
+
# for ts in text_splits:
|
| 111 |
+
# prompt2 = f"Give you a text: {ts} \n\n Please help me summarize the headline of the text in bullet points, and each bullet point only have 1-2 sentences."
|
| 112 |
+
|
| 113 |
+
# messages = [
|
| 114 |
+
# {"role": "user",
|
| 115 |
+
# "content": prompt2},
|
| 116 |
+
# ]
|
| 117 |
+
|
| 118 |
+
# response = get_GPT_4_judgment(api_url, api_key, messages)
|
| 119 |
+
# response = json.loads(response)
|
| 120 |
+
# response = response['choices'][0]['message']['content']
|
| 121 |
+
# bullet_points.append(response)
|
| 122 |
+
# item['bullet_points'] = bullet_points
|
| 123 |
+
# new_data.append(item)
|
| 124 |
+
|
| 125 |
+
# save_json(new_data, './wiki/wiki_data_summarize_300.json', indent=4, ensure_ascii=False)
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
|