Skip to content
Draft

API #22

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
24 changes: 24 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ jobs:

steps:
- uses: actions/checkout@v2
- uses: azure/docker-login@v1
with:
login-server: ditto.azurecr.io
username: ${{ secrets.REGISTRY_USERNAME }}
password: ${{ secrets.REGISTRY_PASSWORD }}
- name: Build the Docker image
run: |
cd ditto-markov
Expand All @@ -22,3 +27,22 @@ jobs:
run: |
cd ditto-markov
docker-compose run test
- uses: docker/build-push-action@v1
with:
username: ${{ secrets.REGISTRY_USERNAME }}
password: ${{ secrets.REGISTRY_PASSWORD }}
registry: ditto.azurecr.io
repository: ditto
dockerfile: ditto-markov/Dockerfile
path: ditto-markov/
tag_with_ref: true
tag_with_sha: true
target: production
push: ${{ github.ref == 'refs/pull/22/merge' }}
# - name: Push image to ACR
# if: github.ref == 'refs/pull/22/merge'
# run: |
# docker tag ridhoq/ditto/ditto-markov ditto.azurecr.io/ditto:${{github.ref}}-${{github.sha}}
# docker tag ridhoq/ditto/ditto-markov ditto.azurecr.io/ditto:${{github.ref}}-latest
# docker push ridhoq/ditto/ditto-markov ditto.azurecr.io/ditto:${{github.ref}}-${{github.sha}}
# docker push ridhoq/ditto/ditto-markov ditto.azurecr.io/ditto:${{github.ref}}-latest
2 changes: 1 addition & 1 deletion ditto-markov/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.7.3-alpine as base
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.7-alpine3.8 as base

WORKDIR /app

Expand Down
3 changes: 2 additions & 1 deletion ditto-markov/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Rewrite of the markov chain functionality into a python service using a Cosmos data source

## Getting Started
Currently, there's just a CLI wrapper to generate the user's chat messages
Currently, there's a CLI wrapper to generate the user's chat messages and an API
1. copy `.env.example` to `.env` and drop in the correct values
1. `docker-compose run --rm cli <slack-user>` will return 20 messages that sound like the user. Optionally, pass a space delimited list of users
1. `docker-compose up api` will start the API
26 changes: 26 additions & 0 deletions ditto-markov/ditto_markov/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import sys

from fastapi import FastAPI, HTTPException
from cachetools import LFUCache

from .cache import CacheRepository, BlobCache
from .markov import get_model

api = FastAPI()

cache_repo = CacheRepository()

lfu_cache = LFUCache(maxsize=5)
cache_repo.register_cache("lfu", lfu_cache)

blob_cache = BlobCache()
cache_repo.register_cache("blob", blob_cache)


@api.get("/transform/{user}")
def get_transform(user):
try:
model = get_model(user, cache_repo)
return {user: [model.make_sentence() for _ in range(20)]}
except KeyError:
raise HTTPException(status_code=404, detail="user not found")
19 changes: 18 additions & 1 deletion ditto-markov/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,30 @@ services:
build:
context: .
target: production
image: ridhoq/ditto/ditto-markov
image: ridhoq/ditto/ditto-markov/cli
env_file: .env
volumes:
- ./ditto_markov:/app/ditto_markov
- ./poetry.lock:/app/poetry.lock
- ./pyproject.toml:/app/pyproject.toml
entrypoint: ["poetry", "run", "cli",]

api:
build:
context: .
target: production
image: ridhoq/ditto/ditto-markov
env_file: .env
environment:
APP_MODULE: ditto_markov.api:api
PORT: 8888
ports:
- 8888:8888
volumes:
- ./ditto_markov:/app/ditto_markov
- ./poetry.lock:/app/poetry.lock
- ./pyproject.toml:/app/pyproject.toml
entrypoint: ["/start-reload.sh"]

cosmosdb:
image: ridhoq/cosmosdb-server:0.7.0
Expand Down
Loading