Spaces:
Sleeping
Sleeping
File size: 4,214 Bytes
bf7bb36 d471a4c bf7bb36 d297372 bf7bb36 4d28f06 bf7bb36 d297372 8a5008d bf7bb36 4d28f06 bf7bb36 4d28f06 bf7bb36 4d28f06 bf7bb36 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
import os
import sys
import subprocess
if not os.path.exists("ChatGLM-6b-onnx-u8s8"):
subprocess.run(["git", "lfs", "install"])
subprocess.run(["git", "clone", "https://huggingface.co/K024/ChatGLM-6b-onnx-u8s8"])
os.chdir("ChatGLM-6b-onnx-u8s8")
subprocess.run(["pip", "install", "-r", "requirements.txt"])
sys.path.append(os.getcwd())
else:
sys.path.append(os.path.join(os.getcwd(), "ChatGLM-6b-onnx-u8s8"))
from model import ChatGLMModel#, chat_template
model = ChatGLMModel()
# history = []
max_tokens = 512
temperature = 1.0
top_p = 0.7
top_k = 50
from typing import Any, List, Mapping, Optional
from langchain.callbacks.manager import CallbackManagerForLLMRun
from langchain.llms.base import LLM
class CustomLLM(LLM):
model: ChatGLMModel
# history: List
@property
def _llm_type(self) -> str:
return "custom"
def _call(
self,
prompt: str,
stop: Optional[List[str]] = None,
run_manager: Optional[CallbackManagerForLLMRun] = None,
) -> str:
if stop is not None:
raise ValueError("stop kwargs are not permitted.")
# prompt = chat_template(self.history, prompt)
for answer in self.model.generate_iterate(prompt,
max_generated_tokens=max_tokens,
top_k=top_k,
top_p=top_p,
temperature=temperature):
pass
# self.history = self.history + [(question, answer)]
return answer
@property
def _identifying_params(self) -> Mapping[str, Any]:
"""Get the identifying parameters."""
return {"model": "ChatGLMModel"}
llm = CustomLLM(model=model)
import gradio as gr
from langchain.prompts import PromptTemplate
from langchain.docstore.document import Document
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.chains.summarize import load_summarize_chain
# from langchain.chains.question_answering import load_qa_chain
# from langchain.embeddings import HuggingFaceEmbeddings
# from langchain.vectorstores import Chroma
# embeddings = HuggingFaceEmbeddings()
query = "總結並以點列形式舉出重點"
prompt_template = """總結下文並列舉出重點:
{text}
摘要及各項重點:"""
PROMPT = PromptTemplate(template=prompt_template, input_variables=["text"])
# chain = load_summarize_chain(llm, chain_type="stuff", prompt=PROMPT)
chain = load_summarize_chain(llm, chain_type="map_reduce", map_prompt=PROMPT, combine_prompt=PROMPT)
# refine_template = (
# "你的任務是整理出一段摘要以及例舉所有重點\n"
# "我們之前已經整理出這些內容: {existing_answer}\n"
# "請再整合這些摘要並將重點整理到一個列表"
# "(如果需要) 下文這裡有更多的參考資料:\n"
# "------------\n"
# "{text}\n"
# "------------\n"
# "根據新的資料,完善原有的摘要和重點列表"
# "如果新資料對已經整理出的文字沒有補充,請重複原來的重點文字。"
# )
# refine_prompt = PromptTemplate(
# input_variables=["existing_answer", "text"],
# template=refine_template,
# )
# chain = load_summarize_chain(llm, chain_type="refine", question_prompt=PROMPT, refine_prompt=refine_prompt)
# chain = load_qa_chain(llm, chain_type="map_reduce", map_prompt=PROMPT, combine_prompt=PROMPT)
# chain = load_qa_chain(llm, chain_type="refine", question_prompt=PROMPT, refine_prompt=refine_prompt)
def greet(text):
docs = [Document(page_content=text)]
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=512, # 分割最大尺寸
chunk_overlap=64, # 重复字数
length_function=len
)
texts = text_splitter.split_documents(docs)
# docsearch = Chroma.from_texts(texts, embeddings).as_retriever()
# docs = docsearch.get_relevant_documents(query)
return chain.run(texts)
# return chain.run(input_documents=texts, question=query)
iface = gr.Interface(fn=greet,
inputs=gr.Textbox(lines=20,
placeholder="Text Here..."),
outputs="text")
iface.launch()
|