Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Deta for python back-end #46

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions .github/workflows/backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@ jobs:
- name: Checkout 🛎️
uses: actions/checkout@v2

- name: Prepare Files 🔧
- name: Setup PDM 🔧
uses: pdm-project/setup-pdm@v3

- name: Export requirements.txt 🔧
run: |
echo "nothing to do!"
cd back-end
make requirements.txt

- name: Deploy 🚀
uses: peaceiris/[email protected].1
uses: BogDAAAMN/[email protected].1
with:
publish_dir: ./back-end/
publish_branch: heroku
github_token: ${{ secrets.GITHUB_TOKEN }}
deta-access-token: ${{ secrets.DETA_TOKEN }}
deta-name: 'meldy'
deta-project: 'bubblefishstudio'
deta-project-dir: 'back-end'
6 changes: 6 additions & 0 deletions back-end/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
*.pyc
build
dist
.venv
.pdm.toml
.deta
profile.perf
requirements.txt
20 changes: 0 additions & 20 deletions back-end/Pipfile

This file was deleted.

171 changes: 0 additions & 171 deletions back-end/Pipfile.lock

This file was deleted.

1 change: 0 additions & 1 deletion back-end/Procfile

This file was deleted.

43 changes: 0 additions & 43 deletions back-end/app.py

This file was deleted.

1 change: 1 addition & 0 deletions back-end/app.py
5 changes: 5 additions & 0 deletions back-end/deta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "meldy",
"description": "melody generation back-end for the Meldy app",
"runtime": "python3.9"
}
43 changes: 43 additions & 0 deletions back-end/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from tempfile import NamedTemporaryFile
from datetime import date
from cProfile import Profile
from os import environ

from flask import Flask, Response, request
from flask_cors import cross_origin
import music21 as m21

from meldy import MelodyGenerator

app = Flask(__name__)

@app.route("/")
@cross_origin(["http://localhost:8080", "https://meldy.bubblefish.studio"])
def make_melody():
valence = request.args.get("valence", default=1, type=float)
arousal = request.args.get("arousal", default=1, type=float)

if environ.get("PROFILE", False):
print("~~ profiling ~~")
pr = Profile()
pr.enable()

mg = MelodyGenerator(valence, arousal)
m = mg.gen_melody()

if environ.get("PROFILE", False):
pr.disable()
print("~~ end profile ~~")
pr.dump_stats("profile.perf")

# TODO: move this where appropriate maybe
meta = m21.metadata.Metadata()
meta.title = "Mood of the day" # TODO: maybe we can generate random titles
meta.composer = "Meldy"
meta.date = date.today().strftime("%Y/%m/%d")
m.metadata = meta

with NamedTemporaryFile() as t:
m.write("musicxml", fp=t.name)
t.seek(0)
return Response(t.read(), mimetype="application/vnd.recordare.musicxml")
10 changes: 10 additions & 0 deletions back-end/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.PHONY: deploy

requirements.txt: pdm.lock
pdm export --without-hashes -o $@
# pdm adds --index-url, but deta can't parse it correctly
# so we need to remove it
sed -i '' '/^--index-url/d' $@

deploy: requirements.txt
deta deploy
1 change: 1 addition & 0 deletions back-end/meldy/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .melody import MelodyGenerator
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading