Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,40 +7,33 @@ from langchain import PromptTemplate
|
|
7 |
import os
|
8 |
import tempfile
|
9 |
|
10 |
-
|
11 |
-
|
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 |
-
|
18 |
-
|
|
|
19 |
|
20 |
-
|
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 |
-
|
30 |
|
31 |
-
#
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
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
|
43 |
-
|
44 |
|
45 |
# Combine the text chunks into a single string
|
46 |
resume_text = " ".join(docs)
|
@@ -98,14 +91,18 @@ def format_resume_to_yaml(api_key, file_info):
|
|
98 |
res = llm_chain.predict(human_input=resume_text)
|
99 |
return res['output_text']
|
100 |
|
|
|
|
|
|
|
|
|
101 |
def main():
|
102 |
-
input_api_key =
|
103 |
-
input_pdf_file =
|
104 |
-
output_yaml =
|
105 |
|
106 |
iface = gr.Interface(
|
107 |
fn=format_resume_to_yaml,
|
108 |
-
inputs=[input_api_key,
|
109 |
outputs=output_yaml,
|
110 |
title="Resume to YAML Formatter",
|
111 |
description="Upload a PDF resume and enter your OpenAI API key to get it formatted to a YAML template.",
|
@@ -113,5 +110,6 @@ def main():
|
|
113 |
|
114 |
iface.launch(debug=True)
|
115 |
|
|
|
116 |
if __name__ == "__main__":
|
117 |
-
main()
|
|
|
7 |
import os
|
8 |
import tempfile
|
9 |
|
10 |
+
# Updated imports for Gradio components
|
11 |
+
from gradio.components import File, Textbox
|
|
|
12 |
|
|
|
|
|
13 |
|
14 |
+
def format_resume_to_yaml(api_key, file):
|
15 |
+
# Set the API key for OpenAI
|
16 |
+
os.environ['OPENAI_API_KEY'] = api_key
|
17 |
|
18 |
+
file_content = file.read()
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
# Check if the file content is not empty
|
21 |
if not file_content:
|
22 |
+
raise ValueError("The uploaded file is empty.")
|
23 |
|
24 |
+
# Save the uploaded file content to a temporary file
|
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 |
+
# Now we can use PyPDFLoader with the path to the temporary file
|
32 |
+
try:
|
33 |
loader = PyPDFLoader(temp_file_path)
|
34 |
docs = loader.load_and_split() # This will return a list of text chunks from the PDF
|
35 |
+
except (IOError, PyPDF2.errors.PdfReaderError) as e: # Handle potential PDF reading errors
|
36 |
+
raise ValueError(f"An error occurred while processing the PDF: {e}")
|
37 |
|
38 |
# Combine the text chunks into a single string
|
39 |
resume_text = " ".join(docs)
|
|
|
91 |
res = llm_chain.predict(human_input=resume_text)
|
92 |
return res['output_text']
|
93 |
|
94 |
+
def on_file_upload(filename, file_content):
|
95 |
+
if not file_content:
|
96 |
+
gr.Interface.alert(title="Error", message="Please upload a valid PDF resume.")
|
97 |
+
|
98 |
def main():
|
99 |
+
input_api_key = Textbox(label="Enter your OpenAI API Key")
|
100 |
+
input_pdf_file = File(label="Upload your PDF resume")
|
101 |
+
output_yaml = Textbox(label="Formatted Resume in YAML")
|
102 |
|
103 |
iface = gr.Interface(
|
104 |
fn=format_resume_to_yaml,
|
105 |
+
inputs=[input_api_key, File(label="Upload your PDF resume", upload_event=on_file_upload)],
|
106 |
outputs=output_yaml,
|
107 |
title="Resume to YAML Formatter",
|
108 |
description="Upload a PDF resume and enter your OpenAI API key to get it formatted to a YAML template.",
|
|
|
110 |
|
111 |
iface.launch(debug=True)
|
112 |
|
113 |
+
|
114 |
if __name__ == "__main__":
|
115 |
+
main()
|