awacke1 commited on
Commit
f506f1d
·
1 Parent(s): b049614

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -25
app.py CHANGED
@@ -281,18 +281,11 @@ def create_file(filename, prompt, response, should_save=True):
281
  if not should_save:
282
  return
283
  base_filename, ext = os.path.splitext(filename)
284
- #has_python_code = bool(re.search(r"```python([\s\S]*?)```", response))
285
- has_python_code = False
286
  if ext in ['.txt', '.htm', '.md']:
287
  with open(f"{base_filename}.md", 'w') as file:
288
- try:
289
- content = prompt.strip() + '\r\n' + response
290
- file.write(content)
291
- except:
292
- st.write('Skipping write of file. Prompt and Response below.')
293
- st.write(prompt)
294
- st.write(response)
295
-
296
  if has_python_code:
297
  python_code = re.findall(r"```python([\s\S]*?)```", response)[0].strip()
298
  with open(f"{base_filename}-Code.py", 'w') as file:
@@ -310,11 +303,8 @@ def divide_document(document, max_length):
310
  @st.cache_resource
311
  def get_table_download_link(file_path):
312
  with open(file_path, 'r') as file:
313
- try:
314
- data = file.read()
315
- except:
316
- st.write('')
317
- return file_path
318
  b64 = base64.b64encode(data.encode()).decode()
319
  file_name = os.path.basename(file_path)
320
  ext = os.path.splitext(file_name)[1] # get the file extension
@@ -436,16 +426,13 @@ def pdf2txt(docs):
436
  for file in docs:
437
  file_extension = extract_file_extension(file)
438
  st.write(f"File type extension: {file_extension}")
439
- try:
440
- if file_extension.lower() in ['py', 'txt', 'html', 'htm', 'xml', 'json']:
441
- text += file.getvalue().decode('utf-8')
442
- elif file_extension.lower() == 'pdf':
443
- from PyPDF2 import PdfReader
444
- pdf = PdfReader(BytesIO(file.getvalue()))
445
- for page in range(len(pdf.pages)):
446
- text += pdf.pages[page].extract_text() # new PyPDF2 syntax
447
- except Exception as e:
448
- st.write(f"Error processing file {file.name}: {e}")
449
  return text
450
 
451
  def txt2chunks(text):
 
281
  if not should_save:
282
  return
283
  base_filename, ext = os.path.splitext(filename)
284
+ has_python_code = bool(re.search(r"```python([\s\S]*?)```", response))
 
285
  if ext in ['.txt', '.htm', '.md']:
286
  with open(f"{base_filename}.md", 'w') as file:
287
+ content = prompt.strip() + '\r\n' + response
288
+ file.write(content)
 
 
 
 
 
 
289
  if has_python_code:
290
  python_code = re.findall(r"```python([\s\S]*?)```", response)[0].strip()
291
  with open(f"{base_filename}-Code.py", 'w') as file:
 
303
  @st.cache_resource
304
  def get_table_download_link(file_path):
305
  with open(file_path, 'r') as file:
306
+ data = file.read()
307
+
 
 
 
308
  b64 = base64.b64encode(data.encode()).decode()
309
  file_name = os.path.basename(file_path)
310
  ext = os.path.splitext(file_name)[1] # get the file extension
 
426
  for file in docs:
427
  file_extension = extract_file_extension(file)
428
  st.write(f"File type extension: {file_extension}")
429
+ if file_extension.lower() in ['py', 'txt', 'html', 'htm', 'xml', 'json']:
430
+ text += file.getvalue().decode('utf-8')
431
+ elif file_extension.lower() == 'pdf':
432
+ from PyPDF2 import PdfReader
433
+ pdf = PdfReader(BytesIO(file.getvalue()))
434
+ for page in range(len(pdf.pages)):
435
+ text += pdf.pages[page].extract_text() # new PyPDF2 syntax
 
 
 
436
  return text
437
 
438
  def txt2chunks(text):