npc0 commited on
Commit
bf7bb36
·
1 Parent(s): 15ee623

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -0
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import subprocess
3
+
4
+ subprocess.run(["git", "lfs", "install"])
5
+ subprocess.run(["git", "clone", "https://huggingface.co/K024/ChatGLM-6b-onnx-u8s8"])
6
+ os.chdir("ChatGLM-6b-onnx-u8s8")
7
+ subprocess.run(["pip", "install", "-r", "requirements.txt"])
8
+
9
+ from model import ChatGLMModel#, chat_template
10
+
11
+ model = ChatGLMModel()
12
+ # history = []
13
+
14
+ max_tokens = 512
15
+ temperature = 1.0
16
+ top_p = 0.7
17
+ top_k = 50
18
+
19
+ from typing import Any, List, Mapping, Optional
20
+ from langchain.callbacks.manager import CallbackManagerForLLMRun
21
+ from langchain.llms.base import LLM
22
+
23
+ class CustomLLM(LLM):
24
+ model: ChatGLMModel
25
+ # history: List
26
+
27
+ @property
28
+ def _llm_type(self) -> str:
29
+ return "custom"
30
+
31
+ def _call(
32
+ self,
33
+ prompt: str,
34
+ stop: Optional[List[str]] = None,
35
+ run_manager: Optional[CallbackManagerForLLMRun] = None,
36
+ ) -> str:
37
+ if stop is not None:
38
+ raise ValueError("stop kwargs are not permitted.")
39
+ # prompt = chat_template(self.history, prompt)
40
+ for answer in self.model.generate_iterate(prompt,
41
+ max_generated_tokens=max_tokens,
42
+ top_k=top_k,
43
+ top_p=top_p,
44
+ temperature=temperature):
45
+ pass
46
+
47
+ # self.history = self.history + [(question, answer)]
48
+ return answer
49
+
50
+ @property
51
+ def _identifying_params(self) -> Mapping[str, Any]:
52
+ """Get the identifying parameters."""
53
+ return {"model": "ChatGLMModel"}
54
+
55
+
56
+ llm = CustomLLM(model=model)
57
+
58
+ import gradio as gr
59
+ from langchain.docstore.document import Document
60
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
61
+ from langchain.chains.question_answering import load_qa_chain
62
+ # from langchain.embeddings import HuggingFaceEmbeddings
63
+ # from langchain.vectorstores import Chroma
64
+
65
+ # embeddings = HuggingFaceEmbeddings()
66
+ query = "總結並以點列形式舉出重點"
67
+ chain = load_qa_chain(llm, chain_type="map_reduce")
68
+ # chain = load_qa_chain(llm, chain_type="refine")
69
+
70
+ def greet(text):
71
+ docs = [Document(page_content=text)]
72
+
73
+ text_splitter = RecursiveCharacterTextSplitter(
74
+ chunk_size=1024, # 分割最大尺寸
75
+ chunk_overlap=64, # 重复字数
76
+ length_function=len
77
+ )
78
+ texts = text_splitter.split_documents(docs)
79
+ # docsearch = Chroma.from_texts(texts, embeddings).as_retriever()
80
+ # docs = docsearch.get_relevant_documents(query)
81
+ return chain.run(input_documents=texts, question=query)
82
+
83
+ iface = gr.Interface(fn=greet,
84
+ inputs=gr.Textbox(lines=20,
85
+ placeholder="Text Here..."),
86
+ outputs="text")
87
+ iface.launch()