Skip to content

Commit

Permalink
Merge pull request #43 from reactome/dep-update
Browse files Browse the repository at this point in the history
Dep update
  • Loading branch information
adamjohnwright authored Oct 29, 2024
2 parents 2501b60 + 970cfd6 commit 5de56a1
Show file tree
Hide file tree
Showing 18 changed files with 2,925 additions and 2,305 deletions.
96 changes: 96 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
name: reactome_chatbot CI

on:
workflow_dispatch:
pull_request:
types:
- opened
- synchronize
push:
branches:
- main

permissions:
id-token: write
contents: read

jobs:

lint:
if: ${{ github.event_name == 'pull_request' || github.event_name == 'workflow_dispatch' }}
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install poetry
poetry install --no-root
- name: Run linters
run: |
poetry run ruff check .
poetry run mypy .
poetry run isort --check .
docker-build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Build and push Docker image
uses: docker/build-push-action@v4
with:
context: .
file: ./Dockerfile
push: false
tags: reactome_chatbot:${{ github.sha }}

- uses: actions/upload-artifact@v3
with:
name: docker-image
path: /tmp/image.tar

docker-push:
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
needs: docker-build
runs-on: ubuntu-latest

steps:
- uses: actions/download-artifact@v3
with:
name: docker-image
path: /tmp

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1

- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2

- name: Build, tag, and push image to Amazon ECR
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
IMAGE_TAG: ${{ github.sha }}
run: |
docker load --input /tmp/image.tar
docker tag reactome_chatbot:${{ github.sha }} $ECR_REGISTRY/reactome-chatbot:$IMAGE_TAG
docker tag reactome_chatbot:${{ github.sha }} $ECR_REGISTRY/reactome-chatbot:latest
docker push $ECR_REGISTRY/reactome_chatbot:$IMAGE_TAG
docker push $ECR_REGISTRY/reactome_chatbot:latest
29 changes: 0 additions & 29 deletions .github/workflows/ruff.yml

This file was deleted.

2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Use an official Python runtime as a parent image
FROM python:3.9-slim
FROM python:3.10-slim

# Set the working directory in the container
WORKDIR /app
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ poetry run isort .

To run these inside of docker run a command like
```bash
docker build -t reactome-chatbot .; docker run -v $(pwd)/bin:/app/bin -v$(pwd)/src:/app/src reactome-chatbot /bin/bash -c "poetry run ruff ."
docker build -t reactome-chatbot .; docker run -v $(pwd)/bin:/app/bin -v$(pwd)/src:/app/src reactome-chatbot /bin/bash -c "poetry run ruff check ."
sudo chown $(id -u):$(id -g) * -R
```

Expand Down
11 changes: 6 additions & 5 deletions bin/chat-chainlit.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import os

import chainlit as cl

from dotenv import load_dotenv

from retreival_chain import initialize_retrieval_chain
from util.embedding_environment import EmbeddingEnvironment

Expand Down Expand Up @@ -47,14 +47,15 @@ async def chat_profile():
cl.ChatProfile(
name="React-to-me",
markdown_description="An AI assistant specialized in exploring **Reactome** biological pathways and processes.",
icon="https://reactome.org/templates/favourite/favicon.ico"
icon="https://reactome.org/templates/favourite/favicon.ico",
)
]


@cl.on_chat_start
async def quey_llm() -> None:
async def start() -> None:
chat_profile = cl.user_session.get("chat_profile")

embeddings_directory = EmbeddingEnvironment.get_dir(env)
llm_chain = initialize_retrieval_chain(
env,
Expand All @@ -71,15 +72,15 @@ async def quey_llm() -> None:


@cl.on_message
async def query_llm(message: cl.Message) -> None:
async def main(message: cl.Message) -> None:
llm_chain = cl.user_session.get("llm_chain")
cb = cl.AsyncLangchainCallbackHandler(
stream_final_answer=True, answer_prefix_tokens=["FINAL", "ANSWER"]
)
cb.answer_reached = True

res = await llm_chain.ainvoke(message.content, callbacks=[cb])
if cb.has_streamed_final_answer:
if cb.has_streamed_final_answer and cb.final_stream is not None:
await cb.final_stream.update()
else:
await cl.Message(content=res).send()
28 changes: 15 additions & 13 deletions bin/chat-fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
import hmac
import os

import requests
from chainlit.utils import mount_chainlit
from dotenv import load_dotenv
from fastapi import FastAPI, HTTPException, Request, Response
from fastapi.responses import RedirectResponse
import requests

load_dotenv()

Expand All @@ -16,17 +16,19 @@
CLOUDFLARE_SITE_KEY = os.getenv("CLOUDFLARE_SITE_KEY")


def make_signature(value:str) -> str:
def make_signature(value: str) -> str:
if CLOUDFLARE_SECRET_KEY is None:
raise ValueError("CLOUDFLARE_SECRET_KEY is not set")
return hmac.new(
CLOUDFLARE_SECRET_KEY.encode(),
value.encode(),
hashlib.sha256
CLOUDFLARE_SECRET_KEY.encode(), value.encode(), hashlib.sha256
).hexdigest()

def create_secure_cookie(value:str) -> str:

def create_secure_cookie(value: str) -> str:
signature = make_signature(value)
return f"{value}|{signature}"


def verify_secure_cookie(cookie_value: str) -> bool:
try:
value, signature = cookie_value.split("|", 1)
Expand All @@ -40,7 +42,8 @@ def verify_secure_cookie(cookie_value: str) -> bool:
async def verify_captcha_middleware(request: Request, call_next):
# Allow access to CAPTCHA pages and static files
if (
request.url.path in ["/chat/verify_captcha", "/chat/verify_captcha_page", "/chat/static"]
request.url.path
in ["/chat/verify_captcha", "/chat/verify_captcha_page", "/chat/static"]
or request.url.path.startswith("/static")
or not os.getenv("CLOUDFLARE_SECRET_KEY")
):
Expand Down Expand Up @@ -93,16 +96,15 @@ async def captcha_page():
async def verify_captcha(request: Request):
form_data = await request.form()
cf_turnstile_response = form_data.get("cf-turnstile-response")

if not cf_turnstile_response:
raise HTTPException(status_code=400, detail="CAPTCHA response is missing")
if not isinstance(cf_turnstile_response, str):
raise HTTPException(status_code=400, detail="CAPTCHA response is invalid")

# Verify the CAPTCHA with Cloudflare
url = "https://challenges.cloudflare.com/turnstile/v0/siteverify"
data = {
"secret": os.getenv("CLOUDFLARE_SECRET_KEY"),
"response": cf_turnstile_response,
"remoteip": request.client.host
"remoteip": request.client.host if request.client else "127.0.0.1",
}

# Perform request to Cloudflare Turnstile verification endpoint
Expand All @@ -120,8 +122,8 @@ async def verify_captcha(request: Request):
key="captcha_verified",
value=cookie_value,
max_age=3600, # Cookie expires in 1 hour
secure=True, # HTTPS only
httponly=True # inaccessible to client side JS
secure=True, # HTTPS only
httponly=True, # inaccessible to client side JS
)

return redirect_response
Expand Down
8 changes: 8 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[mypy]
ignore_missing_imports = True
allow_untyped_calls = True
allow_untyped_defs = True
allow_untyped_globals = True

[mypy.plugins.pandas.*]
init_forbid_dynamic = False
Loading

0 comments on commit 5de56a1

Please sign in to comment.