Skip to content

Commit f0e0968

Browse files
author
Frank Jia
committed
Add backend
1 parent 4232a15 commit f0e0968

15 files changed

+385
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

Backend/.DS_Store

6 KB
Binary file not shown.

Backend/.gitignore

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
gcloud_creds.json
2+
3+
.idea
4+
5+
# Byte-compiled / optimized / DLL files
6+
__pycache__/
7+
*.py[cod]
8+
*$py.class
9+
10+
# C extensions
11+
*.so
12+
13+
# Distribution / packaging
14+
.Python
15+
build/
16+
develop-eggs/
17+
dist/
18+
downloads/
19+
eggs/
20+
.eggs/
21+
lib/
22+
lib64/
23+
parts/
24+
sdist/
25+
var/
26+
wheels/
27+
share/python-wheels/
28+
*.egg-info/
29+
.installed.cfg
30+
*.egg
31+
MANIFEST
32+
33+
# PyInstaller
34+
# Usually these files are written by a python script from a template
35+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
36+
*.manifest
37+
*.spec
38+
39+
# Installer logs
40+
pip-log.txt
41+
pip-delete-this-directory.txt
42+
43+
# Unit test / coverage reports
44+
htmlcov/
45+
.tox/
46+
.nox/
47+
.coverage
48+
.coverage.*
49+
.cache
50+
nosetests.xml
51+
coverage.xml
52+
*.cover
53+
*.py,cover
54+
.hypothesis/
55+
.pytest_cache/
56+
cover/
57+
58+
# Translations
59+
*.mo
60+
*.pot
61+
62+
# Django stuff:
63+
*.log
64+
local_settings.py
65+
db.sqlite3
66+
db.sqlite3-journal
67+
68+
# Flask stuff:
69+
instance/
70+
.webassets-cache
71+
72+
# Scrapy stuff:
73+
.scrapy
74+
75+
# Sphinx documentation
76+
docs/_build/
77+
78+
# PyBuilder
79+
.pybuilder/
80+
target/
81+
82+
# Jupyter Notebook
83+
.ipynb_checkpoints
84+
85+
# IPython
86+
profile_default/
87+
ipython_config.py
88+
89+
# pyenv
90+
# For a library or package, you might want to ignore these files since the code is
91+
# intended to run in multiple environments; otherwise, check them in:
92+
# .python-version
93+
94+
# pipenv
95+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
96+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
97+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
98+
# install all needed dependencies.
99+
#Pipfile.lock
100+
101+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
102+
__pypackages__/
103+
104+
# Celery stuff
105+
celerybeat-schedule
106+
celerybeat.pid
107+
108+
# SageMath parsed files
109+
*.sage.py
110+
111+
# Environments
112+
.env
113+
.venv
114+
env/
115+
venv/
116+
ENV/
117+
env.bak/
118+
venv.bak/
119+
120+
# Spyder project settings
121+
.spyderproject
122+
.spyproject
123+
124+
# Rope project settings
125+
.ropeproject
126+
127+
# mkdocs documentation
128+
/site
129+
130+
# mypy
131+
.mypy_cache/
132+
.dmypy.json
133+
dmypy.json
134+
135+
# Pyre type checker
136+
.pyre/
137+
138+
# pytype static type analyzer
139+
.pytype/
140+
141+
# Cython debug symbols
142+
cython_debug/

Backend/Dockerfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM python:3.7
2+
3+
ADD ./requirements.txt .
4+
RUN pip install -r requirements.txt
5+
6+
RUN mkdir /code/
7+
8+
WORKDIR /code

