sourabhzanwar commited on
Commit
7d397b0
1 Parent(s): 9d27621

added seperate file to chec pydantic version

Browse files
Files changed (2) hide show
  1. app.py +11 -36
  2. utils/check_pydantic_version.py +26 -0
app.py CHANGED
@@ -1,31 +1,5 @@
1
- import pydantic
2
- module_file_path = pydantic.__file__
3
-
4
- module_file_path = module_file_path.split('pydantic')[0] + 'haystack'
5
-
6
- import os
7
- import fileinput
8
-
9
-
10
- def replace_string_in_files(folder_path, old_str, new_str):
11
- for subdir, dirs, files in os.walk(folder_path):
12
- for file in files:
13
- file_path = os.path.join(subdir, file)
14
-
15
- # Check if the file is a text file (you can modify this condition based on your needs)
16
- if file.endswith(".txt") or file.endswith(".py"):
17
- # Open the file in place for editing
18
- with fileinput.FileInput(file_path, inplace=True) as f:
19
- for line in f:
20
- # Replace the old string with the new string
21
- print(line.replace(old_str, new_str), end='')
22
-
23
- with open(module_file_path+'/schema.py','r') as f:
24
- haystack_schema_file = f.read()
25
-
26
- if 'from pydantic.v1' not in haystack_schema_file:
27
- replace_string_in_files(module_file_path, 'from pydantic', 'from pydantic.v1')
28
-
29
 
30
  from operator import index
31
  import streamlit as st
@@ -52,6 +26,7 @@ txt_converter = TextConverter(remove_numeric_tables=True, valid_languages=["en",
52
 
53
  # Whether the file upload should be enabled or not
54
  DISABLE_FILE_UPLOAD = bool(os.getenv("DISABLE_FILE_UPLOAD"))
 
55
  # Define a function to handle file uploads
56
  def upload_files():
57
  uploaded_files = upload_container.file_uploader(
@@ -60,7 +35,6 @@ def upload_files():
60
  return uploaded_files
61
 
62
  # Define a function to process a single file
63
-
64
  def process_file(data_file, preprocesor, document_store):
65
  # read file and add content
66
  file_contents = data_file.read().decode("utf-8")
@@ -85,10 +59,8 @@ def process_file(data_file, preprocesor, document_store):
85
  except Exception as e:
86
  print(e)
87
 
88
- def reset_documents():
89
- print('\nReseting documents list at ' + str(datetime.now()) + '\n')
90
- document_store.delete_documents()
91
 
 
92
  def upload_document():
93
  print(f'Uploading document store at {datetime.now()}')
94
  upload_status = 0
@@ -106,6 +78,11 @@ def upload_document():
106
  upload_container.write(str(data_file.name) + "    ❌ ")
107
  upload_container.write("_This file could not be parsed, see the logs for more information._")
108
 
 
 
 
 
 
109
  try:
110
  args = parser.parse_args()
111
  preprocesor = start_preprocessor_node()
@@ -123,7 +100,6 @@ try:
123
  }
124
  )
125
  st.sidebar.image("ml_logo.png", use_column_width=True)
126
-
127
 
128
  # Sidebar for Task Selection
129
  st.sidebar.header('Options:')
@@ -154,13 +130,12 @@ try:
154
  if not DISABLE_FILE_UPLOAD:
155
  upload_container = st.sidebar.container()
156
  upload_container.write("## File Upload:")
157
- #data_files = st.sidebar.file_uploader(
158
- # "upload", type=["pdf", "txt", "docx"], accept_multiple_files=True, label_visibility="hidden"
159
- #)
160
  data_files = upload_files()
161
 
 
162
  upload_container.button('Upload Files', on_click=upload_document, args=())
163
 
 
164
  st.sidebar.button("Reset documents", on_click=reset_documents, args=())
165
 
166
  if "question" not in st.session_state:
 
1
+ from utils.check_pydantic_version import use_pydantic_v1
2
+ use_pydantic_v1() #This function has to be run before importing haystack. as haystack requires pydantic v1 to run
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
  from operator import index
5
  import streamlit as st
 
26
 
27
  # Whether the file upload should be enabled or not
28
  DISABLE_FILE_UPLOAD = bool(os.getenv("DISABLE_FILE_UPLOAD"))
29
+
30
  # Define a function to handle file uploads
31
  def upload_files():
32
  uploaded_files = upload_container.file_uploader(
 
35
  return uploaded_files
36
 
37
  # Define a function to process a single file
 
38
  def process_file(data_file, preprocesor, document_store):
39
  # read file and add content
40
  file_contents = data_file.read().decode("utf-8")
 
59
  except Exception as e:
60
  print(e)
61
 
 
 
 
62
 
63
+ # Define a function to upload the documents to haystack document store
64
  def upload_document():
65
  print(f'Uploading document store at {datetime.now()}')
66
  upload_status = 0
 
78
  upload_container.write(str(data_file.name) + "    ❌ ")
79
  upload_container.write("_This file could not be parsed, see the logs for more information._")
80
 
81
+ # Define a function to reset the documents in haystack document store
82
+ def reset_documents():
83
+ print('\nReseting documents list at ' + str(datetime.now()) + '\n')
84
+ document_store.delete_documents()
85
+
86
  try:
87
  args = parser.parse_args()
88
  preprocesor = start_preprocessor_node()
 
100
  }
101
  )
102
  st.sidebar.image("ml_logo.png", use_column_width=True)
 
103
 
104
  # Sidebar for Task Selection
105
  st.sidebar.header('Options:')
 
130
  if not DISABLE_FILE_UPLOAD:
131
  upload_container = st.sidebar.container()
132
  upload_container.write("## File Upload:")
 
 
 
133
  data_files = upload_files()
134
 
135
+ # Button to update files in the documentStore
136
  upload_container.button('Upload Files', on_click=upload_document, args=())
137
 
138
+ # Button to reset the documents in DocumentStore
139
  st.sidebar.button("Reset documents", on_click=reset_documents, args=())
140
 
141
  if "question" not in st.session_state:
utils/check_pydantic_version.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pydantic
2
+ import os
3
+ import fileinput
4
+
5
+ def replace_string_in_files(folder_path, old_str, new_str):
6
+ for subdir, dirs, files in os.walk(folder_path):
7
+ for file in files:
8
+ file_path = os.path.join(subdir, file)
9
+
10
+ # Check if the file is a text file (you can modify this condition based on your needs)
11
+ if file.endswith(".txt") or file.endswith(".py"):
12
+ # Open the file in place for editing
13
+ with fileinput.FileInput(file_path, inplace=True) as f:
14
+ for line in f:
15
+ # Replace the old string with the new string
16
+ print(line.replace(old_str, new_str), end='')
17
+
18
+
19
+ def use_pydantic_v1():
20
+ module_file_path = pydantic.__file__
21
+ module_file_path = module_file_path.split('pydantic')[0] + 'haystack'
22
+ with open(module_file_path+'/schema.py','r') as f:
23
+ haystack_schema_file = f.read()
24
+
25
+ if 'from pydantic.v1' not in haystack_schema_file:
26
+ replace_string_in_files(module_file_path, 'from pydantic', 'from pydantic.v1')