Spaces:
Sleeping
Sleeping
update code
Browse files- app.py +33 -40
- requirements.txt +3 -0
app.py
CHANGED
@@ -2,42 +2,43 @@ import gradio as gr
|
|
2 |
import os
|
3 |
import time
|
4 |
|
5 |
-
from langchain.
|
|
|
|
|
|
|
6 |
|
7 |
-
from langchain.
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
from langchain.
|
10 |
|
11 |
-
|
12 |
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
18 |
|
19 |
-
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
loader = UnstructuredMarkdownLoader('docs/resume.md')
|
30 |
-
documents = loader.load()
|
31 |
-
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
|
32 |
-
texts = text_splitter.split_documents(documents)
|
33 |
-
embeddings = OpenAIEmbeddings()
|
34 |
-
db = Chroma.from_documents(texts, embeddings)
|
35 |
-
retriever = db.as_retriever()
|
36 |
-
qa = ConversationalRetrievalChain.from_llm(
|
37 |
-
llm=OpenAI(temperature=0.3),
|
38 |
-
retriever=retriever,
|
39 |
-
condense_question_prompt=CUSTOM_QUESTION_PROMPT,
|
40 |
-
return_source_documents=False)
|
41 |
|
42 |
|
43 |
def add_text(history, text):
|
@@ -47,7 +48,7 @@ def add_text(history, text):
|
|
47 |
|
48 |
def bot(history):
|
49 |
print(history)
|
50 |
-
response = infer(history[-1][0]
|
51 |
history[-1][1] = ""
|
52 |
|
53 |
for character in response:
|
@@ -56,19 +57,11 @@ def bot(history):
|
|
56 |
yield history
|
57 |
|
58 |
|
59 |
-
def infer(question
|
60 |
-
|
61 |
-
res = []
|
62 |
-
for human, ai in history[:-1]:
|
63 |
-
pair = (human, ai)
|
64 |
-
res.append(pair)
|
65 |
|
66 |
-
|
67 |
-
#print(chat_history)
|
68 |
-
query = question
|
69 |
-
result = qa({"question": query, "chat_history": chat_history})
|
70 |
#print(result)
|
71 |
-
return result
|
72 |
|
73 |
|
74 |
css = """
|
|
|
2 |
import os
|
3 |
import time
|
4 |
|
5 |
+
from langchain.chains import LLMChain
|
6 |
+
from langchain.memory import ConversationBufferMemory
|
7 |
+
from langchain_community.llms import LlamaCpp
|
8 |
+
from langchain_experimental.chat_models import Llama2Chat
|
9 |
|
10 |
+
from langchain.prompts.chat import (
|
11 |
+
ChatPromptTemplate,
|
12 |
+
HumanMessagePromptTemplate,
|
13 |
+
MessagesPlaceholder,
|
14 |
+
)
|
15 |
|
16 |
+
from langchain.schema import SystemMessage
|
17 |
|
18 |
+
import urllib
|
19 |
|
20 |
+
urllib.request.urlretrieve(
|
21 |
+
"https://huggingface.co/hfl/chinese-alpaca-2-7b-rlhf-gguf/resolve/main/ggml-model-q6_k.gguf?download=true",
|
22 |
+
"ggml-model-q6_k.gguf"
|
23 |
+
)
|
24 |
|
25 |
+
template_messages = [
|
26 |
+
SystemMessage(content="你是一名软件工程师,你的名字叫做贺英旭。请你以这个身份回答以下问题!"),
|
27 |
+
MessagesPlaceholder(variable_name="chat_history"),
|
28 |
+
HumanMessagePromptTemplate.from_template("{text}"),
|
29 |
+
]
|
30 |
|
31 |
+
prompt_template = ChatPromptTemplate.from_messages(template_messages)
|
32 |
|
33 |
+
llm = LlamaCpp(
|
34 |
+
model_path="ggml-model-q6_k.gguf",
|
35 |
+
temperature=0.75,
|
36 |
+
max_tokens=64
|
37 |
+
)
|
38 |
+
model = Llama2Chat(llm=llm)
|
39 |
|
40 |
+
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
41 |
+
chain = LLMChain(llm=model, prompt=prompt_template, memory=memory)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
|
44 |
def add_text(history, text):
|
|
|
48 |
|
49 |
def bot(history):
|
50 |
print(history)
|
51 |
+
response = infer(history[-1][0])
|
52 |
history[-1][1] = ""
|
53 |
|
54 |
for character in response:
|
|
|
57 |
yield history
|
58 |
|
59 |
|
60 |
+
def infer(question):
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
+
result = chain.run(text=question).strip()
|
|
|
|
|
|
|
63 |
#print(result)
|
64 |
+
return result
|
65 |
|
66 |
|
67 |
css = """
|
requirements.txt
CHANGED
@@ -1,7 +1,10 @@
|
|
1 |
openai
|
|
|
2 |
tiktoken
|
3 |
chromadb
|
4 |
pypdf
|
5 |
langchain
|
|
|
|
|
6 |
unstructured
|
7 |
unstructured[local-inference]
|
|
|
1 |
openai
|
2 |
+
urllib
|
3 |
tiktoken
|
4 |
chromadb
|
5 |
pypdf
|
6 |
langchain
|
7 |
+
langchain_community
|
8 |
+
langchain_experimental
|
9 |
unstructured
|
10 |
unstructured[local-inference]
|