winglian commited on
Commit
cfb13df
1 Parent(s): 93db6b5

Upload 2 files

Browse files
Files changed (3) hide show
  1. .gitattributes +1 -0
  2. derive_guancao.py +55 -0
  3. guanaco.jsonl +3 -0
.gitattributes CHANGED
@@ -52,3 +52,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
52
  *.jpg filter=lfs diff=lfs merge=lfs -text
53
  *.jpeg filter=lfs diff=lfs merge=lfs -text
54
  *.webp filter=lfs diff=lfs merge=lfs -text
 
 
52
  *.jpg filter=lfs diff=lfs merge=lfs -text
53
  *.jpeg filter=lfs diff=lfs merge=lfs -text
54
  *.webp filter=lfs diff=lfs merge=lfs -text
55
+ guanaco.jsonl filter=lfs diff=lfs merge=lfs -text
derive_guancao.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import logging
3
+
4
+ import pandas as pd
5
+ from datasets import load_dataset, Dataset
6
+
7
+ train_dataset = load_dataset("OpenAssistant/oasst1")["train"]
8
+
9
+
10
+ def get_children(df, parent_ids):
11
+ children = df[df['parent_id'].isin(parent_ids)]
12
+ return children.sort_values('rank', ascending=True).drop_duplicates('parent_id')
13
+
14
+
15
+ def trace_conversations(df, parent_ids):
16
+ conversations = []
17
+ children = get_children(df, parent_ids)
18
+
19
+ while not children.empty:
20
+ conversations.extend(children.to_dict('records'))
21
+ parent_ids = children['message_id']
22
+ children = get_children(df, parent_ids)
23
+
24
+ return conversations
25
+
26
+ # Convert the HuggingFace's dataset to pandas dataframe
27
+ df = pd.DataFrame.from_records(train_dataset)
28
+
29
+ # Get the root nodes
30
+ root_nodes = df[df['parent_id'].isnull()]
31
+
32
+ conversations = []
33
+ for idx, root in root_nodes.iterrows():
34
+ conversation_chain = [root.to_dict()]
35
+ conversation_chain.extend(trace_conversations(df, [root['message_id']]))
36
+ conversations.append(conversation_chain)
37
+
38
+ # Select only necessary columns for each conversation
39
+ for conversation in conversations:
40
+ for message in conversation:
41
+ keys_to_delete = set(message.keys()) - {'message_id', 'parent_id', 'role', 'text'}
42
+ for key in keys_to_delete:
43
+ del message[key]
44
+
45
+ # Create a new dataframe with only the 'conversations' field
46
+ result_df = pd.DataFrame({'conversations': conversations})
47
+
48
+ # Convert dataframe back to HuggingFace's dataset
49
+ result_dataset = Dataset.from_pandas(result_df)
50
+
51
+ logging.info(result_dataset)
52
+
53
+ with open("guanaco.jsonl", "w") as f:
54
+ for row in result_dataset:
55
+ f.write(json.dumps(row))
guanaco.jsonl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:109f3b5c63c30a340bec3a30bda94b22f8c05c1df1073623be8a6467270f8672
3
+ size 24376150