Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -10,11 +10,31 @@ from docx import Document
|
|
10 |
import subprocess
|
11 |
import shutil
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
# Checking poppler installation
|
14 |
st.write("PDFInfo Path:", os.popen("which pdfinfo").read().strip())
|
15 |
st.write("LibreOffice Path:", os.popen("which libreoffice").read().strip())
|
16 |
st.write("System PATH:", os.environ['PATH'])
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
# Helper function to save uploaded files temporarily and return their paths
|
20 |
def save_uploaded_file(content):
|
@@ -177,8 +197,16 @@ def save_file(file_name):
|
|
177 |
f.write(uploaded_file)
|
178 |
return file_path
|
179 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
180 |
|
181 |
-
def
|
182 |
# Save document content as a .docx file
|
183 |
temp_doc_path = 'temp.docx'
|
184 |
doc = Document()
|
@@ -192,7 +220,8 @@ def save_docx_as_pdf(doc_content, output_path='output.pdf'):
|
|
192 |
|
193 |
# subprocess.run(['libreoffice', '--headless', '--convert-to', 'pdf', temp_doc_path, '--outdir', os.path.dirname(output_path)])
|
194 |
|
195 |
-
|
|
|
196 |
if input_path.lower().endswith('.docx'):
|
197 |
from docx2pdf import convert
|
198 |
convert(input_path, output_path)
|
@@ -205,25 +234,40 @@ def save_docx_as_pdf(input_path, output_path='output.pdf'):
|
|
205 |
# save_docx_as_pdf(resume_path, 'uploaded_resume.pdf')
|
206 |
# display_doc_as_image('uploaded_resume.pdf')
|
207 |
|
208 |
-
def
|
209 |
try:
|
210 |
images = convert_from_path(pdf_path, size=800)
|
211 |
display(Image(filename=images[0].filename))
|
212 |
except Exception as e:
|
213 |
st.error(f"Failed to display image: {str(e)}")
|
214 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
215 |
def display_doc_as_image1(pdf_path):
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
|
223 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
224 |
|
225 |
-
|
226 |
-
|
|
|
|
|
227 |
|
228 |
# Process if files are uploaded
|
229 |
if uploaded_resume and uploaded_job_description:
|
@@ -286,34 +330,70 @@ if uploaded_resume and uploaded_job_description:
|
|
286 |
if generated_resume is not None:
|
287 |
|
288 |
from io import BytesIO
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
resume_bytes = BytesIO()
|
295 |
-
doc.save(resume_bytes)
|
296 |
-
resume_bytes.seek(0)
|
297 |
-
|
298 |
-
gen_resume_path = save_uploaded_file(resume_bytes)
|
299 |
-
# uploaded_resume_path = save_uploaded_file(resume)
|
300 |
-
|
301 |
-
col1, col2 = st.columns(2)
|
302 |
-
with col1:
|
303 |
-
save_docx_as_pdf(resume_path, 'uploaded_resume.pdf')
|
304 |
-
display_doc_as_image('uploaded_resume.pdf')
|
305 |
-
with col2:
|
306 |
-
st.markdown("### Tailored Resume:")
|
307 |
-
save_docx_as_pdf(gen_resume_path, 'tailored_resume.pdf')
|
308 |
-
display_doc_as_image('tailored_resume.pdf')
|
309 |
-
|
310 |
|
311 |
-
|
312 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
313 |
data=resume_bytes,
|
314 |
file_name="tailored_resume.docx",
|
315 |
mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
316 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
317 |
|
318 |
else:
|
319 |
st.warning("Please upload both the resume and job description files.")
|
|
|
10 |
import subprocess
|
11 |
import shutil
|
12 |
|
13 |
+
import tempfile
|
14 |
+
from PIL import Image
|
15 |
+
import io
|
16 |
+
from docx2pdf import convert
|
17 |
+
import docx
|
18 |
+
import numpy as np
|
19 |
+
|
20 |
+
# Create temporary directories
|
21 |
+
temp_dir = tempfile.mkdtemp()
|
22 |
+
|
23 |
+
|
24 |
# Checking poppler installation
|
25 |
st.write("PDFInfo Path:", os.popen("which pdfinfo").read().strip())
|
26 |
st.write("LibreOffice Path:", os.popen("which libreoffice").read().strip())
|
27 |
st.write("System PATH:", os.environ['PATH'])
|
28 |
|
29 |
+
import subprocess
|
30 |
+
|
31 |
+
def check_poppler():
|
32 |
+
try:
|
33 |
+
subprocess.check_call(["poppler-utils", "--version"])
|
34 |
+
print("Poppler is installed and ready to use.")
|
35 |
+
except subprocess.CalledProcessError:
|
36 |
+
print("Poppler is not installed. Please install it using 'apt-get install poppler-utils'.")
|
37 |
+
|
38 |
|
39 |
# Helper function to save uploaded files temporarily and return their paths
|
40 |
def save_uploaded_file(content):
|
|
|
197 |
f.write(uploaded_file)
|
198 |
return file_path
|
199 |
|
200 |
+
def save_docx_as_pdf(input_path, output_path='output.pdf'):
|
201 |
+
if input_path.lower().endswith('.docx'):
|
202 |
+
from docx2pdf import convert
|
203 |
+
convert(input_path, output_path)
|
204 |
+
elif input_path.lower().endswith('.pdf'):
|
205 |
+
shutil.copy(input_path, output_path)
|
206 |
+
else:
|
207 |
+
raise ValueError("Unsupported file format. Please upload a .docx or .pdf file.")
|
208 |
|
209 |
+
def save_docx_as_pdf2(doc_content, output_path='output.pdf'):
|
210 |
# Save document content as a .docx file
|
211 |
temp_doc_path = 'temp.docx'
|
212 |
doc = Document()
|
|
|
220 |
|
221 |
# subprocess.run(['libreoffice', '--headless', '--convert-to', 'pdf', temp_doc_path, '--outdir', os.path.dirname(output_path)])
|
222 |
|
223 |
+
|
224 |
+
def save_docx_as_pdf1(input_path, output_path='output.pdf'):
|
225 |
if input_path.lower().endswith('.docx'):
|
226 |
from docx2pdf import convert
|
227 |
convert(input_path, output_path)
|
|
|
234 |
# save_docx_as_pdf(resume_path, 'uploaded_resume.pdf')
|
235 |
# display_doc_as_image('uploaded_resume.pdf')
|
236 |
|
237 |
+
def display_doc_as_image2(pdf_path):
|
238 |
try:
|
239 |
images = convert_from_path(pdf_path, size=800)
|
240 |
display(Image(filename=images[0].filename))
|
241 |
except Exception as e:
|
242 |
st.error(f"Failed to display image: {str(e)}")
|
243 |
|
244 |
+
def display_doc_as_image(pdf_path):
|
245 |
+
try:
|
246 |
+
images = convert_from_path(pdf_path, size=800)
|
247 |
+
display(Image.fromarray(images[0]))
|
248 |
+
except Exception as e:
|
249 |
+
st.error(f"Failed to display image: {str(e)}")
|
250 |
+
|
251 |
def display_doc_as_image1(pdf_path):
|
252 |
+
try:
|
253 |
+
img = Image.open(pdf_path)
|
254 |
+
st.image(img)
|
255 |
+
except Exception as e:
|
256 |
+
st.error(f"Failed to display image: {str(e)}")
|
257 |
+
|
258 |
|
259 |
+
# def display_doc_as_image1(pdf_path):
|
260 |
+
# # poppler_path = 'usr/bin'
|
261 |
+
# # images = convert_from_path(pdf_path, poppler_path=poppler_path)
|
262 |
+
# # for img in images:
|
263 |
+
# # buf = BytesIO()
|
264 |
+
# # img.save(buf, format="PNG")
|
265 |
+
# # st.image(buf)
|
266 |
|
267 |
+
# from IPython.display import display, Image
|
268 |
+
|
269 |
+
# images = convert_from_bytes(open(pdf_path, 'rb').read(), size=800)
|
270 |
+
# display(images[0])
|
271 |
|
272 |
# Process if files are uploaded
|
273 |
if uploaded_resume and uploaded_job_description:
|
|
|
330 |
if generated_resume is not None:
|
331 |
|
332 |
from io import BytesIO
|
333 |
+
|
334 |
+
with tempfile.NamedTemporaryFile(suffix='.docx') as temp_doc:
|
335 |
+
doc = Document()
|
336 |
+
doc.add_paragraph(generated_resume)
|
337 |
+
doc.save(temp_doc.name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
338 |
|
339 |
+
# Convert DOCX to PDF
|
340 |
+
pdf_path = f"temp_{os.path.basename(temp_doc.name)}.pdf"
|
341 |
+
convert(temp_doc.name, pdf_path)
|
342 |
+
|
343 |
+
# Display resumes side by side
|
344 |
+
col1, col2 = st.columns(2)
|
345 |
+
with col1:
|
346 |
+
st.write("Uploaded Resume:")
|
347 |
+
display_doc_as_image(resume_path)
|
348 |
+
with col2:
|
349 |
+
st.write("Generated Resume:")
|
350 |
+
display_doc_as_image(pdf_path)
|
351 |
+
|
352 |
+
# Allow users to download both PDFs
|
353 |
+
st.download_button(
|
354 |
+
label="Download Uploaded Resume",
|
355 |
+
data=resume_bytes,
|
356 |
+
file_name="uploaded_resume.pdf",
|
357 |
+
mime="application/pdf"
|
358 |
+
)
|
359 |
+
st.download_button(
|
360 |
+
label="Download Generated Resume",
|
361 |
+
data=open(pdf_path, 'rb').read(),
|
362 |
+
file_name="generated_resume.pdf",
|
363 |
+
mime="application/pdf"
|
364 |
+
)
|
365 |
+
st.download_button(
|
366 |
+
label="Generated Resume (Word)",
|
367 |
data=resume_bytes,
|
368 |
file_name="tailored_resume.docx",
|
369 |
mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
370 |
+
)
|
371 |
+
# doc = Document()
|
372 |
+
# doc.add_paragraph(generated_resume)
|
373 |
+
|
374 |
+
# resume_bytes = BytesIO()
|
375 |
+
# doc.save(resume_bytes)
|
376 |
+
# resume_bytes.seek(0)
|
377 |
+
|
378 |
+
# gen_resume_path = save_uploaded_file(resume_bytes)
|
379 |
+
# # uploaded_resume_path = save_uploaded_file(resume)
|
380 |
+
|
381 |
+
# col1, col2 = st.columns(2)
|
382 |
+
# with col1:
|
383 |
+
# save_docx_as_pdf(resume_path, 'uploaded_resume.pdf')
|
384 |
+
# display_doc_as_image('uploaded_resume.pdf')
|
385 |
+
# with col2:
|
386 |
+
# st.markdown("### Tailored Resume:")
|
387 |
+
# save_docx_as_pdf(gen_resume_path, 'tailored_resume.pdf')
|
388 |
+
# display_doc_as_image('tailored_resume.pdf')
|
389 |
+
|
390 |
+
|
391 |
+
# st.download_button(
|
392 |
+
# label="Download Resume",
|
393 |
+
# data=resume_bytes,
|
394 |
+
# file_name="tailored_resume.docx",
|
395 |
+
# mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
396 |
+
# )
|
397 |
|
398 |
else:
|
399 |
st.warning("Please upload both the resume and job description files.")
|