tchans123 commited on
Commit
d42ddbf
·
verified ·
1 Parent(s): a326889

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -57
app.py CHANGED
@@ -2,88 +2,61 @@ import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import os
4
  import fitz # PyMuPDF for PDF handling
5
- from docx import Document # For handling MS Word documents
6
 
7
  # Initialize Hugging Face Inference Client
8
- try:
9
- client = InferenceClient(
10
- model="meta-llama/Meta-Llama-3.1-8B-Instruct",
11
- token=os.getenv("HUGGINGFACEHUB_API_TOKEN")
12
- )
13
- except Exception as e:
14
- print(f"Error initializing InferenceClient: {e}")
15
 
16
  # Function to optimize resume based on job title
17
  def optimize_resume(resume_text, job_title):
18
  prompt = f"Optimize the following resume for the job title '{job_title}':\n\n{resume_text}"
19
  responses = []
20
- try:
21
- for message in client.chat_completion(
22
- messages=[{"role": "user", "content": prompt}],
23
- max_tokens=1000,
24
- stream=True,
25
- ):
26
- responses.append(message.choices[0].delta.content)
27
- except Exception as e:
28
- return f"Error during model inference: {e}"
29
 
30
  return ''.join(responses)
31
 
32
  # Function to extract text from a PDF file
33
- def extract_text_from_pdf(pdf_file_path):
34
- text = ""
35
- try:
36
- pdf_document = fitz.open(pdf_file_path)
37
- for page_num in range(len(pdf_document)):
38
- page = pdf_document.load_page(page_num)
39
- text += page.get_text()
40
- except Exception as e:
41
- return f"Error extracting text from PDF: {e}"
42
- return text
43
-
44
- # Function to extract text from a Word document
45
- def extract_text_from_word(docx_file_path):
46
  text = ""
47
- try:
48
- doc = Document(docx_file_path)
49
- for paragraph in doc.paragraphs:
50
- text += paragraph.text + "\n"
51
- except Exception as e:
52
- return f"Error extracting text from Word document: {e}"
53
  return text
54
 
55
  # Function to process the resume and job title inputs
56
  def process_resume(file, job_title):
57
- try:
58
- file_name = file.name
59
- if file_name.endswith(".pdf"):
60
- # Extract text if the file is a PDF
61
- resume_text = extract_text_from_pdf(file.name)
62
- elif file_name.endswith(".docx"):
63
- # Extract text if the file is a Word document
64
- resume_text = extract_text_from_word(file.name)
65
- else:
66
- # Assume the file is a text file and read it directly
67
- resume_text = file.decode("utf-8")
68
-
69
- # Optimize the resume
70
- optimized_resume = optimize_resume(resume_text, job_title)
71
-
72
- return optimized_resume
73
- except Exception as e:
74
- return f"Error processing resume: {e}"
75
 
76
  # Gradio Interface
77
  interface = gr.Interface(
78
  fn=process_resume,
79
  inputs=[
80
- gr.File(label="Upload your resume (text, PDF, or Word)"),
81
  gr.Textbox(lines=1, placeholder="Enter the job title...", label="Job Title"),
82
  ],
83
  outputs=gr.Textbox(label="Optimized Resume", lines=20),
84
- title="Ai Resume Optimizer",
85
  description="Upload your resume and specify a job title to optimize your resume for that position. Try out other models [here](https://huggingface.co/tchans123)."
86
  )
87
 
88
  # Launch the Gradio app
89
- interface.launch(share=True)
 
2
  from huggingface_hub import InferenceClient
3
  import os
4
  import fitz # PyMuPDF for PDF handling
 
5
 
6
  # Initialize Hugging Face Inference Client
7
+ client = InferenceClient(
8
+ model="meta-llama/Meta-Llama-3.1-8B-Instruct",
9
+ token=os.getenv("HUGGINGFACEHUB_API_TOKEN")
10
+ )
 
 
 
11
 
12
  # Function to optimize resume based on job title
13
  def optimize_resume(resume_text, job_title):
14
  prompt = f"Optimize the following resume for the job title '{job_title}':\n\n{resume_text}"
15
  responses = []
16
+ for message in client.chat_completion(
17
+ messages=[{"role": "user", "content": prompt}],
18
+ max_tokens=1000,
19
+ stream=True,
20
+ ):
21
+ responses.append(message.choices[0].delta.content)
 
 
 
22
 
23
  return ''.join(responses)
24
 
25
  # Function to extract text from a PDF file
26
+ def extract_text_from_pdf(pdf_file):
 
 
 
 
 
 
 
 
 
 
 
 
27
  text = ""
28
+ pdf_document = fitz.open(stream=pdf_file, filetype="pdf")
29
+ for page_num in range(len(pdf_document)):
30
+ page = pdf_document.load_page(page_num)
31
+ text += page.get_text()
 
 
32
  return text
33
 
34
  # Function to process the resume and job title inputs
35
  def process_resume(file, job_title):
36
+ file_name = file.name
37
+ if file_name.endswith(".pdf"):
38
+ # Extract text if the file is a PDF
39
+ resume_text = extract_text_from_pdf(file.read())
40
+ else:
41
+ # Assume the file is a text file and read it directly
42
+ resume_text = file.read().decode("utf-8")
43
+
44
+ # Optimize the resume
45
+ optimized_resume = optimize_resume(resume_text, job_title)
46
+
47
+ return optimized_resume
 
 
 
 
 
 
48
 
49
  # Gradio Interface
50
  interface = gr.Interface(
51
  fn=process_resume,
52
  inputs=[
53
+ gr.File(label="Upload your resume (text or PDF)"),
54
  gr.Textbox(lines=1, placeholder="Enter the job title...", label="Job Title"),
55
  ],
56
  outputs=gr.Textbox(label="Optimized Resume", lines=20),
57
+ title="Resume Optimizer with Meta-LLaMA 3.1",
58
  description="Upload your resume and specify a job title to optimize your resume for that position. Try out other models [here](https://huggingface.co/tchans123)."
59
  )
60
 
61
  # Launch the Gradio app
62
+ interface.launch(share=True)