Skip to content

Commit

Permalink
Merge branch 'master' into where
Browse files Browse the repository at this point in the history
  • Loading branch information
brendaweles authored Aug 27, 2024
2 parents 25877b7 + 7dd6834 commit e371dc7
Show file tree
Hide file tree
Showing 28 changed files with 701 additions and 42 deletions.
11 changes: 6 additions & 5 deletions openai-dalle/README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Generate Images With DALL·E 2 and the OpenAI API
# Generate Images With DALL·E and the OpenAI API

Learn to use the OpenAI Python library to create images with DALL·E, a state-of-the-art latent diffusion model. In the associated tutorial on [generating images with DALL·E 2 and the OpenAI API](https://realpython.com/generate-images-with-dalle-openai-api/), you explore image creation and generating image variations. You learn how to interact with DALL·E using API calls and incorporate this functionality into your Python scripts.
Learn to use the OpenAI Python library to create images with DALL·E, a state-of-the-art latent diffusion model. In the associated tutorial on [generating images with DALL·E and the OpenAI API](https://realpython.com/generate-images-with-dalle-openai-api/), you'll explore image creation and generating image variations. You'll learn how to interact with DALL·E using API calls and incorporate this functionality into your Python scripts.

## Setup

Create and activate a virtual environment, then install the `openai` package:

```console
$ python --version
Python 3.11.0
Python 3.12.5
$ python -m venv venv
$ source venv/bin/activate
(venv) $ python -m pip install openai
Expand All @@ -22,14 +22,15 @@ Follow the instructions in [the tutorial](https://realpython.com/generate-images

You can find the code for each of these steps in dedicated scripts:

- `create.py`: Create an image from a text prompt and save the image data to a file.
- `create_dalle3.py`: Create an image from a text prompt using DALL·E 3 and display the URL to the image.
- `create.py`: Create an image from a text prompt using DALL·E 2 and save the image data to a file.
- `convert.py`: Convert a Base64-encoded PNG image delivered in a JSON response to a PNG image file.
- `vary.py`: Read Base64-encoded image data and make an API request to receive variations of that image.

In the tutorial, you'll walk through each of these scripts and their functionality and output in more detail.

## Edit Images (Inpainting and Outpainting)

The OpenAI Image API also allows you to [edit parts of an image](https://beta.openai.com/docs/guides/images/edits) using text prompts. For this, you need to create a mask with transparent image data in the area where you want to edit the image.
The OpenAI Image API also allows you to [edit parts of an image](https://platform.openai.com/docs/guides/images/edits-dall-e-2-only) using text prompts. For this, you need to create a mask with transparent image data in the area where you want to edit the image.

You can run `edit.py` to give this functionality a try.
14 changes: 7 additions & 7 deletions openai-dalle/create.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import json
import os
from pathlib import Path

import openai
from openai import OpenAI

client = OpenAI()

PROMPT = "An eco-friendly computer from the 90s in the style of vaporwave"
DATA_DIR = Path.cwd() / "responses"

DATA_DIR.mkdir(exist_ok=True)

openai.api_key = os.getenv("OPENAI_API_KEY")

response = openai.Image.create(
response = client.images.generate(
model="dall-e-2",
prompt=PROMPT,
n=1,
size="256x256",
response_format="b64_json",
)

file_name = DATA_DIR / f"{PROMPT[:5]}-{response['created']}.json"
file_name = DATA_DIR / f"{PROMPT[:5]}-{response.created}.json"

with open(file_name, mode="w", encoding="utf-8") as file:
json.dump(response, file)
json.dump(response.to_dict(), file)
14 changes: 14 additions & 0 deletions openai-dalle/create_dalle3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from openai import OpenAI

client = OpenAI()

PROMPT = "A vaporwave computer"


response = client.images.generate(
model="dall-e-3",
prompt=PROMPT,
)

print(response.data[0].url)
print(response.data[0].revised_prompt)
13 changes: 6 additions & 7 deletions openai-dalle/edit.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import json
import os
from pathlib import Path

import openai
from openai import OpenAI

client = OpenAI()

SOURCE_PATH = Path.cwd() / "images" / "An ec-1667994848"
DESTINATION_PATH = Path.cwd() / "responses"
Expand All @@ -11,9 +12,7 @@
SOURCE_PATH.mkdir(parents=True, exist_ok=True)
DESTINATION_PATH.mkdir(parents=True, exist_ok=True)

openai.api_key = os.getenv("OPENAI_API_KEY")

response = openai.Image.create_edit(
response = client.images.edit(
image=open(SOURCE_PATH / "computer.png", mode="rb"),
mask=open(SOURCE_PATH / "mask.png", mode="rb"),
prompt=PROMPT,
Expand All @@ -23,8 +22,8 @@
)

with open(
DESTINATION_PATH / f"edit-{PROMPT[:5]}-{response['created']}.json",
DESTINATION_PATH / f"edit-{PROMPT[:5]}-{response.created}.json",
mode="w",
encoding="utf-8",
) as file:
json.dump(response, file)
json.dump(response.to_dict(), file)
31 changes: 15 additions & 16 deletions openai-dalle/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
certifi==2022.9.24
charset-normalizer==2.1.1
et-xmlfile==1.1.0
idna==3.4
numpy==1.23.4
openai==0.25.0
openpyxl==3.0.10
pandas==1.5.1
pandas-stubs==1.2.0.62
python-dateutil==2.8.2
pytz==2022.6
requests==2.28.1
six==1.16.0
tqdm==4.64.1
typing_extensions==4.4.0
urllib3==1.26.12
annotated-types==0.7.0
anyio==4.4.0
certifi==2024.7.4
distro==1.9.0
h11==0.14.0
httpcore==1.0.5
httpx==0.27.0
idna==3.7
jiter==0.5.0
openai==1.40.6
pydantic==2.8.2
pydantic_core==2.20.1
sniffio==1.3.1
tqdm==4.66.5
typing_extensions==4.12.2
13 changes: 6 additions & 7 deletions openai-dalle/vary.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
import json
import os
from base64 import b64decode
from pathlib import Path

import openai
from openai import OpenAI

client = OpenAI()

DATA_DIR = Path.cwd() / "responses"
SOURCE_FILE = DATA_DIR / "An ec-1667994848.json"

openai.api_key = os.getenv("OPENAI_API_KEY")

with open(SOURCE_FILE, mode="r", encoding="utf-8") as json_file:
saved_response = json.load(json_file)
image_data = b64decode(saved_response["data"][0]["b64_json"])

response = openai.Image.create_variation(
response = client.images.create_variation(
image=image_data,
n=3,
size="256x256",
response_format="b64_json",
)

new_file_name = f"vary-{SOURCE_FILE.stem[:5]}-{response['created']}.json"
new_file_name = f"vary-{SOURCE_FILE.stem[:5]}-{response.created}.json"

with open(DATA_DIR / new_file_name, mode="w", encoding="utf-8") as file:
json.dump(response, file)
json.dump(response.to_dict(), file)
3 changes: 3 additions & 0 deletions python-lists-tuples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Lists vs Tuples in Python

This folder provides the code examples for the Real Python tutorial [Lists vs Tuples in Python](https://realpython.com/python-lists-tuples/).
17 changes: 17 additions & 0 deletions python-lists-tuples/create_lists_tuples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
colors = ["red", "green", "blue", "yellow"]
print(colors)

person = ("Jane Doe", 25, "Python Developer", "Canada")
print(person)

digits = list(range(10))
print(digits)

even_digits = [number for number in range(1, 10) if not number % 2]
print(even_digits)

print(["Pythonista", 7, False, 3.14159])
print(("Pythonista", 7, False, 3.14159))

print(list(range(1_000_000)))
print(tuple(range(1_000_000)))
5 changes: 5 additions & 0 deletions python-lists-tuples/functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
numbers = [2, 7, 5, 4, 8]
print(len(numbers))
print(min(numbers))
print(max(numbers))
print(sum(numbers))
19 changes: 19 additions & 0 deletions python-lists-tuples/list_methods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
a = ["a", "b"]
a.append("c")
print(a)

a = ["a", "c"]
a.insert(1, "b")
print(a)

a = ["a", "b", "c", "d", "e"]
a.remove("b")
print(a)
a.remove("c")
print(a)

a = ["a", "b", "c", "d", "e"]
a.pop()
print(a)
a.pop()
print(a)
11 changes: 11 additions & 0 deletions python-lists-tuples/nested_lists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
x = ["a", ["bb", ["ccc", "ddd"], "ee", "ff"], "g", ["hh", "ii"], "j"]

print(x[0], x[2], x[4])

print(x[1])
print(x[3])

print(x[1][0])
print(x[1][1])
print(x[1][2])
print(x[1][3])
7 changes: 7 additions & 0 deletions python-lists-tuples/operators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
words = ["foo", "bar", "baz", "qux", "quux", "corge"]
print("qux" in words)
print("py" in words)
print("thud" not in words)

print(words + ["grault", "garply"])
print(words * 2)
7 changes: 7 additions & 0 deletions python-lists-tuples/remove_items.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fruits = ["apple", "orange", "mango", "grape"]
del fruits[0] # Remove apple

print(fruits)

person = ("John Doe", 35, "Web Dev")
# del person[1] # Try to remove the age value
7 changes: 7 additions & 0 deletions python-lists-tuples/slicing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
numbers = [1, 2, 3, 7]
numbers[3:4] = [4, 5, 6, 7]
print(numbers)

numbers = [1, 2, 3, 7]
numbers[3:3] = [4, 5, 6]
print(numbers)
21 changes: 21 additions & 0 deletions python-lists-tuples/unpacking.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
t = ("foo", "bar", "baz", "qux")

s1, s2, s3, s4 = t
print(s1)
print(s2)
print(s3)
print(s4)

a = "foo"
b = "bar"
# Using a temporary variable
temp = a
a = b
b = temp
print(a, b)

a = "foo"
b = "bar"
# Using unpacking
a, b = b, a
print(a, b)
41 changes: 41 additions & 0 deletions web-scraping-with-scrapy-and-mongodb/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Web Scraping With Scrapy and MongoDB

[Web Scraping With Scrapy and MongoDB](https://realpython.com/web-scraping-with-scrapy-and-mongodb/) is an example project for building a robust web scraper for static sites leveraging Scrapy and MongoDB.

## Installation and Setup

1. Create a Python virtual environment

```sh
$ python -m venv ./venv
$ source venv/bin/activate
(venv) $
```

2. Install the requirements

```sh
(venv) $ pip install -r requirements.txt
```

You'll also need to [set up a MongoDB collection](https://realpython.com/web-scraping-with-scrapy-and-mongodb/#set-up-a-mongodb-collection-on-your-computer) like described in the tutorial.

## Run the Scraper

Navigate into the `books/` project directory.

Then you can start crawling the site:

```sh
(venv) $ scrapy crawl book
```

If set up correctly, this will populate your MongoDB collection with the book information scraped from the example site.

## About the Author

Martin Breuss - Email: [email protected]

## License

Distributed under the MIT license. See ``LICENSE`` for more information.
Empty file.
8 changes: 8 additions & 0 deletions web-scraping-with-scrapy-and-mongodb/books/books/items.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import scrapy


class BooksItem(scrapy.Item):
_id = scrapy.Field()
url = scrapy.Field()
title = scrapy.Field()
price = scrapy.Field()
Loading

0 comments on commit e371dc7

Please sign in to comment.