pktpaulie commited on
Commit
fcf1770
·
verified ·
1 Parent(s): 0fba36c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -288
app.py CHANGED
@@ -19,7 +19,7 @@ import pdfplumber
19
  import docx
20
  import numpy as np
21
  import pypandoc
22
-
23
 
24
  # Create temporary directories
25
  temp_dir = tempfile.mkdtemp()
@@ -145,12 +145,14 @@ st.markdown("""
145
  .stButton>:active, focus {
146
  box-shadow: none;
147
  transform: translateY(0);
 
 
 
148
  }
149
  </style>
150
  """, unsafe_allow_html=True)
151
 
152
  # Add ResumeMagic Logo
153
- # st.image("logo.jpeg", use_container_width=True)
154
  st.markdown('<div class="logo-container"></div>', unsafe_allow_html=True)
155
  st.image("logo.jpeg", width=80)
156
  st.markdown('</div>', unsafe_allow_html=True)
@@ -249,7 +251,7 @@ def display_content_with_page_numbers(content, words_per_page=500):
249
  end_index = start_index + words_per_page
250
  page_content = ' '.join(words[start_index:end_index])
251
 
252
- st.markdown(f"### Page {i + 1}")
253
  st.write(page_content)
254
 
255
  def save_docx_as_pdf(input_path, output_path='output.pdf'):
@@ -265,44 +267,8 @@ def save_docx_as_pdf(input_path, output_path='output.pdf'):
265
  shutil.copy(input_path, output_path)
266
  else:
267
  raise ValueError("Unsupported file format. Please upload a .docx or .pdf file.")
268
-
269
- # def save_docx_as_pdf(input_path, output_path='output.pdf'):
270
- # if input_path.lower().endswith('.docx'):
271
- # try:
272
- # # Attempt to use unoconv
273
- # subprocess.run(['unoconv', '-o', output_path, input_path])
274
- # if not os.path.exists(output_path):
275
- # raise FileNotFoundError("unoconv failed to convert DOCX to PDF")
276
- # except FileNotFoundError:
277
- # # Fallback to text-based display
278
- # with open(output_path, 'w') as f:
279
- # f.write(input_path.read())
280
- # elif input_path.lower().endswith('.pdf'):
281
- # shutil.copy(input_path, output_path)
282
- # else:
283
- # raise ValueError("Unsupported file format. Please upload a .docx or .pdf file.")
284
-
285
 
286
 
287
- def save_docx_as_pdf6(input_path, output_path='output.pdf'):
288
- if input_path.lower().endswith('.docx'):
289
- try:
290
- # Convert .docx to .pdf using docx2pdf
291
- convert(input_path, output_path)
292
- if not os.path.exists(output_path):
293
- raise FileNotFoundError("Conversion failed; output PDF not found.")
294
- except Exception as e:
295
- st.error(f"Failed to convert DOCX to PDF: {str(e)}")
296
- elif input_path.lower().endswith('.pdf'):
297
- shutil.copy(input_path, output_path)
298
- else:
299
- raise ValueError("Unsupported file format. Please upload a .docx or .pdf file.")
300
-
301
- # Install docx2pdf if not already installed
302
- # try:
303
- # import docx2pdf
304
- # except ImportError:
305
- # !pip install docx2pdf
306
 
307
  # def save_docx_as_pdf(input_path, output_path='output.pdf'):
308
  # if input_path.lower().endswith('.docx'):
@@ -313,21 +279,7 @@ def save_docx_as_pdf6(input_path, output_path='output.pdf'):
313
  # else:
314
  # raise ValueError("Unsupported file format. Please upload a .docx or .pdf file.")
315
 
316
- def save_docx_as_pdf2(doc_content, output_path='output.pdf'):
317
- # Save document content as a .docx file
318
- temp_doc_path = 'temp.docx'
319
- doc = Document()
320
- doc.add_paragraph(doc_content)
321
- doc.save(temp_doc_path)
322
-
323
- # Convert .docx to PDF
324
- from docx2pdf import convert
325
- convert(temp_doc_path, output_path)
326
- os.remove(temp_doc_path)
327
-
328
- # subprocess.run(['libreoffice', '--headless', '--convert-to', 'pdf', temp_doc_path, '--outdir', os.path.dirname(output_path)])
329
 
