diff --git a/backend/Dockerfile b/backend/Dockerfile new file mode 100644 index 0000000..56f313f --- /dev/null +++ b/backend/Dockerfile @@ -0,0 +1,17 @@ +FROM python:latest + +RUN pip install --upgrade pip + +COPY . /usr/src/app + +RUN CFLAGS="-Wno-narrowing" pip install cld2-cffi +RUN pip install -r /usr/src/app/requirements.txt +RUN pip install slackers +RUN pip install pandas +RUN pip install docx +EXPOSE 5000 +WORKDIR /usr/src/app +CMD ["uvicorn", "index:app", "--host", "0.0.0.0" ,"--port", "5000", "--log-level", "info"] + + + diff --git a/backend/README.md b/backend/README.md index 841489c..5cb0683 100644 --- a/backend/README.md +++ b/backend/README.md @@ -16,3 +16,10 @@ Start backend server (development): ``` uvicorn index:app --host 0.0.0.0 --port 5000 --log-level debug --reload ``` + +# Build and run dockerfile + +``` +docker build -t comprehendum . +docker run comprehendum (-d for detached mode) +``` diff --git a/backend/gptapi.py b/backend/gptapi.py index ecdd4f1..ff7559d 100644 --- a/backend/gptapi.py +++ b/backend/gptapi.py @@ -17,7 +17,7 @@ header_contenttype = "application/json" search_model = "ada" -completion_model = "davinci" +completion_model = "curie" examples_context = """6.6.1.3 Decent Working Time @@ -65,8 +65,6 @@ def get_answer(question: str, paragraphs: List[str]): - # question = "Is a payment under duress deemed an act of corruption?" - response = requests.post( answers_url, headers={ @@ -86,3 +84,26 @@ def get_answer(question: str, paragraphs: List[str]): } ) return response.json()['answers'][0] + + +compl_model = "davinci" +completion_url = f"https://api.openai.com/v1/engines/{compl_model}/completions" + + +def get_completion(question: str): + response = requests.post( + completion_url, + headers={ + "Authorization": header_auth, + "Content-Type": header_contenttype + }, + json={ + "prompt": """ + What is the german legal warranty period? + """, + "max_tokens": 50, + "temperature": 0.1, + "n": 1, + } + ) + return response.json()['choices'][0]['text'] diff --git a/backend/index.py b/backend/index.py index 321ea76..646a6b9 100644 --- a/backend/index.py +++ b/backend/index.py @@ -8,7 +8,7 @@ from gptapi import get_answer from key_word_search import get_sentences -from hardcode import * +from intelligent_parse import * # set permitted cors origins origins = [ @@ -89,7 +89,7 @@ async def shutdown(): import signal -TIMEOUT = 2 # number of seconds your want for timeout +TIMEOUT = 1 # number of seconds your want for timeout def interrupted(): diff --git a/backend/hardcode.py b/backend/intelligent_parse.py similarity index 100% rename from backend/hardcode.py rename to backend/intelligent_parse.py diff --git a/backend/requirements.txt b/backend/requirements.txt index a47aae6..a602ded 100644 --- a/backend/requirements.txt +++ b/backend/requirements.txt @@ -1,9 +1,25 @@ +certifi==2020.12.5 +cffi==1.14.5 +chardet==4.0.0 +cld2-cffi==0.1.4 click==7.1.2 fastapi==0.63.0 h11==0.12.0 +idna==2.10 +joblib==1.0.1 +multi-rake==0.0.1 +nltk==3.6.1 +numpy==1.20.2 +openai==0.6.3 +pycparser==2.20 pydantic==1.8.1 +pyrsistent==0.17.3 +python-dotenv==0.17.0 +regex==2021.4.4 +requests==2.25.1 +six==1.15.0 starlette==0.13.6 +tqdm==4.60.0 typing-extensions==3.7.4.3 -uvicorn==0.13.4 -python-dotenv==0.17.0 -openai==0.6.3 \ No newline at end of file +urllib3==1.26.4 +uvicorn==0.13.4 \ No newline at end of file diff --git a/backend/slack.py b/backend/slack.py index 04ab417..d9e59bb 100644 --- a/backend/slack.py +++ b/backend/slack.py @@ -1,15 +1,58 @@ from fastapi import Request from index import app, ask_question from pydantic import BaseModel +import random import requests +from multi_rake import Rake +rake = Rake() import logging from slackers.hooks import commands log = logging.getLogger(__name__) +users = [ + { + "id": "U01T7Q8NE3U", + "name": "Silas Alberti", + "pronoun": "He" + }, + { + "id": "U01T468DJ6R", + "name": "Marc Schneider", + "pronoun": "He" + }, + { + "id": "U01T7AR57V0", + "name": "Simon Bohnen", + "pronoun": "He" + }, + { + "id": "U01TZA1M800", + "name": "Ria Rosenauer", + "pronoun": "She" + }, + { + "id": "U01T4ASN329", + "name": "Alexander von Recum", + "pronoun": "He" + } +] + @commands.on("comprehendum") def handle_mention(payload): + # requests.post(payload["response_url"], json={ + # "blocks": [ + # { + # "type": "section", + # "text": { + # "type": "mrkdwn", + # "text": f"_Processing..._" + # } + # }, + # ] + # }) + print(payload["channel_id"]) channel_id = payload["channel_id"] @@ -18,6 +61,12 @@ def handle_mention(payload): question = payload["text"] result = ask_question(question) + l = rake.apply(question) + keyword = max(l, key=lambda _:_[0])[0] if l is not None and len(l) > 0 else "legal & compliance" + random.seed(hash(keyword)) + user = random.choice(users) + nl = "\n" + requests.post(payload["response_url"], json= { "blocks": [ { @@ -31,9 +80,20 @@ def handle_mention(payload): "type": "section", "text": { "type": "mrkdwn", - "text": f"*Answer:* {result}" + "text": f"> *Answer:* Hey <@{payload['user_id']}>! " + f"{result.split(nl)[0]}{nl}{nl.join(['> ' + line for line in result.split(nl)[1:]])}" } - } + }, + { + "type": "section", + "text": { + "type": "mrkdwn", + "text": f"_For specific support contact <@{user['id']}> from the " + f"legal & compliance " + f"department. {user['pronoun']} is the correct contact " + f"person for issues concerning *\"{keyword}\"*. 😀_" + } + }, ] }) else: diff --git a/frontend/package.json b/frontend/package.json index ee6a070..82a3b3b 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -16,6 +16,7 @@ "react-scripts": "4.0.3", "semantic-ui-css": "^2.4.1", "semantic-ui-react": "^2.0.3", + "styled-components": "^5.2.3", "typescript": "^4.0.3", "web-vitals": "^0.2.4", "workbox-background-sync": "^5.1.3", @@ -54,5 +55,8 @@ "last 1 firefox version", "last 1 safari version" ] + }, + "devDependencies": { + "@types/styled-components": "^5.1.9" } } diff --git a/frontend/public/index.html b/frontend/public/index.html index 6a9f8c2..6dc40fb 100644 --- a/frontend/public/index.html +++ b/frontend/public/index.html @@ -5,8 +5,16 @@ - + + +