bstraehle commited on
Commit
3dd4f5e
1 Parent(s): dc4a6d7

Upload airbnb_embed.ipynb

Browse files
Files changed (1) hide show
  1. airbnb_embed.ipynb +85 -0
airbnb_embed.ipynb ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {},
6
+ "source": [
7
+ "### How to generate the dataset\n",
8
+ "\n",
9
+ "1. Use PyGen.AI with prompt: \"Generate code to read dataset bstraehle/airbnb-san-francisco-202403 from Hugging Face. Process line by line to embed field 'description' using OpenAI model 'text-embedding-3-small', then append the line and embedded field 'description_embedding' to file 'c:\\temp\\airbnb-san-francisco-202403-embed.jsonl'.\n",
10
+ "2. Replace hard-coded OpenAI API key with getpass\n",
11
+ "3. Fix embedding code, see https://platform.openai.com/docs/guides/embeddings/how-to-get-embeddings"
12
+ ]
13
+ },
14
+ {
15
+ "cell_type": "code",
16
+ "execution_count": null,
17
+ "metadata": {},
18
+ "outputs": [],
19
+ "source": [
20
+ "!pip install openai transformers"
21
+ ]
22
+ },
23
+ {
24
+ "cell_type": "code",
25
+ "execution_count": null,
26
+ "metadata": {},
27
+ "outputs": [],
28
+ "source": [
29
+ "# Execution time: ~34 minutes\n",
30
+ "\n",
31
+ "from datasets import load_dataset\n",
32
+ "from openai import OpenAI\n",
33
+ "import json, getpass\n",
34
+ "\n",
35
+ "dataset = load_dataset(\"bstraehle/airbnb-san-francisco-202403\")\n",
36
+ "\n",
37
+ "client = OpenAI(api_key = getpass.getpass(\"OpenAI API Key\"))\n",
38
+ "\n",
39
+ "def embed_text(text):\n",
40
+ " \"\"\"Function to embed text using OpenAI's text-embedding-3-small model.\"\"\"\n",
41
+ " response = client.embeddings.create(\n",
42
+ " input=text,\n",
43
+ " model=\"text-embedding-3-small\"\n",
44
+ " )\n",
45
+ " return response.data[0].embedding\n",
46
+ "\n",
47
+ "def process_dataset(dataset):\n",
48
+ " \"\"\"Process each row in the dataset, embed 'description', and write to a new file.\"\"\"\n",
49
+ " with open(\"c:\\\\temp\\\\airbnb-san-francisco-202403-embed.jsonl\", \"w\") as f:\n",
50
+ " for item in dataset:\n",
51
+ " description = item[\"description\"]\n",
52
+ "\n",
53
+ " if description:\n",
54
+ " embedding = embed_text(description)\n",
55
+ " new_item = {**item, \"description_embedding\": embedding}\n",
56
+ " f.write(json.dumps(new_item) + '\\n')\n",
57
+ "\n",
58
+ "process_dataset(dataset['train'])\n",
59
+ "\n",
60
+ "print(\"Processing completed.\")"
61
+ ]
62
+ }
63
+ ],
64
+ "metadata": {
65
+ "kernelspec": {
66
+ "display_name": "Python 3",
67
+ "language": "python",
68
+ "name": "python3"
69
+ },
70
+ "language_info": {
71
+ "codemirror_mode": {
72
+ "name": "ipython",
73
+ "version": 3
74
+ },
75
+ "file_extension": ".py",
76
+ "mimetype": "text/x-python",
77
+ "name": "python",
78
+ "nbconvert_exporter": "python",
79
+ "pygments_lexer": "ipython3",
80
+ "version": "3.9.13"
81
+ }
82
+ },
83
+ "nbformat": 4,
84
+ "nbformat_minor": 2
85
+ }