Spaces:
Sleeping
Sleeping
new prompt and without langchain
Browse files
app.py
CHANGED
|
@@ -20,99 +20,56 @@ max_tokens = 2048
|
|
| 20 |
temperature = 1.0
|
| 21 |
top_p = 0.7
|
| 22 |
top_k = 50
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
max_generated_tokens=max_tokens,
|
| 47 |
top_k=top_k,
|
| 48 |
top_p=top_p,
|
| 49 |
temperature=temperature):
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
return answer
|
| 54 |
-
|
| 55 |
-
@property
|
| 56 |
-
def _identifying_params(self) -> Mapping[str, Any]:
|
| 57 |
-
"""Get the identifying parameters."""
|
| 58 |
-
return {"model": "ChatGLMModel"}
|
| 59 |
-
|
| 60 |
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
import gradio as gr
|
| 64 |
-
from langchain.prompts import PromptTemplate
|
| 65 |
-
from langchain.docstore.document import Document
|
| 66 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 67 |
-
from langchain.chains.summarize import load_summarize_chain
|
| 68 |
-
# from langchain.chains.question_answering import load_qa_chain
|
| 69 |
-
# from langchain.embeddings import HuggingFaceEmbeddings
|
| 70 |
-
# from langchain.vectorstores import Chroma
|
| 71 |
-
|
| 72 |
-
# embeddings = HuggingFaceEmbeddings()
|
| 73 |
-
query = "總結並以點列形式舉出重點"
|
| 74 |
-
prompt_template = """總結下文並列舉出重點:
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
{text}
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
摘要及各項重點:"""
|
| 81 |
-
PROMPT = PromptTemplate(template=prompt_template, input_variables=["text"])
|
| 82 |
-
# chain = load_summarize_chain(llm, chain_type="stuff", prompt=PROMPT)
|
| 83 |
-
chain = load_summarize_chain(llm, chain_type="map_reduce", map_prompt=PROMPT, combine_prompt=PROMPT)
|
| 84 |
-
# refine_template = (
|
| 85 |
-
# "你的任務是整理出一段摘要以及例舉所有重點\n"
|
| 86 |
-
# "我們之前已經整理出這些內容: {existing_answer}\n"
|
| 87 |
-
# "請再整合這些摘要並將重點整理到一個列表"
|
| 88 |
-
# "(如果需要) 下文這裡有更多的參考資料:\n"
|
| 89 |
-
# "------------\n"
|
| 90 |
-
# "{text}\n"
|
| 91 |
-
# "------------\n"
|
| 92 |
-
# "根據新的資料,完善原有的摘要和重點列表"
|
| 93 |
-
# "如果新資料對已經整理出的文字沒有補充,請重複原來的重點文字。"
|
| 94 |
-
# )
|
| 95 |
-
# refine_prompt = PromptTemplate(
|
| 96 |
-
# input_variables=["existing_answer", "text"],
|
| 97 |
-
# template=refine_template,
|
| 98 |
-
# )
|
| 99 |
-
# chain = load_summarize_chain(llm, chain_type="refine", question_prompt=PROMPT, refine_prompt=refine_prompt)
|
| 100 |
-
# chain = load_qa_chain(llm, chain_type="map_reduce", map_prompt=PROMPT, combine_prompt=PROMPT)
|
| 101 |
-
# chain = load_qa_chain(llm, chain_type="refine", question_prompt=PROMPT, refine_prompt=refine_prompt)
|
| 102 |
-
|
| 103 |
-
def greet(text):
|
| 104 |
-
docs = [Document(page_content=text)]
|
| 105 |
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
# docsearch = Chroma.from_texts(texts, embeddings).as_retriever()
|
| 113 |
-
# docs = docsearch.get_relevant_documents(query)
|
| 114 |
-
return chain.run(texts)
|
| 115 |
-
# return chain.run(input_documents=texts, question=query)
|
| 116 |
|
| 117 |
iface = gr.Interface(fn=greet,
|
| 118 |
inputs=gr.Textbox(lines=20,
|
|
|
|
| 20 |
temperature = 1.0
|
| 21 |
top_p = 0.7
|
| 22 |
top_k = 50
|
| 23 |
+
prompt = """
|
| 24 |
+
現在有些文本,文本詳細且複雜。 它包含細節,可以縮減和綜合為關鍵要點。 你的任務是提取最重要的概念,重點關注主要思路,提供一個概述而不失去精髓。 你的總結應該:
|
| 25 |
+
|
| 26 |
+
• 簡潔但足夠充分,可以代表所有重要信息
|
| 27 |
+
• 使用正確的句式和連貫的流程
|
| 28 |
+
• 捕捉誰、什麼、何時、在哪裡、為什麼和如何
|
| 29 |
+
• 盡可能地保留原始風格和風格
|
| 30 |
+
• 你必須遵循“摘要”格式:
|
| 31 |
+
|
| 32 |
+
摘要:
|
| 33 |
+
用2至3個句子簡要陳述主要主題和主要發現。
|
| 34 |
+
|
| 35 |
+
主要要點:
|
| 36 |
+
• 要點1 - 最重要的發現或細節
|
| 37 |
+
• 要點2 - 第二重要的觀黵
|
| 38 |
+
• 要點3 - 第三個重要的信息
|
| 39 |
+
• 要點4(可選) - 另一個要點
|
| 40 |
+
• 要點5(可選) - 最後的關鍵總結要點
|
| 41 |
+
|
| 42 |
+
文本:
|
| 43 |
+
"""
|
| 44 |
+
|
| 45 |
+
def sum_chain_l1(text, p_bar):
|
| 46 |
+
docs = []
|
| 47 |
+
for i in p_bar(range(len(text)//2000+1)):
|
| 48 |
+
t = text[i*2000:i*2000+2048]
|
| 49 |
+
if len(t) > 0:
|
| 50 |
+
for answer in model.generate_iterate(prompt+t,
|
| 51 |
max_generated_tokens=max_tokens,
|
| 52 |
top_k=top_k,
|
| 53 |
top_p=top_p,
|
| 54 |
temperature=temperature):
|
| 55 |
+
yield answer
|
| 56 |
+
docs.append(answer)
|
| 57 |
+
return docs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
def sum_chain_l2_deprecated(docs):
|
| 60 |
+
hist = ''
|
| 61 |
+
for doc in tqdm(docs):
|
| 62 |
+
hist = model.response(prompt+"\n"+hist+"\n"+doc)
|
| 63 |
+
return hist
|
| 64 |
|
| 65 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
+
def greet(x, progress=gr.Progress()):
|
| 68 |
+
progress(0, desc="Starting...")
|
| 69 |
+
docs = []
|
| 70 |
+
for doc in sum_chain_l1(x, progress.tqdm):
|
| 71 |
+
yield doc
|
| 72 |
+
return '===== summarized parts ====='.join(doc)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
iface = gr.Interface(fn=greet,
|
| 75 |
inputs=gr.Textbox(lines=20,
|