Spaces:
Runtime error
Runtime error
LennardZuendorf
commited on
Commit
•
bf15c20
1
Parent(s):
517fd4c
fix/chore: final fix of attention, preperating for last release
Browse files- Dockerfile +4 -4
- Dockerfile-Base +3 -3
- README.md +20 -8
- main.py +2 -2
- model/mistral.py +5 -4
- public/credits_dataprotection_license.md +15 -3
- utils/formatting.py +19 -3
Dockerfile
CHANGED
@@ -1,10 +1,10 @@
|
|
1 |
# standard Dockerfile to build a complete, working image of the webapp
|
2 |
|
3 |
# complete build based on clean python (slower)
|
4 |
-
|
5 |
|
6 |
# build based on thesis base with dependencies (quicker) - for dev
|
7 |
-
FROM thesis-base:0.
|
8 |
|
9 |
# install dependencies and copy files into image folder
|
10 |
COPY requirements.txt .
|
@@ -19,5 +19,5 @@ RUN ls --recursive .
|
|
19 |
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8080"]
|
20 |
|
21 |
# build and run commands:
|
22 |
-
## docker build -t thesis:1.
|
23 |
-
## docker run -d --name thesis -e HOSTING=local -p 8080:8080 thesis:1.
|
|
|
1 |
# standard Dockerfile to build a complete, working image of the webapp
|
2 |
|
3 |
# complete build based on clean python (slower)
|
4 |
+
FROM python:3.11.6
|
5 |
|
6 |
# build based on thesis base with dependencies (quicker) - for dev
|
7 |
+
#FROM thesis-base:0.2.0
|
8 |
|
9 |
# install dependencies and copy files into image folder
|
10 |
COPY requirements.txt .
|
|
|
19 |
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8080"]
|
20 |
|
21 |
# build and run commands:
|
22 |
+
## docker build -t thesis:1.3.1 -f Dockerfile .
|
23 |
+
## docker run -d --name thesis -e HOSTING=local -p 8080:8080 thesis:1.3.1
|
Dockerfile-Base
CHANGED
@@ -1,12 +1,12 @@
|
|
1 |
# Dockerfile to create base image with all needed dependencies. for the quick dockerfile build of the webapp
|
2 |
# because all dependencies are already installed, the next webapp build using this base image is much quicker
|
3 |
|
4 |
-
#
|
5 |
-
FROM
|
6 |
|
7 |
# install dependencies based on requirements
|
8 |
COPY requirements.txt ./
|
9 |
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
10 |
|
11 |
# build and run commands
|
12 |
-
## docker build -t thesis-base:
|
|
|
1 |
# Dockerfile to create base image with all needed dependencies. for the quick dockerfile build of the webapp
|
2 |
# because all dependencies are already installed, the next webapp build using this base image is much quicker
|
3 |
|
4 |
+
# complete build based on clean python (slower)
|
5 |
+
FROM python:3.11.6
|
6 |
|
7 |
# install dependencies based on requirements
|
8 |
COPY requirements.txt ./
|
9 |
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
10 |
|
11 |
# build and run commands
|
12 |
+
## docker build -t thesis-base:2.0.0 -f Dockerfile-Base .
|
README.md
CHANGED
@@ -19,6 +19,8 @@ disable_embedding: true
|
|
19 |
## 📜 Info:
|
20 |
This is the UI showcase for my thesis about the interpretability of LLM based chatbot application and applications of XAI.
|
21 |
|
|
|
|
|
22 |
### 🔗 Links:
|
23 |
|
24 |
**[GitHub Repository](https://github.com/LennardZuendorf/thesis-webapp)**
|
@@ -49,7 +51,7 @@ This Project was part of my studies of Business Computing at the University of A
|
|
49 |
|
50 |
1. Clone the repository using git or GitHub cli.
|
51 |
2. Start the (virtual) environment.
|
52 |
-
3. Set the environment variable "HOSTING", i.e. like this `export HOSTING=
|
53 |
3. Install the requirements using `pip install -r requirements.txt`
|
54 |
4. Run the app using `uvicorn main:app`. You can add `--reload` to enable hot reloading. The app will be available at `localhost:8000`.
|
55 |
|
@@ -58,14 +60,14 @@ This Project was part of my studies of Business Computing at the University of A
|
|
58 |
|
59 |
1. Clone the repository using git or GitHub cli.
|
60 |
2. Build the docker image using `docker build -t thesis-webapp -f Dockerfile . .`, the command commented in the docker file or the command referenced by your hosting service.
|
61 |
-
3. Run the docker image using `docker run --name thesis-webapp -e HOSTING=local -p 8080:8080 thesis-webapp`, the command commented in the docker file or the command referenced by your hosting service.
|
62 |
4. The app will be available at `localhost:8080`. If you are using a hosting service, the port may be different.
|
63 |
|
64 |
### 🐳 Docker Image :
|
65 |
(This assumes you have set up docker desktop or are using a hosting service able to handle Docker images.)
|
66 |
|
67 |
-
1. Pull the docker image from ghcr using `docker pull ghcr.io/LennardZuendorf/thesis-webapp:
|
68 |
-
2. Run the docker image in terminal using `docker run --name thesis-webapp -e HOSTING=local -p 8080:8080 lennardzuendorf/thesis-webapp
|
69 |
3. The app will be available at `localhost:8080`. If you are using a hosting service, the port may be different.
|
70 |
|
71 |
## 📝 License and Credits:
|
@@ -79,15 +81,25 @@ This project is licensed under the MIT License, see [LICENSE](LICENSE.md) for mo
|
|
79 |
|
80 |
See code for in detailed credits, work is strongly based on:
|
81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
#### GODEL
|
83 |
- [HGF Model Page](https://huggingface.co/microsoft/GODEL-v1_1-large-seq2seq?text=Hey+my+name+is+Mariama%21+How+are+you%3F)
|
84 |
- [Paper on HGF](https://huggingface.co/papers/2206.11309)
|
85 |
- [Paper Print](https://arxiv.org/abs/2206.11309)
|
86 |
|
87 |
-
####
|
88 |
-
- [
|
89 |
-
- [
|
|
|
|
|
90 |
|
91 |
#### Custom Component (/components/iframe/)
|
92 |
|
93 |
-
Is based on Gradio component, see
|
|
|
19 |
## 📜 Info:
|
20 |
This is the UI showcase for my thesis about the interpretability of LLM based chatbot application and applications of XAI.
|
21 |
|
22 |
+
**Current Release: v1.3.1**
|
23 |
+
|
24 |
### 🔗 Links:
|
25 |
|
26 |
**[GitHub Repository](https://github.com/LennardZuendorf/thesis-webapp)**
|
|
|
51 |
|
52 |
1. Clone the repository using git or GitHub cli.
|
53 |
2. Start the (virtual) environment.
|
54 |
+
3. Set the environment variable "HOSTING", i.e. like this `export HOSTING=local USER=admin PW=test`, see [fastAPI Docu](https://fastapi.tiangolo.com/advanced/settings/)
|
55 |
3. Install the requirements using `pip install -r requirements.txt`
|
56 |
4. Run the app using `uvicorn main:app`. You can add `--reload` to enable hot reloading. The app will be available at `localhost:8000`.
|
57 |
|
|
|
60 |
|
61 |
1. Clone the repository using git or GitHub cli.
|
62 |
2. Build the docker image using `docker build -t thesis-webapp -f Dockerfile . .`, the command commented in the docker file or the command referenced by your hosting service.
|
63 |
+
3. Run the docker image using `docker run --name thesis-webapp -e HOSTING=local USER=admin PW=test -p 8080:8080 thesis-webapp`, the command commented in the docker file or the command referenced by your hosting service.
|
64 |
4. The app will be available at `localhost:8080`. If you are using a hosting service, the port may be different.
|
65 |
|
66 |
### 🐳 Docker Image :
|
67 |
(This assumes you have set up docker desktop or are using a hosting service able to handle Docker images.)
|
68 |
|
69 |
+
1. Pull the docker image from ghcr using `docker pull ghcr.io/LennardZuendorf/thesis-webapp:1.3.1`.
|
70 |
+
2. Run the docker image in terminal using `docker run --name thesis-webapp -e HOSTING=local USER=admin PW=test -p 8080:8080 lennardzuendorf/thesis-webapp::1.3.1`, the command commented in the docker file or the command referenced by your hosting service.
|
71 |
3. The app will be available at `localhost:8080`. If you are using a hosting service, the port may be different.
|
72 |
|
73 |
## 📝 License and Credits:
|
|
|
81 |
|
82 |
See code for in detailed credits, work is strongly based on:
|
83 |
|
84 |
+
#### captum
|
85 |
+
- [GitHub](https://github.com/pytorch/captum)
|
86 |
+
- [Inital Paper](https://arxiv.org/pdf/2009.07896.pdf)
|
87 |
+
|
88 |
+
#### shap
|
89 |
+
- [GitHub](https://github.com/shap/shap)
|
90 |
+
- [Inital Paper](https://arxiv.org/abs/1705.07874)
|
91 |
+
|
92 |
#### GODEL
|
93 |
- [HGF Model Page](https://huggingface.co/microsoft/GODEL-v1_1-large-seq2seq?text=Hey+my+name+is+Mariama%21+How+are+you%3F)
|
94 |
- [Paper on HGF](https://huggingface.co/papers/2206.11309)
|
95 |
- [Paper Print](https://arxiv.org/abs/2206.11309)
|
96 |
|
97 |
+
#### Mistral 7B (Instruct)
|
98 |
+
- [HGF Model Page](https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.2)
|
99 |
+
- [Paper on HGF](https://huggingface.co/papers/2310.06825)
|
100 |
+
- [Paper Print](https://arxiv.org/abs/2310.06825)
|
101 |
+
|
102 |
|
103 |
#### Custom Component (/components/iframe/)
|
104 |
|
105 |
+
Is based on Gradio component, see individual README for full changelog.
|
main.py
CHANGED
@@ -300,10 +300,10 @@ with gr.Blocks(
|
|
300 |
],
|
301 |
inputs=[
|
302 |
user_prompt,
|
|
|
303 |
knowledge_input,
|
304 |
system_prompt,
|
305 |
model_selection,
|
306 |
-
xai_selection,
|
307 |
],
|
308 |
)
|
309 |
|
@@ -382,7 +382,7 @@ if __name__ == "__main__":
|
|
382 |
# use standard gradio launch option for hgf spaces
|
383 |
if os.environ["HOSTING"].lower() == "spaces":
|
384 |
# set password to deny public access
|
385 |
-
ui.launch(auth=("
|
386 |
|
387 |
# otherwise run the application on port 8080 in reload mode
|
388 |
## for local development, uses Docker for Prod deployment
|
|
|
300 |
],
|
301 |
inputs=[
|
302 |
user_prompt,
|
303 |
+
xai_selection,
|
304 |
knowledge_input,
|
305 |
system_prompt,
|
306 |
model_selection,
|
|
|
307 |
],
|
308 |
)
|
309 |
|
|
|
382 |
# use standard gradio launch option for hgf spaces
|
383 |
if os.environ["HOSTING"].lower() == "spaces":
|
384 |
# set password to deny public access
|
385 |
+
ui.launch(auth=(os.environ["USER"], os.environ["PW"]))
|
386 |
|
387 |
# otherwise run the application on port 8080 in reload mode
|
388 |
## for local development, uses Docker for Prod deployment
|
model/mistral.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
# Mistral model module for chat interaction and model instance control
|
2 |
|
3 |
# external imports
|
|
|
4 |
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig
|
5 |
import torch
|
6 |
import gradio as gr
|
@@ -92,8 +93,9 @@ def format_answer(answer: str):
|
|
92 |
# empty answer string
|
93 |
formatted_answer = ""
|
94 |
|
95 |
-
# splitting answer by instruction tokens
|
96 |
-
|
|
|
97 |
|
98 |
# checking if proper history got returned
|
99 |
if len(segments) > 1:
|
@@ -103,11 +105,10 @@ def format_answer(answer: str):
|
|
103 |
# return warning and full answer if not enough [/INST] tokens found
|
104 |
gr.Warning("""
|
105 |
There was an issue with answer formatting...\n
|
106 |
-
|
107 |
""")
|
108 |
formatted_answer = answer
|
109 |
|
110 |
-
print(f"CUT:\n {answer}\nINTO:\n{formatted_answer}")
|
111 |
return formatted_answer
|
112 |
|
113 |
|
|
|
1 |
# Mistral model module for chat interaction and model instance control
|
2 |
|
3 |
# external imports
|
4 |
+
import re
|
5 |
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig
|
6 |
import torch
|
7 |
import gradio as gr
|
|
|
93 |
# empty answer string
|
94 |
formatted_answer = ""
|
95 |
|
96 |
+
# splitting answer by instruction tokens using re and a pattern
|
97 |
+
pattern = r"\[/INST\]|\[ / INST\]|\[ / INST \]|\[/ INST \]"
|
98 |
+
segments = re.split(pattern, answer)
|
99 |
|
100 |
# checking if proper history got returned
|
101 |
if len(segments) > 1:
|
|
|
105 |
# return warning and full answer if not enough [/INST] tokens found
|
106 |
gr.Warning("""
|
107 |
There was an issue with answer formatting...\n
|
108 |
+
Returning the full answer.
|
109 |
""")
|
110 |
formatted_answer = answer
|
111 |
|
|
|
112 |
return formatted_answer
|
113 |
|
114 |
|
public/credits_dataprotection_license.md
CHANGED
@@ -1,18 +1,30 @@
|
|
1 |
# Credits
|
2 |
|
3 |
### Models
|
4 |
-
This implementation is build on GODEL by Microsoft, Inc.
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
##### GODEL
|
7 |
GODEL is an open source model by Microsoft. See [offical paper](https://arxiv.org/abs/2206.11309) for more information.
|
8 |
|
9 |
- the version used in this project is GODEL Large, see [huggingface model hub](https://huggingface.co/microsoft/GODEL-v1_1-large-seq2seq?text=Hey+my+name+is+Thomas%21+How+are+you%3F)
|
10 |
- the model as is a generative seq2seq transformer fine-tuned for goal directed dialog
|
11 |
-
- it supports context and knowledge base inputs
|
12 |
|
13 |
### Libraries
|
14 |
This project uses a number of open source libraries, only the most important ones are listed below.
|
15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
##### Shap
|
17 |
This application uses a custom version of the shap library, which is available at [GitHub](https://github.com/shap/shap).
|
18 |
|
@@ -23,7 +35,7 @@ This application uses a custom version of the shap library, which is available a
|
|
23 |
This application uses attention visualization inspired by the bertviz library, which is available at[GitHub](https://github.com/jessevig/bertviz). It doesn't actually use BERTViz.
|
24 |
|
25 |
- The bertviz was introduced by Vig et al. (2019), see [offical paper](https://arxiv.org/pdf/1906.05714.pdf) for more information.
|
26 |
-
- This project only uses
|
27 |
|
28 |
|
29 |
# Data Protection
|
|
|
1 |
# Credits
|
2 |
|
3 |
### Models
|
4 |
+
This implementation is build on GODEL by Microsoft, Inc. and Mistral 7B Instruct by Mistral AI.
|
5 |
+
|
6 |
+
##### Mistral 7B Instruct
|
7 |
+
Mistral 7B Instruct is an open source model by Mistral AI. See [offical paper](https://arxiv.org/abs/2310.06825) for more information.
|
8 |
+
|
9 |
+
- the version used in this project is Mistral 7B Instruct v0.2, see [huggingface model hub](https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.2)
|
10 |
+
- the model is an autoregressive language model fine-tuned for instruction following
|
11 |
|
12 |
##### GODEL
|
13 |
GODEL is an open source model by Microsoft. See [offical paper](https://arxiv.org/abs/2206.11309) for more information.
|
14 |
|
15 |
- the version used in this project is GODEL Large, see [huggingface model hub](https://huggingface.co/microsoft/GODEL-v1_1-large-seq2seq?text=Hey+my+name+is+Thomas%21+How+are+you%3F)
|
16 |
- the model as is a generative seq2seq transformer fine-tuned for goal directed dialog
|
|
|
17 |
|
18 |
### Libraries
|
19 |
This project uses a number of open source libraries, only the most important ones are listed below.
|
20 |
|
21 |
+
#### captum
|
22 |
+
This application uses the captum library for the interpretation of the Mistral 7B Instruct model. The captum library is available at [GitHub](https://github.com/pytorch/captum).
|
23 |
+
|
24 |
+
- Please refer to the [captum](https://captum.ai/) website for more information about the library.
|
25 |
+
- The used KernelExplainer is based on work by Lundberg et al. - see below for more information.
|
26 |
+
- For original paper about captum see [Inital Paper](https://arxiv.org/pdf/2009.07896.pdf).
|
27 |
+
|
28 |
##### Shap
|
29 |
This application uses a custom version of the shap library, which is available at [GitHub](https://github.com/shap/shap).
|
30 |
|
|
|
35 |
This application uses attention visualization inspired by the bertviz library, which is available at[GitHub](https://github.com/jessevig/bertviz). It doesn't actually use BERTViz.
|
36 |
|
37 |
- The bertviz was introduced by Vig et al. (2019), see [offical paper](https://arxiv.org/pdf/1906.05714.pdf) for more information.
|
38 |
+
- This project only uses decoder attention visualization with gradio and matplotlib and not BERTViz itself.
|
39 |
|
40 |
|
41 |
# Data Protection
|
utils/formatting.py
CHANGED
@@ -41,8 +41,24 @@ def format_output_text(output: list):
|
|
41 |
|
42 |
# add all other list items with a space in between
|
43 |
for txt in formatted_output[1:]:
|
44 |
-
# check if the token is a punctuation mark
|
45 |
-
if txt in [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
# add punctuation mark without space
|
47 |
output_str += txt
|
48 |
# add token with space if not empty
|
@@ -50,7 +66,7 @@ def format_output_text(output: list):
|
|
50 |
output_str += " " + txt
|
51 |
|
52 |
# return the combined string with multiple spaces removed
|
53 |
-
return re.sub("
|
54 |
|
55 |
|
56 |
# format the tokens by removing special tokens and special characters
|
|
|
41 |
|
42 |
# add all other list items with a space in between
|
43 |
for txt in formatted_output[1:]:
|
44 |
+
# check if the token is a punctuation mark or other special character
|
45 |
+
if txt in [
|
46 |
+
".",
|
47 |
+
",",
|
48 |
+
"!",
|
49 |
+
"?",
|
50 |
+
":",
|
51 |
+
";",
|
52 |
+
")",
|
53 |
+
"]",
|
54 |
+
"}",
|
55 |
+
"'",
|
56 |
+
'"',
|
57 |
+
"[",
|
58 |
+
"{",
|
59 |
+
"(",
|
60 |
+
"<",
|
61 |
+
]:
|
62 |
# add punctuation mark without space
|
63 |
output_str += txt
|
64 |
# add token with space if not empty
|
|
|
66 |
output_str += " " + txt
|
67 |
|
68 |
# return the combined string with multiple spaces removed
|
69 |
+
return re.sub(r"\s+", " ", output_str)
|
70 |
|
71 |
|
72 |
# format the tokens by removing special tokens and special characters
|