CazimirRoman commited on
Commit
73e6e91
1 Parent(s): 66869f0

do not download model on each docker run

Browse files
Files changed (5) hide show
  1. Dockerfile +4 -3
  2. app/app.py +12 -20
  3. cache/.DS_Store +0 -0
  4. cache/README.txt +0 -0
  5. requirements.txt +2 -1
Dockerfile CHANGED
@@ -2,12 +2,14 @@ FROM python:3.9-slim
2
 
3
  WORKDIR /code
4
 
5
- ENV TRANSFORMERS_CACHE=./code/cache
6
-
7
  COPY ./requirements.txt /code/requirements.txt
8
 
9
  RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
10
 
 
 
 
 
11
  # Set up a new user named "user" with user ID 1000
12
  RUN useradd -m -u 1000 user
13
  # Switch to the "user" user
@@ -23,6 +25,5 @@ WORKDIR $HOME/app
23
  COPY --chown=user . $HOME/app
24
 
25
  COPY ./app /code/app
26
- COPY ./cache /code/cache
27
 
28
  CMD ["uvicorn", "app.app:app", "--host", "0.0.0.0", "--port", "7860"]
 
2
 
3
  WORKDIR /code
4
 
 
 
5
  COPY ./requirements.txt /code/requirements.txt
6
 
7
  RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
8
 
9
+ RUN python3 -c 'from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM;model=AutoModelForSeq2SeqLM.from_pretrained("facebook/bart-large-cnn");model.save_pretrained("models");tokenizer=AutoTokenizer.from_pretrained("facebook/bart-large-cnn");tokenizer.save_pretrained("models")'
10
+
11
+ ENV TRANSFORMERS_CACHE=./code/models
12
+
13
  # Set up a new user named "user" with user ID 1000
14
  RUN useradd -m -u 1000 user
15
  # Switch to the "user" user
 
25
  COPY --chown=user . $HOME/app
26
 
27
  COPY ./app /code/app
 
28
 
29
  CMD ["uvicorn", "app.app:app", "--host", "0.0.0.0", "--port", "7860"]
app/app.py CHANGED
@@ -4,27 +4,11 @@ from fastapi import FastAPI
4
 
5
  from transformers import pipeline
6
 
7
- ARTICLE = """ New York (CNN)When Liana Barrientos was 23 years old, she got married in Westchester County, New York.
8
- A year later, she got married again in Westchester County, but to a different man and without divorcing her first husband.
9
- Only 18 days after that marriage, she got hitched yet again. Then, Barrientos declared "I do" five more times, sometimes only within two weeks of each other.
10
- In 2010, she married once more, this time in the Bronx. In an application for a marriage license, she stated it was her "first and only" marriage.
11
- Barrientos, now 39, is facing two criminal counts of "offering a false instrument for filing in the first degree," referring to her false statements on the
12
- 2010 marriage license application, according to court documents.
13
- Prosecutors said the marriages were part of an immigration scam.
14
- On Friday, she pleaded not guilty at State Supreme Court in the Bronx, according to her attorney, Christopher Wright, who declined to comment further.
15
- After leaving court, Barrientos was arrested and charged with theft of service and criminal trespass for allegedly sneaking into the New York subway through an emergency exit, said Detective
16
- Annette Markowski, a police spokeswoman. In total, Barrientos has been married 10 times, with nine of her marriages occurring between 1999 and 2002.
17
- All occurred either in Westchester County, Long Island, New Jersey or the Bronx. She is believed to still be married to four men, and at one time, she was married to eight men at once, prosecutors say.
18
- Prosecutors said the immigration scam involved some of her husbands, who filed for permanent residence status shortly after the marriages.
19
- Any divorces happened only after such filings were approved. It was unclear whether any of the men will be prosecuted.
20
- The case was referred to the Bronx District Attorney\'s Office by Immigration and Customs Enforcement and the Department of Homeland Security\'s
21
- Investigation Division. Seven of the men are from so-called "red-flagged" countries, including Egypt, Turkey, Georgia, Pakistan and Mali.
22
- Her eighth husband, Rashid Rajput, was deported in 2006 to his native Pakistan after an investigation by the Joint Terrorism Task Force.
23
- If convicted, Barrientos faces up to four years in prison. Her next court appearance is scheduled for May 18.
24
- """
25
 
26
  app = FastAPI()
27
- summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
 
28
 
29
  @app.get("/")
30
  def read_root():
@@ -37,5 +21,13 @@ def read_item(item_id: int, q: Union[str, None] = None):
37
 
38
  @app.post("/summarize")
39
  async def summarize(text: str):
40
- return {"result": f"{summarizer(text, max_length=130, min_length=30, do_sample=False)}"}
 
 
 
 
 
 
 
 
41
 
 
4
 
5
  from transformers import pipeline
6
 
7
+ from newspaper import Article
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  app = FastAPI()
10
+
11
+ summarizer = pipeline("summarization", "/code/models")
12
 
13
  @app.get("/")
14
  def read_root():
 
21
 
22
  @app.post("/summarize")
23
  async def summarize(text: str):
24
+
25
+ url = 'https://towardsdatascience.com/containerizing-huggingface-transformers-for-gpu-inference-with-docker-and-fastapi-on-aws-d4a83edede2f'
26
+
27
+ article = Article(url)
28
+ article.download()
29
+
30
+ article.parse()
31
+
32
+ return {"summary": f"{summarizer(article.text, max_length=130, min_length=30, do_sample=False)}"}
33
 
cache/.DS_Store DELETED
Binary file (6.15 kB)
 
cache/README.txt DELETED
File without changes
requirements.txt CHANGED
@@ -1,4 +1,5 @@
1
  fastapi
2
  uvicorn
3
  transformers
4
- torch
 
 
1
  fastapi
2
  uvicorn
3
  transformers
4
+ torch
5
+ newspaper3k