muhammadsalmanalfaridzi commited on
Commit
c9135a2
Β·
verified Β·
1 Parent(s): 95242f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -55
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(resume_file, jd_string):
95
- """
96
- Process a resume file against a job description to create an optimized version.
97
- """
98
  try:
99
- # Convert the uploaded file to Markdown
100
- result = md_converter.convert(resume_file.name)
101
- resume_string = result.text_content
102
 
103
- # Create prompt for optimization
104
- prompt = create_prompt(resume_string, jd_string)
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
- # Extract new resume and suggestions for improvement
111
- new_resume = response_list[0]
112
- suggestions = "## Additional Suggestions \n\n" + response_list[1]
113
 
114
- return new_resume, new_resume, suggestions
 
 
 
 
115
 
 
116
  except Exception as e:
117
- return f"Error processing file: {str(e)}", "", ""
118
 
119
- def export_resume(new_resume):
120
- """
121
- Convert a markdown resume to PDF format and save it.
122
- """
123
  try:
124
- # Save as PDF
125
- output_pdf_file = "resumes/resume_new.pdf"
126
-
127
- # Convert Markdown to HTML
128
- html_content = new_resume
129
-
130
- # Convert HTML to PDF and save
131
- HTML(string=html_content).write_pdf(output_pdf_file, stylesheets=['resumes/style.css'])
132
 
133
- return f"Successfully exported resume to {output_pdf_file} πŸŽ‰"
 
 
 
 
 
 
 
134
  except Exception as e:
135
- return f"Failed to export resume: {str(e)} πŸ’”"
136
 
137
- # Hugging Face Space UI
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 (Supported Files)")
146
- jd_input = gr.Textbox(label="Paste the Job Description Here", lines=9, interactive=True, placeholder="Paste job description...")
147
 
148
- run_button = gr.Button("Optimize Resume πŸ€–")
149
-
150
- # Display outputs
151
- output_resume_md = gr.Markdown(label="New Resume")
152
- output_suggestions = gr.Markdown(label="Suggestions")
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
- # Event binding
160
- run_button.click(process_resume, inputs=[resume_input, jd_input], outputs=[output_resume_md, output_resume, output_suggestions])
161
- export_button.click(export_resume, inputs=[output_resume], outputs=[export_result])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
162
 
163
- # Launch the app
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()