Skip to content

Commit 00a2a80

Browse files
Merge pull request #583 from DefangLabs/jordan/rename-long-sample-names
Shortening some long sample names
0 parents  commit 00a2a80

7 files changed

Lines changed: 282 additions & 0 deletions

File tree

.github/workflows/deploy.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Deploy
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
deploy:
10+
environment: playground
11+
runs-on: ubuntu-latest
12+
permissions:
13+
contents: read
14+
id-token: write
15+
16+
steps:
17+
- name: Checkout Repo
18+
uses: actions/checkout@v4
19+
20+
- name: Deploy
21+
uses: DefangLabs/defang-github-action@v1.1.0

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Python & Flask & OpenAI
2+
3+
[![1-click-deploy](https://raw.githubusercontent.com/DefangLabs/defang-assets/main/Logos/Buttons/SVG/deploy-with-defang.svg)](https://portal.defang.dev/redirect?url=https%3A%2F%2Fgithub.com%2Fnew%3Ftemplate_name%3Dsample-python-openai-template%26template_owner%3DDefangSamples)
4+
5+
## Setup
6+
7+
This sample requires an API key to access the OpenAI API. The name of the config value is referenced in the compose.yaml file. To provide a value for it, you can use the Defang CLI like this:
8+
9+
```
10+
defang config set --name OPENAI_KEY
11+
```
12+
13+
and then enter the value when prompted.
14+
15+
## Testing
16+
17+
```
18+
echo "Hello" | curl -H "Content-Type: application/text" -d @- https://xxxxxxxx/prompt
19+
```
20+
21+
or
22+
23+
```
24+
cat prompt.txt | curl -H "Content-Type: application/text" -d @- https://xxxxxxxx/prompt
25+
```
26+
27+
---
28+
29+
Title: Python & Flask & OpenAI
30+
31+
Short Description: An app that demonstrates how to use the OpenAI API with Python and Flask.
32+
33+
Tags: Python, Flask, OpenAI, AI, Python
34+
35+
Languages: python

app/.gitignore

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
2+
# --- Python ---
3+
# Byte-compiled / optimized / DLL files
4+
__pycache__/
5+
*.py[cod]
6+
*$py.class
7+
8+
# C extensions
9+
*.so
10+
11+
# Distribution / packaging
12+
.Python
13+
build/
14+
develop-eggs/
15+
dist/
16+
downloads/
17+
eggs/
18+
.eggs/
19+
lib/
20+
lib64/
21+
parts/
22+
sdist/
23+
var/
24+
wheels/
25+
share/python-wheels/
26+
*.egg-info/
27+
.installed.cfg
28+
*.egg
29+
MANIFEST
30+
31+
# PyInstaller
32+
*.manifest
33+
*.spec
34+
35+
# Installer logs
36+
pip-log.txt
37+
pip-delete-this-directory.txt
38+
39+
# Unit test / coverage reports
40+
htmlcov/
41+
.tox/
42+
.nox/
43+
.coverage
44+
.coverage.*
45+
.cache
46+
nosetests.xml
47+
coverage.xml
48+
*.cover
49+
*.py,cover
50+
.hypothesis/
51+
.pytest_cache/
52+
cover/
53+
54+
# Translations
55+
*.mo
56+
*.pot
57+
58+
# Django stuff:
59+
*.log
60+
local_settings.py
61+
db.sqlite3
62+
db.sqlite3-journal
63+
64+
# Flask stuff:
65+
instance/
66+
.webassets-cache
67+
68+
# Scrapy stuff:
69+
.scrapy
70+
71+
# Sphinx documentation
72+
docs/_build/
73+
74+
# PyBuilder
75+
.pybuilder/
76+
target/
77+
78+
# Jupyter Notebook
79+
.ipynb_checkpoints
80+
81+
# IPython
82+
profile_default/
83+
ipython_config.py
84+
85+
# pdm
86+
.pdm.toml
87+
.pdm-python
88+
.pdm-build/
89+
90+
# PEP 582
91+
__pypackages__/
92+
93+
# Celery stuff
94+
celerybeat-schedule
95+
celerybeat.pid
96+
97+
# SageMath parsed files
98+
*.sage.py
99+
100+
# Environments
101+
.env
102+
.venv
103+
env/
104+
venv/
105+
ENV/
106+
env.bak/
107+
venv.bak/
108+
109+
# Spyder project settings
110+
.spyderproject
111+
.spyproject
112+
113+
# Rope project settings
114+
.ropeproject
115+
116+
# mkdocs documentation
117+
/site
118+
119+
# mypy
120+
.mypy_cache/
121+
.dmypy.json
122+
dmypy.json
123+
124+
# Pyre type checker
125+
.pyre/
126+
127+
# pytype static type analyzer
128+
.pytype/
129+
130+
# Cython debug symbols
131+
cython_debug/
132+
133+
# PyCharm
134+
.idea/

app/Dockerfile

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Use an official Python runtime as a parent image
2+
FROM python:3.11-slim
3+
4+
# Set the working directory to /app
5+
WORKDIR /app
6+
7+
# Install required C++11 libraries and ca-certificates
8+
RUN apt-get update -qq \
9+
&& apt-get install -y \
10+
build-essential \
11+
python3-dev \
12+
ca-certificates \
13+
curl \
14+
&& apt-get clean \
15+
&& rm -rf /var/lib/apt/lists/*
16+
17+
# Install any needed packages specified in requirements.txt
18+
COPY requirements.txt /app/
19+
RUN pip install --no-cache-dir -r requirements.txt
20+
21+
# Copy the current directory contents into the container at /app
22+
COPY . /app
23+
24+
# Make port 5000 available to the world outside this container
25+
EXPOSE 5000
26+
27+
# Run main when the container launches
28+
ENTRYPOINT ["uwsgi", "--http", "0.0.0.0:5000", "--master", "-p", "2", "-w", "main:app"]
29+
USER nobody

app/main.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import os
2+
import json
3+
import openai
4+
from flask import Flask
5+
from flask import request
6+
7+
8+
app = Flask(__name__)
9+
10+
11+
@app.route("/")
12+
def index():
13+
return { 'status' : 'ok' }
14+
15+
16+
@app.route("/prompt", methods=['POST'])
17+
def prompt():
18+
openai.api_key = os.environ['OPENAI_KEY']
19+
20+
messages = [
21+
# System prompt used to set context for the conversation
22+
# { "role": "system", "content": "You are an experienced software engineer." }
23+
]
24+
25+
prompt_text = request.get_data()
26+
prompt_text = prompt_text.decode('utf-8')
27+
28+
# append user prompt from request
29+
messages.append({ "role": "user", "content": prompt_text })
30+
31+
# call the ChatGPT API
32+
response = openai.ChatCompletion.create(
33+
model="gpt-3.5-turbo",
34+
#engine="gpt-4",
35+
messages=messages,
36+
)
37+
38+
return { 'status' : 'ok', 'response' : response }
39+
40+
41+
if __name__ == '__main__':
42+
app.run(debug=True, host='0.0.0.0')
43+

app/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
uwsgi
2+
flask
3+
openai

compose.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
services:
2+
app:
3+
restart: unless-stopped
4+
build:
5+
context: ./app
6+
dockerfile: Dockerfile
7+
ports:
8+
- mode: ingress
9+
target: 5000
10+
environment:
11+
- OPENAI_KEY
12+
deploy:
13+
resources:
14+
reservations:
15+
memory: 256M
16+
healthcheck:
17+
test: ["CMD", "curl", "-f", "http://localhost:5000/"]

0 commit comments

Comments
 (0)