Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,38 +6,80 @@ from langchain.memory import ConversationBufferMemory
|
|
6 |
from langchain import PromptTemplate
|
7 |
import os
|
8 |
import tempfile
|
9 |
-
from gradio.components import File, Textbox
|
10 |
|
11 |
-
def format_resume_to_yaml(api_key,
|
12 |
# Set the API key for OpenAI
|
13 |
os.environ['OPENAI_API_KEY'] = api_key
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
16 |
return "Please upload a valid PDF resume."
|
17 |
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
# Check if the file content is not empty
|
21 |
if not file_content:
|
22 |
return "The uploaded file is empty."
|
23 |
|
24 |
-
#
|
25 |
-
with tempfile.NamedTemporaryFile(delete=False, mode='wb+') as tmp_file:
|
26 |
-
tmp_file.write(file_content)
|
27 |
-
tmp_file.flush()
|
28 |
-
os.fsync(tmp_file.fileno()) # Ensure data is written to disk
|
29 |
-
temp_file_path = tmp_file.name
|
30 |
-
|
31 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
loader = PyPDFLoader(temp_file_path)
|
33 |
docs = loader.load_and_split() # This will return a list of text chunks from the PDF
|
34 |
-
except
|
35 |
-
|
36 |
|
37 |
# Combine the text chunks into a single string
|
38 |
resume_text = " ".join(docs)
|
39 |
|
40 |
-
template = """
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
prompt = PromptTemplate(
|
43 |
input_variables=["chat_history", "human_input"],
|
@@ -57,9 +99,9 @@ def format_resume_to_yaml(api_key, file):
|
|
57 |
return res['output_text']
|
58 |
|
59 |
def main():
|
60 |
-
input_api_key = Textbox(label="Enter your OpenAI API Key")
|
61 |
-
input_pdf_file = File(label="Upload your PDF resume")
|
62 |
-
output_yaml = Textbox(label="Formatted Resume in YAML", interactive=False)
|
63 |
|
64 |
iface = gr.Interface(
|
65 |
fn=format_resume_to_yaml,
|
|
|
6 |
from langchain import PromptTemplate
|
7 |
import os
|
8 |
import tempfile
|
|
|
9 |
|
10 |
+
def format_resume_to_yaml(api_key, file_info):
|
11 |
# Set the API key for OpenAI
|
12 |
os.environ['OPENAI_API_KEY'] = api_key
|
13 |
+
|
14 |
+
# Extract the file path
|
15 |
+
file_path = file_info['path'] if isinstance(file_info, dict) and 'path' in file_info else None
|
16 |
+
|
17 |
+
if not file_path:
|
18 |
return "Please upload a valid PDF resume."
|
19 |
|
20 |
+
# Read the file content
|
21 |
+
try:
|
22 |
+
with open(file_path, 'rb') as file:
|
23 |
+
file_content = file.read()
|
24 |
+
except Exception as e:
|
25 |
+
return f"An error occurred while reading the file: {e}"
|
26 |
|
27 |
# Check if the file content is not empty
|
28 |
if not file_content:
|
29 |
return "The uploaded file is empty."
|
30 |
|
31 |
+
# Proceed with processing the file content
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
try:
|
33 |
+
# Save the uploaded file content to a temporary file
|
34 |
+
with tempfile.NamedTemporaryFile(delete=False, mode='wb+') as tmp_file:
|
35 |
+
tmp_file.write(file_content)
|
36 |
+
tmp_file.flush()
|
37 |
+
os.fsync(tmp_file.fileno()) # Ensure data is written to disk
|
38 |
+
temp_file_path = tmp_file.name
|
39 |
+
|
40 |
loader = PyPDFLoader(temp_file_path)
|
41 |
docs = loader.load_and_split() # This will return a list of text chunks from the PDF
|
42 |
+
except Exception as e: # Adjusted for a broader range of exceptions
|
43 |
+
return f"An error occurred while processing the PDF: {e}"
|
44 |
|
45 |
# Combine the text chunks into a single string
|
46 |
resume_text = " ".join(docs)
|
47 |
|
48 |
+
template = """Format the provided resume to this YAML template:
|
49 |
+
---
|
50 |
+
name: ''
|
51 |
+
phoneNumbers:
|
52 |
+
- ''
|
53 |
+
websites:
|
54 |
+
- ''
|
55 |
+
emails:
|
56 |
+
- ''
|
57 |
+
dateOfBirth: ''
|
58 |
+
addresses:
|
59 |
+
- street: ''
|
60 |
+
city: ''
|
61 |
+
state: ''
|
62 |
+
zip: ''
|
63 |
+
country: ''
|
64 |
+
summary: ''
|
65 |
+
education:
|
66 |
+
- school: ''
|
67 |
+
degree: ''
|
68 |
+
fieldOfStudy: ''
|
69 |
+
startDate: ''
|
70 |
+
endDate: ''
|
71 |
+
workExperience:
|
72 |
+
- company: ''
|
73 |
+
position: ''
|
74 |
+
startDate: ''
|
75 |
+
endDate: ''
|
76 |
+
skills:
|
77 |
+
- name: ''
|
78 |
+
certifications:
|
79 |
+
- name: ''
|
80 |
+
|
81 |
+
{chat_history}
|
82 |
+
{human_input}"""
|
83 |
|
84 |
prompt = PromptTemplate(
|
85 |
input_variables=["chat_history", "human_input"],
|
|
|
99 |
return res['output_text']
|
100 |
|
101 |
def main():
|
102 |
+
input_api_key = gr.components.Textbox(label="Enter your OpenAI API Key")
|
103 |
+
input_pdf_file = gr.components.File(label="Upload your PDF resume", type="file")
|
104 |
+
output_yaml = gr.components.Textbox(label="Formatted Resume in YAML", interactive=False)
|
105 |
|
106 |
iface = gr.Interface(
|
107 |
fn=format_resume_to_yaml,
|