File size: 3,082 Bytes
5320c98
 
 
a759c07
427998f
5320c98
 
427998f
 
 
 
 
 
 
5320c98
a759c07
 
 
5320c98
427998f
 
 
 
 
 
 
 
 
5320c98
 
 
a759c07
427998f
 
 
 
 
 
 
 
 
 
 
 
 
c1be6bd
427998f
 
 
 
 
 
c1be6bd
 
a759c07
 
427998f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85bf59f
 
 
 
 
fcc3913
85bf59f
 
 
427998f
8c7a5de
85bf59f
 
 
d42ddbf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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)