ruslanmv commited on
Commit
39890ac
·
verified ·
1 Parent(s): 281442a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -32
app.py CHANGED
@@ -2,37 +2,66 @@ import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import PyPDF2
4
  import io
5
- from docx import Document # Fix the import issue
6
 
 
7
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
9
- def extract_text_from_pdf(pdf_file):
10
- pdf_reader = PyPDF2.PdfReader(io.BytesIO(pdf_file))
11
- text = ""
12
- for page in pdf_reader.pages:
13
- text += page.extract_text() + "\n"
14
- return text
 
 
 
 
 
 
15
 
16
- def extract_text_from_docx(docx_file):
17
- doc = Document(io.BytesIO(docx_file)) # Fixed import usage
18
- return "\n".join([para.text for para in doc.paragraphs])
 
 
 
 
 
19
 
20
  def parse_cv(file, job_description):
 
21
  if file is None:
22
  return "Please upload a CV file."
23
-
24
  file_ext = file.name.split(".")[-1].lower()
25
- file_bytes = file.read()
26
-
 
 
 
27
  if file_ext == "pdf":
28
  text = extract_text_from_pdf(file_bytes)
29
  elif file_ext == "docx":
30
  text = extract_text_from_docx(file_bytes)
31
  else:
32
  return "Unsupported file format. Please upload a PDF or DOCX file."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
- prompt = f"Analyze the following CV against the job description provided. Provide a summary, an assessment of fit, and a score from 0 to 10.\n\nJob Description:\n{job_description}\n\nCandidate CV:\n{text}"
35
- response = client.text_generation(prompt, max_tokens=512)
36
  return response
37
 
38
  def respond(
@@ -43,32 +72,40 @@ def respond(
43
  temperature,
44
  top_p,
45
  ):
 
 
 
46
  messages = [{"role": "system", "content": system_message}]
47
 
48
- for val in history:
49
- if val[0]:
50
- messages.append({"role": "user", "content": val[0]})
51
- if val[1]:
52
- messages.append({"role": "assistant", "content": val[1]})
53
 
54
  messages.append({"role": "user", "content": message})
55
  response = ""
56
 
57
- for message in client.chat_completion(
58
- messages,
59
- max_tokens=max_tokens,
60
- stream=True,
61
- temperature=temperature,
62
- top_p=top_p,
63
- ):
64
- token = message.choices[0].delta.content
65
- response += token
66
- yield response
 
 
 
67
 
 
68
  demo = gr.Blocks()
69
 
70
  with demo:
71
  gr.Markdown("## AI-powered CV Analyzer and Chatbot")
 
72
  with gr.Tab("Chatbot"):
73
  chat_interface = gr.ChatInterface(
74
  respond,
@@ -87,7 +124,9 @@ with demo:
87
  )
88
 
89
  with gr.Tab("CV Analyzer"):
90
- gr.Markdown("### Upload your CV (PDF or DOCX) and provide the job description to receive a professional analysis and suitability score.")
 
 
91
  file_input = gr.File(label="Upload CV", type="file")
92
  job_desc_input = gr.Textbox(label="Job Description", lines=5)
93
  output_text = gr.Textbox(label="CV Analysis Report", lines=10)
@@ -96,4 +135,4 @@ with demo:
96
  analyze_button.click(parse_cv, inputs=[file_input, job_desc_input], outputs=output_text)
97
 
98
  if __name__ == "__main__":
99
- demo.launch()
 
2
  from huggingface_hub import InferenceClient
3
  import PyPDF2
4
  import io
5
+ from docx import Document # Make sure you have installed python-docx
6
 
7
+ # Initialize the client for Hugging Face inference.
8
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
9
 
10
+ def extract_text_from_pdf(pdf_file_bytes):
11
+ """Extract text from a PDF file given as bytes."""
12
+ try:
13
+ pdf_reader = PyPDF2.PdfReader(io.BytesIO(pdf_file_bytes))
14
+ text = ""
15
+ for page in pdf_reader.pages:
16
+ page_text = page.extract_text()
17
+ if page_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: {str(e)}"
22
 
23
+ def extract_text_from_docx(docx_file_bytes):
24
+ """Extract text from a DOCX file given as 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: {str(e)}"
31
 
32
  def parse_cv(file, job_description):
33
+ """Analyze a CV (PDF or DOCX) against a job description and generate a 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: {str(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 reading"):
51
+ return text # Return error from extraction 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: {str(e)}"
64
 
 
 
65
  return response
66
 
67
  def respond(
 
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,
93
+ stream=True,
94
+ temperature=temperature,
95
+ top_p=top_p,
96
+ ):
97
+ token = message_chunk.choices[0].delta.content
98
+ response += token
99
+ yield response
100
+ except Exception as e:
101
+ yield f"Error during chat generation: {str(e)}"
102
 
103
+ # Build the Gradio interface
104
  demo = gr.Blocks()
105
 
106
  with demo:
107
  gr.Markdown("## AI-powered CV Analyzer and Chatbot")
108
+
109
  with gr.Tab("Chatbot"):
110
  chat_interface = gr.ChatInterface(
111
  respond,
 
124
  )
125
 
126
  with gr.Tab("CV Analyzer"):
127
+ gr.Markdown(
128
+ "### Upload your CV (PDF or DOCX) and provide the job description to receive a professional analysis and suitability score."
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)
 
135
  analyze_button.click(parse_cv, inputs=[file_input, job_desc_input], outputs=output_text)
136
 
137
  if __name__ == "__main__":
138
+ demo.launch()