-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpractice.py
More file actions
66 lines (50 loc) · 1.89 KB
/
practice.py
File metadata and controls
66 lines (50 loc) · 1.89 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
from dotenv import load_dotenv
import os
from PyPDF2 import PdfReader
import streamlit as st
from langchain.text_splitter import CharacterTextSplitter
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain_community.vectorstores import FAISS
from langchain.chains.question_answering import load_qa_chain
from langchain.llms import OpenAI
import tiktoken
from langchain.callbacks import get_openai_callback
import openai
from text2voice import text_to_speech
load_dotenv()
def process_text(text):
text_splitter = CharacterTextSplitter(
separator="\n",
chunk_size=1000,
chunk_overlap=200,
length_function=len
)
chunks = text_splitter.split_text(text)
os.environ["OPENAI_API_KEY"] = os.getenv('MY_API_KEY')
embeddings = OpenAIEmbeddings()
knowledgeBase = FAISS.from_texts(chunks, embeddings)
return knowledgeBase
def main():
st.title("pdf")
pdf = st.file_uploader('Upload your PDF Document', type='pdf')
if pdf is not None:
pdf_reader = PdfReader(pdf)
text = ""
for page in pdf_reader.pages:
text += page.extract_text()
knowledgeBase = process_text(text)
query = st.text_input('Ask a question to the PDF')
cancel_button = st.button('Cancel')
if cancel_button:
st.stop()
if query:
docs = knowledgeBase.similarity_search(query)
chain = load_qa_chain(llm=OpenAI(), chain_type="stuff")
response = chain.run(input_documents=docs, question=query)
with get_openai_callback() as cost:
response = chain.run(input_documents=docs, question=query)
print(cost)
st.write(response)
text_to_speech(response)
if __name__ == "__main__":
main()