Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,11 +2,12 @@ import gradio as gr
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import PyPDF2
|
| 4 |
import io
|
| 5 |
-
from docx import Document
|
| 6 |
|
| 7 |
# Initialize the inference client from Hugging Face.
|
| 8 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 9 |
|
|
|
|
| 10 |
def extract_text_from_pdf(pdf_file_bytes):
|
| 11 |
"""Extract text from PDF bytes."""
|
| 12 |
try:
|
|
@@ -20,6 +21,7 @@ def extract_text_from_pdf(pdf_file_bytes):
|
|
| 20 |
except Exception as e:
|
| 21 |
return f"Error reading PDF: {e}"
|
| 22 |
|
|
|
|
| 23 |
def extract_text_from_docx(docx_file_bytes):
|
| 24 |
"""Extract text from DOCX bytes."""
|
| 25 |
try:
|
|
@@ -29,41 +31,53 @@ def extract_text_from_docx(docx_file_bytes):
|
|
| 29 |
except Exception as e:
|
| 30 |
return f"Error reading DOCX: {e}"
|
| 31 |
|
|
|
|
| 32 |
def parse_cv(file, job_description):
|
| 33 |
"""Analyze the CV (PDF or DOCX) against the job description and return an analysis report."""
|
| 34 |
if file is None:
|
| 35 |
return "Please upload a CV file."
|
| 36 |
-
|
| 37 |
-
|
| 38 |
try:
|
| 39 |
-
file_bytes = file
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
except Exception as e:
|
| 41 |
return f"Error reading the uploaded file: {e}"
|
| 42 |
-
|
| 43 |
if file_ext == "pdf":
|
| 44 |
text = extract_text_from_pdf(file_bytes)
|
| 45 |
elif file_ext == "docx":
|
| 46 |
text = extract_text_from_docx(file_bytes)
|
| 47 |
else:
|
| 48 |
return "Unsupported file format. Please upload a PDF or DOCX file."
|
| 49 |
-
|
| 50 |
if text.startswith("Error"):
|
| 51 |
return text # Return extraction error if any.
|
| 52 |
-
|
| 53 |
prompt = (
|
| 54 |
f"Analyze the following CV against the provided job description. "
|
| 55 |
f"Provide a summary, an assessment of fit, and a score from 0 to 10.\n\n"
|
| 56 |
f"Job Description:\n{job_description}\n\n"
|
| 57 |
f"Candidate CV:\n{text}"
|
| 58 |
)
|
| 59 |
-
|
| 60 |
try:
|
| 61 |
response = client.text_generation(prompt, max_tokens=512)
|
| 62 |
except Exception as e:
|
| 63 |
return f"Error during CV analysis: {e}"
|
| 64 |
-
|
| 65 |
return response
|
| 66 |
|
|
|
|
| 67 |
def respond(message, history: list[tuple[str, str]], system_message, max_tokens, temperature, top_p):
|
| 68 |
"""Generate a chatbot response based on the conversation history and parameters."""
|
| 69 |
messages = [{"role": "system", "content": system_message}]
|
|
@@ -73,7 +87,7 @@ def respond(message, history: list[tuple[str, str]], system_message, max_tokens,
|
|
| 73 |
if bot_msg:
|
| 74 |
messages.append({"role": "assistant", "content": bot_msg})
|
| 75 |
messages.append({"role": "user", "content": message})
|
| 76 |
-
|
| 77 |
response = ""
|
| 78 |
try:
|
| 79 |
# Stream response tokens from the chat completion endpoint.
|
|
@@ -90,16 +104,17 @@ def respond(message, history: list[tuple[str, str]], system_message, max_tokens,
|
|
| 90 |
except Exception as e:
|
| 91 |
yield f"Error during chat generation: {e}"
|
| 92 |
|
|
|
|
| 93 |
# Build the Gradio interface
|
| 94 |
demo = gr.Blocks()
|
| 95 |
-
|
| 96 |
with demo:
|
| 97 |
gr.Markdown("## AI-powered CV Analyzer and Chatbot")
|
| 98 |
-
|
| 99 |
with gr.Tab("Chatbot"):
|
| 100 |
# Set type="messages" to use the OpenAI-style message format.
|
| 101 |
chat_interface = gr.ChatInterface(
|
| 102 |
respond,
|
|
|
|
| 103 |
type="messages",
|
| 104 |
additional_inputs=[
|
| 105 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
|
@@ -108,16 +123,18 @@ with demo:
|
|
| 108 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
| 109 |
],
|
| 110 |
)
|
| 111 |
-
|
| 112 |
with gr.Tab("CV Analyzer"):
|
| 113 |
-
gr.Markdown(
|
|
|
|
|
|
|
| 114 |
# Use type="binary" for the file component.
|
| 115 |
-
file_input = gr.File(label="Upload CV", type="
|
| 116 |
job_desc_input = gr.Textbox(label="Job Description", lines=5)
|
| 117 |
output_text = gr.Textbox(label="CV Analysis Report", lines=10)
|
| 118 |
analyze_button = gr.Button("Analyze CV")
|
| 119 |
-
|
| 120 |
analyze_button.click(parse_cv, inputs=[file_input, job_desc_input], outputs=output_text)
|
| 121 |
|
| 122 |
if __name__ == "__main__":
|
| 123 |
-
demo.launch()
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import PyPDF2
|
| 4 |
import io
|
| 5 |
+
from docx import Document
|
| 6 |
|
| 7 |
# Initialize the inference client from Hugging Face.
|
| 8 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 9 |
|
| 10 |
+
|
| 11 |
def extract_text_from_pdf(pdf_file_bytes):
|
| 12 |
"""Extract text from PDF bytes."""
|
| 13 |
try:
|
|
|
|
| 21 |
except Exception as e:
|
| 22 |
return f"Error reading PDF: {e}"
|
| 23 |
|
| 24 |
+
|
| 25 |
def extract_text_from_docx(docx_file_bytes):
|
| 26 |
"""Extract text from DOCX bytes."""
|
| 27 |
try:
|
|
|
|
| 31 |
except Exception as e:
|
| 32 |
return f"Error reading DOCX: {e}"
|
| 33 |
|
| 34 |
+
|
| 35 |
def parse_cv(file, job_description):
|
| 36 |
"""Analyze the CV (PDF or DOCX) against the job description and return an analysis report."""
|
| 37 |
if file is None:
|
| 38 |
return "Please upload a CV file."
|
| 39 |
+
|
| 40 |
+
# Correctly handle the file object when type="binary"
|
| 41 |
try:
|
| 42 |
+
file_bytes = file
|
| 43 |
+
file_ext = "pdf" # Assume PDF if we can't determine from name
|
| 44 |
+
if file_bytes:
|
| 45 |
+
# Heuristic to detect file type based on content
|
| 46 |
+
if file_bytes.startswith(b'%PDF'):
|
| 47 |
+
file_ext = "pdf"
|
| 48 |
+
elif file_bytes.startswith(b'PK\x03\x04'): #DOCX magic number
|
| 49 |
+
file_ext = "docx"
|
| 50 |
+
else:
|
| 51 |
+
return "Unsupported file format. Cannot determine type from content"
|
| 52 |
+
|
| 53 |
except Exception as e:
|
| 54 |
return f"Error reading the uploaded file: {e}"
|
| 55 |
+
|
| 56 |
if file_ext == "pdf":
|
| 57 |
text = extract_text_from_pdf(file_bytes)
|
| 58 |
elif file_ext == "docx":
|
| 59 |
text = extract_text_from_docx(file_bytes)
|
| 60 |
else:
|
| 61 |
return "Unsupported file format. Please upload a PDF or DOCX file."
|
| 62 |
+
|
| 63 |
if text.startswith("Error"):
|
| 64 |
return text # Return extraction error if any.
|
| 65 |
+
|
| 66 |
prompt = (
|
| 67 |
f"Analyze the following CV against the provided job description. "
|
| 68 |
f"Provide a summary, an assessment of fit, and a score from 0 to 10.\n\n"
|
| 69 |
f"Job Description:\n{job_description}\n\n"
|
| 70 |
f"Candidate CV:\n{text}"
|
| 71 |
)
|
| 72 |
+
|
| 73 |
try:
|
| 74 |
response = client.text_generation(prompt, max_tokens=512)
|
| 75 |
except Exception as e:
|
| 76 |
return f"Error during CV analysis: {e}"
|
| 77 |
+
|
| 78 |
return response
|
| 79 |
|
| 80 |
+
|
| 81 |
def respond(message, history: list[tuple[str, str]], system_message, max_tokens, temperature, top_p):
|
| 82 |
"""Generate a chatbot response based on the conversation history and parameters."""
|
| 83 |
messages = [{"role": "system", "content": system_message}]
|
|
|
|
| 87 |
if bot_msg:
|
| 88 |
messages.append({"role": "assistant", "content": bot_msg})
|
| 89 |
messages.append({"role": "user", "content": message})
|
| 90 |
+
|
| 91 |
response = ""
|
| 92 |
try:
|
| 93 |
# Stream response tokens from the chat completion endpoint.
|
|
|
|
| 104 |
except Exception as e:
|
| 105 |
yield f"Error during chat generation: {e}"
|
| 106 |
|
| 107 |
+
|
| 108 |
# Build the Gradio interface
|
| 109 |
demo = gr.Blocks()
|
|
|
|
| 110 |
with demo:
|
| 111 |
gr.Markdown("## AI-powered CV Analyzer and Chatbot")
|
| 112 |
+
|
| 113 |
with gr.Tab("Chatbot"):
|
| 114 |
# Set type="messages" to use the OpenAI-style message format.
|
| 115 |
chat_interface = gr.ChatInterface(
|
| 116 |
respond,
|
| 117 |
+
chatbot=gr.Chatbot(value=[], label="Chatbot"),
|
| 118 |
type="messages",
|
| 119 |
additional_inputs=[
|
| 120 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
|
|
|
| 123 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
| 124 |
],
|
| 125 |
)
|
| 126 |
+
|
| 127 |
with gr.Tab("CV Analyzer"):
|
| 128 |
+
gr.Markdown(
|
| 129 |
+
"### Upload your CV (PDF or DOCX) and provide the job description to receive a professional analysis and suitability score."
|
| 130 |
+
)
|
| 131 |
# Use type="binary" for the file component.
|
| 132 |
+
file_input = gr.File(label="Upload CV", type="bytes")
|
| 133 |
job_desc_input = gr.Textbox(label="Job Description", lines=5)
|
| 134 |
output_text = gr.Textbox(label="CV Analysis Report", lines=10)
|
| 135 |
analyze_button = gr.Button("Analyze CV")
|
| 136 |
+
|
| 137 |
analyze_button.click(parse_cv, inputs=[file_input, job_desc_input], outputs=output_text)
|
| 138 |
|
| 139 |
if __name__ == "__main__":
|
| 140 |
+
demo.queue().launch()
|