Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,10 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
|
| 3 |
-
from langchain.chains import LLMChain
|
| 4 |
-
from langchain.chains.summarize import load_summarize_chain
|
| 5 |
from langchain.document_loaders import TextLoader, PyPDFLoader
|
| 6 |
-
from langchain.llms import LlamaCpp
|
| 7 |
-
from langchain.prompts import PromptTemplate
|
| 8 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 9 |
import gradio as gr
|
| 10 |
import time
|
|
|
|
| 11 |
|
| 12 |
VERBOSE = True
|
| 13 |
MAX_TOKENS = 2048
|
|
@@ -35,29 +32,10 @@ LANGUAGES = ["Default", "English", "Polish", "Portuguese",
|
|
| 35 |
"Spanish", "Czech", "Turkish", "French", "German", ]
|
| 36 |
|
| 37 |
# Model params
|
| 38 |
-
|
| 39 |
-
MODEL_CONTEXT_WINDOW = 8192
|
| 40 |
-
|
| 41 |
-
# Chunk params in characters (not tokens)
|
| 42 |
-
CHUNK_SIZE = 10000
|
| 43 |
-
CHUNK_OVERLAP = 500
|
| 44 |
-
|
| 45 |
-
llm = LlamaCpp(
|
| 46 |
-
model_path=MODEL_FILE,
|
| 47 |
-
n_ctx=MODEL_CONTEXT_WINDOW,
|
| 48 |
-
# Don't be creative.
|
| 49 |
-
temperature=0,
|
| 50 |
-
max_tokens=MAX_TOKENS,
|
| 51 |
-
verbose=VERBOSE,
|
| 52 |
-
|
| 53 |
-
# Remove next two lines if NOT using macOS & M1 processor:
|
| 54 |
-
n_batch=512,
|
| 55 |
-
n_gpu_layers=1,
|
| 56 |
-
)
|
| 57 |
-
|
| 58 |
|
| 59 |
combine_prompt_template = """
|
| 60 |
-
Write a summary of the following text delimited by
|
| 61 |
{style}
|
| 62 |
|
| 63 |
```{content}```
|
|
@@ -65,94 +43,8 @@ Write a summary of the following text delimited by tripple backquotes.
|
|
| 65 |
{trigger} {in_language}:
|
| 66 |
"""
|
| 67 |
|
| 68 |
-
map_prompt_template = """
|
| 69 |
-
Write a concise summary of the following text which covers the main points and key facts and figures:
|
| 70 |
-
{text}
|
| 71 |
-
|
| 72 |
-
CONCISE SUMMARY {in_language}:
|
| 73 |
-
"""
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
def summarize_base(llm, content, style, language):
|
| 77 |
-
"""Summarize whole content at once. The content needs to fit into model's context window."""
|
| 78 |
-
|
| 79 |
-
prompt = PromptTemplate.from_template(
|
| 80 |
-
combine_prompt_template
|
| 81 |
-
).partial(
|
| 82 |
-
style=STYLES[style]["style"],
|
| 83 |
-
trigger=STYLES[style]["trigger"],
|
| 84 |
-
in_language=f"in {language}" if language != "Default" else "",
|
| 85 |
-
)
|
| 86 |
-
|
| 87 |
-
chain = LLMChain(llm=llm, prompt=prompt, verbose=VERBOSE)
|
| 88 |
-
output = chain.run(content)
|
| 89 |
-
|
| 90 |
-
return output
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
def summarize_map_reduce(llm, content, style, language):
|
| 94 |
-
"""Summarize content potentially larger that model's context window using map-reduce approach."""
|
| 95 |
-
|
| 96 |
-
text_splitter = RecursiveCharacterTextSplitter(
|
| 97 |
-
chunk_size=CHUNK_SIZE,
|
| 98 |
-
chunk_overlap=CHUNK_OVERLAP,
|
| 99 |
-
)
|
| 100 |
-
|
| 101 |
-
split_docs = text_splitter.create_documents([content])
|
| 102 |
-
print(
|
| 103 |
-
f"Map-Reduce content splits ({len(split_docs)} splits): {[len(sd.page_content) for sd in split_docs]}")
|
| 104 |
-
|
| 105 |
-
map_prompt = PromptTemplate.from_template(
|
| 106 |
-
map_prompt_template
|
| 107 |
-
).partial(
|
| 108 |
-
in_language=f"in {language}" if language != "Default" else "",
|
| 109 |
-
)
|
| 110 |
-
combine_prompt = PromptTemplate.from_template(
|
| 111 |
-
combine_prompt_template
|
| 112 |
-
).partial(
|
| 113 |
-
style=STYLES[style]["style"],
|
| 114 |
-
trigger=STYLES[style]["trigger"],
|
| 115 |
-
in_language=f"in {language}" if language != "Default" else "",
|
| 116 |
-
)
|
| 117 |
-
|
| 118 |
-
chain = load_summarize_chain(
|
| 119 |
-
llm=llm,
|
| 120 |
-
chain_type="map_reduce",
|
| 121 |
-
map_prompt=map_prompt,
|
| 122 |
-
combine_prompt=combine_prompt,
|
| 123 |
-
combine_document_variable_name="content",
|
| 124 |
-
verbose=VERBOSE,
|
| 125 |
-
)
|
| 126 |
-
|
| 127 |
-
output = chain.run(split_docs)
|
| 128 |
-
return output
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
def load_input_file(input_file):
|
| 132 |
-
if not input_file:
|
| 133 |
-
return None
|
| 134 |
-
|
| 135 |
-
start_time = time.perf_counter()
|
| 136 |
-
|
| 137 |
-
if input_file.name.endswith(".pdf"):
|
| 138 |
-
loader = PyPDFLoader(input_file.name)
|
| 139 |
-
docs = loader.load()
|
| 140 |
-
|
| 141 |
-
end_time = time.perf_counter()
|
| 142 |
-
print(
|
| 143 |
-
f"PDF: loaded {len(docs)} pages, in {round(end_time - start_time, 1)} secs")
|
| 144 |
-
return "\n".join([d.page_content for d in docs])
|
| 145 |
-
|
| 146 |
-
docs = TextLoader(input_file.name).load()
|
| 147 |
-
|
| 148 |
-
end_time = time.perf_counter()
|
| 149 |
-
print(f"Input file load time {round(end_time - start_time, 1)} secs")
|
| 150 |
-
|
| 151 |
-
return docs[0].page_content
|
| 152 |
-
|
| 153 |
-
|
| 154 |
def summarize_text(content, style, language, progress=gr.Progress()):
|
| 155 |
-
content_tokens =
|
| 156 |
|
| 157 |
print("Content length:", len(content))
|
| 158 |
print("Content tokens:", content_tokens)
|
|
@@ -161,36 +53,20 @@ def summarize_text(content, style, language, progress=gr.Progress()):
|
|
| 161 |
info = f"Content length: {len(content)} chars, {content_tokens} tokens."
|
| 162 |
progress(None, desc=info)
|
| 163 |
|
| 164 |
-
# Keep part of context window for models output & some buffor for the promopt.
|
| 165 |
-
base_threshold = MODEL_CONTEXT_WINDOW - MAX_TOKENS - 256
|
| 166 |
-
|
| 167 |
start_time = time.perf_counter()
|
| 168 |
|
| 169 |
-
|
| 170 |
-
info += "\n"
|
| 171 |
-
info += "Using summarizer: base"
|
| 172 |
-
progress(None, desc=info)
|
| 173 |
-
|
| 174 |
-
print("Using summarizer: base")
|
| 175 |
-
summary = summarize_base(llm, content, style, language)
|
| 176 |
-
else:
|
| 177 |
-
info += "\n"
|
| 178 |
-
info += "Using summarizer: map-reduce"
|
| 179 |
-
progress(None, desc=info)
|
| 180 |
-
|
| 181 |
-
print("Using summarizer: map-reduce")
|
| 182 |
-
summary = summarize_map_reduce(llm, content, style, language)
|
| 183 |
|
| 184 |
end_time = time.perf_counter()
|
| 185 |
|
| 186 |
print("Summary length:", len(summary))
|
| 187 |
-
print("Summary tokens:",
|
| 188 |
print("Summary:\n" + summary + "\n\n")
|
| 189 |
|
| 190 |
info += "\n"
|
| 191 |
info += f"Processing time: {round(end_time - start_time, 1)} secs."
|
| 192 |
info += "\n"
|
| 193 |
-
info += f"Summary length: {
|
| 194 |
|
| 195 |
print("Info", info)
|
| 196 |
return summary, info
|
|
@@ -266,4 +142,4 @@ with gr.Blocks() as ui:
|
|
| 266 |
)
|
| 267 |
|
| 268 |
|
| 269 |
-
ui.queue().launch(inbrowser=True)
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
|
|
|
|
|
|
|
| 3 |
from langchain.document_loaders import TextLoader, PyPDFLoader
|
|
|
|
|
|
|
| 4 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 5 |
import gradio as gr
|
| 6 |
import time
|
| 7 |
+
from transformers import pipeline
|
| 8 |
|
| 9 |
VERBOSE = True
|
| 10 |
MAX_TOKENS = 2048
|
|
|
|
| 32 |
"Spanish", "Czech", "Turkish", "French", "German", ]
|
| 33 |
|
| 34 |
# Model params
|
| 35 |
+
summarization_pipeline = pipeline("summarization", model="TheBloke/Mistral-7B-OpenOrca-GGUF")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
combine_prompt_template = """
|
| 38 |
+
Write a summary of the following text delimited by triple backquotes.
|
| 39 |
{style}
|
| 40 |
|
| 41 |
```{content}```
|
|
|
|
| 43 |
{trigger} {in_language}:
|
| 44 |
"""
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
def summarize_text(content, style, language, progress=gr.Progress()):
|
| 47 |
+
content_tokens = len(content.split())
|
| 48 |
|
| 49 |
print("Content length:", len(content))
|
| 50 |
print("Content tokens:", content_tokens)
|
|
|
|
| 53 |
info = f"Content length: {len(content)} chars, {content_tokens} tokens."
|
| 54 |
progress(None, desc=info)
|
| 55 |
|
|
|
|
|
|
|
|
|
|
| 56 |
start_time = time.perf_counter()
|
| 57 |
|
| 58 |
+
summary = summarization_pipeline(content, max_length=2048, min_length=30, do_sample=False)[0]['summary_text']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
end_time = time.perf_counter()
|
| 61 |
|
| 62 |
print("Summary length:", len(summary))
|
| 63 |
+
print("Summary tokens:", len(summary.split()))
|
| 64 |
print("Summary:\n" + summary + "\n\n")
|
| 65 |
|
| 66 |
info += "\n"
|
| 67 |
info += f"Processing time: {round(end_time - start_time, 1)} secs."
|
| 68 |
info += "\n"
|
| 69 |
+
info += f"Summary length: {len(summary.split())} tokens."
|
| 70 |
|
| 71 |
print("Info", info)
|
| 72 |
return summary, info
|
|
|
|
| 142 |
)
|
| 143 |
|
| 144 |
|
| 145 |
+
ui.queue().launch(inbrowser=True)
|