330
-
331
  def save_docx_as_pdf1(input_path, output_path='output.pdf'):
332
  if input_path.lower().endswith('.docx'):
333
  from docx2pdf import convert
@@ -337,9 +289,6 @@ def save_docx_as_pdf1(input_path, output_path='output.pdf'):
337
  else:
338
  raise ValueError("Unsupported file format. Please upload a .docx or .pdf file.")
339
 
340
- # if uploaded_resume.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
341
- # save_docx_as_pdf(resume_path, 'uploaded_resume.pdf')
342
- # display_doc_as_image('uploaded_resume.pdf')
343
 
344
  def display_pdf_page1(pdf_path):
345
  try:
@@ -368,7 +317,7 @@ def display_pdf_pages_as_images(pdf_path):
368
  try:
369
  with pdfplumber.open(pdf_path) as pdf:
370
  for i, page in enumerate(pdf.pages):
371
- st.markdown(f"### Page {i + 1}")
372
  # Convert the page to an image
373
  image = page.to_image()
374
  # Render the image using Streamlit
@@ -394,146 +343,14 @@ def display_pdf_page(pdf_path, page_number=1):
394
  # Display the image
395
  display(img)
396
 
397
- def display_doc_as_image2(pdf_path):
398
- # try:
399
- # images = convert_from_path(pdf_path, size=800)
400
- # display(Image(filename=images[0].filename))
401
- # except Exception as e:
402
- # st.error(f"Failed to display image: {str(e)}")
403
-
404
-
405
  iframe_code = f"""
406
  <iframe src="{pdf_path}" width="100%" height="600px"></iframe>
407
  """
408
  st.markdown(iframe_code, unsafe_allow_html=True)
409
 
