ThomasBlumet
commited on
Commit
·
03071d5
1
Parent(s):
38d893e
new try with new app
Browse files- Dockerfile +8 -8
- app.py +82 -76
- requirements.txt +5 -4
Dockerfile
CHANGED
@@ -1,10 +1,10 @@
|
|
1 |
# For more information, please refer to https://aka.ms/vscode-docker-python
|
2 |
-
FROM python:3.10-slim
|
3 |
|
4 |
-
|
5 |
|
6 |
# Install Python and pip
|
7 |
-
|
8 |
|
9 |
# Where we'll copy the code
|
10 |
WORKDIR /code
|
@@ -13,15 +13,15 @@ WORKDIR /code
|
|
13 |
COPY ./requirements.txt /code/requirements.txt
|
14 |
|
15 |
# Install pip requirements without venv
|
16 |
-
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
17 |
|
18 |
# Create a virtual environment and install pip requirements
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
|
23 |
# Set the PATH to include the virtual environment's bin directory
|
24 |
-
|
25 |
|
26 |
# Creates a non-root user with an explicit UID
|
27 |
# For more info, please refer to https://aka.ms/vscode-docker-python-configure-containers
|
|
|
1 |
# For more information, please refer to https://aka.ms/vscode-docker-python
|
2 |
+
#FROM python:3.10-slim
|
3 |
|
4 |
+
FROM nvidia/cuda:11.7.0-cudnn8-devel-ubuntu22.04
|
5 |
|
6 |
# Install Python and pip
|
7 |
+
RUN apt-get update && apt-get install -y python3-pip python3-venv
|
8 |
|
9 |
# Where we'll copy the code
|
10 |
WORKDIR /code
|
|
|
13 |
COPY ./requirements.txt /code/requirements.txt
|
14 |
|
15 |
# Install pip requirements without venv
|
16 |
+
#RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
17 |
|
18 |
# Create a virtual environment and install pip requirements
|
19 |
+
RUN python3 -m venv /code/venv
|
20 |
+
RUN /code/venv/bin/pip install --no-cache-dir --upgrade pip
|
21 |
+
RUN /code/venv/bin/pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
22 |
|
23 |
# Set the PATH to include the virtual environment's bin directory
|
24 |
+
ENV PATH="/code/venv/bin:$PATH"
|
25 |
|
26 |
# Creates a non-root user with an explicit UID
|
27 |
# For more info, please refer to https://aka.ms/vscode-docker-python-configure-containers
|
app.py
CHANGED
@@ -1,80 +1,86 @@
|
|
1 |
-
|
2 |
-
from transformers
|
|
|
|
|
|
|
3 |
import gradio as gr
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
#
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
# output_text = gr.Textbox(label="History")
|
75 |
|
76 |
-
|
77 |
-
|
78 |
-
|
|
|
|
|
79 |
|
80 |
|
|
|
|
|
|
1 |
+
#last version of app.py
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, GPTQConfig
|
3 |
+
import torch
|
4 |
+
import optimum
|
5 |
+
import auto_gptq
|
6 |
import gradio as gr
|
7 |
+
import time
|
8 |
+
|
9 |
+
device = torch.device("cuda:0") if torch.cuda.is_available() else torch.device("cpu")
|
10 |
+
|
11 |
+
model_name = "TheBloke/zephyr-7B-beta-GPTQ"
|
12 |
+
|
13 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name,use_fast=True,padding_side="left")
|
14 |
+
quantization_config_loading = GPTQConfig(
|
15 |
+
bits=4,
|
16 |
+
group_size=128,
|
17 |
+
disable_exllama=False)
|
18 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, quantization_config=quantization_config_loading, device_map="auto")
|
19 |
+
model = model.to(device)
|
20 |
+
|
21 |
+
def generate_text(input_text,max_new_tokens=512,top_k=50,top_p=0.95,temperature=0.7,no_grad=False):
|
22 |
+
tokenizer.pad_token_id = tokenizer.eos_token_id
|
23 |
+
input_ids = tokenizer.encode(input_text, padding=True, return_tensors="pt").to(device)
|
24 |
+
attention_mask = input_ids.ne(tokenizer.pad_token_id).long().to(device)
|
25 |
+
output = None
|
26 |
+
if no_grad:
|
27 |
+
with torch.no_grad():
|
28 |
+
output = model.generate(input_ids, attention_mask=attention_mask, max_new_tokens=max_new_tokens, top_k=top_k, top_p=top_p, temperature=temperature,do_sample=True)
|
29 |
+
else:
|
30 |
+
output = model.generate(input_ids, attention_mask=attention_mask, max_new_tokens=max_new_tokens, top_k=top_k, top_p=top_p, temperature=temperature,do_sample=True)
|
31 |
+
return tokenizer.decode(output[0][input_ids['input_ids'].shape[1]:], skip_special_tokens=True)
|
32 |
+
|
33 |
+
|
34 |
+
time_story = 0
|
35 |
+
|
36 |
+
def generate_response(input,history: list[tuple[str, str]],max_tokens, temperature, top_p):
|
37 |
+
messages=[]
|
38 |
+
for val in history:
|
39 |
+
# Directly access content using "content" key
|
40 |
+
messages.extend([{"role": "user", "content": val.get("content")}, {"role": "assistant", "content": val.get("content")}]) if val else None
|
41 |
+
|
42 |
+
messages.append({"role": "user", "content": input})
|
43 |
+
print(f'Time to generate the story: {time_story}')
|
44 |
+
start = time.time()
|
45 |
+
output = generate_text(input,max_new_tokens=max_tokens, top_p=top_p, temperature=temperature)
|
46 |
+
end = time.time()
|
47 |
+
time_story= end-start
|
48 |
+
history.append((input,output))
|
49 |
+
yield output
|
50 |
+
|
51 |
+
#define the chatinterface
|
52 |
+
title = "TeLLMyStory"
|
53 |
+
description = "A LLM for stories generation aiming the reinforcement of the controllability aspect"
|
54 |
+
theme = gr.Theme.from_hub("Yntec/HaleyCH_Theme_Yellow_Blue")
|
55 |
+
examples=[["Once upon a time a witch named Malefique was against the wedding of her daughter with the son of the king of the nearby kingdom."],
|
56 |
+
["Once upon a time an ice-cream met a spoon and they fell in love"],
|
57 |
+
["The neverending day began with a beautiful sunshine and an AI robot which was seeking humans on the desert Earth."]]
|
58 |
+
|
59 |
+
demo = gr.ChatInterface(
|
60 |
+
generate_response,
|
61 |
+
type="messages",
|
62 |
+
title=title,
|
63 |
+
description=description,
|
64 |
+
theme=theme,
|
65 |
+
examples=examples,
|
66 |
+
additional_inputs=[
|
67 |
+
gr.Slider(minimum=1, maximum=2048, value=100, step=1, label="Max new tokens"),
|
68 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
69 |
+
gr.Slider(
|
70 |
+
minimum=0.1,
|
71 |
+
maximum=1.0,
|
72 |
+
value=0.95,
|
73 |
+
step=0.05,
|
74 |
+
label="Top-p (nucleus sampling)",
|
75 |
+
),
|
76 |
+
],
|
|
|
77 |
|
78 |
+
stop_btn="Stop",
|
79 |
+
delete_cache=[60,60],
|
80 |
+
show_progress="full",
|
81 |
+
save_history=True,
|
82 |
+
)
|
83 |
|
84 |
|
85 |
+
if __name__ == "__main__":
|
86 |
+
demo.launch(share=True,debug=True)
|
requirements.txt
CHANGED
@@ -1,8 +1,9 @@
|
|
1 |
-
|
2 |
-
|
3 |
torch
|
4 |
transformers
|
5 |
gradio
|
6 |
-
huggingface_hub
|
7 |
optimum
|
8 |
-
|
|
|
|
|
|
1 |
+
huggingface_hub
|
2 |
+
--extra-index-url https://download.pytorch.org/whl/cu117
|
3 |
torch
|
4 |
transformers
|
5 |
gradio
|
|
|
6 |
optimum
|
7 |
+
accelerate
|
8 |
+
--extra-index-url https://huggingface.github.io/autogptq-index/whl/cu117/
|
9 |
+
auto-gptq
|