Upload 3 files
Browse files- app.py +84 -0
- requirements.txt +8 -0
- slide.pptx +0 -0
app.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from dotenv import load_dotenv
|
3 |
+
from PyPDF2 import PdfReader
|
4 |
+
import openai
|
5 |
+
from langchain.embeddings import OpenAIEmbeddings
|
6 |
+
from langchain.text_splitter import CharacterTextSplitter
|
7 |
+
from langchain.vectorstores import Chroma
|
8 |
+
from langchain.vectorstores.pinecone import Pinecone
|
9 |
+
#from langchain import LLMChain
|
10 |
+
from langchain.chains import ConversationalRetrievalChain
|
11 |
+
from langchain.llms import OpenAI
|
12 |
+
from langchain.memory import ConversationBufferMemory
|
13 |
+
import streamlit as st
|
14 |
+
import os
|
15 |
+
|
16 |
+
os.environ['OPEN_API_KEY'] ='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'
|
17 |
+
|
18 |
+
def db(texts,text_splitter,name):
|
19 |
+
chunks = text_splitter.split_text(texts)
|
20 |
+
embeddings = OpenAIEmbeddings()
|
21 |
+
db = Chroma.from_texts(chunks, embeddings,index_name="pdfgpt",namespace=name)
|
22 |
+
retriever = db.as_retriever(search_type="similarity", search_kwargs={"k":2})
|
23 |
+
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
24 |
+
qa = ConversationalRetrievalChain.from_llm(llm=OpenAI(temperature=0), retriever=retriever, chain_type="refine",memory=memory)
|
25 |
+
return qa
|
26 |
+
|
27 |
+
def ai(prompt):
|
28 |
+
file = open("save.txt","r")
|
29 |
+
system_prompt = str(file.read())
|
30 |
+
print(system_prompt)
|
31 |
+
file.close()
|
32 |
+
#prompt=system_prompt+str(": question is :")+prompt
|
33 |
+
result = qa({"question": prompt, "chat_history": chat_history})
|
34 |
+
return result["answer"]
|
35 |
+
|
36 |
+
def main():
|
37 |
+
global qa, chat_history
|
38 |
+
placeholder=st.empty()
|
39 |
+
placeholder.empty()
|
40 |
+
placeholder.title("Upload or Chat PDF")
|
41 |
+
#st.header("PDF/URL QA")
|
42 |
+
#global system_prompt
|
43 |
+
names = ['Upload', 'Random talk']
|
44 |
+
page = st.radio('Format', names)
|
45 |
+
|
46 |
+
if page == 'Upload':
|
47 |
+
pdf = st.file_uploader("Upload your PDF", type="pdf")
|
48 |
+
if pdf is not None:
|
49 |
+
#print(pdf)
|
50 |
+
name=st.text_input("Name of your pdf")
|
51 |
+
name=name.lower()
|
52 |
+
pdf_reader = PdfReader(pdf)
|
53 |
+
texts = ""
|
54 |
+
for page in pdf_reader.pages:
|
55 |
+
texts += page.extract_text()
|
56 |
+
text_splitter = CharacterTextSplitter(
|
57 |
+
separator="\n",
|
58 |
+
chunk_size = 1000,
|
59 |
+
chunk_overlap = 0
|
60 |
+
)
|
61 |
+
qa=db(texts,text_splitter,name)
|
62 |
+
chat_history = []
|
63 |
+
st.header("PDF/URL QA")
|
64 |
+
query = st.text_input("Ask a question in PDF")
|
65 |
+
if query:
|
66 |
+
output = ai(query)
|
67 |
+
chat_history=chat_history.append(query)
|
68 |
+
st.write(output)
|
69 |
+
|
70 |
+
elif page == 'Random talk':
|
71 |
+
chat_history=[]
|
72 |
+
st.header("Start Chatting")
|
73 |
+
name=st.text_input("Your message")
|
74 |
+
prompt = "\"Act like a personal assistant. You can respond to questions, translate sentences, summarize news, and give recommendations. " + user_message + "\""
|
75 |
+
# Call the OpenAI Api to process our prompt
|
76 |
+
openai_response = openai.Completion.create(model="text-davinci-003", prompt=prompt,max_tokens=4000)
|
77 |
+
print("openai response:", openai_response)
|
78 |
+
# Parse the response to get the response text for our prompt
|
79 |
+
response_text = openai_response.choices[0].text
|
80 |
+
st.write( response_text)
|
81 |
+
|
82 |
+
|
83 |
+
if __name__ == "__main__":
|
84 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
openai
|
2 |
+
goose3
|
3 |
+
chromadb
|
4 |
+
langchain
|
5 |
+
PyPDF2
|
6 |
+
python-dotenv
|
7 |
+
tiktoken
|
8 |
+
streamlit
|
slide.pptx
ADDED
Binary file (65.3 kB). View file
|
|