Spaces:
Paused
Paused
return to v1.0.0
Browse files
app.py
CHANGED
@@ -8,90 +8,58 @@ from langchain_core.output_parsers import StrOutputParser
|
|
8 |
from langchain_core.runnables import RunnablePassthrough
|
9 |
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
|
10 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
11 |
-
from pypdf import PdfReader, PdfWriter
|
12 |
-
from pathlib import Path
|
13 |
|
14 |
|
15 |
data_root = './data/pdf/'
|
|
|
16 |
|
17 |
-
|
18 |
-
return [data_root+path for path in os.listdir(data_root)]
|
19 |
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
docs = []
|
25 |
-
for loader in loaders:
|
26 |
-
docs.extend(
|
27 |
-
loader.load()[0:] # skip first page
|
28 |
-
)
|
29 |
-
|
30 |
-
chunk_size = 1000
|
31 |
-
chunk_overlap = 200
|
32 |
-
|
33 |
-
text_splitter = RecursiveCharacterTextSplitter(chunk_size=chunk_size,
|
34 |
-
chunk_overlap=chunk_overlap)
|
35 |
-
|
36 |
-
splits = text_splitter.split_documents(docs)
|
37 |
-
|
38 |
-
vectorstore = Chroma.from_documents(documents=splits, embedding=OpenAIEmbeddings())
|
39 |
-
retriever = vectorstore.as_retriever()
|
40 |
-
prompt = hub.pull("rlm/rag-prompt")
|
41 |
-
|
42 |
-
# model_name = 'gpt-3.5-turbo-0125'
|
43 |
-
# model_name = 'gpt-4-1106-preview'
|
44 |
-
model_name = 'gpt-4-0125-preview'
|
45 |
-
llm = ChatOpenAI(model_name=model_name, temperature=0)
|
46 |
|
47 |
-
|
48 |
-
|
49 |
|
50 |
-
|
51 |
-
{"context": retriever | format_docs, "question": RunnablePassthrough()}
|
52 |
-
| prompt
|
53 |
-
| llm
|
54 |
-
| StrOutputParser()
|
55 |
-
)
|
56 |
|
57 |
-
|
|
|
|
|
58 |
|
|
|
|
|
|
|
|
|
59 |
|
60 |
-
def
|
61 |
-
|
62 |
-
if pdf_file:
|
63 |
-
pdf_path = Path(pdf_file)
|
64 |
-
pdf_reader = PdfReader(pdf_path)
|
65 |
-
pdf_writer = PdfWriter()
|
66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
-
|
69 |
-
pdf_path = data_root + pdf_name
|
70 |
-
|
71 |
-
if pdf_path not in load_pdf_paths(data_root):
|
72 |
-
print('Saving file...')
|
73 |
-
for page in pdf_reader.pages:
|
74 |
-
pdf_writer.add_page(page)
|
75 |
-
|
76 |
-
with open(pdf_path, 'wb') as f:
|
77 |
-
pdf_writer.write(f)
|
78 |
-
os.system("ls data/pdf")
|
79 |
-
|
80 |
-
pdf_paths = load_pdf_paths(data_root)
|
81 |
-
rag_chain = build_rag_chain(pdf_paths)
|
82 |
return rag_chain.invoke(query)
|
83 |
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
|
93 |
textbox = gr.Textbox(label="اكتب سؤالك هنا", placeholder="", lines=4)
|
94 |
-
|
95 |
-
|
96 |
-
iface = gr.Interface(fn=predict, inputs=[textbox, upload_btn], outputs="text")
|
97 |
iface.launch(share=True)
|
|
|
8 |
from langchain_core.runnables import RunnablePassthrough
|
9 |
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
|
10 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
|
|
|
|
11 |
|
12 |
|
13 |
data_root = './data/pdf/'
|
14 |
+
pdf_paths = [data_root+path for path in os.listdir(data_root)]
|
15 |
|
16 |
+
loaders = [PyPDFLoader(path) for path in pdf_paths]
|
|
|
17 |
|
18 |
+
docs = []
|
19 |
+
for loader in loaders:
|
20 |
+
docs.extend(
|
21 |
+
loader.load()[0:] # skip first page
|
22 |
+
)
|
23 |
|
24 |
+
chunk_size = 1000
|
25 |
+
chunk_overlap = 200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=chunk_size,
|
28 |
+
chunk_overlap=chunk_overlap)
|
29 |
|
30 |
+
splits = text_splitter.split_documents(docs)
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
+
vectorstore = Chroma.from_documents(documents=splits, embedding=OpenAIEmbeddings())
|
33 |
+
retriever = vectorstore.as_retriever()
|
34 |
+
prompt = hub.pull("rlm/rag-prompt")
|
35 |
|
36 |
+
model_name = 'gpt-3.5-turbo-0125'
|
37 |
+
# model_name = 'gpt-4-1106-preview'
|
38 |
+
model_name = 'gpt-4-0125-preview'
|
39 |
+
llm = ChatOpenAI(model_name=model_name, temperature=0)
|
40 |
|
41 |
+
def format_docs(docs):
|
42 |
+
return '\n\n'.join(doc.page_content for doc in docs)
|
|
|
|
|
|
|
|
|
43 |
|
44 |
+
rag_chain = (
|
45 |
+
{"context": retriever | format_docs, "question": RunnablePassthrough()}
|
46 |
+
| prompt
|
47 |
+
| llm
|
48 |
+
| StrOutputParser()
|
49 |
+
)
|
50 |
|
51 |
+
def predict(query):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
return rag_chain.invoke(query)
|
53 |
|
54 |
+
examples = [
|
55 |
+
"هل هناك غرامة للتخلف عن سداد ضريبة القيمة المضافة؟",
|
56 |
+
"ما هي ضريبة القيمة المضافة؟",
|
57 |
+
"ما الواجب على الخاضغين لضريبة القيمة المضافة؟",
|
58 |
+
"من هو الشخص الخاضغ لضريبة القيمة المضافة؟",
|
59 |
+
"متى يجب على الشخص التسجيل لضريبة القيمة المضافة؟",
|
60 |
+
"أريد بيع منزل, هل يخضع ذلك لضريبة القيمة المضافة؟"
|
61 |
+
]
|
62 |
|
63 |
textbox = gr.Textbox(label="اكتب سؤالك هنا", placeholder="", lines=4)
|
64 |
+
iface = gr.Interface(fn=predict, inputs=textbox, outputs="text", examples=examples)
|
|
|
|
|
65 |
iface.launch(share=True)
|