AdamNovotnyCom commited on
Commit
74909f9
1 Parent(s): aa22a2e

Llama2 init

Browse files
Files changed (5) hide show
  1. Dockerfile +2 -6
  2. README.md +3 -2
  3. app.py +35 -5
  4. docker-compose.yml +2 -0
  5. requirements.txt +1 -0
Dockerfile CHANGED
@@ -6,15 +6,11 @@ RUN useradd -m -u 1000 user
6
  # Switch to the "user" user
7
  USER user
8
 
9
- # Set home to the user's home directory
10
- ENV HOME=/home/user \
11
- PATH=/home/user/.local/bin:$PATH
12
-
13
  # Set the working directory to the user's home directory
14
- WORKDIR $HOME/app
15
 
16
  # Copy the current directory contents into the container at $HOME/app setting the owner to the user
17
- COPY --chown=user . $HOME/app
18
 
19
  RUN pip install -r requirements.txt
20
 
 
6
  # Switch to the "user" user
7
  USER user
8
 
 
 
 
 
9
  # Set the working directory to the user's home directory
10
+ WORKDIR /home/user/app
11
 
12
  # Copy the current directory contents into the container at $HOME/app setting the owner to the user
13
+ COPY --chown=user . /home/user/app
14
 
15
  RUN pip install -r requirements.txt
16
 
README.md CHANGED
@@ -17,7 +17,7 @@ See live app on [Hugging Face](https://huggingface.co/spaces/AdamNovotnyCom/llam
17
 
18
  Start
19
 
20
- docker-compose -f docker-compose.yml up --detach llama2hf
21
 
22
  View app in browser
23
 
@@ -28,4 +28,5 @@ Exec command
28
  docker exec -it llama2hf bash -c 'pip install torch==2.1.*'
29
 
30
  ## References
31
- - https://huggingface.co/blog/llama2
 
 
17
 
18
  Start
19
 
20
+ export HF_TOKEN=paste_HF_token && docker-compose -f docker-compose.yml up llama2hf
21
 
22
  View app in browser
23
 
 
28
  docker exec -it llama2hf bash -c 'pip install torch==2.1.*'
29
 
30
  ## References
31
+ - [huggingface.co/llama2](https://huggingface.co/blog/llama2)
32
+ - [demo-docker-gradio](https://huggingface.co/spaces/sayakpaul/demo-docker-gradio/tree/main)
app.py CHANGED
@@ -1,11 +1,41 @@
1
  import gradio as gr
2
- from transformers import pipeline
 
 
 
3
 
4
- pipe_flan = pipeline("text2text-generation", model="google/flan-t5-small")
 
 
5
 
6
- def llm(input_text):
7
- return pipe_flan(input_text)["generated_text"]
 
 
 
 
 
 
 
 
 
 
 
8
 
9
- demo = gr.Interface(fn=llm, inputs="text", outputs="text")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  demo.launch(server_name="0.0.0.0", server_port=7860)
 
1
  import gradio as gr
2
+ import os
3
+ import torch
4
+ import transformers
5
+ from transformers import AutoTokenizer
6
 
7
+ # pipe_flan = transformers.pipeline("text2text-generation", model="google/flan-t5-small")
8
+ # def google_flan(input_text):
9
+ # return pipe_flan(input_text)["generated_text"]
10
 
11
+ model = "meta-llama/Llama-2-7b-chat-hf"
12
+ tokenizer = AutoTokenizer.from_pretrained(
13
+ model,
14
+ token=os.environ["HF_TOKEN"],
15
+ )
16
+ pipeline = transformers.pipeline(
17
+ "text-generation",
18
+ model=model,
19
+ torch_dtype=torch.float16,
20
+ device_map="auto",
21
+ token=os.environ["HF_TOKEN"],
22
+ low_cpu_mem_usage=False,
23
+ )
24
 
25
+ def llama2(input_text):
26
+ sequences = pipeline(
27
+ input_text,
28
+ do_sample=True,
29
+ top_k=10,
30
+ num_return_sequences=1,
31
+ eos_token_id=tokenizer.eos_token_id,
32
+ max_length=200,
33
+ )
34
+ output_text = ""
35
+ for seq in sequences:
36
+ output_text += seq["generated_text"] + "\n"
37
+ return output_text
38
+
39
+ demo = gr.Interface(fn=llama2, inputs="text", outputs="text")
40
 
41
  demo.launch(server_name="0.0.0.0", server_port=7860)
docker-compose.yml CHANGED
@@ -9,6 +9,8 @@ services:
9
  volumes:
10
  - ./:/home/user/app
11
  working_dir: /home/user/app
 
 
12
  stdin_open: true
13
  tty: true
14
  restart: always
 
9
  volumes:
10
  - ./:/home/user/app
11
  working_dir: /home/user/app
12
+ environment:
13
+ - HF_TOKEN=${HF_TOKEN}
14
  stdin_open: true
15
  tty: true
16
  restart: always
requirements.txt CHANGED
@@ -1,3 +1,4 @@
 
1
  gradio==3.47.*
2
  pandas==2.1.*
3
  pytest==7.4.*
 
1
+ accelerate==0.23.*
2
  gradio==3.47.*
3
  pandas==2.1.*
4
  pytest==7.4.*