Update app.py
Browse files
app.py
CHANGED
@@ -1,82 +1,84 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import pickle
|
3 |
-
import docx
|
4 |
-
import PyPDF2
|
5 |
-
import re
|
6 |
-
|
7 |
-
# Load pre-trained model and TF-IDF vectorizer
|
8 |
-
svc_model = pickle.load(open('clf.pkl', 'rb')) # Update with your model path
|
9 |
-
tfidf = pickle.load(open('tfidf.pkl', 'rb')) # Update with your vectorizer path
|
10 |
-
le = pickle.load(open('encoder.pkl', 'rb')) # Update with your encoder path
|
11 |
-
|
12 |
-
# Function to clean resume text
|
13 |
-
def clean_resume(txt):
|
14 |
-
clean_text = re.sub('http\S+\s', ' ', txt)
|
15 |
-
clean_text = re.sub('RT|cc', ' ', clean_text)
|
16 |
-
clean_text = re.sub('#\S+\s', ' ', clean_text)
|
17 |
-
clean_text = re.sub('@\S+', ' ', clean_text)
|
18 |
-
clean_text = re.sub('[%s]' % re.escape("""!"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"""), ' ', clean_text)
|
19 |
-
clean_text = re.sub(r'[^\x00-\x7f]', ' ', clean_text)
|
20 |
-
clean_text = re.sub('\s+', ' ', clean_text)
|
21 |
-
return clean_text
|
22 |
-
|
23 |
-
# Function to extract text from PDF
|
24 |
-
def extract_text_from_pdf(file):
|
25 |
-
pdf_reader = PyPDF2.PdfReader(file)
|
26 |
-
text = ''
|
27 |
-
for page in pdf_reader.pages:
|
28 |
-
text += page.extract_text()
|
29 |
-
return text
|
30 |
-
|
31 |
-
# Function to extract text from DOCX
|
32 |
-
def extract_text_from_docx(file):
|
33 |
-
doc = docx.Document(file)
|
34 |
-
text = ''
|
35 |
-
for paragraph in doc.paragraphs:
|
36 |
-
text += paragraph.text + '\n'
|
37 |
-
return text
|
38 |
-
|
39 |
-
# Function to extract text from TXT
|
40 |
-
def extract_text_from_txt(file):
|
41 |
-
try:
|
42 |
-
text = file.read().decode('utf-8')
|
43 |
-
except UnicodeDecodeError:
|
44 |
-
text = file.read().decode('latin-1')
|
45 |
-
return text
|
46 |
-
|
47 |
-
# Function to handle file upload and extraction
|
48 |
-
def handle_file_upload(uploaded_file):
|
49 |
-
file_extension = uploaded_file.name.split('.')[-1].lower()
|
50 |
-
if file_extension == 'pdf':
|
51 |
-
text = extract_text_from_pdf(uploaded_file)
|
52 |
-
elif file_extension == 'docx':
|
53 |
-
text = extract_text_from_docx(uploaded_file)
|
54 |
-
elif file_extension == 'txt':
|
55 |
-
text = extract_text_from_txt(uploaded_file)
|
56 |
-
else:
|
57 |
-
raise ValueError("Unsupported file type. Please upload a PDF, DOCX, or TXT file.")
|
58 |
-
return text
|
59 |
-
|
60 |
-
# Function to predict the category of a resume
|
61 |
-
def predict_category(file):
|
62 |
-
try:
|
63 |
-
resume_text = handle_file_upload(file)
|
64 |
-
cleaned_text = clean_resume(resume_text)
|
65 |
-
vectorized_text = tfidf.transform([cleaned_text])
|
66 |
-
vectorized_text = vectorized_text.toarray()
|
67 |
-
predicted_category = svc_model.predict(vectorized_text)
|
68 |
-
predicted_category_name = le.inverse_transform(predicted_category)
|
69 |
-
return f"Predicted Category: {predicted_category_name[0]}"
|
70 |
-
except Exception as e:
|
71 |
-
return f"Error: {str(e)}"
|
72 |
-
|
73 |
-
# Define Gradio interface
|
74 |
-
inputs = gr.File(label="Upload Resume (PDF, DOCX, TXT)")
|
75 |
-
outputs = gr.Textbox(label="Prediction")
|
76 |
-
|
77 |
-
interface = gr.Interface(fn=predict_category, inputs=inputs, outputs=outputs, title="Resume Classifier",
|
78 |
-
description="Upload your resume to predict its job category using an AI model.")
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pickle
|
3 |
+
import docx
|
4 |
+
import PyPDF2
|
5 |
+
import re
|
6 |
+
|
7 |
+
# Load pre-trained model and TF-IDF vectorizer
|
8 |
+
svc_model = pickle.load(open('clf.pkl', 'rb')) # Update with your model path
|
9 |
+
tfidf = pickle.load(open('tfidf.pkl', 'rb')) # Update with your vectorizer path
|
10 |
+
le = pickle.load(open('encoder.pkl', 'rb')) # Update with your encoder path
|
11 |
+
|
12 |
+
# Function to clean resume text
|
13 |
+
def clean_resume(txt):
|
14 |
+
clean_text = re.sub('http\S+\s', ' ', txt)
|
15 |
+
clean_text = re.sub('RT|cc', ' ', clean_text)
|
16 |
+
clean_text = re.sub('#\S+\s', ' ', clean_text)
|
17 |
+
clean_text = re.sub('@\S+', ' ', clean_text)
|
18 |
+
clean_text = re.sub('[%s]' % re.escape("""!"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"""), ' ', clean_text)
|
19 |
+
clean_text = re.sub(r'[^\x00-\x7f]', ' ', clean_text)
|
20 |
+
clean_text = re.sub('\s+', ' ', clean_text)
|
21 |
+
return clean_text
|
22 |
+
|
23 |
+
# Function to extract text from PDF
|
24 |
+
def extract_text_from_pdf(file):
|
25 |
+
pdf_reader = PyPDF2.PdfReader(file)
|
26 |
+
text = ''
|
27 |
+
for page in pdf_reader.pages:
|
28 |
+
text += page.extract_text()
|
29 |
+
return text
|
30 |
+
|
31 |
+
# Function to extract text from DOCX
|
32 |
+
def extract_text_from_docx(file):
|
33 |
+
doc = docx.Document(file)
|
34 |
+
text = ''
|
35 |
+
for paragraph in doc.paragraphs:
|
36 |
+
text += paragraph.text + '\n'
|
37 |
+
return text
|
38 |
+
|
39 |
+
# Function to extract text from TXT
|
40 |
+
def extract_text_from_txt(file):
|
41 |
+
try:
|
42 |
+
text = file.read().decode('utf-8')
|
43 |
+
except UnicodeDecodeError:
|
44 |
+
text = file.read().decode('latin-1')
|
45 |
+
return text
|
46 |
+
|
47 |
+
# Function to handle file upload and extraction
|
48 |
+
def handle_file_upload(uploaded_file):
|
49 |
+
file_extension = uploaded_file.name.split('.')[-1].lower()
|
50 |
+
if file_extension == 'pdf':
|
51 |
+
text = extract_text_from_pdf(uploaded_file)
|
52 |
+
elif file_extension == 'docx':
|
53 |
+
text = extract_text_from_docx(uploaded_file)
|
54 |
+
elif file_extension == 'txt':
|
55 |
+
text = extract_text_from_txt(uploaded_file)
|
56 |
+
else:
|
57 |
+
raise ValueError("Unsupported file type. Please upload a PDF, DOCX, or TXT file.")
|
58 |
+
return text
|
59 |
+
|
60 |
+
# Function to predict the category of a resume
|
61 |
+
def predict_category(file):
|
62 |
+
try:
|
63 |
+
resume_text = handle_file_upload(file)
|
64 |
+
cleaned_text = clean_resume(resume_text)
|
65 |
+
vectorized_text = tfidf.transform([cleaned_text])
|
66 |
+
vectorized_text = vectorized_text.toarray()
|
67 |
+
predicted_category = svc_model.predict(vectorized_text)
|
68 |
+
predicted_category_name = le.inverse_transform(predicted_category)
|
69 |
+
return f"Predicted Category: {predicted_category_name[0]}"
|
70 |
+
except Exception as e:
|
71 |
+
return f"Error: {str(e)}"
|
72 |
+
|
73 |
+
# Define Gradio interface
|
74 |
+
inputs = gr.File(label="Upload Resume (PDF, DOCX, TXT)")
|
75 |
+
outputs = gr.Textbox(label="Prediction")
|
76 |
+
|
77 |
+
interface = gr.Interface(fn=predict_category, inputs=inputs, outputs=outputs, title="Resume Classifier",
|
78 |
+
description="Upload your resume to predict its job category using an AI model.")
|
79 |
+
|
80 |
+
interface.launch(server_port=7861)
|
81 |
+
|
82 |
+
# Launch the interface
|
83 |
+
if __name__ == "__main__":
|
84 |
+
interface.launch()
|