Spaces:
Sleeping
Sleeping
File size: 2,699 Bytes
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 |
import os
import subprocess
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"])
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.docstore.document import Document
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.chains.question_answering import load_qa_chain
# from langchain.embeddings import HuggingFaceEmbeddings
# from langchain.vectorstores import Chroma
# embeddings = HuggingFaceEmbeddings()
query = "總結並以點列形式舉出重點"
chain = load_qa_chain(llm, chain_type="map_reduce")
# chain = load_qa_chain(llm, chain_type="refine")
def greet(text):
docs = [Document(page_content=text)]
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1024, # 分割最大尺寸
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(input_documents=texts, question=query)
iface = gr.Interface(fn=greet,
inputs=gr.Textbox(lines=20,
placeholder="Text Here..."),
outputs="text")
iface.launch()
|