Spaces:
Runtime error
Runtime error
Upload with huggingface_hub
Browse files- app.py +44 -0
- requirements.txt +3 -0
- summary.pmpt.tpl +7 -0
app.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Summarize a long document by chunking and summarizing parts. Uses
|
| 2 |
+
# aynchronous calls to the API. Adapted from LangChain [Map-Reduce
|
| 3 |
+
# summary](https://langchain.readthedocs.io/en/stable/_modules/langchain/chains/mapreduce.html).
|
| 4 |
+
|
| 5 |
+
import trio
|
| 6 |
+
|
| 7 |
+
from minichain import TemplatePrompt, show_log, start_chain
|
| 8 |
+
|
| 9 |
+
# Prompt that asks LLM to produce a bash command.
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class SummaryPrompt(TemplatePrompt):
|
| 13 |
+
template_file = "summary.pmpt.tpl"
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def chunk(f, width=4000, overlap=800):
|
| 17 |
+
"Split a documents into 4800 character overlapping chunks"
|
| 18 |
+
text = open(f).read().replace("\n\n", "\n")
|
| 19 |
+
chunks = []
|
| 20 |
+
for i in range(4):
|
| 21 |
+
if i * width > len(text):
|
| 22 |
+
break
|
| 23 |
+
chunks.append({"text": text[i * width : (i + 1) * width + overlap]})
|
| 24 |
+
return chunks
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
with start_chain("summary") as backend:
|
| 28 |
+
prompt = SummaryPrompt(backend.OpenAI())
|
| 29 |
+
list_prompt = prompt.map()
|
| 30 |
+
|
| 31 |
+
# Map - Summarize each chunk in parallel
|
| 32 |
+
out = trio.run(list_prompt.arun, chunk("../state_of_the_union.txt"))
|
| 33 |
+
|
| 34 |
+
# Reduce - Summarize the summarized chunks
|
| 35 |
+
print(prompt({"text": "\n".join(out)}))
|
| 36 |
+
|
| 37 |
+
# + tags=["hide_inp"]
|
| 38 |
+
SummaryPrompt().show(
|
| 39 |
+
{"text": "One way to fight is to drive down wages and make Americans poorer."},
|
| 40 |
+
"Make Americans poorer",
|
| 41 |
+
)
|
| 42 |
+
# -
|
| 43 |
+
|
| 44 |
+
show_log("summary.log")
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
git+https://github.com/srush/minichain
|
| 3 |
+
manifest-ml
|
summary.pmpt.tpl
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Write a concise summary of the following:
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
"{{text}}"
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
CONCISE SUMMARY:
|