Update app.py
Browse files
app.py
CHANGED
|
@@ -2,14 +2,50 @@ import os
|
|
| 2 |
from cerebras.cloud.sdk import Cerebras
|
| 3 |
from markitdown import MarkItDown
|
| 4 |
from weasyprint import HTML
|
|
|
|
|
|
|
|
|
|
| 5 |
import gradio as gr
|
|
|
|
| 6 |
|
| 7 |
-
# Ensure you get the API key from environment variables
|
| 8 |
api_key = os.environ.get("CEREBRAS_API_KEY")
|
| 9 |
-
|
| 10 |
-
# Initialize MarkItDown instance
|
| 11 |
md_converter = MarkItDown()
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
# Functions for resume optimization
|
| 14 |
def create_prompt(resume_string: str, jd_string: str) -> str:
|
| 15 |
"""
|
|
@@ -91,74 +127,84 @@ def get_resume_response(prompt: str, api_key: str, model: str = "llama-3.3-70b",
|
|
| 91 |
|
| 92 |
return response_string
|
| 93 |
|
| 94 |
-
def process_resume(
|
| 95 |
-
"""
|
| 96 |
-
Process a resume file against a job description to create an optimized version.
|
| 97 |
-
"""
|
| 98 |
try:
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
resume_string = result.text_content
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
# Generate the optimized resume using Cerebras' Llama 3.3 70B model
|
| 107 |
-
response_string = get_resume_response(prompt, api_key)
|
| 108 |
-
response_list = response_string.split("## Additional Suggestions")
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
suggestions = "## Additional Suggestions \n\n" + response_list[1]
|
| 113 |
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
|
|
|
| 116 |
except Exception as e:
|
| 117 |
-
return f"Error processing file: {str(e)}", "", ""
|
| 118 |
|
| 119 |
-
def
|
| 120 |
-
"""
|
| 121 |
-
Convert a markdown resume to PDF format and save it.
|
| 122 |
-
"""
|
| 123 |
try:
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
# Convert HTML to PDF and save
|
| 131 |
-
HTML(string=html_content).write_pdf(output_pdf_file, stylesheets=['resumes/style.css'])
|
| 132 |
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
except Exception as e:
|
| 135 |
-
return f"Failed to export
|
| 136 |
|
| 137 |
-
#
|
| 138 |
with gr.Blocks() as app:
|
| 139 |
-
# Create header and app description
|
| 140 |
gr.Markdown("# Resume Optimizer π")
|
| 141 |
gr.Markdown("Upload your resume, paste the job description, and get actionable insights!")
|
| 142 |
|
| 143 |
-
# Gather inputs
|
| 144 |
with gr.Row():
|
| 145 |
-
resume_input = gr.File(label="Upload Your Resume
|
| 146 |
-
jd_input = gr.Textbox(label="Paste
|
| 147 |
|
| 148 |
-
run_button = gr.Button("Optimize Resume
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
# Editing results
|
| 155 |
-
output_resume = gr.Textbox(label="Edit resume and export!", interactive=True)
|
| 156 |
-
export_button = gr.Button("Export Resume as PDF π")
|
| 157 |
-
export_result = gr.Markdown(label="Export Result")
|
| 158 |
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
-
|
| 164 |
-
app.launch()
|
|
|
|
| 2 |
from cerebras.cloud.sdk import Cerebras
|
| 3 |
from markitdown import MarkItDown
|
| 4 |
from weasyprint import HTML
|
| 5 |
+
from docx import Document
|
| 6 |
+
from pptx import Presentation
|
| 7 |
+
from PyPDF2 import PdfReader
|
| 8 |
import gradio as gr
|
| 9 |
+
from PIL import Image
|
| 10 |
|
|
|
|
| 11 |
api_key = os.environ.get("CEREBRAS_API_KEY")
|
|
|
|
|
|
|
| 12 |
md_converter = MarkItDown()
|
| 13 |
|
| 14 |
+
def extract_file_preview(file_path):
|
| 15 |
+
"""
|
| 16 |
+
Extracts a preview of the file based on its format.
|
| 17 |
+
"""
|
| 18 |
+
try:
|
| 19 |
+
file_ext = os.path.splitext(file_path)[-1].lower()
|
| 20 |
+
|
| 21 |
+
if file_ext in [".jpg", ".jpeg", ".png"]:
|
| 22 |
+
return Image.open(file_path)
|
| 23 |
+
|
| 24 |
+
elif file_ext == ".pdf":
|
| 25 |
+
reader = PdfReader(file_path)
|
| 26 |
+
return "\n".join([page.extract_text() for page in reader.pages[:2]])
|
| 27 |
+
|
| 28 |
+
elif file_ext in [".docx"]:
|
| 29 |
+
doc = Document(file_path)
|
| 30 |
+
return "\n".join([para.text for para in doc.paragraphs[:20]])
|
| 31 |
+
|
| 32 |
+
elif file_ext in [".pptx"]:
|
| 33 |
+
ppt = Presentation(file_path)
|
| 34 |
+
slides_text = []
|
| 35 |
+
for slide in ppt.slides[:5]:
|
| 36 |
+
slide_text = []
|
| 37 |
+
for shape in slide.shapes:
|
| 38 |
+
if hasattr(shape, "text"):
|
| 39 |
+
slide_text.append(shape.text)
|
| 40 |
+
slides_text.append("\n".join(slide_text))
|
| 41 |
+
return "\n---\n".join(slides_text)
|
| 42 |
+
|
| 43 |
+
else:
|
| 44 |
+
return "File preview not supported for this format."
|
| 45 |
+
|
| 46 |
+
except Exception as e:
|
| 47 |
+
return f"Error extracting file preview: {str(e)}"
|
| 48 |
+
|
| 49 |
# Functions for resume optimization
|
| 50 |
def create_prompt(resume_string: str, jd_string: str) -> str:
|
| 51 |
"""
|
|
|
|
| 127 |
|
| 128 |
return response_string
|
| 129 |
|
| 130 |
+
def process_resume(file, jd_string):
|
|
|
|
|
|
|
|
|
|
| 131 |
try:
|
| 132 |
+
file_path = file.name
|
| 133 |
+
original_preview = extract_file_preview(file_path)
|
|
|
|
| 134 |
|
| 135 |
+
result = md_converter.convert(file_path)
|
| 136 |
+
resume_string = result.text_content
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
+
prompt = f"Optimize resume based on job description: {jd_string}"
|
| 139 |
+
optimized_resume = "Optimized resume placeholder" # Simulate response for now.
|
|
|
|
| 140 |
|
| 141 |
+
# Save the files for download
|
| 142 |
+
original_file_path = file_path
|
| 143 |
+
optimized_file_path = "resumes/optimized_resume.md"
|
| 144 |
+
with open(optimized_file_path, "w", encoding="utf-8") as f:
|
| 145 |
+
f.write(optimized_resume)
|
| 146 |
|
| 147 |
+
return original_preview, resume_string, optimized_resume, original_file_path, optimized_file_path
|
| 148 |
except Exception as e:
|
| 149 |
+
return f"Error processing file: {str(e)}", "", "", "", ""
|
| 150 |
|
| 151 |
+
def export_as_pdf(resume_md):
|
|
|
|
|
|
|
|
|
|
| 152 |
try:
|
| 153 |
+
pdf_path = "resumes/optimized_resume.pdf"
|
| 154 |
+
HTML(string=resume_md).write_pdf(pdf_path)
|
| 155 |
+
return pdf_path
|
| 156 |
+
except Exception as e:
|
| 157 |
+
return f"Failed to export PDF: {str(e)}"
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
+
def export_as_word(resume_md):
|
| 160 |
+
try:
|
| 161 |
+
doc_path = "resumes/optimized_resume.docx"
|
| 162 |
+
doc = Document()
|
| 163 |
+
for line in resume_md.split("\n"):
|
| 164 |
+
doc.add_paragraph(line)
|
| 165 |
+
doc.save(doc_path)
|
| 166 |
+
return doc_path
|
| 167 |
except Exception as e:
|
| 168 |
+
return f"Failed to export Word: {str(e)}"
|
| 169 |
|
| 170 |
+
# Gradio UI
|
| 171 |
with gr.Blocks() as app:
|
|
|
|
| 172 |
gr.Markdown("# Resume Optimizer π")
|
| 173 |
gr.Markdown("Upload your resume, paste the job description, and get actionable insights!")
|
| 174 |
|
|
|
|
| 175 |
with gr.Row():
|
| 176 |
+
resume_input = gr.File(label="Upload Your Resume")
|
| 177 |
+
jd_input = gr.Textbox(label="Paste Job Description", lines=5)
|
| 178 |
|
| 179 |
+
run_button = gr.Button("Optimize Resume")
|
| 180 |
+
|
| 181 |
+
with gr.Row():
|
| 182 |
+
before_preview = gr.Markdown(label="Original File Preview")
|
| 183 |
+
before_md = gr.Markdown(label="Original (Markdown)")
|
| 184 |
+
after_md = gr.Markdown(label="Optimized (Markdown)")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
|
| 186 |
+
with gr.Row():
|
| 187 |
+
download_before = gr.File(label="Download Original")
|
| 188 |
+
#download_after_md = gr.File(label="Download Optimized (Markdown)")
|
| 189 |
+
download_after_pdf = gr.File(label="Download Optimized (PDF)")
|
| 190 |
+
download_after_word = gr.File(label="Download Optimized (Word)")
|
| 191 |
+
|
| 192 |
+
run_button.click(
|
| 193 |
+
process_resume,
|
| 194 |
+
inputs=[resume_input, jd_input],
|
| 195 |
+
outputs=[before_preview, before_md, after_md, download_before, download_after_md]
|
| 196 |
+
)
|
| 197 |
+
|
| 198 |
+
gr.Button("Export as PDF").click(
|
| 199 |
+
export_as_pdf,
|
| 200 |
+
inputs=[after_md],
|
| 201 |
+
outputs=[download_after_pdf]
|
| 202 |
+
)
|
| 203 |
+
|
| 204 |
+
gr.Button("Export as Word").click(
|
| 205 |
+
export_as_word,
|
| 206 |
+
inputs=[after_md],
|
| 207 |
+
outputs=[download_after_word]
|
| 208 |
+
)
|
| 209 |
|
| 210 |
+
app.launch()
|
|
|