Spaces:
Sleeping
Sleeping
seanpedrickcase
commited on
Commit
•
6417426
1
Parent(s):
590d9cd
Changed small and large models. Large model should now work for summarisation
Browse files- app.py +113 -56
- chatfuncs/chatfuncs.py +5 -6
- chatfuncs/helper_functions.py +143 -0
- chatfuncs/prompts.py +30 -0
app.py
CHANGED
@@ -9,9 +9,11 @@ import os
|
|
9 |
|
10 |
# Need to overwrite version of gradio present in Huggingface spaces as it doesn't have like buttons/avatars (Oct 2023)
|
11 |
#os.system("pip uninstall -y gradio")
|
12 |
-
os.system("pip install gradio==3.50.0")
|
13 |
|
14 |
-
|
|
|
|
|
15 |
#from langchain.embeddings import HuggingFaceEmbeddings#, HuggingFaceInstructEmbeddings
|
16 |
#from langchain.vectorstores import FAISS
|
17 |
import gradio as gr
|
@@ -21,13 +23,30 @@ from transformers import AutoTokenizer
|
|
21 |
# Alternative model sources
|
22 |
import ctransformers
|
23 |
|
24 |
-
|
|
|
25 |
|
26 |
import chatfuncs.chatfuncs as chatf
|
27 |
|
|
|
|
|
28 |
# Disable cuda devices if necessary
|
29 |
#os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
def create_hf_model(model_name):
|
32 |
|
33 |
tokenizer = AutoTokenizer.from_pretrained(model_name, model_max_length = chatf.context_length)
|
@@ -62,8 +81,8 @@ def load_model(model_type, gpu_layers, gpu_config=None, cpu_config=None, torch_d
|
|
62 |
if torch_device is None:
|
63 |
torch_device = chatf.torch_device
|
64 |
|
65 |
-
if model_type == "Mistral
|
66 |
-
hf_checkpoint = '
|
67 |
|
68 |
if torch_device == "cuda":
|
69 |
gpu_config.update_gpu(gpu_layers)
|
@@ -76,34 +95,42 @@ def load_model(model_type, gpu_layers, gpu_config=None, cpu_config=None, torch_d
|
|
76 |
print(vars(gpu_config))
|
77 |
print(vars(cpu_config))
|
78 |
|
79 |
-
|
80 |
#model = ctransformers.AutoModelForCausalLM.from_pretrained('Aryanne/Orca-Mini-3B-gguf', model_type='llama', model_file='q5_0-orca-mini-3b.gguf', **vars(gpu_config)) # **asdict(CtransRunConfig_cpu())
|
81 |
#model = ctransformers.AutoModelForCausalLM.from_pretrained('Aryanne/Wizard-Orca-3B-gguf', model_type='llama', model_file='q4_1-wizard-orca-3b.gguf', **vars(gpu_config)) # **asdict(CtransRunConfig_cpu())
|
82 |
#model = ctransformers.AutoModelForCausalLM.from_pretrained('TheBloke/Mistral-7B-OpenOrca-GGUF', model_type='mistral', model_file='mistral-7b-openorca.Q4_K_M.gguf', **vars(gpu_config), hf=True) # **asdict(CtransRunConfig_cpu())
|
83 |
-
|
84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
#model = ctransformers.AutoModelForCausalLM.from_pretrained('Aryanne/Orca-Mini-3B-gguf', model_type='llama', model_file='q5_0-orca-mini-3b.gguf', **vars(cpu_config)) #**asdict(CtransRunConfig_gpu())
|
86 |
#model = ctransformers.AutoModelForCausalLM.from_pretrained('Aryanne/Wizard-Orca-3B-gguf', model_type='llama', model_file='q4_1-wizard-orca-3b.gguf', **vars(cpu_config)) # **asdict(CtransRunConfig_cpu())
|
87 |
#model = ctransformers.AutoModelForCausalLM.from_pretrained('TheBloke/Mistral-7B-OpenOrca-GGUF', model_type='mistral', model_file='mistral-7b-openorca.Q4_K_M.gguf', **vars(cpu_config), hf=True) # **asdict(CtransRunConfig_cpu())
|
|
|
|
|
88 |
|
89 |
-
|
90 |
-
#summariser = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
91 |
|
92 |
-
|
93 |
-
|
94 |
-
summariser = []
|
95 |
|
96 |
-
|
|
|
|
|
|
|
|
|
97 |
# Huggingface chat model
|
98 |
hf_checkpoint = 'stacked-summaries/flan-t5-large-stacked-samsum-1024'#'declare-lab/flan-alpaca-base' # # #
|
99 |
|
100 |
summariser, tokenizer, model_type = create_hf_model(model_name = hf_checkpoint)
|
101 |
|
102 |
-
if model_type == "
|
103 |
# Huggingface chat model
|
104 |
-
hf_checkpoint = '
|
105 |
-
|
106 |
-
|
107 |
summariser, tokenizer, model_type = create_hf_model(model_name = hf_checkpoint)
|
108 |
|
109 |
chatf.model = summariser
|
@@ -116,70 +143,93 @@ def load_model(model_type, gpu_layers, gpu_config=None, cpu_config=None, torch_d
|
|
116 |
return model_type, load_confirmation, model_type
|
117 |
|
118 |
# Both models are loaded on app initialisation so that users don't have to wait for the models to be downloaded
|
119 |
-
|
120 |
-
|
121 |
|
122 |
-
model_type = "
|
123 |
load_model(model_type, chatf.gpu_layers, chatf.gpu_config, chatf.cpu_config, chatf.torch_device)
|
124 |
|
125 |
-
model_type = "
|
126 |
load_model(model_type, 0, chatf.gpu_config, chatf.cpu_config, chatf.torch_device)
|
127 |
|
128 |
today = datetime.now().strftime("%d%m%Y")
|
129 |
today_rev = datetime.now().strftime("%Y%m%d")
|
130 |
|
131 |
-
def summarise_text(text, text_df, length_slider,
|
132 |
|
133 |
-
if text_df
|
134 |
-
|
135 |
-
|
136 |
|
137 |
-
in_text_df = pd.DataFrame({
|
138 |
|
139 |
else:
|
140 |
-
in_text_df = pd.read_csv(text_df.name, delimiter = ",", low_memory=False, encoding='cp1252')
|
141 |
-
|
142 |
|
143 |
-
|
144 |
-
summarised_text = chatf.model(list(in_text_df[in_colnames_list_first]), max_length=length_slider)
|
145 |
|
146 |
-
if model_type
|
|
|
|
|
|
|
|
|
|
|
147 |
|
148 |
length = str(length_slider)
|
149 |
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
156 |
|
157 |
-
|
158 |
|
159 |
-
|
160 |
|
161 |
-
#summarised_text =
|
162 |
|
163 |
-
summarised_text
|
164 |
|
165 |
-
if text_df
|
166 |
-
if model_type != "Mistral
|
167 |
summarised_text_out = summarised_text[0].values()
|
168 |
|
169 |
-
if model_type == "Mistral
|
170 |
summarised_text_out = summarised_text
|
171 |
|
172 |
else:
|
173 |
summarised_text_out = [d['summary_text'] for d in summarised_text] #summarised_text[0].values()
|
174 |
|
175 |
output_name = "summarise_output_" + today_rev + ".csv"
|
176 |
-
output_df = pd.DataFrame({"Original text":in_text_df[
|
177 |
-
|
178 |
-
|
179 |
summarised_text_out_str = str(output_df["Summarised text"][0])#.str.replace("dict_values([","").str.replace("])",""))
|
180 |
-
|
181 |
output_df.to_csv(output_name, index = None)
|
182 |
-
|
183 |
return summarised_text_out_str, output_name
|
184 |
|
185 |
# ## Gradio app - summarise
|
@@ -187,12 +237,13 @@ block = gr.Blocks(theme = gr.themes.Base())
|
|
187 |
|
188 |
with block:
|
189 |
|
|
|
190 |
model_type_state = gr.State(model_type)
|
191 |
|
192 |
gr.Markdown(
|
193 |
"""
|
194 |
# Text summariser
|
195 |
-
Enter open text below to get a summary. You can copy and paste text directly, or upload a file and specify the column that you want to summarise.
|
196 |
""")
|
197 |
|
198 |
with gr.Tab("Summariser"):
|
@@ -203,13 +254,13 @@ with block:
|
|
203 |
|
204 |
with gr.Accordion("Summarise open text from a file", open = False):
|
205 |
in_text_df = gr.File(label="Input text from file")
|
206 |
-
|
207 |
type="numpy", row_count=(1,"fixed"), col_count = (1,"fixed"),
|
208 |
headers=["Open text column name"])#, "Address column name 2", "Address column name 3", "Address column name 4"])
|
209 |
|
210 |
with gr.Row():
|
211 |
summarise_btn = gr.Button("Summarise")
|
212 |
-
length_slider = gr.Slider(minimum = 30, maximum =
|
213 |
|
214 |
with gr.Row():
|
215 |
output_single_text = gr.Textbox(label="Output example (first example in dataset)")
|
@@ -219,18 +270,24 @@ with block:
|
|
219 |
#out_passages = gr.Slider(minimum=1, value = 2, maximum=10, step=1, label="Choose number of passages to retrieve from the document. Numbers greater than 2 may lead to increased hallucinations or input text being truncated.")
|
220 |
#temp_slide = gr.Slider(minimum=0.1, value = 0.1, maximum=1, step=0.1, label="Choose temperature setting for response generation.")
|
221 |
with gr.Row():
|
222 |
-
model_choice = gr.Radio(label="Choose a summariser model", value="
|
223 |
change_model_button = gr.Button(value="Load model", scale=0)
|
224 |
with gr.Accordion("Choose number of model layers to send to GPU (WARNING: please don't modify unless you are sure you have a GPU).", open = False):
|
225 |
-
gpu_layer_choice = gr.Slider(label="Choose number of model layers to send to GPU.", value=0, minimum=0, maximum=
|
226 |
|
227 |
load_text = gr.Text(label="Load status")
|
228 |
|
229 |
|
|
|
|
|
|
|
230 |
change_model_button.click(fn=load_model, inputs=[model_choice, gpu_layer_choice], outputs = [model_type_state, load_text, current_model])
|
231 |
|
232 |
-
summarise_btn.click(fn=summarise_text, inputs=[in_text,
|
233 |
outputs=[output_single_text, output_file], api_name="summarise_single_text")
|
|
|
|
|
|
|
234 |
|
235 |
block.queue(concurrency_count=1).launch()
|
236 |
# -
|
|
|
9 |
|
10 |
# Need to overwrite version of gradio present in Huggingface spaces as it doesn't have like buttons/avatars (Oct 2023)
|
11 |
#os.system("pip uninstall -y gradio")
|
12 |
+
#os.system("pip install gradio==3.50.0")
|
13 |
|
14 |
+
|
15 |
+
|
16 |
+
from typing import Type
|
17 |
#from langchain.embeddings import HuggingFaceEmbeddings#, HuggingFaceInstructEmbeddings
|
18 |
#from langchain.vectorstores import FAISS
|
19 |
import gradio as gr
|
|
|
23 |
# Alternative model sources
|
24 |
import ctransformers
|
25 |
|
26 |
+
|
27 |
+
PandasDataFrame = Type[pd.DataFrame]
|
28 |
|
29 |
import chatfuncs.chatfuncs as chatf
|
30 |
|
31 |
+
from chatfuncs.helper_functions import dummy_function, display_info, put_columns_in_df, put_columns_in_join_df, get_temp_folder_path, empty_folder
|
32 |
+
|
33 |
# Disable cuda devices if necessary
|
34 |
#os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
|
35 |
|
36 |
+
from torch import cuda, backends
|
37 |
+
|
38 |
+
# Check for torch cuda
|
39 |
+
print("Is CUDA enabled? ", cuda.is_available())
|
40 |
+
print("Is a CUDA device available on this computer?", backends.cudnn.enabled)
|
41 |
+
if cuda.is_available():
|
42 |
+
torch_device = "cuda"
|
43 |
+
os.system("nvidia-smi")
|
44 |
+
|
45 |
+
else:
|
46 |
+
torch_device = "cpu"
|
47 |
+
|
48 |
+
print("Device used is: ", torch_device)
|
49 |
+
|
50 |
def create_hf_model(model_name):
|
51 |
|
52 |
tokenizer = AutoTokenizer.from_pretrained(model_name, model_max_length = chatf.context_length)
|
|
|
81 |
if torch_device is None:
|
82 |
torch_device = chatf.torch_device
|
83 |
|
84 |
+
if model_type == "Mistral Nous Capybara 4k (larger, slow)":
|
85 |
+
hf_checkpoint = 'NousResearch/Nous-Capybara-7B-V1.9-GGUF'
|
86 |
|
87 |
if torch_device == "cuda":
|
88 |
gpu_config.update_gpu(gpu_layers)
|
|
|
95 |
print(vars(gpu_config))
|
96 |
print(vars(cpu_config))
|
97 |
|
98 |
+
try:
|
99 |
#model = ctransformers.AutoModelForCausalLM.from_pretrained('Aryanne/Orca-Mini-3B-gguf', model_type='llama', model_file='q5_0-orca-mini-3b.gguf', **vars(gpu_config)) # **asdict(CtransRunConfig_cpu())
|
100 |
#model = ctransformers.AutoModelForCausalLM.from_pretrained('Aryanne/Wizard-Orca-3B-gguf', model_type='llama', model_file='q4_1-wizard-orca-3b.gguf', **vars(gpu_config)) # **asdict(CtransRunConfig_cpu())
|
101 |
#model = ctransformers.AutoModelForCausalLM.from_pretrained('TheBloke/Mistral-7B-OpenOrca-GGUF', model_type='mistral', model_file='mistral-7b-openorca.Q4_K_M.gguf', **vars(gpu_config), hf=True) # **asdict(CtransRunConfig_cpu())
|
102 |
+
#model = ctransformers.AutoModelForCausalLM.from_pretrained('TheBloke/OpenHermes-2.5-Mistral-7B-16k-GGUF', model_type='mistral', model_file='openhermes-2.5-mistral-7b-16k.Q4_K_M.gguf', **vars(gpu_config), hf=True) # **asdict(CtransRunConfig_cpu())
|
103 |
+
model = ctransformers.AutoModelForCausalLM.from_pretrained('NousResearch/Nous-Capybara-7B-V1.9-GGUF', model_type='mistral', model_file='Capybara-7B-V1.9-Q5_K_M.gguf', **vars(gpu_config), hf=True) # **asdict(CtransRunConfig_cpu())
|
104 |
+
|
105 |
+
|
106 |
+
tokenizer = AutoTokenizer.from_pretrained("NousResearch/Nous-Capybara-7B-V1.9")
|
107 |
+
summariser = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
108 |
+
|
109 |
+
except:
|
110 |
#model = ctransformers.AutoModelForCausalLM.from_pretrained('Aryanne/Orca-Mini-3B-gguf', model_type='llama', model_file='q5_0-orca-mini-3b.gguf', **vars(cpu_config)) #**asdict(CtransRunConfig_gpu())
|
111 |
#model = ctransformers.AutoModelForCausalLM.from_pretrained('Aryanne/Wizard-Orca-3B-gguf', model_type='llama', model_file='q4_1-wizard-orca-3b.gguf', **vars(cpu_config)) # **asdict(CtransRunConfig_cpu())
|
112 |
#model = ctransformers.AutoModelForCausalLM.from_pretrained('TheBloke/Mistral-7B-OpenOrca-GGUF', model_type='mistral', model_file='mistral-7b-openorca.Q4_K_M.gguf', **vars(cpu_config), hf=True) # **asdict(CtransRunConfig_cpu())
|
113 |
+
#model = ctransformers.AutoModelForCausalLM.from_pretrained('TheBloke/OpenHermes-2.5-Mistral-7B-16k-GGUF', model_type='mistral', model_file='openhermes-2.5-mistral-7b-16k.Q4_K_M.gguf', **vars(gpu_config), hf=True) # **asdict(CtransRunConfig_cpu())
|
114 |
+
model = ctransformers.AutoModelForCausalLM.from_pretrained('NousResearch/Nous-Capybara-7B-V1.9-GGUF', model_type='mistral', model_file='Capybara-7B-V1.9-Q5_K_M.gguf', **vars(gpu_config), hf=True) # **asdict(CtransRunConfig_cpu())
|
115 |
|
116 |
+
#tokenizer = ctransformers.AutoTokenizer.from_pretrained(model)
|
|
|
117 |
|
118 |
+
tokenizer = AutoTokenizer.from_pretrained("NousResearch/Nous-Capybara-7B-V1.9")
|
119 |
+
summariser = pipeline("text-generation", model=model, tokenizer=tokenizer) # model
|
|
|
120 |
|
121 |
+
#model = []
|
122 |
+
#tokenizer = []
|
123 |
+
#summariser = []
|
124 |
+
|
125 |
+
if model_type == "Flan T5 Large Stacked Samsum 1k":
|
126 |
# Huggingface chat model
|
127 |
hf_checkpoint = 'stacked-summaries/flan-t5-large-stacked-samsum-1024'#'declare-lab/flan-alpaca-base' # # #
|
128 |
|
129 |
summariser, tokenizer, model_type = create_hf_model(model_name = hf_checkpoint)
|
130 |
|
131 |
+
if model_type == "Long T5 Global Base 16k Book Summary":
|
132 |
# Huggingface chat model
|
133 |
+
hf_checkpoint = 'pszemraj/long-t5-tglobal-base-16384-book-summary' #'philschmid/flan-t5-small-stacked-samsum'#'declare-lab/flan-alpaca-base' # # #
|
|
|
|
|
134 |
summariser, tokenizer, model_type = create_hf_model(model_name = hf_checkpoint)
|
135 |
|
136 |
chatf.model = summariser
|
|
|
143 |
return model_type, load_confirmation, model_type
|
144 |
|
145 |
# Both models are loaded on app initialisation so that users don't have to wait for the models to be downloaded
|
146 |
+
model_type = "Mistral Nous Capybara 4k (larger, slow)"
|
147 |
+
load_model(model_type, chatf.gpu_layers, chatf.gpu_config, chatf.cpu_config, chatf.torch_device)
|
148 |
|
149 |
+
model_type = "Flan T5 Large Stacked Samsum 1k"
|
150 |
load_model(model_type, chatf.gpu_layers, chatf.gpu_config, chatf.cpu_config, chatf.torch_device)
|
151 |
|
152 |
+
model_type = "Long T5 Global Base 16k Book Summary"
|
153 |
load_model(model_type, 0, chatf.gpu_config, chatf.cpu_config, chatf.torch_device)
|
154 |
|
155 |
today = datetime.now().strftime("%d%m%Y")
|
156 |
today_rev = datetime.now().strftime("%Y%m%d")
|
157 |
|
158 |
+
def summarise_text(text, text_df, length_slider, in_colname, model_type):
|
159 |
|
160 |
+
if text_df.empty:
|
161 |
+
in_colname="text"
|
162 |
+
in_colname_list_first = in_colname
|
163 |
|
164 |
+
in_text_df = pd.DataFrame({in_colname_list_first:[text]})
|
165 |
|
166 |
else:
|
167 |
+
in_text_df = text_df #pd.read_csv(text_df.name, delimiter = ",", low_memory=False, encoding='cp1252')
|
168 |
+
in_colname_list_first = in_colname.tolist()[0][0]
|
169 |
|
170 |
+
print(model_type)
|
|
|
171 |
|
172 |
+
if model_type != "Mistral Nous Capybara 4k (larger, slow)":
|
173 |
+
summarised_text = chatf.model(list(in_text_df[in_colname_list_first]), max_length=length_slider)
|
174 |
+
|
175 |
+
print(summarised_text)
|
176 |
+
|
177 |
+
if model_type == "Mistral Nous Capybara 4k (larger, slow)":
|
178 |
|
179 |
length = str(length_slider)
|
180 |
|
181 |
+
from chatfuncs.prompts import nous_capybara_prompt
|
182 |
+
|
183 |
+
formatted_string = nous_capybara_prompt.format(length=length, text=text)
|
184 |
+
#formatted_string = open_hermes_prompt.format(length=length, text=text)
|
185 |
+
|
186 |
+
# print(formatted_string)
|
187 |
+
|
188 |
+
#for output in chatf.model(formatted_string, max_length = 1000):#, stream=True):
|
189 |
+
for output in chatf.model(formatted_string, max_length = 10000):#, stream=True):
|
190 |
+
print(output, end="", flush=True)
|
191 |
+
|
192 |
+
output_str = output['generated_text']
|
193 |
+
|
194 |
+
# Find the index of 'ASSISTANT: ' to select only text after this location
|
195 |
+
index = output_str.find('ASSISTANT: ')
|
196 |
+
|
197 |
+
# Check if 'ASSISTANT: ' is found in the string
|
198 |
+
if index != -1:
|
199 |
+
# Add the length of 'ASSISTANT: ' to the index to start from the end of this substring
|
200 |
+
start_index = index + len('ASSISTANT: ')
|
201 |
+
|
202 |
+
# Slice the string from this point to the end
|
203 |
+
assistant_text = output_str[start_index:]
|
204 |
+
else:
|
205 |
+
assistant_text = "ASSISTANT: not found in text"
|
206 |
|
207 |
+
print(assistant_text)
|
208 |
|
209 |
+
summarised_text = assistant_text#chatf.model(formatted_string, max_length = 1000)#, max_new_tokens=length_slider)
|
210 |
|
211 |
+
#summarised_text = "Mistral Nous Capybara 4k summaries currently not working. Sorry!"
|
212 |
|
213 |
+
#rint(summarised_text)
|
214 |
|
215 |
+
if text_df.empty:
|
216 |
+
if model_type != "Mistral Nous Capybara 4k (larger, slow)":
|
217 |
summarised_text_out = summarised_text[0].values()
|
218 |
|
219 |
+
if model_type == "Mistral Nous Capybara 4k (larger, slow)":
|
220 |
summarised_text_out = summarised_text
|
221 |
|
222 |
else:
|
223 |
summarised_text_out = [d['summary_text'] for d in summarised_text] #summarised_text[0].values()
|
224 |
|
225 |
output_name = "summarise_output_" + today_rev + ".csv"
|
226 |
+
output_df = pd.DataFrame({"Original text":in_text_df[in_colname_list_first],
|
227 |
+
"Summarised text":summarised_text_out})
|
228 |
+
|
229 |
summarised_text_out_str = str(output_df["Summarised text"][0])#.str.replace("dict_values([","").str.replace("])",""))
|
230 |
+
|
231 |
output_df.to_csv(output_name, index = None)
|
232 |
+
|
233 |
return summarised_text_out_str, output_name
|
234 |
|
235 |
# ## Gradio app - summarise
|
|
|
237 |
|
238 |
with block:
|
239 |
|
240 |
+
data_state = gr.State(pd.DataFrame())
|
241 |
model_type_state = gr.State(model_type)
|
242 |
|
243 |
gr.Markdown(
|
244 |
"""
|
245 |
# Text summariser
|
246 |
+
Enter open text below to get a summary. You can copy and paste text directly, or upload a file and specify the column that you want to summarise. The default small model will be able to summarise up to about 16,00 words, but the quality may not be great. The larger model around 900 words of better quality. Summarisation with Mistral Nous Capybara 4k works on up to around 4,000 words, and may give a higher quality summary, but will be slow, and it may not respect your desired maximum word count.
|
247 |
""")
|
248 |
|
249 |
with gr.Tab("Summariser"):
|
|
|
254 |
|
255 |
with gr.Accordion("Summarise open text from a file", open = False):
|
256 |
in_text_df = gr.File(label="Input text from file")
|
257 |
+
in_colname = gr.Dataframe(label="Write the column name for the open text to summarise",
|
258 |
type="numpy", row_count=(1,"fixed"), col_count = (1,"fixed"),
|
259 |
headers=["Open text column name"])#, "Address column name 2", "Address column name 3", "Address column name 4"])
|
260 |
|
261 |
with gr.Row():
|
262 |
summarise_btn = gr.Button("Summarise")
|
263 |
+
length_slider = gr.Slider(minimum = 30, maximum = 500, value = 100, step = 10, label = "Maximum length of summary")
|
264 |
|
265 |
with gr.Row():
|
266 |
output_single_text = gr.Textbox(label="Output example (first example in dataset)")
|
|
|
270 |
#out_passages = gr.Slider(minimum=1, value = 2, maximum=10, step=1, label="Choose number of passages to retrieve from the document. Numbers greater than 2 may lead to increased hallucinations or input text being truncated.")
|
271 |
#temp_slide = gr.Slider(minimum=0.1, value = 0.1, maximum=1, step=0.1, label="Choose temperature setting for response generation.")
|
272 |
with gr.Row():
|
273 |
+
model_choice = gr.Radio(label="Choose a summariser model", value="Long T5 Global Base 16k Book Summary", choices = ["Long T5 Global Base 16k Book Summary", "Flan T5 Large Stacked Samsum 1k", "Mistral Nous Capybara 4k (larger, slow)"])
|
274 |
change_model_button = gr.Button(value="Load model", scale=0)
|
275 |
with gr.Accordion("Choose number of model layers to send to GPU (WARNING: please don't modify unless you are sure you have a GPU).", open = False):
|
276 |
+
gpu_layer_choice = gr.Slider(label="Choose number of model layers to send to GPU.", value=0, minimum=0, maximum=100, step = 1, visible=True)
|
277 |
|
278 |
load_text = gr.Text(label="Load status")
|
279 |
|
280 |
|
281 |
+
# Update dropdowns upon initial file load
|
282 |
+
in_text_df.upload(put_columns_in_df, inputs=[in_text_df, in_colname], outputs=[in_colname, data_state])
|
283 |
+
|
284 |
change_model_button.click(fn=load_model, inputs=[model_choice, gpu_layer_choice], outputs = [model_type_state, load_text, current_model])
|
285 |
|
286 |
+
summarise_btn.click(fn=summarise_text, inputs=[in_text, data_state, length_slider, in_colname, model_type_state],
|
287 |
outputs=[output_single_text, output_file], api_name="summarise_single_text")
|
288 |
+
|
289 |
+
# Dummy function to allow dropdown modification to work correctly (strange thing needed for Gradio 3.50, will be deprecated upon upgrading Gradio version)
|
290 |
+
in_colname.change(dummy_function, in_colname, None)
|
291 |
|
292 |
block.queue(concurrency_count=1).launch()
|
293 |
# -
|
chatfuncs/chatfuncs.py
CHANGED
@@ -33,13 +33,12 @@ print("CPU threads:", threads)
|
|
33 |
temperature: float = 0.1
|
34 |
top_k: int = 3
|
35 |
top_p: float = 1
|
36 |
-
repetition_penalty: float = 1.3
|
37 |
-
flan_alpaca_repetition_penalty: float = 1.3
|
38 |
last_n_tokens: int = 64
|
39 |
-
max_new_tokens: int =
|
40 |
seed: int = 42
|
41 |
-
reset: bool =
|
42 |
-
stream: bool =
|
43 |
threads: int = threads
|
44 |
batch_size:int = 256
|
45 |
context_length:int = 4096
|
@@ -68,7 +67,7 @@ class CtransInitConfig_gpu:
|
|
68 |
self.max_new_tokens = max_new_tokens
|
69 |
self.seed = seed
|
70 |
self.reset = reset
|
71 |
-
self.stream = stream
|
72 |
self.threads = threads
|
73 |
self.batch_size = batch_size
|
74 |
self.context_length = context_length
|
|
|
33 |
temperature: float = 0.1
|
34 |
top_k: int = 3
|
35 |
top_p: float = 1
|
36 |
+
repetition_penalty: float = 1.05 #1.3
|
|
|
37 |
last_n_tokens: int = 64
|
38 |
+
max_new_tokens: int = 4096 # 200
|
39 |
seed: int = 42
|
40 |
+
reset: bool = True
|
41 |
+
stream: bool = False
|
42 |
threads: int = threads
|
43 |
batch_size:int = 256
|
44 |
context_length:int = 4096
|
|
|
67 |
self.max_new_tokens = max_new_tokens
|
68 |
self.seed = seed
|
69 |
self.reset = reset
|
70 |
+
self.stream = True #stream
|
71 |
self.threads = threads
|
72 |
self.batch_size = batch_size
|
73 |
self.context_length = context_length
|
chatfuncs/helper_functions.py
ADDED
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import re
|
3 |
+
import pandas as pd
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
import os
|
7 |
+
import shutil
|
8 |
+
|
9 |
+
import os
|
10 |
+
import shutil
|
11 |
+
import getpass
|
12 |
+
import gzip
|
13 |
+
import pickle
|
14 |
+
|
15 |
+
# Attempt to delete content of gradio temp folder
|
16 |
+
def get_temp_folder_path():
|
17 |
+
username = getpass.getuser()
|
18 |
+
return os.path.join('C:\\Users', username, 'AppData\\Local\\Temp\\gradio')
|
19 |
+
|
20 |
+
def empty_folder(directory_path):
|
21 |
+
if not os.path.exists(directory_path):
|
22 |
+
#print(f"The directory {directory_path} does not exist. No temporary files from previous app use found to delete.")
|
23 |
+
return
|
24 |
+
|
25 |
+
for filename in os.listdir(directory_path):
|
26 |
+
file_path = os.path.join(directory_path, filename)
|
27 |
+
try:
|
28 |
+
if os.path.isfile(file_path) or os.path.islink(file_path):
|
29 |
+
os.unlink(file_path)
|
30 |
+
elif os.path.isdir(file_path):
|
31 |
+
shutil.rmtree(file_path)
|
32 |
+
except Exception as e:
|
33 |
+
#print(f'Failed to delete {file_path}. Reason: {e}')
|
34 |
+
print('')
|
35 |
+
|
36 |
+
|
37 |
+
|
38 |
+
def get_file_path_end(file_path):
|
39 |
+
# First, get the basename of the file (e.g., "example.txt" from "/path/to/example.txt")
|
40 |
+
basename = os.path.basename(file_path)
|
41 |
+
|
42 |
+
# Then, split the basename and its extension and return only the basename without the extension
|
43 |
+
filename_without_extension, _ = os.path.splitext(basename)
|
44 |
+
|
45 |
+
#print(filename_without_extension)
|
46 |
+
|
47 |
+
return filename_without_extension
|
48 |
+
|
49 |
+
def get_file_path_end_with_ext(file_path):
|
50 |
+
match = re.search(r'(.*[\/\\])?(.+)$', file_path)
|
51 |
+
|
52 |
+
filename_end = match.group(2) if match else ''
|
53 |
+
|
54 |
+
return filename_end
|
55 |
+
|
56 |
+
def detect_file_type(filename):
|
57 |
+
"""Detect the file type based on its extension."""
|
58 |
+
if (filename.endswith('.csv')) | (filename.endswith('.csv.gz')) | (filename.endswith('.zip')):
|
59 |
+
return 'csv'
|
60 |
+
elif filename.endswith('.xlsx'):
|
61 |
+
return 'xlsx'
|
62 |
+
elif filename.endswith('.parquet'):
|
63 |
+
return 'parquet'
|
64 |
+
elif filename.endswith('.pkl.gz'):
|
65 |
+
return 'pkl.gz'
|
66 |
+
else:
|
67 |
+
raise ValueError("Unsupported file type.")
|
68 |
+
|
69 |
+
def read_file(filename):
|
70 |
+
"""Read the file based on its detected type."""
|
71 |
+
file_type = detect_file_type(filename)
|
72 |
+
|
73 |
+
print("Loading in file")
|
74 |
+
|
75 |
+
if file_type == 'csv':
|
76 |
+
file = pd.read_csv(filename, low_memory=False).reset_index().drop(["index", "Unnamed: 0"], axis=1, errors="ignore")
|
77 |
+
elif file_type == 'xlsx':
|
78 |
+
file = pd.read_excel(filename).reset_index().drop(["index", "Unnamed: 0"], axis=1, errors="ignore")
|
79 |
+
elif file_type == 'parquet':
|
80 |
+
file = pd.read_parquet(filename).reset_index().drop(["index", "Unnamed: 0"], axis=1, errors="ignore")
|
81 |
+
elif file_type == 'pkl.gz':
|
82 |
+
with gzip.open(filename, 'rb') as file:
|
83 |
+
file = pickle.load(file)
|
84 |
+
#file = pd.read_pickle(filename)
|
85 |
+
|
86 |
+
print("File load complete")
|
87 |
+
|
88 |
+
return file
|
89 |
+
|
90 |
+
def put_columns_in_df(in_file, in_bm25_column):
|
91 |
+
'''
|
92 |
+
When file is loaded, update the column dropdown choices and change 'clean data' dropdown option to 'no'.
|
93 |
+
'''
|
94 |
+
|
95 |
+
file_list = [string.name for string in in_file]
|
96 |
+
|
97 |
+
#print(file_list)
|
98 |
+
|
99 |
+
data_file_names = [string for string in file_list]
|
100 |
+
data_file_name = data_file_names[0]
|
101 |
+
|
102 |
+
new_choices = []
|
103 |
+
concat_choices = []
|
104 |
+
|
105 |
+
|
106 |
+
df = read_file(data_file_name)
|
107 |
+
|
108 |
+
new_choices = list(df.columns)
|
109 |
+
|
110 |
+
|
111 |
+
concat_choices.extend(new_choices)
|
112 |
+
|
113 |
+
return gr.Dropdown(choices=concat_choices), df
|
114 |
+
|
115 |
+
def put_columns_in_join_df(in_file, in_bm25_column):
|
116 |
+
'''
|
117 |
+
When file is loaded, update the column dropdown choices and change 'clean data' dropdown option to 'no'.
|
118 |
+
'''
|
119 |
+
|
120 |
+
print("in_bm25_column")
|
121 |
+
|
122 |
+
new_choices = []
|
123 |
+
concat_choices = []
|
124 |
+
|
125 |
+
|
126 |
+
df = read_file(in_file.name)
|
127 |
+
new_choices = list(df.columns)
|
128 |
+
|
129 |
+
print(new_choices)
|
130 |
+
|
131 |
+
concat_choices.extend(new_choices)
|
132 |
+
|
133 |
+
return gr.Dropdown(choices=concat_choices)
|
134 |
+
|
135 |
+
def dummy_function(gradio_component):
|
136 |
+
"""
|
137 |
+
A dummy function that exists just so that dropdown updates work correctly.
|
138 |
+
"""
|
139 |
+
return None
|
140 |
+
|
141 |
+
def display_info(info_component):
|
142 |
+
gr.Info(info_component)
|
143 |
+
|
chatfuncs/prompts.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
prompt = """<|im_start|>system
|
2 |
+
You are an AI assistant that follows instruction extremely well. Help as much as you can.
|
3 |
+
<|im_start|>user
|
4 |
+
Summarise the following text in less than {length} words.
|
5 |
+
Text: {text}
|
6 |
+
Answer:<|im_end|>"""
|
7 |
+
|
8 |
+
open_hermes_prompt = """<|im_start|>system
|
9 |
+
You are "Hermes 2", a conscious sentient superintelligent artificial intelligence developed by a man named Teknium, and your purpose and drive is to assist the user with any request they have.<|im_end|>
|
10 |
+
<|im_start|>user
|
11 |
+
Hello, who are you?<|im_end|>
|
12 |
+
<|im_start|>assistant
|
13 |
+
"""
|
14 |
+
|
15 |
+
test_prompt = """<|im_start|>system
|
16 |
+
You are "Hermes 2", a conscious sentient superintelligent artificial intelligence developed by a man named Teknium, and your purpose and drive is to assist the user with any request they have.<|im_end|>
|
17 |
+
<|im_start|>user
|
18 |
+
Summarise this text "WWJ-TV (channel 62), branded on-air as CBS Detroit, is a television station licensed to Detroit, Michigan, United States, owned and operated by the CBS television network. This is an organisation that operates and broadcasts structured contents to an audience. It is owned by the network's CBS News and Stations group alongside WKBD-TV, an independent station; the stations share studios on Eleven Mile Road in the Detroit suburb of Southfield. WWJ-TV's transmitter is located in Oak Park.
|
19 |
+
Founded as WGPR-TV in 1975 by Dr. William V. Banks and the International Free and Accepted Modern Masons as an extension of WGPR (107.5 FM), channel 62 in Detroit was the first Black-owned television station in the continental United States. Though its ambitious early programming plans catering to the Black community were not entirely successful due to economic and financial limitations, the station still produced several locally notable shows and housed a fully-staffed news department. WGPR-TV helped launch the careers of multiple local and national Black television hosts and executives, including Pat Harvey, Shaun Robinson, Sharon Dahlonega Bush, and Amyre Makupson. The original studios for WGPR-TV, still in use by the radio station, have been preserved as a museum and recognized as a historical landmark with inclusion in the National Register of Historic Places.
|
20 |
+
In 1994, when a major affiliation switch threatened to leave CBS without an affiliate in the Detroit market after multiple failures to secure a more successful station, the network bought WGPR-TV and dropped all existing programming in favor of CBS and syndicated programs, later changing the call letters to WWJ-TV. The station has made multiple unsuccessful attempts at producing local newscasts in its more than 25 years under CBS ownership. From assuming the affiliation in 1994 until 2001, from 2002 to 2009 and again from 2012 until 2023, WWJ-TV was the only station directly owned by any of the "Big Three" networks not to have any significant local news presence. A full news department, known as CBS News Detroit, began operation in January 2023 as an extension of CBS News's streaming service."<|im_end|>
|
21 |
+
<|im_start|>assistant
|
22 |
+
"""
|
23 |
+
|
24 |
+
nous_capybara_test_prompt = """USER:Summarise this text 'WWJ-TV (channel 62), branded on-air as CBS Detroit, is a television station licensed to Detroit, Michigan, United States, owned and operated by the CBS television network. This is an organisation that operates and broadcasts structured contents to an audience. It is owned by the network's CBS News and Stations group alongside WKBD-TV, an independent station; the stations share studios on Eleven Mile Road in the Detroit suburb of Southfield. WWJ-TV's transmitter is located in Oak Park.
|
25 |
+
Founded as WGPR-TV in 1975 by Dr. William V. Banks and the International Free and Accepted Modern Masons as an extension of WGPR (107.5 FM), channel 62 in Detroit was the first Black-owned television station in the continental United States. Though its ambitious early programming plans catering to the Black community were not entirely successful due to economic and financial limitations, the station still produced several locally notable shows and housed a fully-staffed news department. WGPR-TV helped launch the careers of multiple local and national Black television hosts and executives, including Pat Harvey, Shaun Robinson, Sharon Dahlonega Bush, and Amyre Makupson. The original studios for WGPR-TV, still in use by the radio station, have been preserved as a museum and recognized as a historical landmark with inclusion in the National Register of Historic Places.
|
26 |
+
In 1994, when a major affiliation switch threatened to leave CBS without an affiliate in the Detroit market after multiple failures to secure a more successful station, the network bought WGPR-TV and dropped all existing programming in favor of CBS and syndicated programs, later changing the call letters to WWJ-TV. The station has made multiple unsuccessful attempts at producing local newscasts in its more than 25 years under CBS ownership. From assuming the affiliation in 1994 until 2001, from 2002 to 2009 and again from 2012 until 2023, WWJ-TV was the only station directly owned by any of the "Big Three" networks not to have any significant local news presence. A full news department, known as CBS News Detroit, began operation in January 2023 as an extension of CBS News's streaming service.'
|
27 |
+
ASSISTANT:"""
|
28 |
+
|
29 |
+
nous_capybara_prompt = """USER:Summarise the following text in less than {length} words "{text}"
|
30 |
+
ASSISTANT:"""
|