Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,12 +2,44 @@ import gradio as gr
|
|
2 |
import io
|
3 |
import PyPDF2
|
4 |
from langchain.llms import OpenAIChat
|
5 |
-
from langchain.chains import
|
6 |
from langchain.memory import ConversationBufferMemory
|
7 |
-
from langchain import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
# Limited functionality - Replace with your actual OpenAI API Key
|
10 |
-
os.environ['OPENAI_API_KEY'] = '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' # Placeholder, needs replacement
|
11 |
|
12 |
template = """Format the provided resume to this YAML template:
|
13 |
---
|
@@ -21,22 +53,22 @@ emails:
|
|
21 |
dateOfBirth: ''
|
22 |
addresses:
|
23 |
- street: ''
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
summary: ''
|
29 |
education:
|
30 |
- school: ''
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
workExperience:
|
36 |
- company: ''
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
skills:
|
41 |
- name: ''
|
42 |
certifications:
|
@@ -59,55 +91,39 @@ llm_chain = LLMChain(
|
|
59 |
memory=memory,
|
60 |
)
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
#
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
# Use the uploaded PDF from Gradio
|
84 |
-
pdf_data = pdf_file.read()
|
85 |
-
pdf_data = io.BytesIO(pdf_data)
|
86 |
-
|
87 |
-
reader = PyPDF2.PdfReader(pdf_data)
|
88 |
-
resume_text = ""
|
89 |
-
num_pages = len(reader.pages)
|
90 |
-
|
91 |
-
for page in range(num_pages):
|
92 |
-
current_page = reader.pages[page]
|
93 |
-
page_text = current_page.extract_text()
|
94 |
-
if page_text:
|
95 |
-
resume_text += page_text
|
96 |
-
res = llm_chain.predict(human_input=resume_text)
|
97 |
-
return res
|
98 |
-
|
99 |
-
|
100 |
-
# Gradio interface with testing flag
|
101 |
-
interface = gr.Interface(
|
102 |
-
fn=format_resume_to_yaml,
|
103 |
-
inputs=[
|
104 |
-
gr.File(label="Upload your resume (PDF)"),
|
105 |
-
# Security Risk - Not recommended to take API key from user input
|
106 |
-
gr.Textbox(label="Enter your OpenAI API Key (not recommended)"),
|
107 |
-
],
|
108 |
-
outputs="text",
|
109 |
-
title="Resume to YAML Formatter",
|
110 |
-
description="Upload your resume (PDF) and get it formatted in YAML. **Please note: Due to security concerns, entering your OpenAI API Key directly is",
|
111 |
-
)
|
112 |
|
113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import io
|
3 |
import PyPDF2
|
4 |
from langchain.llms import OpenAIChat
|
5 |
+
from langchain.chains import LLMChain
|
6 |
from langchain.memory import ConversationBufferMemory
|
7 |
+
from langchain import PromptTemplate
|
8 |
+
|
9 |
+
def process_pdf(file_info, open_ai_key):
|
10 |
+
file_content = file_info["content"]
|
11 |
+
extracted_text = extract_text_from_pdf_bytes(file_content)
|
12 |
+
|
13 |
+
# Set the OpenAI API key
|
14 |
+
os.environ['OPENAI_API_KEY'] = open_ai_key
|
15 |
+
|
16 |
+
# Format the resume text to YAML
|
17 |
+
formatted_resume_yaml = format_resume_to_yaml(extracted_text)
|
18 |
+
|
19 |
+
return formatted_resume_yaml
|
20 |
+
|
21 |
+
# Assuming you have already defined the template, prompt, memory, and llm_chain as before
|
22 |
+
|
23 |
+
def format_resume_to_yaml(resume):
|
24 |
+
"""
|
25 |
+
Formats the resume text into a YAML format using the LLMChain.
|
26 |
+
"""
|
27 |
+
# Before calling the LLMChain, ensure the OPENAI_API_KEY environment variable is set
|
28 |
+
res = llm_chain.predict(human_input=resume)
|
29 |
+
return res.output # Assuming the 'predict' method returns an object with an 'output' attribute containing the result
|
30 |
+
|
31 |
+
def process_pdf(file_info, open_ai_key):
|
32 |
+
file_content = file_info["content"] # Extract file content as bytes
|
33 |
+
extracted_text = extract_text_from_pdf_bytes(file_content)
|
34 |
+
|
35 |
+
# Set the OpenAI API key
|
36 |
+
os.environ['OPENAI_API_KEY'] = open_ai_key
|
37 |
+
|
38 |
+
# Format the resume text to YAML
|
39 |
+
formatted_resume_yaml = format_resume_to_yaml(extracted_text)
|
40 |
+
|
41 |
+
return formatted_resume_yaml
|
42 |
|
|
|
|
|
43 |
|
44 |
template = """Format the provided resume to this YAML template:
|
45 |
---
|
|
|
53 |
dateOfBirth: ''
|
54 |
addresses:
|
55 |
- street: ''
|
56 |
+
city: ''
|
57 |
+
state: ''
|
58 |
+
zip: ''
|
59 |
+
country: ''
|
60 |
summary: ''
|
61 |
education:
|
62 |
- school: ''
|
63 |
+
degree: ''
|
64 |
+
fieldOfStudy: ''
|
65 |
+
startDate: ''
|
66 |
+
endDate: ''
|
67 |
workExperience:
|
68 |
- company: ''
|
69 |
+
position: ''
|
70 |
+
startDate: ''
|
71 |
+
endDate: ''
|
72 |
skills:
|
73 |
- name: ''
|
74 |
certifications:
|
|
|
91 |
memory=memory,
|
92 |
)
|
93 |
|
94 |
+
def format_resume_to_yaml(resume):
|
95 |
+
# Before calling the LLMChain, ensure the OPENAI_API_KEY environment variable is set
|
96 |
+
res = llm_chain.predict(human_input=resume)
|
97 |
+
return res
|
98 |
+
|
99 |
+
def process_pdf(file, open_ai_key):
|
100 |
+
# Save the uploaded PDF temporarily
|
101 |
+
temp_pdf_path = "/tmp/uploaded_resume.pdf"
|
102 |
+
with open(temp_pdf_path, 'wb') as f:
|
103 |
+
f.write(file.read())
|
104 |
+
|
105 |
+
# Extract text from the PDF
|
106 |
+
extracted_text = extract_text_from_pdf(temp_pdf_path)
|
107 |
+
|
108 |
+
# Set the OpenAI API key
|
109 |
+
os.environ['OPENAI_API_KEY'] = open_ai_key
|
110 |
+
|
111 |
+
# Format the resume text to YAML
|
112 |
+
formatted_resume_yaml = format_resume_to_yaml(extracted_text)
|
113 |
+
|
114 |
+
return formatted_resume_yaml
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
|
116 |
+
with gr.Blocks() as demo:
|
117 |
+
gr.Markdown("### Upload a Resume in PDF Format")
|
118 |
+
with gr.Row():
|
119 |
+
pdf_file = gr.File(label="Select a PDF file", type="file")
|
120 |
+
openai_key_input = gr.Textbox(label="OpenAI API Key", type="password")
|
121 |
+
format_button = gr.Button("Format Resume")
|
122 |
+
output_textbox = gr.Textbox(label="Formatted Resume in YAML", lines=20)
|
123 |
+
|
124 |
+
format_button.click(
|
125 |
+
fn=process_pdf,
|
126 |
+
inputs=[pdf_file, openai_key_input],
|
127 |
+
outputs=[output_textbox]
|
128 |
+
)
|
129 |
+
demo.launch(debug=True)
|