fehmikaya commited on
Commit
0ce97ce
·
verified ·
1 Parent(s): b7921f3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -18
app.py CHANGED
@@ -11,21 +11,21 @@ from langchain_core.output_parsers import JsonOutputParser
11
  from langchain_core.output_parsers import StrOutputParser
12
  from langchain_core.prompts import PromptTemplate
13
  from langchain_community.document_loaders import WebBaseLoader
14
- from langchain_community.document_loaders import PyPDFLoader
15
  from langchain_community.vectorstores import Chroma
16
  from langchain_community.embeddings.sentence_transformer import SentenceTransformerEmbeddings
17
  from langchain_text_splitters import RecursiveCharacterTextSplitter
18
 
19
 
20
  icons = {"assistant": "robot.png", "user": "man-kddi.png"}
21
- # DATA_DIR = "data"
22
  # Ensure data directory exists
23
- # os.makedirs(DATA_DIR, exist_ok=True)
24
 
25
  # if not hasattr(st.session_state, 'agent'):
26
  # st.session_state.agent = "None"
27
 
28
- '''
29
  def init_agent_with_docs():
30
 
31
  docs=[]
@@ -42,10 +42,10 @@ def init_agent_with_docs():
42
  return RAGAgent(docs)
43
 
44
  def remove_old_files():
45
- st.session_state["console_out"] += "remove_old_files\n"
46
- shutil.rmtree(DATA_DIR)
47
- os.makedirs(DATA_DIR)
48
- '''
49
 
50
  def streamer(text):
51
  for i in text:
@@ -73,21 +73,13 @@ with st.sidebar:
73
  if st.button("Submit & Process"):
74
  with st.spinner("Processing..."):
75
  st.session_state["console_out"] = ""
76
- #if len(os.listdir(DATA_DIR)) !=0:
77
- # remove_old_files()
78
- '''
79
  for index, file in enumerate(uploaded_files):
80
  filepath = os.path.join(DATA_DIR, f"saved_pdf_{index}.pdf")
81
  with open(filepath, "wb") as f:
82
  f.write(file.getbuffer())
83
  st.session_state.agent = init_agent_with_docs()
84
- '''
85
- if uploaded_files:
86
- pdf_docs = []
87
- for uploaded_file in uploaded_files:
88
- pdf_loader = PyPDFLoader(uploaded_file)
89
- pdf_docs.append(pdf_loader)
90
- st.session_state.agent = RAGAgent(pdf_docs)
91
  st.success("Done")
92
  st.text_area("Console", st.session_state["console_out"], height=250)
93
 
 
11
  from langchain_core.output_parsers import StrOutputParser
12
  from langchain_core.prompts import PromptTemplate
13
  from langchain_community.document_loaders import WebBaseLoader
14
+ from langchain_community.document_loaders import PyPDFDirectoryLoader
15
  from langchain_community.vectorstores import Chroma
16
  from langchain_community.embeddings.sentence_transformer import SentenceTransformerEmbeddings
17
  from langchain_text_splitters import RecursiveCharacterTextSplitter
18
 
19
 
20
  icons = {"assistant": "robot.png", "user": "man-kddi.png"}
21
+ DATA_DIR = "data"
22
  # Ensure data directory exists
23
+ os.makedirs(DATA_DIR, exist_ok=True)
24
 
25
  # if not hasattr(st.session_state, 'agent'):
26
  # st.session_state.agent = "None"
27
 
28
+
29
  def init_agent_with_docs():
30
 
31
  docs=[]
 
42
  return RAGAgent(docs)
43
 
44
  def remove_old_files():
45
+ if len(os.listdir(DATA_DIR)) !=0:
46
+ st.session_state["console_out"] += "remove_old_files\n"
47
+ shutil.rmtree(DATA_DIR)
48
+ os.makedirs(DATA_DIR)
49
 
50
  def streamer(text):
51
  for i in text:
 
73
  if st.button("Submit & Process"):
74
  with st.spinner("Processing..."):
75
  st.session_state["console_out"] = ""
76
+ remove_old_files()
 
 
77
  for index, file in enumerate(uploaded_files):
78
  filepath = os.path.join(DATA_DIR, f"saved_pdf_{index}.pdf")
79
  with open(filepath, "wb") as f:
80
  f.write(file.getbuffer())
81
  st.session_state.agent = init_agent_with_docs()
82
+ remove_old_files()
 
 
 
 
 
 
83
  st.success("Done")
84
  st.text_area("Console", st.session_state["console_out"], height=250)
85