-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainn.py
More file actions
73 lines (48 loc) · 1.29 KB
/
Copy pathmainn.py
File metadata and controls
73 lines (48 loc) · 1.29 KB
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
import os
from langchain_community.document_loaders import TextLoader
from langchain_text_splitters import CharacterTextSplitter
from langchain_community.vectorstores import FAISS
from langchain_huggingface import HuggingFaceEmbeddings
from transformers import pipeline
# Load files
files = [
"data/bookkeeping.txt",
"data/invoice.txt",
"data/profit_loss.txt",
"data/balance_sheet.txt",
"data/cash_flow.txt"
]
documents = []
for file in files:
loader = TextLoader(file)
documents.extend(loader.load())
# Split documents
splitter = CharacterTextSplitter(
chunk_size=500,
chunk_overlap=50
)
docs = splitter.split_documents(documents)
# Create embeddings
embeddings = HuggingFaceEmbeddings()
# Create vector database
db = FAISS.from_documents(docs, embeddings)
retriever = db.as_retriever()
# Load local model
generator = pipeline(
"text-generation",
model="google/flan-t5-base"
)
# Ask questions
while True:
question = input("Ask question: ")
docs = retriever.invoke(question)
context = "\n".join([d.page_content for d in docs])
prompt = f"""
Answer the question based on the following context.
Context:
{context}
Question:
{question}
"""
result = generator(prompt, max_length=200)
print("\nAnswer:", result[0]["generated_text"], "\n")