Spaces:
Sleeping
Sleeping
seanpedrickcase
commited on
Commit
•
1f9788f
1
Parent(s):
49679bb
Added a stop processing button and timeout for Mistral model
Browse files
app.py
CHANGED
@@ -1,28 +1,13 @@
|
|
1 |
import gradio as gr
|
2 |
from datetime import datetime
|
3 |
import pandas as pd
|
4 |
-
from transformers import pipeline
|
5 |
-
# # Load in packages
|
6 |
-
|
7 |
-
# +
|
8 |
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 |
-
|
16 |
from typing import Type
|
17 |
-
#from langchain.embeddings import HuggingFaceEmbeddings#, HuggingFaceInstructEmbeddings
|
18 |
-
#from langchain.vectorstores import FAISS
|
19 |
import gradio as gr
|
20 |
-
|
21 |
-
from transformers import AutoTokenizer
|
22 |
-
|
23 |
-
# Alternative model sources
|
24 |
import ctransformers
|
25 |
-
|
|
|
26 |
|
27 |
PandasDataFrame = Type[pd.DataFrame]
|
28 |
|
@@ -191,6 +176,14 @@ def summarise_text(text, text_df, length_slider, in_colname, model_type, progres
|
|
191 |
|
192 |
if model_type == "Mistral Nous Capybara 4k (larger, slow)":
|
193 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
194 |
length = str(length_slider)
|
195 |
|
196 |
from chatfuncs.prompts import nous_capybara_prompt
|
@@ -201,11 +194,16 @@ def summarise_text(text, text_df, length_slider, in_colname, model_type, progres
|
|
201 |
|
202 |
formatted_string = nous_capybara_prompt.format(length=length, text=single_text)
|
203 |
|
204 |
-
#
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
|
|
|
|
|
|
|
|
|
|
209 |
|
210 |
print(output)
|
211 |
|
@@ -233,11 +231,11 @@ def summarise_text(text, text_df, length_slider, in_colname, model_type, progres
|
|
233 |
#pd.Series(summarised_texts).to_csv("summarised_texts_out.csv")
|
234 |
|
235 |
if text_df.empty:
|
236 |
-
if model_type != "Mistral Nous Capybara 4k (larger, slow)":
|
237 |
-
|
238 |
|
239 |
-
if model_type == "Mistral Nous Capybara 4k (larger, slow)":
|
240 |
-
|
241 |
|
242 |
else:
|
243 |
summarised_text_out = summarised_texts #[d['summary_text'] for d in summarised_texts] #summarised_text[0].values()
|
@@ -263,7 +261,7 @@ with block:
|
|
263 |
gr.Markdown(
|
264 |
"""
|
265 |
# Text summariser
|
266 |
-
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,
|
267 |
""")
|
268 |
|
269 |
with gr.Tab("Summariser"):
|
@@ -278,6 +276,7 @@ with block:
|
|
278 |
|
279 |
with gr.Row():
|
280 |
summarise_btn = gr.Button("Summarise")
|
|
|
281 |
length_slider = gr.Slider(minimum = 30, maximum = 500, value = 100, step = 10, label = "Maximum length of summary")
|
282 |
|
283 |
with gr.Row():
|
@@ -301,9 +300,14 @@ with block:
|
|
301 |
|
302 |
change_model_button.click(fn=load_model, inputs=[model_choice, gpu_layer_choice], outputs = [model_type_state, load_text, current_model])
|
303 |
|
304 |
-
summarise_btn.click(fn=summarise_text, inputs=[in_text, data_state, length_slider, in_colname, model_type_state],
|
305 |
outputs=[output_single_text, output_file], api_name="summarise_single_text")
|
|
|
|
|
306 |
|
|
|
|
|
|
|
307 |
# Dummy function to allow dropdown modification to work correctly (strange thing needed for Gradio 3.50, will be deprecated upon upgrading Gradio version)
|
308 |
in_colname.change(dummy_function, in_colname, None)
|
309 |
|
|
|
1 |
import gradio as gr
|
2 |
from datetime import datetime
|
3 |
import pandas as pd
|
4 |
+
from transformers import pipeline, AutoTokenizer
|
|
|
|
|
|
|
5 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
from typing import Type
|
|
|
|
|
7 |
import gradio as gr
|
|
|
|
|
|
|
|
|
8 |
import ctransformers
|
9 |
+
# Concurrent futures is used to cancel processes that are taking too long
|
10 |
+
import concurrent.futures
|
11 |
|
12 |
PandasDataFrame = Type[pd.DataFrame]
|
13 |
|
|
|
176 |
|
177 |
if model_type == "Mistral Nous Capybara 4k (larger, slow)":
|
178 |
|
179 |
+
|
180 |
+
# Define a function that calls your model
|
181 |
+
def call_model(formatted_string, max_length=10000):
|
182 |
+
return chatf.model(formatted_string, max_length=max_length)
|
183 |
+
|
184 |
+
# Set your timeout duration (in seconds)
|
185 |
+
timeout_duration = 300 # Adjust this value as needed
|
186 |
+
|
187 |
length = str(length_slider)
|
188 |
|
189 |
from chatfuncs.prompts import nous_capybara_prompt
|
|
|
194 |
|
195 |
formatted_string = nous_capybara_prompt.format(length=length, text=single_text)
|
196 |
|
197 |
+
# Use ThreadPoolExecutor to enforce a timeout
|
198 |
+
with concurrent.futures.ThreadPoolExecutor() as executor:
|
199 |
+
future = executor.submit(call_model, formatted_string, 10000)
|
200 |
+
try:
|
201 |
+
output = future.result(timeout=timeout_duration)
|
202 |
+
# Process the output here
|
203 |
+
except concurrent.futures.TimeoutError:
|
204 |
+
error_text = f"Timeout (five minutes) occurred for text: {single_text}. Consider using a smaller model."
|
205 |
+
print(error_text)
|
206 |
+
return error_text, None
|
207 |
|
208 |
print(output)
|
209 |
|
|
|
231 |
#pd.Series(summarised_texts).to_csv("summarised_texts_out.csv")
|
232 |
|
233 |
if text_df.empty:
|
234 |
+
#if model_type != "Mistral Nous Capybara 4k (larger, slow)":
|
235 |
+
summarised_text_out = summarised_texts[0]#.values()
|
236 |
|
237 |
+
#if model_type == "Mistral Nous Capybara 4k (larger, slow)":
|
238 |
+
# summarised_text_out = summarised_texts[0]
|
239 |
|
240 |
else:
|
241 |
summarised_text_out = summarised_texts #[d['summary_text'] for d in summarised_texts] #summarised_text[0].values()
|
|
|
261 |
gr.Markdown(
|
262 |
"""
|
263 |
# Text summariser
|
264 |
+
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,000 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.
|
265 |
""")
|
266 |
|
267 |
with gr.Tab("Summariser"):
|
|
|
276 |
|
277 |
with gr.Row():
|
278 |
summarise_btn = gr.Button("Summarise")
|
279 |
+
stop = gr.Button(value="Interrupt processing", variant="secondary", scale=0)
|
280 |
length_slider = gr.Slider(minimum = 30, maximum = 500, value = 100, step = 10, label = "Maximum length of summary")
|
281 |
|
282 |
with gr.Row():
|
|
|
300 |
|
301 |
change_model_button.click(fn=load_model, inputs=[model_choice, gpu_layer_choice], outputs = [model_type_state, load_text, current_model])
|
302 |
|
303 |
+
summarise_click = summarise_btn.click(fn=summarise_text, inputs=[in_text, data_state, length_slider, in_colname, model_type_state],
|
304 |
outputs=[output_single_text, output_file], api_name="summarise_single_text")
|
305 |
+
summarise_enter = summarise_btn.click(fn=summarise_text, inputs=[in_text, data_state, length_slider, in_colname, model_type_state],
|
306 |
+
outputs=[output_single_text, output_file])
|
307 |
|
308 |
+
# Stop processing if it's taking too long
|
309 |
+
stop.click(fn=None, inputs=None, outputs=None, cancels=[summarise_click, summarise_enter])
|
310 |
+
|
311 |
# Dummy function to allow dropdown modification to work correctly (strange thing needed for Gradio 3.50, will be deprecated upon upgrading Gradio version)
|
312 |
in_colname.change(dummy_function, in_colname, None)
|
313 |
|