Spaces:
Sleeping
Sleeping
Download app.py from tchans123/resume_optimizer: direct link, hf CLI and curl.
- Browser
- Download file 3.08 kB
-
https://huggingface.co/spaces/tchans123/resume_optimizer/resolve/main/app.py
- Command line
-
hf download hf://spaces/tchans123/resume_optimizer/app.py
-
curl -L -o app.py https://huggingface.co/spaces/tchans123/resume_optimizer/resolve/main/app.py
3.08 kB
| import gradio as gr | |
| from huggingface_hub import InferenceClient | |
| import os | |
| import fitz # PyMuPDF for PDF handling | |
| from docx import Document # For handling MS Word documents | |
| # Initialize Hugging Face Inference Client | |
| try: | |
| client = InferenceClient( | |
| model="meta-llama/Meta-Llama-3.1-8B-Instruct", | |
| token=os.getenv("HUGGINGFACEHUB_API_TOKEN") | |
| ) | |
| except Exception as e: | |
| print(f"Error initializing InferenceClient: {e}") | |
| # Function to optimize resume based on job title | |
| def optimize_resume(resume_text, job_title): | |
| prompt = f"Optimize the following resume for the job title '{job_title}':\n\n{resume_text}" | |
| responses = [] | |
| try: | |
| for message in client.chat_completion( | |
| messages=[{"role": "user", "content": prompt}], | |
| max_tokens=1000, | |
| stream=True, | |
| ): | |
| responses.append(message.choices[0].delta.content) | |
| except Exception as e: | |
| return f"Error during model inference: {e}" | |
| return ''.join(responses) | |
| # Function to extract text from a PDF file | |
| def extract_text_from_pdf(pdf_file_path): | |
| text = "" | |
| try: | |
| pdf_document = fitz.open(pdf_file_path) | |
| for page_num in range(len(pdf_document)): | |
| page = pdf_document.load_page(page_num) | |
| text += page.get_text() | |
| except Exception as e: | |
| return f"Error extracting text from PDF: {e}" | |
| return text | |
| # Function to extract text from a Word document | |
| def extract_text_from_word(docx_file_path): | |
| text = "" | |
| try: | |
| doc = Document(docx_file_path) | |
| for paragraph in doc.paragraphs: | |
| text += paragraph.text + "\n" | |
| except Exception as e: | |
| return f"Error extracting text from Word document: {e}" | |
| return text | |
| # Function to process the resume and job title inputs | |
| def process_resume(file, job_title): | |
| try: | |
| file_name = file.name | |
| if file_name.endswith(".pdf"): | |
| # Extract text if the file is a PDF | |
| resume_text = extract_text_from_pdf(file.name) | |
| elif file_name.endswith(".docx"): | |
| # Extract text if the file is a Word document | |
| resume_text = extract_text_from_word(file.name) | |
| else: | |
| # Assume the file is a text file and read it directly | |
| resume_text = file.decode("utf-8") | |
| # Optimize the resume | |
| optimized_resume = optimize_resume(resume_text, job_title) | |
| return optimized_resume | |
| except Exception as e: | |
| return f"Error processing resume: {e}" | |
| # Gradio Interface | |
| interface = gr.Interface( | |
| fn=process_resume, | |
| inputs=[ | |
| gr.File(label="Upload your resume (PDF or Word)"), | |
| gr.Textbox(lines=1, placeholder="Enter the job title...", label="Job Title"), | |
| ], | |
| outputs=gr.Textbox(label="Optimized Resume", lines=20), | |
| title="Ai Resume Optimizer", | |
| description="Upload your resume and specify a job title to optimize your resume for that position. Try out other models [here](https://huggingface.co/tchans123)." | |
| ) | |
| # Launch the Gradio app | |
| interface.launch(share=True) |