ChihChiu29 commited on
Commit
3933325
1 Parent(s): 2103519

basic working version

Browse files
Files changed (4) hide show
  1. Dockerfile +11 -5
  2. main.py +40 -1
  3. requirements.txt +2 -1
  4. tutorial.md +14 -1
Dockerfile CHANGED
@@ -1,6 +1,3 @@
1
- # read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
2
- # you will also find guides on how best to write your Dockerfile
3
-
4
  FROM python:3.9
5
 
6
  WORKDIR /code
@@ -9,6 +6,15 @@ COPY ./requirements.txt /code/requirements.txt
9
 
10
  RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
11
 
12
- COPY . .
 
 
 
 
 
 
 
 
 
13
 
14
- CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]
 
 
 
 
1
  FROM python:3.9
2
 
3
  WORKDIR /code
 
6
 
7
  RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
8
 
9
+ RUN useradd -m -u 1000 user
10
+
11
+ USER user
12
+
13
+ ENV HOME=/home/user \
14
+ PATH=/home/user/.local/bin:$PATH
15
+
16
+ WORKDIR $HOME/app
17
+
18
+ COPY --chown=user . $HOME/app
19
 
20
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
main.py CHANGED
@@ -1,8 +1,47 @@
1
- from fastapi import FastAPI
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  app = FastAPI()
4
 
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  @app.get("/")
7
  def read_root():
8
  return {"Hello": "World!"}
 
1
+ """Model hosted on Hugging face.
2
+
3
+ Based on: https://huggingface.co/docs/hub/spaces-sdks-docker-first-demo
4
+ """
5
+
6
+ from fastapi import FastAPI, Request
7
+
8
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
9
+ from transformers import T5Tokenizer, T5ForConditionalGeneration
10
+
11
+
12
+ # FROM: https://huggingface.co/facebook/blenderbot-400M-distill?text=Hey+my+name+is+Thomas%21+How+are+you%3F
13
+ # tokenizer = AutoTokenizer.from_pretrained("facebook/blenderbot-400M-distill")
14
+ # model = AutoModelForSeq2SeqLM.from_pretrained("facebook/blenderbot-400M-distill")
15
+ # tokenizer = AutoTokenizer.from_pretrained("facebook/blenderbot-1B-distill")
16
+ # model = AutoModelForSeq2SeqLM.from_pretrained("facebook/blenderbot-1B-distill")
17
+ tokenizer = T5Tokenizer.from_pretrained("google/flan-t5-small")
18
+ model = T5ForConditionalGeneration.from_pretrained(
19
+ "google/flan-t5-small", device_map="auto")
20
+
21
+ token_size_limit = 128
22
+
23
 
24
  app = FastAPI()
25
 
26
 
27
+ @app.post('/reply')
28
+ async def Reply(req: Request):
29
+ request = await req.json()
30
+ msg = request['msg']
31
+ print(f'MSG: {msg}')
32
+
33
+ input_ids = tokenizer(msg, return_tensors='pt').input_ids # .to('cuda')
34
+ output = model.generate(
35
+ input_ids[:, -token_size_limit:],
36
+ do_sample=True,
37
+ temperature=0.9,
38
+ max_length=100,
39
+ )
40
+ reply = tokenizer.batch_decode(output)[0]
41
+ print(f'REPLY: {reply}')
42
+ return {'reply': reply}
43
+
44
+
45
  @app.get("/")
46
  def read_root():
47
  return {"Hello": "World!"}
requirements.txt CHANGED
@@ -3,4 +3,5 @@ requests==2.27.*
3
  sentencepiece==0.1.*
4
  torch==1.11.*
5
  transformers==4.*
6
- uvicorn[standard]==0.17.*
 
 
3
  sentencepiece==0.1.*
4
  torch==1.11.*
5
  transformers==4.*
6
+ uvicorn[standard]==0.17.*
7
+ accelerate
tutorial.md CHANGED
@@ -1,3 +1,16 @@
1
  ## Use git to push changes to huggingface repository
2
 
3
- First use `huggingface_cli.exe login` to login (follow its instruction), then use git commands for pushing.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ## Use git to push changes to huggingface repository
2
 
3
+ First use `huggingface_cli.exe login` to login (follow its instruction), then use git commands for pushing.
4
+
5
+ ## Build/run via docker locally
6
+
7
+ ```bash
8
+ docker build -t fastapi .
9
+ docker run -it -p 7860:7860 fastapi
10
+ ```
11
+
12
+ ## CURL POST example
13
+
14
+ ```bash
15
+ curl -X POST http://localhost:7860/reply -H 'Content-Type: application/json' -d '{"msg": "hi"}'
16
+ ```