Backend/README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# nwHacks 2021 - Backend
2+
3+
## Getting Started
4+
5+
The recommended IDE is [PyCharm](https://www.jetbrains.com/pycharm/). It's available for free for students.
6+
It's recommended that you use a virtual environment to run this project. Follow [this tutorial](https://www.jetbrains.com/help/pycharm/creating-virtual-environment.html#python_create_virtual_env)
7+
to create a new **Virtualenv** environment. Python 3.7+ is recommended.
8+
9+
With a new virtual env, you should now be able to run `pip install -r requirements.txt` to install all the required libraries.
10+
11+
**Starting the Server**
12+
13+
Run `python main.py`. You can do this using PyCharm. Expect to see:
14+
```
15+
INFO: Started server process [85895]
16+
INFO: Waiting for application startup.
17+
Startup Called
18+
[nltk_data] Downloading package punkt to /Users/frankjia/nltk_data...
19+
[nltk_data] Package punkt is already up-to-date!
20+
Initialized app services
21+
INFO: Application startup complete.
22+
INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
23+
```
24+
25+
The service should now be reachable through http://0.0.0.0:8000.
26+
27+
## Endpoints
28+
29+
Currently, the only endpoint is `POST /extract`. It expects the following JSON structure:
30+
31+
```json
32+
{
33+
"text": "Text you want to extract key sentences from"
34+
}
35+
```
36+
37+
It should respond with this structure:
38+
39+
```json
40+
{
41+
"sentences": [
42+
"Important sentence one.",
43+
"Important sentence two."
44+
]
45+
}
46+
```
47+
48+
Here's an example CURL command:
49+
```
50+
curl --request POST \
51+
--url http://0.0.0.0:8000/extract \
52+
--header 'Content-Type: application/json' \
53+
--data '{
54+
"text": "U.S. President Donald Trump has finally conceded\u00A0the 2020 election to president-elect Joe Biden in a new video\u00A0condemning his violent supporters who stormed the Capitol building on Wednesday.U.S. President Donald Trump finally conceded the 2020 election on Thursday, one day after his supporters stormed the U.S. Capitol. (Al Drago\/Getty Images)U.S. President Donald Trump has finally conceded the 2020 election to president-elect Joe Biden in a new video condemning his violent supporters who stormed the Capitol building on Wednesday. In the statement posted to Twitter, Trump declined to mention Biden by name or explicitly admit he'\''d lost the election, instead saying now that Congress has certified the election results, the \"new administration will be inaugurated on January 20\" and his focus now turns to \"ensuring a smooth, orderly and seamless transition of power.\" He called\u00A0the riot in the Capitol a \"heinous attack\" that left him \"outraged by the violence, lawlessness and mayhem.\" However, in a video to the pro-Trump rioters on Capitol Hill Wednesday, he told them to go home, but also that\u00A0he loved them, that they were special people and that he felt their pain. Twitter removed that video.\u00A0 In the new statement, Trump did not address what Democrats and even some Republicans say was his role in inciting the violence. He\u00A0did say\u00A0he \"immediately deployed the National Guard,\" although it took a long time for order to be restored on Capitol Hill and\u00A0CNN has reported that it was Vice-President Mike Pence who co-ordinated bringing in the troops.\u00A0 In the short message, Trump told\u00A0his supporters that while he knows they are \"disappointed,\"\u00A0their \"incredible journey is only just beginning.\" WATCH | Trump'\''s statement about the attack on the U.S. Capitol:\u00A0 U.S. President Donald Trump has posted a new video on Twitter, more than 24 hours after an angry mob of his supporters stormed the Capitol building, saying he was outraged by the \"heinous attack.\" He also conceded to president-elect Joe Biden and promised a \"smooth, orderly and seamless transition of power.\" 2:41 '\''We will stop the steal,'\'' Trump told supporters The address came at the end of a day where\u00A0the\u00A0president stayed out of sight in the White House. Silenced on some of his favourite\u00A0social media\u00A0lines of communication, he didn'\''t comment as several of his top aides, including a cabinet secretary, announced their resignations. The statement was also a stark reversal for Trump, who has spent months insisting widespread voter fraud cost him\u00A0the Nov. 3 presidential election\u00A0despite providing no evidence. During a rally in Washington on Wednesday, he encouraged his\u00A0thousands of supporters to\u00A0march to the Capitol to protest the certification of the electoral college vote. \"We will stop the steal,\" he told the crowd, using the rallying cry of protests against the election results. A large mob of rioters later overran police officers and invaded the Capitol building, forcing members of Congress into hiding for their own safety. As recently as Thursday morning, Trump was still maintaining the election was stolen from him. House Speaker Nancy Pelosi and Senate Minority Leader Chuck Schumer, seen here in August 2020, have called on Vice-President Mike Pence to use the 25th Amendment to oust Trump from office. (Carolyn Kaster\/The Associated Press) Before Trump released his video message on Thursday, the top Democrats in Congress, House Speaker Nancy Pelosi and Senate Minority Leader Chuck Schumer, called on Vice-President Mike Pence and Trump'\''s cabinet\u00A0to invoke the 25th Amendment, a provision of the U.S. Constitution that allows a cabinet majority\u00A0to remove the president from power if he is unable to discharge the duties of the office. How the 25th Amendment could be used to remove Trump from office But a Pence adviser says\u00A0the vice-president, who would have to lead any such effort, is opposed to using the amendment to oust Trump from the White House. Barring that, Pelosi\u00A0has said she would likely reconvene the House to initiate impeachment proceedings against Trump for his role in Wednesday'\''s violence, which claimed four lives. A day later, Republicans and Democrats alike\u00A0struggled with how best to contain the impulses of a president deemed too dangerous to control his own social media accounts but who remains commander-in-chief of the world'\''s largest military. \"I'\''m not worried about the next election, I'\''m worried about getting through the next 14 days,\" said Republican Sen. Lindsey Graham of South Carolina, one of Trump'\''s staunchest allies. He condemned the president'\''s role in Wednesday'\''s riots and said, \"If something else happens, all options would be on the table.\" In Pelosi'\''s words,\u00A0\"the president of the United States incited an armed insurrection against America.\" She called him \"a very dangerous person who should not continue in office. This is urgent, an emergency of the highest magnitude.\" UpdatedJoe Biden blames Trump for violence on Capitol Hill as he picks Merrick Garland for attorney generalU.S. Capitol Police rejected offers of federal help to stop pro-Trump rioters"
55+
}'
56+
```
57+
58+
You should get:
59+
60+
```json
61+
{
62+
"sentences": [
63+
"(Al Drago/Getty Images)U.S. President Donald Trump has finally conceded the 2020 election to president-elect Joe Biden in a new video condemning his violent supporters who stormed the Capitol building on Wednesday.",
64+
"The statement was also a stark reversal for Trump, who has spent months insisting widespread voter fraud cost him the Nov. 3 presidential election despite providing no evidence.",
65+
"(Carolyn Kaster/The Associated Press) Before Trump released his video message on Thursday, the top Democrats in Congress, House Speaker Nancy Pelosi and Senate Minority Leader Chuck Schumer, called on Vice-President Mike Pence and Trump's cabinet to invoke the 25th Amendment, a provision of the U.S. Constitution that allows a cabinet majority to remove the president from power if he is unable to discharge the duties of the office.",
66+
"He condemned the president's role in Wednesday's riots and said, \"If something else happens, all options would be on the table.\"",
67+
"She called him \"a very dangerous person who should not continue in office.",
68+
"This is urgent, an emergency of the highest magnitude.\""
69+
]
70+
}
71+
```

Backend/extract.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from fuzzywuzzy import process
2+
3+
from models import ExtractRequest, ExtractResponse
4+
from nltk.tokenize import sent_tokenize
5+
6+
7+
def execute_extract(req: ExtractRequest, ctx) -> ExtractResponse:
8+
summary = ctx['services'].extractive_summarizer.extract_summary(req.text)
9+
summary_sents = sent_tokenize(summary)
10+
deduped_summary_sents = list(process.dedupe(summary_sents))
11+
return ExtractResponse(sentences=deduped_summary_sents)

Backend/main.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from fastapi import FastAPI
2+
import uvicorn
3+
import nltk
4+
5+
from models import ExtractRequest
6+
from services.app_services import AppServices
7+
from extract import execute_extract
8+
9+
app = FastAPI()
10+
11+
ctx = {}
12+
13+
14+
@app.on_event('startup')
15+
async def init_services():
16+
print("Startup Called")
17+
nltk.download('punkt')
18+
ctx['services'] = AppServices()
19+
20+
21+
@app.get("/lifecheck")
22+
async def lifecheck():
23+
return {"message": "OK"}
24+
25+
26+
@app.post('/extract')
27+
async def extract(req: ExtractRequest):
28+
return execute_extract(req, ctx)
29+
30+
if __name__ == "__main__":
31+
uvicorn.run(app, host="0.0.0.0", port=8000)

Backend/models.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from typing import List
2+
3+
from pydantic import BaseModel
4+
5+
6+
class ExtractRequest(BaseModel):
7+
text: str
8+
9+
10+
class ExtractResponse(BaseModel):
11+
sentences: List[str]

Backend/requirements.txt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
bert-extractive-summarizer==0.6.1
2+
blis==0.7.4
3+
catalogue==1.0.0
4+
certifi==2020.12.5
5+
chardet==4.0.0
6+
click==7.1.2
7+
cymem==2.0.5
8+
fastapi==0.63.0
9+
filelock==3.0.12
10+
fuzzywuzzy==0.18.0
11+
h11==0.12.0
12+
idna==2.10
13+
joblib==1.0.0
14+
murmurhash==1.0.5
15+
nltk==3.5
16+
numpy==1.19.5
17+
packaging==20.8
18+
plac==1.1.3
19+
preshed==3.0.5
20+
pydantic==1.7.3
21+
pyparsing==2.4.7
22+
python-Levenshtein==0.12.0
23+
regex==2020.11.13
24+
requests==2.25.1
25+
sacremoses==0.0.43
26+
scikit-learn==0.24.0
27+
scipy==1.6.0
28+
six==1.15.0
29+
spacy==2.3.5
30+
srsly==1.0.5
31+
starlette==0.13.6
32+
thinc==7.4.5
33+
threadpoolctl==2.1.0
34+
tokenizers==0.9.4
35+
torch==1.7.1
36+
tqdm==4.55.1
37+
transformers==4.1.1
38+
typing-extensions==3.7.4.3
39+
urllib3==1.26.2
40+
uvicorn==0.13.3
41+
wasabi==0.8.0

Backend/services/.DS_Store

6 KB
Binary file not shown.

0 commit comments

Comments
 (0)