Spaces:
Runtime error
Runtime error
chore: file moving and renaming
Browse files- app.py +34 -16
- interpret.py → components/shap_explanation.py +0 -0
- visualize.py → explanation/interpret.py +0 -0
- explanation/visualize.py +0 -0
- model/llama2.md +6 -0
- model/llama2.py +66 -0
- model/mistral.md +6 -0
- mistral.py → model/mistral.py +19 -2
app.py
CHANGED
@@ -1,51 +1,67 @@
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
import mistral
|
3 |
-
import interpret as shap
|
4 |
-
import visualize as viz
|
5 |
import markdown
|
6 |
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
# credit: official python-markdown documentation (https://python-markdown.github.io/reference/)
|
11 |
with open(path, "r") as file:
|
12 |
text = file.read()
|
13 |
|
|
|
14 |
return markdown.markdown(text)
|
15 |
|
|
|
16 |
with gr.Blocks() as ui:
|
|
|
17 |
with gr.Row():
|
18 |
gr.Markdown(
|
19 |
"""
|
20 |
# Thesis Demo - AI Chat Application with XAI
|
21 |
### Select between tabs below for the different views.
|
22 |
""")
|
23 |
-
with
|
|
|
24 |
with gr.Row():
|
25 |
gr.Markdown(
|
26 |
"""
|
27 |
### ChatBot Demo
|
28 |
Mitral AI 7B Model fine-tuned for instruction and fully open source (see at [HGF](https://huggingface.co/mistralai/Mistral-7B-v0.1))
|
29 |
""")
|
30 |
-
|
31 |
with gr.Row():
|
32 |
chatbot = gr.Chatbot(layout="panel", show_copy_button=True,avatar_images=("./public/human.jpg","./public/bot.jpg"))
|
|
|
33 |
with gr.Row():
|
34 |
gr.Markdown(
|
35 |
"""
|
36 |
##### ⚠️ All Conversations are recorded for qa assurance and explanation functionality!
|
37 |
""")
|
|
|
38 |
with gr.Row():
|
39 |
prompt = gr.Textbox(label="Input Message")
|
|
|
40 |
with gr.Row():
|
41 |
with gr.Column(scale=1):
|
|
|
42 |
clear_btn = gr.ClearButton([prompt, chatbot])
|
43 |
with gr.Column(scale=1):
|
44 |
submit_btn = gr.Button("Submit")
|
45 |
|
|
|
46 |
submit_btn.click(mistral.chat, [prompt, chatbot], [prompt, chatbot])
|
47 |
prompt.submit(mistral.chat, [prompt, chatbot], [prompt, chatbot])
|
48 |
|
|
|
49 |
with gr.Tab("Explanations"):
|
50 |
with gr.Row():
|
51 |
gr.Markdown(
|
@@ -54,7 +70,7 @@ with gr.Blocks() as ui:
|
|
54 |
SHAP Visualization Dashboard adopted from [shapash](https://github.com/MAIF/shapash)
|
55 |
""")
|
56 |
|
57 |
-
|
58 |
with gr.Tab("SHAP Dashboard"):
|
59 |
with gr.Row():
|
60 |
gr.Markdown(
|
@@ -63,6 +79,7 @@ with gr.Blocks() as ui:
|
|
63 |
SHAP Visualization Dashboard adopted from [shapash](https://github.com/MAIF/shapash)
|
64 |
""")
|
65 |
|
|
|
66 |
with gr.Tab("Visualize Dashboard"):
|
67 |
with gr.Row():
|
68 |
gr.Markdown(
|
@@ -71,17 +88,18 @@ with gr.Blocks() as ui:
|
|
71 |
Visualization Dashboard adopted from [BERTViz](https://github.com/jessevig/bertviz)
|
72 |
""")
|
73 |
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
""")
|
81 |
|
|
|
82 |
with gr.Row():
|
83 |
with gr.Accordion("Credits, Data Protection and License", open=False):
|
84 |
-
gr.Markdown(value=load_md("credits_dataprotection_license.md"))
|
85 |
|
|
|
86 |
if __name__ == "__main__":
|
87 |
ui.launch(debug=True)
|
|
|
1 |
+
# main application file initializing the gradio based ui and calling other modules
|
2 |
+
|
3 |
+
# external package imports
|
4 |
import gradio as gr
|
|
|
|
|
|
|
5 |
import markdown
|
6 |
|
7 |
+
|
8 |
+
# internal imports
|
9 |
+
from model import mistral
|
10 |
+
from explanation import interpret as shap
|
11 |
+
|
12 |
+
|
13 |
+
# function to load md files in pthon as a string
|
14 |
+
def load_md(path):
|
15 |
|
16 |
# credit: official python-markdown documentation (https://python-markdown.github.io/reference/)
|
17 |
with open(path, "r") as file:
|
18 |
text = file.read()
|
19 |
|
20 |
+
# return markdown as a string
|
21 |
return markdown.markdown(text)
|
22 |
|
23 |
+
# ui interface based on Gradio Blocks (see documentation: https://www.gradio.app/docs/interface)
|
24 |
with gr.Blocks() as ui:
|
25 |
+
# header row with markdown based text
|
26 |
with gr.Row():
|
27 |
gr.Markdown(
|
28 |
"""
|
29 |
# Thesis Demo - AI Chat Application with XAI
|
30 |
### Select between tabs below for the different views.
|
31 |
""")
|
32 |
+
# ChatBot tab used to chat with the AI chatbot
|
33 |
+
with gr.Tab("AI ChatBot"):
|
34 |
with gr.Row():
|
35 |
gr.Markdown(
|
36 |
"""
|
37 |
### ChatBot Demo
|
38 |
Mitral AI 7B Model fine-tuned for instruction and fully open source (see at [HGF](https://huggingface.co/mistralai/Mistral-7B-v0.1))
|
39 |
""")
|
40 |
+
# row with chatbot ui displaying "conversation" with the model (see documentation: https://www.gradio.app/docs/chatbot)
|
41 |
with gr.Row():
|
42 |
chatbot = gr.Chatbot(layout="panel", show_copy_button=True,avatar_images=("./public/human.jpg","./public/bot.jpg"))
|
43 |
+
# disclaimer information row - data is recorded for shap dashboard and model explanability
|
44 |
with gr.Row():
|
45 |
gr.Markdown(
|
46 |
"""
|
47 |
##### ⚠️ All Conversations are recorded for qa assurance and explanation functionality!
|
48 |
""")
|
49 |
+
# row with input textbox
|
50 |
with gr.Row():
|
51 |
prompt = gr.Textbox(label="Input Message")
|
52 |
+
# row with columns for buttons to submit and clear content
|
53 |
with gr.Row():
|
54 |
with gr.Column(scale=1):
|
55 |
+
# default clear button which clearn the given components (see documentation: https://www.gradio.app/docs/clearbutton)
|
56 |
clear_btn = gr.ClearButton([prompt, chatbot])
|
57 |
with gr.Column(scale=1):
|
58 |
submit_btn = gr.Button("Submit")
|
59 |
|
60 |
+
# two functions performing the same action (triggered the model response), when the button is used or the textbox submit function is used (clicking enter).
|
61 |
submit_btn.click(mistral.chat, [prompt, chatbot], [prompt, chatbot])
|
62 |
prompt.submit(mistral.chat, [prompt, chatbot], [prompt, chatbot])
|
63 |
|
64 |
+
# explanations tab used to provide explanations for a specific conversation
|
65 |
with gr.Tab("Explanations"):
|
66 |
with gr.Row():
|
67 |
gr.Markdown(
|
|
|
70 |
SHAP Visualization Dashboard adopted from [shapash](https://github.com/MAIF/shapash)
|
71 |
""")
|
72 |
|
73 |
+
# shap dashboard tab for shapash inspired dashboard
|
74 |
with gr.Tab("SHAP Dashboard"):
|
75 |
with gr.Row():
|
76 |
gr.Markdown(
|
|
|
79 |
SHAP Visualization Dashboard adopted from [shapash](https://github.com/MAIF/shapash)
|
80 |
""")
|
81 |
|
82 |
+
# visualize dashboard to display global visualization provided by the BERTViz adoption
|
83 |
with gr.Tab("Visualize Dashboard"):
|
84 |
with gr.Row():
|
85 |
gr.Markdown(
|
|
|
88 |
Visualization Dashboard adopted from [BERTViz](https://github.com/jessevig/bertviz)
|
89 |
""")
|
90 |
|
91 |
+
# model overview tab for transparency
|
92 |
+
with gr.Tab("Model Overview"):
|
93 |
+
with gr.Tab("Mistral 7B Instruct"):
|
94 |
+
gr.Markdown(value=load_md("./model/mistral.md"))
|
95 |
+
with gr.Tab("LlaMa 2 7B Chat"):
|
96 |
+
gr.Markdown(value=load_md("./model/llama2.md"))
|
|
|
97 |
|
98 |
+
# final row to show legal information - credits, data protection and link to the LICENSE on GitHub
|
99 |
with gr.Row():
|
100 |
with gr.Accordion("Credits, Data Protection and License", open=False):
|
101 |
+
gr.Markdown(value=load_md("./public/credits_dataprotection_license.md"))
|
102 |
|
103 |
+
# launch function for Gradio Interface
|
104 |
if __name__ == "__main__":
|
105 |
ui.launch(debug=True)
|
interpret.py → components/shap_explanation.py
RENAMED
File without changes
|
visualize.py → explanation/interpret.py
RENAMED
File without changes
|
explanation/visualize.py
ADDED
File without changes
|
model/llama2.md
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
### Credits
|
2 |
+
|
3 |
+
### Data Protection
|
4 |
+
|
5 |
+
### License
|
6 |
+
This Product is licensed under the MIT license.
|
model/llama2.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from huggingface_hub import InferenceClient
|
2 |
+
import os
|
3 |
+
|
4 |
+
# huggingface token used to load closed off models
|
5 |
+
token = os.environ.get("HGFTOKEN")
|
6 |
+
|
7 |
+
# interference client created from mistral 7b instruction fine tuned model
|
8 |
+
# credit: copied 1:1 from Hugging Face, Inc/ Omar Sanseviero (see https://huggingface.co/spaces/osanseviero/mistral-super-fast/)
|
9 |
+
interference = InferenceClient(
|
10 |
+
"mistralai/Mistral-7B-Instruct-v0.1"
|
11 |
+
)
|
12 |
+
|
13 |
+
# default model settings
|
14 |
+
model_temperature = 0.7
|
15 |
+
model_max_new_tokens = 320
|
16 |
+
model_top_p = 0.95
|
17 |
+
model_repetition_penalty = 1.1
|
18 |
+
|
19 |
+
# chat function - basically the main function calling other functions and returning a response to showcase in chatbot ui
|
20 |
+
def chat (prompt, history,):
|
21 |
+
|
22 |
+
# creating formatted prompt and calling for an answer from the model
|
23 |
+
formatted_prompt = format_prompt(prompt, history)
|
24 |
+
answer=respond(formatted_prompt)
|
25 |
+
|
26 |
+
# updating the chat history with the new answer
|
27 |
+
history.append((prompt, answer))
|
28 |
+
|
29 |
+
# returning the chat history to be displayed in the chatbot ui
|
30 |
+
return "",history
|
31 |
+
|
32 |
+
# function to format prompt in a way that is understandable for the text generation model
|
33 |
+
# credit: copied 1:1 from Hugging Face, Inc/ Omar Sanseviero (see https://huggingface.co/spaces/osanseviero/mistral-super-fast/)
|
34 |
+
def format_prompt(message, history):
|
35 |
+
prompt = "<s>"
|
36 |
+
|
37 |
+
# labeling each message in the history as bot or user
|
38 |
+
for user_prompt, bot_response in history:
|
39 |
+
prompt += f"[INST] {user_prompt} [/INST]"
|
40 |
+
prompt += f" {bot_response}</s> "
|
41 |
+
prompt += f"[INST] {message} [/INST]"
|
42 |
+
return prompt
|
43 |
+
|
44 |
+
# function to get the response
|
45 |
+
# credit: minimally changed from Hugging Face, Inc/ Omar Sanseviero (see https://huggingface.co/spaces/osanseviero/mistral-super-fast/)
|
46 |
+
def respond(formatted_prompt):
|
47 |
+
|
48 |
+
# setting model temperature and
|
49 |
+
temperature = float(model_temperature)
|
50 |
+
if temperature < 1e-2:
|
51 |
+
temperature = 1e-2
|
52 |
+
top_p = float(model_top_p)
|
53 |
+
|
54 |
+
# creating model arguments/settings
|
55 |
+
generate_kwargs = dict(
|
56 |
+
temperature=temperature,
|
57 |
+
max_new_tokens=model_max_new_tokens,
|
58 |
+
top_p=top_p,
|
59 |
+
repetition_penalty=model_repetition_penalty,
|
60 |
+
do_sample=True,
|
61 |
+
seed=42,
|
62 |
+
)
|
63 |
+
|
64 |
+
# calling for model output and returning it
|
65 |
+
output = interference.text_generation(formatted_prompt, **generate_kwargs, stream=False, details=True, return_full_text=False).generated_text
|
66 |
+
return output
|
model/mistral.md
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
### Credits
|
2 |
+
|
3 |
+
### Data Protection
|
4 |
+
|
5 |
+
### License
|
6 |
+
This Product is licensed under the MIT license.
|
mistral.py → model/mistral.py
RENAMED
@@ -1,41 +1,57 @@
|
|
1 |
from huggingface_hub import InferenceClient
|
2 |
import os
|
3 |
-
import gradio as gr
|
4 |
|
|
|
5 |
token = os.environ.get("HGFTOKEN")
|
6 |
|
|
|
|
|
7 |
interference = InferenceClient(
|
8 |
"mistralai/Mistral-7B-Instruct-v0.1"
|
9 |
)
|
10 |
|
|
|
11 |
model_temperature = 0.7
|
12 |
-
model_max_new_tokens =
|
13 |
model_top_p = 0.95
|
14 |
model_repetition_penalty = 1.1
|
15 |
|
|
|
16 |
def chat (prompt, history,):
|
17 |
|
|
|
18 |
formatted_prompt = format_prompt(prompt, history)
|
19 |
answer=respond(formatted_prompt)
|
20 |
|
|
|
21 |
history.append((prompt, answer))
|
22 |
|
|
|
23 |
return "",history
|
24 |
|
|
|
|
|
25 |
def format_prompt(message, history):
|
26 |
prompt = "<s>"
|
|
|
|
|
27 |
for user_prompt, bot_response in history:
|
28 |
prompt += f"[INST] {user_prompt} [/INST]"
|
29 |
prompt += f" {bot_response}</s> "
|
30 |
prompt += f"[INST] {message} [/INST]"
|
31 |
return prompt
|
32 |
|
|
|
|
|
33 |
def respond(formatted_prompt):
|
|
|
|
|
34 |
temperature = float(model_temperature)
|
35 |
if temperature < 1e-2:
|
36 |
temperature = 1e-2
|
37 |
top_p = float(model_top_p)
|
38 |
|
|
|
39 |
generate_kwargs = dict(
|
40 |
temperature=temperature,
|
41 |
max_new_tokens=model_max_new_tokens,
|
@@ -45,5 +61,6 @@ def respond(formatted_prompt):
|
|
45 |
seed=42,
|
46 |
)
|
47 |
|
|
|
48 |
output = interference.text_generation(formatted_prompt, **generate_kwargs, stream=False, details=True, return_full_text=False).generated_text
|
49 |
return output
|
|
|
1 |
from huggingface_hub import InferenceClient
|
2 |
import os
|
|
|
3 |
|
4 |
+
# huggingface token used to load closed off models
|
5 |
token = os.environ.get("HGFTOKEN")
|
6 |
|
7 |
+
# interference client created from mistral 7b instruction fine tuned model
|
8 |
+
# credit: copied 1:1 from Hugging Face, Inc/ Omar Sanseviero (see https://huggingface.co/spaces/osanseviero/mistral-super-fast/)
|
9 |
interference = InferenceClient(
|
10 |
"mistralai/Mistral-7B-Instruct-v0.1"
|
11 |
)
|
12 |
|
13 |
+
# default model settings
|
14 |
model_temperature = 0.7
|
15 |
+
model_max_new_tokens = 320
|
16 |
model_top_p = 0.95
|
17 |
model_repetition_penalty = 1.1
|
18 |
|
19 |
+
# chat function - basically the main function calling other functions and returning a response to showcase in chatbot ui
|
20 |
def chat (prompt, history,):
|
21 |
|
22 |
+
# creating formatted prompt and calling for an answer from the model
|
23 |
formatted_prompt = format_prompt(prompt, history)
|
24 |
answer=respond(formatted_prompt)
|
25 |
|
26 |
+
# updating the chat history with the new answer
|
27 |
history.append((prompt, answer))
|
28 |
|
29 |
+
# returning the chat history to be displayed in the chatbot ui
|
30 |
return "",history
|
31 |
|
32 |
+
# function to format prompt in a way that is understandable for the text generation model
|
33 |
+
# credit: copied 1:1 from Hugging Face, Inc/ Omar Sanseviero (see https://huggingface.co/spaces/osanseviero/mistral-super-fast/)
|
34 |
def format_prompt(message, history):
|
35 |
prompt = "<s>"
|
36 |
+
|
37 |
+
# labeling each message in the history as bot or user
|
38 |
for user_prompt, bot_response in history:
|
39 |
prompt += f"[INST] {user_prompt} [/INST]"
|
40 |
prompt += f" {bot_response}</s> "
|
41 |
prompt += f"[INST] {message} [/INST]"
|
42 |
return prompt
|
43 |
|
44 |
+
# function to get the response
|
45 |
+
# credit: minimally changed from Hugging Face, Inc/ Omar Sanseviero (see https://huggingface.co/spaces/osanseviero/mistral-super-fast/)
|
46 |
def respond(formatted_prompt):
|
47 |
+
|
48 |
+
# setting model temperature and
|
49 |
temperature = float(model_temperature)
|
50 |
if temperature < 1e-2:
|
51 |
temperature = 1e-2
|
52 |
top_p = float(model_top_p)
|
53 |
|
54 |
+
# creating model arguments/settings
|
55 |
generate_kwargs = dict(
|
56 |
temperature=temperature,
|
57 |
max_new_tokens=model_max_new_tokens,
|
|
|
61 |
seed=42,
|
62 |
)
|
63 |
|
64 |
+
# calling for model output and returning it
|
65 |
output = interference.text_generation(formatted_prompt, **generate_kwargs, stream=False, details=True, return_full_text=False).generated_text
|
66 |
return output
|