Spaces:
Sleeping
Sleeping
import gradio as gr | |
import io | |
import os | |
import PyPDF2 | |
from gradio.components import File, Textbox | |
from langchain_openai import ChatOpenAI | |
from langchain.chains import LLMChain | |
from langchain.memory import ConversationBufferMemory | |
from langchain import PromptTemplate | |
from sklearn.feature_extraction.text import TfidfVectorizer | |
from sklearn.metrics.pairwise import cosine_similarity | |
def extract_text_from_pdf_binary(pdf_binary): | |
"""Extracts text from a PDF file binary.""" | |
text = "" | |
pdf_data = io.BytesIO(pdf_binary) | |
reader = PyPDF2.PdfReader(pdf_data) | |
for page in reader.pages: | |
page_text = page.extract_text() | |
if page_text: | |
text += page_text | |
return text | |
def calculate_resume_score(resume_text, job_description): | |
""" | |
Calculates the relevance score of the resume to the job description using cosine similarity. | |
Parameters: | |
- resume_text (str): Text of the resume. | |
- job_description (str): Text of the job description. | |
Returns: | |
- score (float): Similarity score between the resume and job description. | |
""" | |
vectorizer = TfidfVectorizer() | |
tfidf_matrix = vectorizer.fit_transform([resume_text, job_description]) | |
score = cosine_similarity(tfidf_matrix[0:1], tfidf_matrix[1:2])[0][0] | |
return score | |
def format_resume_to_yaml(api_key, file_content, job_description): | |
"""Formats the content of a resume PDF file to YAML and calculates its relevance to a job description.""" | |
if not file_content: | |
raise ValueError("The uploaded file is empty.") | |
os.environ['OPENAI_API_KEY'] = api_key | |
resume_text = extract_text_from_pdf_binary(file_content) | |
# Additional step to calculate the resume score relative to the job description. | |
resume_score = calculate_resume_score(resume_text, job_description) | |
# Formatting the resume to YAML (the existing implementation continues here)... | |
# Assume llm_chain.predict and other logic here as before. | |
# For demonstration, return both formatted resume (in real use, integrate this properly) and score. | |
return "Formatted Resume in YAML (placeholder)", resume_score | |
def main(): | |
"""Main function to launch the Gradio interface with job description input.""" | |
iface = gr.Interface( | |
fn=format_resume_to_yaml, | |
inputs=[ | |
Textbox(label="Enter your OpenAI API Key"), | |
File(label="Upload your PDF resume", type="binary"), | |
Textbox(label="Paste the Job Description here", lines=10) | |
], | |
outputs=[ | |
Textbox(label="Formatted Resume in YAML"), | |
Textbox(label="Resume Score") | |
], | |
title="Resume to YAML Formatter with ATS Scoring", | |
description="Upload a PDF resume, paste the job description, and enter your OpenAI API key to get the resume formatted to a YAML template and score its relevance to the job." | |
) | |
iface.launch(debug=True, share=True) | |
if __name__ == "__main__": | |
main() | |