raseel-zymr commited on
Commit
952eb35
1 Parent(s): 5aee298

Upload TXT and PDF files

Browse files
Files changed (1) hide show
  1. app.py +33 -10
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import os
2
  import streamlit as st
 
3
  from io import StringIO
4
 
5
  #for textfiles
@@ -18,12 +19,42 @@ from langchain.document_loaders import UnstructuredPDFLoader
18
 
19
  os.environ["HUGGINGFACEHUB_API_TOKEN"] = st.secrets["hf_api_key"]
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  st.title('Document Q&A - Ask anything in your Document')
 
22
  st.sidebar.subheader('Upload document')
23
  uploaded_file = st.sidebar.file_uploader("Upload File",type=['txt','pdf'])
 
 
 
 
 
 
 
24
  with st.sidebar.expander('File'):
25
- if(uploaded_file):
26
- st.info(uploaded_file.name)
 
 
 
27
 
28
 
29
 
@@ -42,12 +73,6 @@ if(uploaded_file):
42
  loader = TextLoader(uploaded_file.name)
43
  documents = loader.load()
44
 
45
- # Document Loader
46
- #loader = TextLoader('./KS-all-info_rev1.txt')
47
-
48
- # loader = TextLoader(os.path.join("./", uploaded_file.name))
49
-
50
-
51
  # import textwrap
52
  # def wrap_text_preserve_newlines(text, width=110):
53
  # # Split the input text into lines based on newline characters
@@ -78,8 +103,6 @@ if(uploaded_file):
78
  docs = db.similarity_search(query)
79
  answer = chain.run(input_documents=docs, question=query)
80
 
81
-
82
-
83
  st.subheader('Answer')
84
  st.write(answer)
85
 
 
1
  import os
2
  import streamlit as st
3
+ from pathlib import Path
4
  from io import StringIO
5
 
6
  #for textfiles
 
19
 
20
  os.environ["HUGGINGFACEHUB_API_TOKEN"] = st.secrets["hf_api_key"]
21
 
22
+ def pdf_file(filename):
23
+ st.subheader('Uploaded PDF File:')
24
+ st.write(filename)
25
+
26
+ def text_file(filename):
27
+ st.subheader('Uploaded Text File:')
28
+ st.write(filename)
29
+
30
+ # loader = TextLoader(filename)
31
+ # documents = loader.load()
32
+
33
+ # # Text Splitter
34
+ # text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=10)
35
+ # docs = text_splitter.split_documents(documents)
36
+
37
+ # db = FAISS.from_documents(docs, embeddings)
38
+
39
+ # chain = load_qa_chain(llm2, chain_type="stuff")
40
+
41
  st.title('Document Q&A - Ask anything in your Document')
42
+ st.subheader('This application can be used to upload text(.txt) and PDF(.pdf) files and ask questions about their contents.')
43
  st.sidebar.subheader('Upload document')
44
  uploaded_file = st.sidebar.file_uploader("Upload File",type=['txt','pdf'])
45
+
46
+ if Path(uploaded_file.name).suffix == '.txt':
47
+ text_file(uploaded_file.name)
48
+
49
+ if Path(uploaded_file.name).suffix == '.pdf':
50
+ pdf_file(uploaded_file.name)
51
+
52
  with st.sidebar.expander('File'):
53
+ if (uploaded_file):
54
+ st.info(uploaded_file.name)
55
+ if os.path.exists('/content/'):
56
+ st.info(os.listdir('/content/'))
57
+
58
 
59
 
60
 
 
73
  loader = TextLoader(uploaded_file.name)
74
  documents = loader.load()
75
 
 
 
 
 
 
 
76
  # import textwrap
77
  # def wrap_text_preserve_newlines(text, width=110):
78
  # # Split the input text into lines based on newline characters
 
103
  docs = db.similarity_search(query)
104
  answer = chain.run(input_documents=docs, question=query)
105
 
 
 
106
  st.subheader('Answer')
107
  st.write(answer)
108