heziiiii commited on
Commit
af5aca7
·
verified ·
1 Parent(s): cee5878

Upload tag_info.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. tag_info.py +81 -0
tag_info.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import json
3
+ from tqdm import tqdm
4
+ from concurrent.futures import ProcessPoolExecutor
5
+ from collections import Counter
6
+
7
+ if __name__ == "__main__":
8
+ # 文件路径
9
+ danbooru_parquets_path = "/mnt/data/Booru-parquets/danbooru.parquet"
10
+ danbooru_parquets_path_add = "/mnt/data/danbooru_newest-all/table.parquet"
11
+
12
+ # 读取 Parquet 文件
13
+ df1 = pd.read_parquet(danbooru_parquets_path)
14
+ df2 = pd.read_parquet(danbooru_parquets_path_add)
15
+ df = pd.concat([df1, df2], ignore_index=True)
16
+ print(df.columns)
17
+ print(df.head())
18
+
19
+ # 获取所有 tag_string_artist
20
+ tag_string_artist = df['tag_string_artist'].unique()
21
+ print(tag_string_artist)
22
+
23
+ def map_function(tag):
24
+ """映射函数:获取每个艺术家的 ID 和相关文本的词频"""
25
+ tag_data = df[df['tag_string_artist'] == tag]
26
+ tag_id = tag_data['id'].unique()
27
+ word_counts = Counter()
28
+
29
+ # 假设有一列包含文本,如 'description',替换为实际列名
30
+ text_column = 'tag_string_general' # 替换为实际列名
31
+ for text in tag_data[text_column]:
32
+ if isinstance(text, str): # 确保文本不是 None
33
+ words = text.split(" ") # 根据需要进行分词
34
+ word_counts.update(words)
35
+
36
+ return tag, tag_id, word_counts
37
+
38
+ def reduce_function(results):
39
+ """归约函数:合并所有结果"""
40
+ tag_string_artist_id = {}
41
+
42
+
43
+ for tag, ids, word_counts in results:
44
+ tag_string_artist_id[tag] = {}
45
+ tag_string_artist_id[tag]["ids"] = ids.tolist() # 将 NumPy 数组转换为列表
46
+ tag_string_artist_id[tag]["word_counts"] = dict(word_counts) # 将词频转换为字典
47
+
48
+ return tag_string_artist_id
49
+
50
+
51
+ # tag_string_artist = tag_string_artist[:60]
52
+ # 使用多进程并行处理
53
+ with ProcessPoolExecutor(max_workers=32) as executor:
54
+ # 映射阶段
55
+ mapped_results = list(tqdm(executor.map(map_function, tag_string_artist), total=len(tag_string_artist)))
56
+
57
+ # 归约阶段
58
+ tag_string_artist_id = reduce_function(mapped_results)
59
+
60
+ # 展示前五个艺术家的 ID 和词频
61
+ print(list(tag_string_artist_id.keys())[:5])
62
+ print(list(tag_string_artist_id.values())[:5])
63
+
64
+
65
+ # 保存 ID,词频 为 JSON
66
+ with open("artist_id_with_word_counts.json", "w") as f:
67
+ json.dump(tag_string_artist_id, f)
68
+
69
+
70
+
71
+ # 保存艺术家 ID 和词频为 Parquet
72
+ artist_data = []
73
+ for tag, data in tag_string_artist_id.items():
74
+ artist_data.append({
75
+ 'tag': tag,
76
+ 'id': data["ids"],
77
+ 'word_counts': data["word_counts"]
78
+ })
79
+
80
+ artist_df = pd.DataFrame(artist_data)
81
+ artist_df.to_parquet("artist_id_with_word_counts.parquet")