410
- # try:
411
- # # Convert PDF to images
412
- # images = []
413
- # for i in range(1): # Assuming only one page
414
- # command = [
415
- # "pdf2image", "-j", "--png",
416
- # "-f", str(i+1),
417
- # "-l", str(i+1),
418
- # pdf_path
419
- # ]
420
- # output = subprocess.check_output(command)
421
-
422
- # # Decode the base64 image data
423
- # img_data = base64.b64decode(output.split()[0])
424
- # img = Image.open(BytesIO(img_data))
425
- # images.append(img)
426
-
427
- # # Display the first page
428
- # display(images[0])
429
- # except Exception as e:
430
- # st.error(f"Failed to display image: {str(e)}")
431
-
432
- def display_doc_as_image3(pdf_path):
433
- try:
434
- # Convert PDF to images
435
- images = []
436
- for i in range(1): # Assuming only one page
437
- img = Image.open(BytesIO(open(pdf_path, 'rb').read()))
438
- images.append(img)
439
-
440
- # Display the first page
441
- display(images[0])
442
- except Exception as e:
443
- st.error(f"Failed to display image: {str(e)}")
444
- # try:
445
- # images = convert_from_path(pdf_path, size=800)
446
- # display(Image.fromarray(images[0]))
447
- # except Exception as e:
448
- # st.error(f"Failed to display image: {str(e)}")
449
-
450
- def display_doc_as_image1(pdf_url):
451
- st.components.v1.iframe(pdf_url, width=800, height=600)
452
- # try:
453
- # img = Image.open(pdf_path)
454
- # st.image(img)
455
- # except Exception as e:
456
- # st.error(f"Failed to display image: {str(e)}")
457
-
458
-
459
- def display_doc_as_image(file_path):
460
- """Display document with multiple fallback options"""
461
- try:
462
- # First attempt: Use pdf2image with explicit poppler path
463
- try:
464
- from pdf2image import convert_from_path
465
- # Try common poppler paths on Linux
466
- poppler_paths = [
467
- '/usr/bin/pdftoppm', # Common Linux path
468
- '/opt/homebrew/bin/pdftoppm', # Mac Homebrew path
469
- 'poppler-utils' # Package name
470
- ]
471
-
472
- for poppler_path in poppler_paths:
473
- try:
474
- images = convert_from_path(
475
- file_path,
476
- size=800,
477
- poppler_path=os.path.dirname(poppler_path) if os.path.isfile(poppler_path) else None
478
- )
479
- st.image(images[0])
480
- return
481
- except Exception:
482
- continue
483
-
484
- # If no poppler path worked, try without specifying path
485
- images = convert_from_path(file_path, size=800)
486
- st.image(images[0])
487
-
488
- except Exception as e:
489
- # Second attempt: Use Streamlit's native PDF display
490
- try:
491
- with open(file_path, "rb") as pdf_file:
492
- pdf_bytes = pdf_file.read()
493
- st.pdf(pdf_bytes)
494
- return
495
- except Exception as pdf_error:
496
- st.error(f"Error displaying PDF: {str(pdf_error)}")
497
-
498
- # Third attempt: If it's a DOCX file, try to extract text
499
- if file_path.lower().endswith('.docx'):
500
- try:
501
- doc = Document(file_path)
502
- text = '\n'.join([paragraph.text for paragraph in doc.paragraphs])
503
- st.text(text)
504
- return
505
- except Exception as docx_error:
506
- st.error(f"Error displaying DOCX: {str(docx_error)}")
507
-
508
- except Exception as main_error:
509
- st.error(f"Error in display function: {str(main_error)}")
510
- st.info("Displaying file path for debugging: " + file_path)
511
-
512
- # Last resort: Try to read and display raw bytes
513
- try:
514
- with open(file_path, 'rb') as f:
515
- bytes_data = f.read()
516
- if file_path.lower().endswith('.pdf'):
517
- st.pdf(bytes_data)
518
- else:
519
- st.text("File content preview not available")
520
- except Exception as final_error:
521
- st.error(f"Final fallback failed: {str(final_error)}")
522
-
523
-
524
- def display_doc_as_image5(pdf_path):
525
- poppler_path = 'usr/bin'
526
- images = convert_from_path(pdf_path, poppler_path=poppler_path)
527
- for img in images:
528
- buf = BytesIO()
529
- img.save(buf, format="PNG")
530
- st.image(buf)
531
-
532
- # from IPython.display import display, Image
533
-
534
- # images = convert_from_bytes(open(pdf_path, 'rb').read(), size=800)
535
- # display(images[0])
536
-
537
  # Process if files are uploaded
538
  if uploaded_resume and uploaded_job_description:
539
  # Save files
@@ -578,11 +395,13 @@ if uploaded_resume and uploaded_job_description:
578
  # st.write(generated_resume)
579
 
580
  #Autoscroll
581
- st.markdown("""
582
  <script>
583
- window.scrollTo(0, document.body.scrollHeight);
 
 
584
  </script>
585
- """, unsafe_allow_html=True)
586
 
587
 
588
  # with st.spinner("Computing Match"):
@@ -617,19 +436,17 @@ if uploaded_resume and uploaded_job_description:
617
  with col1:
618
  st.markdown("### Uploaded Resume:")
619
  save_docx_as_pdf(resume_path, '/tmp/uploaded_resume.pdf')
620
- # display_doc_as_image1('/tmp/uploaded_resume.pdf')
621
  display_pdf_pages_as_images(resume_path)
622
  with col2:
623
  st.markdown("### Tailored Resume:")
624
- # display_doc_as_image2(gen_pdf_path)
625
  # display_pdf_pages_as_images(gen_pdf_path)
626
- # st.write(generated_resume)
627
- display_content_with_page_numbers(generated_resume, 290)
628
  st.success(f"Download tailored resume")
