omarsol commited on
Commit
5162c20
·
verified ·
1 Parent(s): e327da0

Upload csv_to_jsonl.ipynb with huggingface_hub

Browse files
Files changed (1) hide show
  1. csv_to_jsonl.ipynb +149 -0
csv_to_jsonl.ipynb ADDED
@@ -0,0 +1,149 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {},
6
+ "source": [
7
+ "# Combine .csv files into one .jsonl file"
8
+ ]
9
+ },
10
+ {
11
+ "cell_type": "code",
12
+ "execution_count": 1,
13
+ "metadata": {},
14
+ "outputs": [
15
+ {
16
+ "data": {
17
+ "text/plain": [
18
+ "'combined_data.jsonl'"
19
+ ]
20
+ },
21
+ "execution_count": 1,
22
+ "metadata": {},
23
+ "output_type": "execute_result"
24
+ }
25
+ ],
26
+ "source": [
27
+ "from pathlib import Path\n",
28
+ "import pandas as pd\n",
29
+ "import json\n",
30
+ "\n",
31
+ "# Define the path for the uploaded files\n",
32
+ "files = [\n",
33
+ " \"data/ai-tutor-csv-files/activeloop.csv\",\n",
34
+ " \"data/ai-tutor-csv-files/advanced_rag_course.csv\",\n",
35
+ " \"data/ai-tutor-csv-files/filtered_tai_v2.csv\",\n",
36
+ " \"data/ai-tutor-csv-files/hf_transformers.csv\",\n",
37
+ " \"data/ai-tutor-csv-files/langchain_course.csv\",\n",
38
+ " \"data/ai-tutor-csv-files/langchain_docs.csv\",\n",
39
+ " \"data/ai-tutor-csv-files/llm_course.csv\",\n",
40
+ " \"data/ai-tutor-csv-files/openai.csv\",\n",
41
+ " \"data/ai-tutor-csv-files/wiki.csv\"\n",
42
+ "]\n",
43
+ "\n",
44
+ "# Function to load and clean CSV data\n",
45
+ "def load_and_clean_csv(file_path):\n",
46
+ " # Attempt to load the CSV file\n",
47
+ " df = pd.read_csv(file_path)\n",
48
+ " \n",
49
+ " # Check if the first column is unnamed and drop it if so\n",
50
+ " if 'Unnamed: 0' in df.columns or df.columns[0] == '':\n",
51
+ " df = df.drop(df.columns[0], axis=1)\n",
52
+ " \n",
53
+ " # Reorder columns based on expected headers\n",
54
+ " expected_headers = ['title', 'url', 'content', 'source']\n",
55
+ " df = df[expected_headers]\n",
56
+ " \n",
57
+ " return df\n",
58
+ "\n",
59
+ "# Load, clean, and combine all CSV files\n",
60
+ "combined_df = pd.concat([load_and_clean_csv(file) for file in files], ignore_index=True)\n",
61
+ "\n",
62
+ "# Convert to JSON - list of JSON objects\n",
63
+ "combined_json_list = combined_df.to_dict(orient=\"records\")\n",
64
+ "\n",
65
+ "# Save the combined JSON list to a new JSONL file, with each line representing a JSON object\n",
66
+ "output_file_jsonl = \"combined_data.jsonl\"\n",
67
+ "with open(output_file_jsonl, \"w\") as file:\n",
68
+ " for record in combined_json_list:\n",
69
+ " file.write(json.dumps(record) + '\\n')\n",
70
+ "\n",
71
+ "output_file_jsonl"
72
+ ]
73
+ },
74
+ {
75
+ "cell_type": "markdown",
76
+ "metadata": {},
77
+ "source": [
78
+ "# Merge chunks together if they have the same title and url\n",
79
+ "Also prepend the titles into the content for the sources that did not have that."
80
+ ]
81
+ },
82
+ {
83
+ "cell_type": "code",
84
+ "execution_count": 7,
85
+ "metadata": {},
86
+ "outputs": [],
87
+ "source": [
88
+ "# import json\n",
89
+ "\n",
90
+ "# def remove_title_from_content_simple(jsonl_file_path, output_file_path):\n",
91
+ "# with open(jsonl_file_path, 'r') as file, open(output_file_path, 'w') as output_file:\n",
92
+ "# for line in file:\n",
93
+ "# # Parse the JSON line\n",
94
+ "# data = json.loads(line)\n",
95
+ "\n",
96
+ "# content = str(data['content'])\n",
97
+ "# title = str(data['title'])\n",
98
+ " \n",
99
+ "# # Replace the title in the content with an empty string\n",
100
+ "# # This removes the exact match of the title from the content\n",
101
+ "# data['content'] = content.replace(title, '', 1)\n",
102
+ " \n",
103
+ "# # Write the updated data to the output file\n",
104
+ "# json.dump(data, output_file)\n",
105
+ "# output_file.write('\\n') # Add newline to separate JSON objects\n",
106
+ "\n",
107
+ "# # Example usage\n",
108
+ "# jsonl_file_path = 'combined_data.jsonl'\n",
109
+ "# output_file_path = 'output.jsonl'\n",
110
+ "# remove_title_from_content_simple(jsonl_file_path, output_file_path)\n"
111
+ ]
112
+ },
113
+ {
114
+ "cell_type": "code",
115
+ "execution_count": null,
116
+ "metadata": {},
117
+ "outputs": [],
118
+ "source": []
119
+ },
120
+ {
121
+ "cell_type": "code",
122
+ "execution_count": null,
123
+ "metadata": {},
124
+ "outputs": [],
125
+ "source": []
126
+ }
127
+ ],
128
+ "metadata": {
129
+ "kernelspec": {
130
+ "display_name": "env",
131
+ "language": "python",
132
+ "name": "python3"
133
+ },
134
+ "language_info": {
135
+ "codemirror_mode": {
136
+ "name": "ipython",
137
+ "version": 3
138
+ },
139
+ "file_extension": ".py",
140
+ "mimetype": "text/x-python",
141
+ "name": "python",
142
+ "nbconvert_exporter": "python",
143
+ "pygments_lexer": "ipython3",
144
+ "version": "3.11.8"
145
+ }
146
+ },
147
+ "nbformat": 4,
148
+ "nbformat_minor": 2
149
+ }