Spaces:
Sleeping
Sleeping
Shoaib Majid
commited on
Commit
β’
aa63618
1
Parent(s):
d0974d9
upload
Browse files- apikey.py +8 -0
- app.py +105 -0
- requirements.txt +5 -0
apikey.py
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
|
3 |
+
def get_apikey():
|
4 |
+
OPENAI_API_KEY = st.text_input(":blue[Enter Your OPENAI API-KEY :]",
|
5 |
+
placeholder="Paste your OpenAI API key here (sk-...)",
|
6 |
+
type="password",
|
7 |
+
)
|
8 |
+
return OPENAI_API_KEY
|
app.py
ADDED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
from apikey import get_apikey
|
4 |
+
from PyPDF2 import PdfReader
|
5 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
6 |
+
from langchain.vectorstores import FAISS
|
7 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
8 |
+
from langchain.chains import LLMChain
|
9 |
+
from langchain.prompts import PromptTemplate
|
10 |
+
from langchain.chat_models import ChatOpenAI
|
11 |
+
from langchain.memory import ConversationBufferMemory
|
12 |
+
|
13 |
+
|
14 |
+
|
15 |
+
def make_context(docs):
|
16 |
+
context = ""
|
17 |
+
for doc in docs:
|
18 |
+
doc = doc.page_content + "\n\nSource: " + doc.metadata
|
19 |
+
context = context + doc + "\n\n"
|
20 |
+
return context
|
21 |
+
|
22 |
+
|
23 |
+
|
24 |
+
|
25 |
+
OPENAI_API_KEY = get_apikey()
|
26 |
+
|
27 |
+
|
28 |
+
os.environ['OPENAI_API_KEY'] = OPENAI_API_KEY
|
29 |
+
|
30 |
+
if OPENAI_API_KEY:
|
31 |
+
llm = ChatOpenAI(model='gpt-3.5-turbo',temperature = 0, openai_api_key=OPENAI_API_KEY, max_tokens=800)
|
32 |
+
gptturbo = ChatOpenAI(model='gpt-3.5-turbo',temperature = 0, openai_api_key=OPENAI_API_KEY, max_tokens=800)
|
33 |
+
|
34 |
+
|
35 |
+
if "generated" not in st.session_state:
|
36 |
+
st.session_state["generated"] = []
|
37 |
+
if "past" not in st.session_state:
|
38 |
+
st.session_state["past"] = []
|
39 |
+
if "input" not in st.session_state:
|
40 |
+
st.session_state["input"] = ""
|
41 |
+
if "stored_session" not in st.session_state:
|
42 |
+
st.session_state["stored_session"] = []
|
43 |
+
if "memory" not in st.session_state:
|
44 |
+
st.session_state.memory = ConversationBufferMemory(memory_key="chat_history")
|
45 |
+
|
46 |
+
|
47 |
+
|
48 |
+
uploaded_file = st.file_uploader('Choose your .pdf file', type="pdf")
|
49 |
+
|
50 |
+
|
51 |
+
if uploaded_file is not None:
|
52 |
+
pdf_reader = PdfReader(uploaded_file)
|
53 |
+
data = ""
|
54 |
+
for page in pdf_reader.pages:
|
55 |
+
data += page.extract_text()
|
56 |
+
|
57 |
+
text_splitter = RecursiveCharacterTextSplitter(
|
58 |
+
chunk_size = 800,
|
59 |
+
chunk_overlap = 0
|
60 |
+
)
|
61 |
+
|
62 |
+
|
63 |
+
texts = text_splitter.split_text(data)
|
64 |
+
embeddings = OpenAIEmbeddings(openai_api_key=OPENAI_API_KEY)
|
65 |
+
docsearch = FAISS.from_texts(texts, embedding = embeddings)
|
66 |
+
|
67 |
+
question = question = st.text_input('Ask any question', key="input")
|
68 |
+
|
69 |
+
if question:
|
70 |
+
docs = docsearch.similarity_search(query=question)
|
71 |
+
for doc in docs:
|
72 |
+
doc.metadata = uploaded_file.name
|
73 |
+
template = """
|
74 |
+
your job is to answer the questions asked by the users. Create a final answer with references ("SOURCES").
|
75 |
+
If the answer is not in the context, then try to answer it using your own knowledge.
|
76 |
+
Source of the context is written at the end of the context.
|
77 |
+
At the end of your answer write the source of the context in the following way: \n\nSource: (source)
|
78 |
+
Chat history is also provided to you.
|
79 |
+
Context: {context}
|
80 |
+
---
|
81 |
+
Chat History: {chat_history}
|
82 |
+
Question: {question}
|
83 |
+
Answer: Let's think step by step and give best answer possible. Use points when needed.
|
84 |
+
"""
|
85 |
+
|
86 |
+
context = make_context(docs)
|
87 |
+
prompt = PromptTemplate(template=template, input_variables=["context", "question", "chat_history"]).partial(context=context)
|
88 |
+
|
89 |
+
llm_chain = LLMChain(prompt=prompt, llm=gptturbo, verbose=False, memory=st.session_state.memory)
|
90 |
+
|
91 |
+
|
92 |
+
response = llm_chain.run(question)
|
93 |
+
st.session_state.past.append(question)
|
94 |
+
st.session_state.generated.append(response)
|
95 |
+
|
96 |
+
with st.expander("Conversation", expanded=True):
|
97 |
+
for i in range(len(st.session_state['generated'])-1, -1, -1):
|
98 |
+
st.info(st.session_state["past"][i],icon="π§")
|
99 |
+
st.success(st.session_state["generated"][i], icon="π€")
|
100 |
+
|
101 |
+
|
102 |
+
|
103 |
+
|
104 |
+
|
105 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
langchain
|
2 |
+
faiss-cpu
|
3 |
+
OpenAI
|
4 |
+
PyPDF2
|
5 |
+
tiktoken
|