-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
57 lines (45 loc) · 2.11 KB
/
app.py
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
import argparse
import gradio as gr
import os
from langchain.chains import RetrievalQA
from langchain.indexes import VectorstoreIndexCreator
from langchain.text_splitter import CharacterTextSplitter
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.document_loaders import TextLoader
from langchain.llms import HuggingFaceHub
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description="Query data from SPARC.")
parser.add_argument('--hf_token', type=str, required=True,
help="HuggingFace account token.")
parser.add_argument("--txt_folder", type=str,
default='./texts', help="Path to txt files.")
args = parser.parse_args()
txt_folder = args.txt_folder
loaders = [TextLoader(os.path.join(txt_folder, fn)) for fn in
os.listdir(txt_folder)]
embeddings = HuggingFaceEmbeddings(
model_name='sentence-transformers/all-mpnet-base-v2')
index = VectorstoreIndexCreator(
embedding=embeddings,
text_splitter=CharacterTextSplitter(chunk_size=2000,
chunk_overlap=1)).from_loaders(
loaders)
llm = HuggingFaceHub(repo_id="tiiuae/falcon-7b-instruct",
model_kwargs={"temperature": 0.05,
"min_length": 2000,
"max_length": 5000,
"max_new_tokens": 100},
huggingfacehub_api_token=args.hf_token)
qa = RetrievalQA.from_chain_type(llm=llm,
chain_type="stuff",
retriever=index.vectorstore.as_retriever())
def ask(query):
result = qa({"query": query})
return result["result"]
iface = gr.Interface(fn=ask,
inputs=gr.components.Textbox(lines=7,
label="Enter your text"),
outputs="text",
title="Ask your SPARC datasets anything!")
iface.launch(share=True)