Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,13 +2,13 @@ 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 client
|
8 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
9 |
|
10 |
def extract_text_from_pdf(pdf_file_bytes):
|
11 |
-
"""Extract text from
|
12 |
try:
|
13 |
pdf_reader = PyPDF2.PdfReader(io.BytesIO(pdf_file_bytes))
|
14 |
text = ""
|
@@ -18,38 +18,38 @@ def extract_text_from_pdf(pdf_file_bytes):
|
|
18 |
text += page_text + "\n"
|
19 |
return text.strip() or "No text could be extracted from the PDF."
|
20 |
except Exception as e:
|
21 |
-
return f"Error reading PDF: {
|
22 |
|
23 |
def extract_text_from_docx(docx_file_bytes):
|
24 |
-
"""Extract text from
|
25 |
try:
|
26 |
doc = Document(io.BytesIO(docx_file_bytes))
|
27 |
text = "\n".join(para.text for para in doc.paragraphs)
|
28 |
return text.strip() or "No text could be extracted from the DOCX file."
|
29 |
except Exception as e:
|
30 |
-
return f"Error reading DOCX: {
|
31 |
|
32 |
def parse_cv(file, job_description):
|
33 |
-
"""Analyze
|
34 |
if file is None:
|
35 |
return "Please upload a CV file."
|
36 |
-
|
37 |
file_ext = file.name.split(".")[-1].lower()
|
38 |
try:
|
39 |
file_bytes = file.read()
|
40 |
except Exception as e:
|
41 |
-
return f"Error reading the uploaded file: {
|
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 error
|
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"
|
@@ -60,33 +60,23 @@ def parse_cv(file, job_description):
|
|
60 |
try:
|
61 |
response = client.text_generation(prompt, max_tokens=512)
|
62 |
except Exception as e:
|
63 |
-
return f"Error during CV analysis: {
|
64 |
|
65 |
return response
|
66 |
|
67 |
-
def respond(
|
68 |
-
|
69 |
-
history: list[tuple[str, str]],
|
70 |
-
system_message,
|
71 |
-
max_tokens,
|
72 |
-
temperature,
|
73 |
-
top_p,
|
74 |
-
):
|
75 |
-
"""
|
76 |
-
Chatbot response generator that interacts with a conversational model.
|
77 |
-
"""
|
78 |
messages = [{"role": "system", "content": system_message}]
|
79 |
-
|
80 |
for user_msg, bot_msg in history:
|
81 |
if user_msg:
|
82 |
messages.append({"role": "user", "content": user_msg})
|
83 |
if bot_msg:
|
84 |
messages.append({"role": "assistant", "content": bot_msg})
|
85 |
-
|
86 |
messages.append({"role": "user", "content": message})
|
|
|
87 |
response = ""
|
88 |
-
|
89 |
try:
|
|
|
90 |
for message_chunk in client.chat_completion(
|
91 |
messages,
|
92 |
max_tokens=max_tokens,
|
@@ -98,7 +88,7 @@ def respond(
|
|
98 |
response += token
|
99 |
yield response
|
100 |
except Exception as e:
|
101 |
-
yield f"Error during chat generation: {
|
102 |
|
103 |
# Build the Gradio interface
|
104 |
demo = gr.Blocks()
|
@@ -107,27 +97,22 @@ with demo:
|
|
107 |
gr.Markdown("## AI-powered CV Analyzer and Chatbot")
|
108 |
|
109 |
with gr.Tab("Chatbot"):
|
|
|
110 |
chat_interface = gr.ChatInterface(
|
111 |
respond,
|
|
|
112 |
additional_inputs=[
|
113 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
114 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
115 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
116 |
-
gr.Slider(
|
117 |
-
minimum=0.1,
|
118 |
-
maximum=1.0,
|
119 |
-
value=0.95,
|
120 |
-
step=0.05,
|
121 |
-
label="Top-p (nucleus sampling)",
|
122 |
-
),
|
123 |
],
|
124 |
)
|
125 |
|
126 |
with gr.Tab("CV Analyzer"):
|
127 |
-
gr.Markdown(
|
128 |
-
|
129 |
-
)
|
130 |
-
file_input = gr.File(label="Upload CV", type="file")
|
131 |
job_desc_input = gr.Textbox(label="Job Description", lines=5)
|
132 |
output_text = gr.Textbox(label="CV Analysis Report", lines=10)
|
133 |
analyze_button = gr.Button("Analyze CV")
|
|
|
2 |
from huggingface_hub import InferenceClient
|
3 |
import PyPDF2
|
4 |
import io
|
5 |
+
from docx import Document # Ensure you have installed "python-docx" (not "docx")
|
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:
|
13 |
pdf_reader = PyPDF2.PdfReader(io.BytesIO(pdf_file_bytes))
|
14 |
text = ""
|
|
|
18 |
text += page_text + "\n"
|
19 |
return text.strip() or "No text could be extracted from the PDF."
|
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:
|
26 |
doc = Document(io.BytesIO(docx_file_bytes))
|
27 |
text = "\n".join(para.text for para in doc.paragraphs)
|
28 |
return text.strip() or "No text could be extracted from the DOCX file."
|
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 |
file_ext = file.name.split(".")[-1].lower()
|
38 |
try:
|
39 |
file_bytes = file.read()
|
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"
|
|
|
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}]
|
|
|
70 |
for user_msg, bot_msg in history:
|
71 |
if user_msg:
|
72 |
messages.append({"role": "user", "content": user_msg})
|
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.
|
80 |
for message_chunk in client.chat_completion(
|
81 |
messages,
|
82 |
max_tokens=max_tokens,
|
|
|
88 |
response += token
|
89 |
yield response
|
90 |
except Exception as e:
|
91 |
+
yield f"Error during chat generation: {e}"
|
92 |
|
93 |
# Build the Gradio interface
|
94 |
demo = gr.Blocks()
|
|
|
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"),
|
106 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
107 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
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("### Upload your CV (PDF or DOCX) and provide the job description to receive a professional analysis and suitability score.")
|
114 |
+
# Use type="binary" for the file component.
|
115 |
+
file_input = gr.File(label="Upload CV", type="binary")
|
|
|
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")
|