Rahulad3 commited on
Commit
c16c86c
·
1 Parent(s): a4b57cd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -3
app.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  import numpy as np
2
  import gradio as gr
3
  import json
@@ -161,17 +163,38 @@ def upload_file(org, bot , files):
161
  file_paths = [file.name for file in files]
162
  allfiles=file_paths
163
  path = os.path.join(os.getcwd()+'\\Organizations\\', org,bot,'Inputs')
164
- shutil.copy(file_paths.name, path)
 
 
 
165
  return file_paths
166
 
 
167
  def train(files):
168
  for file in files:
169
  print(file.name)
170
  if file.name.endswith(".pdf"):
171
- loader = PyPDFLoader(file.name)
172
- documents = loader.load()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173
  return "Training Done"
174
  chat_history = []
 
 
175
 
176
  def construct_index(directory_path):
177
  file_paths=[]
@@ -179,6 +202,7 @@ def construct_index(directory_path):
179
  for file_name in files:
180
  file_path = os.path.join(root, file_name)
181
  file_paths.append(file_path)
 
182
 
183
 
184
  for file in file_paths:
@@ -190,6 +214,8 @@ def construct_index(directory_path):
190
 
191
  def chatbot(input_text):
192
  global chat_history
 
 
193
  query = input_text
194
  text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
195
  texts = text_splitter.split_documents(documents)
@@ -223,6 +249,9 @@ with demo:
223
  with gr.Column():
224
  query_input = gr.Textbox(lines=7, label="Enter your text")
225
  ask_button = gr.Button("Ask")
 
 
 
226
  with gr.TabItem("Settings"):
227
  with gr.Tabs():
228
 
 
1
+ import fitz
2
+ import PyPDF2
3
  import numpy as np
4
  import gradio as gr
5
  import json
 
163
  file_paths = [file.name for file in files]
164
  allfiles=file_paths
165
  path = os.path.join(os.getcwd()+'\\Organizations\\', org,bot,'Inputs')
166
+ os.makedirs(path, exist_ok=True)
167
+
168
+ for file_path in file_paths:
169
+ shutil.copy(file_path, path)
170
  return file_paths
171
 
172
+
173
  def train(files):
174
  for file in files:
175
  print(file.name)
176
  if file.name.endswith(".pdf"):
177
+ pdf_document = fitz.open(file.name)
178
+ documents = []
179
+ for page_num in range(pdf_document.page_count):
180
+ page = pdf_document.load_page(page_num)
181
+ text = page.get_text()
182
+ documents.append(text)
183
+
184
+
185
+ output_folder = "Output"
186
+ os.makedirs(output_folder, exist_ok=True)
187
+ output_file_name = os.path.splitext(os.path.basename(file.name))[0]
188
+ output_file_path = os.path.join(output_folder, f"{output_file_name}_page_{page_num + 1}.txt")
189
+
190
+ with open(output_file_path, "w", encoding="utf-8") as output_file:
191
+ for text in documents:
192
+ output_file.write(text)
193
+
194
  return "Training Done"
195
  chat_history = []
196
+ documents = []
197
+ documents = construct_index(os.path.join(os.getcwd(), 'Organizations'))
198
 
199
  def construct_index(directory_path):
200
  file_paths=[]
 
202
  for file_name in files:
203
  file_path = os.path.join(root, file_name)
204
  file_paths.append(file_path)
205
+
206
 
207
 
208
  for file in file_paths:
 
214
 
215
  def chatbot(input_text):
216
  global chat_history
217
+ global documents
218
+ os.environ["OPENAI_API_KEY"] = "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"
219
  query = input_text
220
  text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
221
  texts = text_splitter.split_documents(documents)
 
249
  with gr.Column():
250
  query_input = gr.Textbox(lines=7, label="Enter your text")
251
  ask_button = gr.Button("Ask")
252
+ text_output = gr.Text(label="Your Bot Answer")
253
+ ask_button.click(fn=lambda btn: [text_output.update(chatbot(query_input.value)), chat_history.append(query_input.value)], inputs=[query_input], outputs=text_output)
254
+
255
  with gr.TabItem("Settings"):
256
  with gr.Tabs():
257