hamidme commited on
Commit
95a367c
1 Parent(s): 28893cf
Files changed (1) hide show
  1. app.py +70 -0
app.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import streamlit as st
3
+ from PyPDF2 import PdfReader
4
+ from langchain.text_splitter import CharacterTextSplitter
5
+ from langchain.embeddings import HuggingFaceEmbeddings
6
+ from langchain.vectorstores import FAISS
7
+ from langchain.chains.question_answering import load_qa_chain
8
+ import random
9
+ from langchain import HuggingFaceHub
10
+ from langchain.callbacks import get_openai_callback
11
+
12
+ def main():
13
+ # ---------------------------- created personal API -----------------------------
14
+ os.environ["HUGGINGFACEHUB_API_TOKEN"] = "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"
15
+
16
+ # ------------------ Designing Page ---------------
17
+ st.set_page_config(page_title="Ask Your PDF")
18
+ st.header("Ask your PDF :")
19
+
20
+ pdf = st.file_uploader("Upload your File here", type="pdf")
21
+
22
+ # Check Pdf
23
+ if pdf is not None:
24
+ pdf_reader = PdfReader(pdf)
25
+
26
+ text = ""
27
+
28
+ # Extract pages from pdf
29
+ for page in pdf_reader.pages:
30
+ text += page.extract_text()
31
+
32
+ # split into chunks
33
+ text_spliter = CharacterTextSplitter(
34
+ separator="\n",
35
+ chunk_size=1000,
36
+ chunk_overlap=0,
37
+ length_function=len
38
+ )
39
+
40
+ chunks = text_spliter.split_text(text)
41
+
42
+ # create embeddings
43
+ embedding = HuggingFaceEmbeddings()
44
+ knowledge_base = FAISS.from_texts(chunks, embedding)
45
+
46
+ user_questions = st.text_input("Ask a Question from PDF : ")
47
+ if user_questions:
48
+
49
+ greeting = ["hy", 'hello', 'hey', "hi"]
50
+ greet_msg = ["Hello Dear!", 'Hey!', 'Hey Friend!']
51
+
52
+ if user_questions in greeting:
53
+ response = random.choice(greet_msg)
54
+ elif user_questions == "by" or user_questions == "bye":
55
+ response = "GoodBye Sir!, Have a Nice Day....."
56
+ else:
57
+
58
+ docs = knowledge_base.similarity_search(user_questions)
59
+
60
+ chain = load_qa_chain(HuggingFaceHub(repo_id="google/flan-t5-xxl", model_kwargs={"temperature":0.1, "max_length":512}), chain_type="stuff")
61
+ with get_openai_callback() as cb:
62
+
63
+ response = chain.run(input_documents=docs, question=user_questions)
64
+ print(cb)
65
+
66
+ st.write(response)
67
+
68
+
69
+ if __name__ == "__main__":
70
+ main()