629
  st.download_button(
630
  label="Generated Resume (PDF)",
631
  data=open(gen_pdf_path, 'rb').read(),
632
- file_name="generated_resume.pdf",
633
  mime="application/pdf"
634
  )
635
  st.download_button(
@@ -653,94 +470,7 @@ if uploaded_resume and uploaded_job_description:
653
  # mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document"
654
  # )
655
 
656
- # from io import BytesIO
657
- # doc = Document()
658
-
659
- # with tempfile.NamedTemporaryFile(delete=False, suffix=".docx") as temp_doc:
660
- # temp_doc_path = temp_doc.name
661
- # doc.save(temp_doc_path)
662
-
663
-
664
- # Convert the DOCX to PDF and display
665
- # pdf_path = '/tmp/generated_resume.pdf'
666
- # save_docx_as_pdf(temp_doc_path, pdf_path)
667
-
668
- # # Now pass the path to any function that expects a file path
669
- # pdf_path = '/tmp/generated_resume.pdf'
670
- # save_bytes_as_pdf(temp_doc_path, pdf_path)
671
-
672
- # # Display the generated PDF or handle further processing
673
- # display_doc_as_image(pdf_path)
674
-
675
- # with tempfile.NamedTemporaryFile(suffix='.docx') as temp_doc:
676
- # doc = Document()
677
- # doc.add_paragraph(generated_resume)
678
- # # doc.save(temp_doc.name)
679
-
680
- # # resume_bytes = BytesIO()
681
- # # doc.save(resume_bytes)
682
- # # resume_bytes.seek(0)
683
-
684
- # # Convert DOCX to PDF
685
- # # pdf_path = f"temp_{os.path.basename(temp_doc.name)}.pdf"
686
- # # convert(temp_doc.name, pdf_path)
687
- # # save_docx_as_pdf(temp_doc.name, pdf_path)
688
- # pdf_path = '/tmp/generated_resume.docx'
689
- # doc.save(pdf_path)
690
-
691
- # # pdf_path = save_uploaded_file(resume_bytes)
692
- # save_docx_as_pdf(generated_resume, pdf_path)
693
-
694
- # # Display resumes side by side
695
- # col1, col2 = st.columns(2)
696
- # with col1:
697
- # st.write("Uploaded Resume:")
698
- # if resume_path:
699
- # display_doc_as_image(resume_path)
700
- # else:
701
- # st.warning("No resume file found")
702
-
703
- # with col2:
704
- # st.write("Generated Resume:")
705
- # if pdf_path:
706
- # display_doc_as_image(pdf_path)
707
- # else:
708
- # st.warning("No generated resume file found")
709
-
710
- # Allow users to download both PDFs
711
- # st.download_button(
712
- # label="Download Uploaded Resume",
713
- # data=resume_bytes,
714
- # file_name="uploaded_resume.pdf",
715
- # mime="application/pdf"
716
- # )
717
-
718
- # doc = Document()
719
- # doc.add_paragraph(generated_resume)
720
-
721
- # resume_bytes = BytesIO()
722
- # doc.save(resume_bytes)
723
- # resume_bytes.seek(0)
724
-
725
- # gen_resume_path = save_uploaded_file(resume_bytes)
726
- # # uploaded_resume_path = save_uploaded_file(resume)
727
-
728
- # col1, col2 = st.columns(2)
729
- # with col1:
730
- # save_docx_as_pdf(resume_path, 'uploaded_resume.pdf')
731
- # display_doc_as_image('uploaded_resume.pdf')
732
- # with col2:
733
- # st.markdown("### Tailored Resume:")
734
- # save_docx_as_pdf(gen_resume_path, 'tailored_resume.pdf')
735
- # display_doc_as_image('tailored_resume.pdf')
736
-
737
-
738
- # st.download_button(
739
- # label="Download Resume",
740
- # data=resume_bytes,
741
- # file_name="tailored_resume.docx",
742
- # mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document"
743
- # )
744
 
745
  else:
746
  st.warning("Please upload both the resume and job description files.")
 
19
  import docx
20
  import numpy as np
21
  import pypandoc
22
+ import streamlit.components.v1 as components
23
 
24
  # Create temporary directories
25
  temp_dir = tempfile.mkdtemp()
 
145
  .stButton>:active, focus {
146
  box-shadow: none;
147
  transform: translateY(0);
148
+ color: #fff;
149
+ border: none !important;
150
+ outline: none;
151
  }
152
  </style>
153
  """, unsafe_allow_html=True)
154
 
155
  # Add ResumeMagic Logo
 
156
  st.markdown('<div class="logo-container"></div>', unsafe_allow_html=True)
157
  st.image("logo.jpeg", width=80)
158
  st.markdown('</div>', unsafe_allow_html=True)
 
251
  end_index = start_index + words_per_page
252
  page_content = ' '.join(words[start_index:end_index])
253
 
254
+ st.markdown(f"#### Page {i + 1}")
255
  st.write(page_content)
256
 
257
  def save_docx_as_pdf(input_path, output_path='output.pdf'):
 
267
  shutil.copy(input_path, output_path)
268
  else:
269
  raise ValueError("Unsupported file format. Please upload a .docx or .pdf file.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
270
 
271
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
272
 
273
  # def save_docx_as_pdf(input_path, output_path='output.pdf'):
274
  # if input_path.lower().endswith('.docx'):
 
279
  # else:
280
  # raise ValueError("Unsupported file format. Please upload a .docx or .pdf file.")
281
 
 
 
 
 
 
 
 
 
 
 
 
 
 
282
 
 
283
  def save_docx_as_pdf1(input_path, output_path='output.pdf'):
284
  if input_path.lower().endswith('.docx'):
285
  from docx2pdf import convert
 
289
  else:
290
  raise ValueError("Unsupported file format. Please upload a .docx or .pdf file.")
291
 
 
 
 
292
 
293
  def display_pdf_page1(pdf_path):
294
  try:
 
317
  try:
318
  with pdfplumber.open(pdf_path) as pdf:
319
  for i, page in enumerate(pdf.pages):
320
+ st.markdown(f"#### Page {i + 1}")
321
  # Convert the page to an image
322
  image = page.to_image()
323
  # Render the image using Streamlit
 
343
  # Display the image
344
  display(img)
345
 
346
+ def display_doc_as_image2(pdf_path):
 
 
 
 
 
 
 
347
  iframe_code = f"""
348
  <iframe src="{pdf_path}" width="100%" height="600px"></iframe>
349
  """
350
  st.markdown(iframe_code, unsafe_allow_html=True)
351
 
352
+
353
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
354
  # Process if files are uploaded
355
  if uploaded_resume and uploaded_job_description:
356
  # Save files
 
395
  # st.write(generated_resume)
396
 
397
  #Autoscroll
398
+ components.html("""
399
  <script>
400
+ window.onload = function() {
401
+ window.scrollTo(0, document.body.scrollHeight);
402
+ };
403
  </script>
404
+ """)
405
 
406
 
407
  # with st.spinner("Computing Match"):
 
436
  with col1:
437
  st.markdown("### Uploaded Resume:")
438
  save_docx_as_pdf(resume_path, '/tmp/uploaded_resume.pdf')
 
439
  display_pdf_pages_as_images(resume_path)
440
  with col2:
441
  st.markdown("### Tailored Resume:")
 
442
  # display_pdf_pages_as_images(gen_pdf_path)
443
+ st.write(generated_resume)
444
+ # display_content_with_page_numbers(generated_resume, 290)
445
  st.success(f"Download tailored resume")
446
  st.download_button(
447
  label="Generated Resume (PDF)",
448
  data=open(gen_pdf_path, 'rb').read(),
449
+ file_name="tailored_resume.pdf",
450
  mime="application/pdf"
451
  )
452
  st.download_button(
 
470
  # mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document"
471
  # )
472
 
473
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
474
 
475
  else:
476
  st.warning("Please upload both the resume and job description files.")