-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
1,233 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
|
||
# C extensions | ||
*.so | ||
|
||
# Distribution / packaging | ||
.Python | ||
build/ | ||
develop-eggs/ | ||
dist/ | ||
downloads/ | ||
eggs/ | ||
.eggs/ | ||
lib/ | ||
lib64/ | ||
parts/ | ||
sdist/ | ||
var/ | ||
wheels/ | ||
pip-wheel-metadata/ | ||
share/python-wheels/ | ||
*.egg-info/ | ||
.installed.cfg | ||
*.egg | ||
MANIFEST | ||
|
||
# PyInstaller | ||
# Usually these files are written by a python script from a template | ||
# before PyInstaller builds the exe, so as to inject date/other infos into it. | ||
*.manifest | ||
*.spec | ||
|
||
# Installer logs | ||
pip-log.txt | ||
pip-delete-this-directory.txt | ||
|
||
# Unit test / coverage reports | ||
htmlcov/ | ||
.tox/ | ||
.nox/ | ||
.coverage | ||
.coverage.* | ||
.cache | ||
nosetests.xml | ||
coverage.xml | ||
*.cover | ||
*.py,cover | ||
.hypothesis/ | ||
.pytest_cache/ | ||
|
||
# Translations | ||
*.mo | ||
*.pot | ||
|
||
# Django stuff: | ||
*.log | ||
local_settings.py | ||
db.sqlite3 | ||
db.sqlite3-journal | ||
|
||
# Flask stuff: | ||
instance/ | ||
.webassets-cache | ||
|
||
# Scrapy stuff: | ||
.scrapy | ||
|
||
# Sphinx documentation | ||
docs/_build/ | ||
|
||
# PyBuilder | ||
target/ | ||
|
||
# Jupyter Notebook | ||
.ipynb_checkpoints | ||
|
||
# IPython | ||
profile_default/ | ||
ipython_config.py | ||
|
||
# pyenv | ||
.python-version | ||
|
||
# pipenv | ||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. | ||
# However, in case of collaboration, if having platform-specific dependencies or dependencies | ||
# having no cross-platform support, pipenv may install dependencies that don't work, or not | ||
# install all needed dependencies. | ||
#Pipfile.lock | ||
|
||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow | ||
__pypackages__/ | ||
|
||
# Celery stuff | ||
celerybeat-schedule | ||
celerybeat.pid | ||
|
||
# SageMath parsed files | ||
*.sage.py | ||
|
||
# Environments | ||
.env | ||
.venv | ||
env/ | ||
venv/ | ||
ENV/ | ||
env.bak/ | ||
venv.bak/ | ||
|
||
# Spyder project settings | ||
.spyderproject | ||
.spyproject | ||
|
||
# Rope project settings | ||
.ropeproject | ||
|
||
# mkdocs documentation | ||
/site | ||
|
||
# mypy | ||
.mypy_cache/ | ||
.dmypy.json | ||
dmypy.json | ||
|
||
# Pyre type checker | ||
.pyre/ | ||
.DS_Store | ||
_build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
[[source]] | ||
url = "https://pypi.org/simple" | ||
verify_ssl = true | ||
name = "pypi" | ||
|
||
[packages] | ||
requests = "*" | ||
aiohttp = "*" | ||
dynamodb-json = "*" | ||
typing-extensions = "*" | ||
joblib = "*" | ||
tabulate = "*" | ||
|
||
[dev-packages] | ||
pipenv = "*" | ||
|
||
[requires] | ||
python_version = "3.9" | ||
|
||
[scripts] | ||
package = "bash -c 'pipenv run pip install -r <(pipenv requirements) --target _build/dependencies && cd _build/dependencies && zip -r ../package.zip . && cd ../../src && zip -gr ../_build/package.zip .'" | ||
deploy = "bash -c 'cd terraform && terraform apply'" |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Dalle Preview | ||
|
||
__The fastest way to generate [Dalle 2](https://openai.com/dall-e-2/) images__ | ||
|
||
This project uses the [Open Graph Protocol](https://ogp.me/) to enable creating and sending Dalle 2 images right from your messaging apps. | ||
|
||
|
||
## Usage | ||
|
||
Write `https://dalle-preview.co/<your image description>` in WhatsApp/Discord/Facebook/etc.. and a preview of the generated image will appear (it can take a couple of seconds for the preview to appear). | ||
|
||
 | ||
|
||
## Deploying to AWS | ||
|
||
First, generate the package for the AWS lambda by running: | ||
``` | ||
pipenv run package | ||
``` | ||
|
||
Then, deploy all the AWS services by running: | ||
``` | ||
pipenv run deploy -var="domain=<the domain name of your web service>" -var="dalle_api_key=<the api key for using the Dalle API>" | ||
``` |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import re | ||
import boto3 | ||
import os | ||
import requests | ||
import urllib.parse | ||
|
||
BUCKET_NAME = 'dalle-preview-images' | ||
|
||
def _parse_prompt(raw_prompt): | ||
return urllib.parse.unquote(raw_prompt).replace('_', ' ') | ||
|
||
def _create_response(body, status): | ||
return { | ||
'statusCode': status, | ||
'headers': { | ||
'Content-Type': 'text/html' | ||
}, | ||
'isBase64Encoded': False, | ||
'multiValueHeaders': { | ||
}, | ||
'body': body, | ||
} | ||
|
||
def _create_success_response(prompt, url): | ||
html_page = f''' | ||
<html prefix="og: https://ogp.me/ns#"> | ||
<head> | ||
<title>Dalle 2 Preview</title> | ||
<meta property="og:title" content="{prompt}" /> | ||
<meta property="og:image" content="{url}" /> | ||
<meta name="twitter:title" content="{prompt}"> | ||
<meta name="twitter:image" content="{url}"> | ||
</head> | ||
<img src="{url}"/> | ||
</html> | ||
''' | ||
return _create_response(html_page, 200) | ||
|
||
def isInS3(s3, prompt): | ||
try: | ||
s3.head_object(Bucket=BUCKET_NAME, Key=prompt) | ||
return True | ||
except Exception: | ||
return False | ||
|
||
def main(event, context): | ||
prompt = _parse_prompt(event['pathParameters']['prompt']) | ||
|
||
print(f'Prompt: {prompt}') | ||
|
||
if not re.match('^[a-zA-Z0-9\s]+$', prompt): | ||
return { | ||
'statusCode': 301, | ||
'headers': { | ||
'Content-Type': 'text/html', | ||
'Location': '/suspicious_duck', | ||
}, | ||
'isBase64Encoded': False, | ||
'multiValueHeaders': { | ||
}, | ||
} | ||
|
||
s3 = boto3.client('s3') | ||
|
||
if not isInS3(s3, prompt): | ||
api_token = os.environ.get('DALLE_API_KEY') | ||
response = requests.post( | ||
'https://api.openai.com/v1/images/generations', | ||
json={ | ||
'prompt': prompt, | ||
'n': 1, | ||
'size': '256x256' | ||
}, | ||
headers={ | ||
'Authorization': f'Bearer {api_token}' | ||
} | ||
) | ||
|
||
image_url = response.json()['data'][0]['url'] | ||
|
||
response = requests.get(image_url, stream=True) | ||
|
||
s3.upload_fileobj(response.raw, BUCKET_NAME, prompt, { 'ContentType': 'image/jpeg'}) | ||
|
||
url = s3.generate_presigned_url( | ||
'get_object', | ||
Params={ | ||
'Bucket': BUCKET_NAME, | ||
'Key': prompt | ||
}, | ||
ExpiresIn=300, | ||
) | ||
return _create_success_response(prompt, url) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.terraform | ||
terraform.tfstate | ||
terraform.tfstate.backup | ||
*.tfvars |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
terraform { | ||
required_providers { | ||
aws = { | ||
source = "hashicorp/aws" | ||
version = "~> 4.0" | ||
} | ||
} | ||
} | ||
|
||
locals { | ||
region = "eu-west-1" | ||
} | ||
|
||
# Configure the AWS Provider | ||
provider "aws" { | ||
region = "eu-west-1" | ||
profile = "Alon" | ||
} | ||
|
||
variable "dalle_api_key" { | ||
description = "API key to access Dalle 2" | ||
type = string | ||
} | ||
|
||
variable "domain" { | ||
description = "The domain name for the web service" | ||
type = string | ||
} | ||
|
||
module "db" { | ||
source = "./modules/db" | ||
} | ||
|
||
module "lambda" { | ||
source = "./modules/lambda" | ||
dalle_api_key = var.dalle_api_key | ||
bucket_arn = module.db.bucket_arn | ||
} | ||
|
||
data "aws_caller_identity" "current" {} | ||
|
||
module "gateway" { | ||
source = "./modules/gateway" | ||
lambda_function_name = module.lambda.lambda_function_name | ||
lambda_invoke_arn = module.lambda.lambda_invoke_arn | ||
region = local.region | ||
account_id = data.aws_caller_identity.current.account_id | ||
domain = var.domain | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
resource "aws_s3_bucket" "images" { | ||
bucket = "dalle-preview-images" | ||
} | ||
|
||
resource "aws_s3_bucket_acl" "images" { | ||
bucket = aws_s3_bucket.images.id | ||
acl = "private" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
output "bucket_arn" { | ||
value = aws_s3_bucket.images.arn | ||
} |
Oops, something went wrong.