Spaces:
Sleeping
Sleeping
Update app_funtions.py
Browse files- app_funtions.py +145 -150
app_funtions.py
CHANGED
@@ -1,150 +1,145 @@
|
|
1 |
-
import re
|
2 |
-
|
3 |
-
from PwsResumeClassifier import PwsResumeClassifier as pws
|
4 |
-
from openai import OpenAI
|
5 |
-
import app_funtions as appfun
|
6 |
-
import os
|
7 |
-
import shutil
|
8 |
-
import glob
|
9 |
-
import gradio as gr
|
10 |
-
import json
|
11 |
-
from pathlib import Path
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
os.
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
)
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
def
|
38 |
-
for
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
def
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
return [
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
response = gr.Markdown('<p>
|
65 |
-
return [response, gr.Row.update(visible=True),
|
66 |
-
gr.Textbox(label="Input", info="", lines=1, placeholder="
|
67 |
-
gr.Button("SEND", interactive=
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
)
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
)
|
112 |
-
|
113 |
-
run
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
)
|
126 |
-
print(
|
127 |
-
json_array
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
history = history or []
|
147 |
-
output = client_pws.query_assistant(user_input)
|
148 |
-
history.append((user_input, output))
|
149 |
-
|
150 |
-
return history, history, text
|
|
|
1 |
+
import re
|
2 |
+
|
3 |
+
from PwsResumeClassifier import PwsResumeClassifier as pws
|
4 |
+
from openai import OpenAI
|
5 |
+
import app_funtions as appfun
|
6 |
+
import os
|
7 |
+
import shutil
|
8 |
+
import glob
|
9 |
+
import gradio as gr
|
10 |
+
import json
|
11 |
+
from pathlib import Path
|
12 |
+
|
13 |
+
job_names = []
|
14 |
+
job_descriptions = []
|
15 |
+
|
16 |
+
client_pws = pws(organization_id=os.getenv('OPENAIORG'), project_id=os.getenv('PROJECT'), api_key=os.getenv('APIKEY'))
|
17 |
+
|
18 |
+
client = OpenAI(
|
19 |
+
organization=os.getenv('OPENAIORG'),
|
20 |
+
project=os.getenv('PROJECT'),
|
21 |
+
api_key=os.getenv('APIKEY')
|
22 |
+
)
|
23 |
+
|
24 |
+
def check_password(password):
|
25 |
+
if password == os.getenv('MILPASSWORD'):
|
26 |
+
|
27 |
+
return [gr.Textbox(visible=False), gr.Button(visible=False), gr.Row.update(visible=False), gr.Row.update(visible=True)]
|
28 |
+
else:
|
29 |
+
return [gr.Textbox(visible=True), gr.Button(visible=True), gr.Row.update(visible=True), gr.Row.update(visible=False)]
|
30 |
+
|
31 |
+
|
32 |
+
def add_resume(resume_list):
|
33 |
+
for resume in resume_list:
|
34 |
+
shutil.copy2(os.path.abspath(resume), os.path.abspath('Resumes'))
|
35 |
+
|
36 |
+
|
37 |
+
def remove_resumes(file_explorer):
|
38 |
+
for file in file_explorer:
|
39 |
+
os.remove(file)
|
40 |
+
|
41 |
+
|
42 |
+
def job_selected(selected_name):
|
43 |
+
print(job_names)
|
44 |
+
if selected_name in job_names:
|
45 |
+
return [selected_name, job_descriptions[job_names.index(selected_name)], gr.Row.update(visible=True),
|
46 |
+
gr.Markdown()]
|
47 |
+
elif selected_name == 'Custom':
|
48 |
+
return ['', '', gr.Row.update(visible=True), gr.Markdown()]
|
49 |
+
else:
|
50 |
+
return ['', '', gr.Row.update(visible=False), gr.Markdown()]
|
51 |
+
|
52 |
+
|
53 |
+
def send_to_openai(name, description):
|
54 |
+
|
55 |
+
try:
|
56 |
+
print('Sending to AI')
|
57 |
+
client_pws.set_vector_store(os.path.abspath('Resumes'))
|
58 |
+
resumes_output = client_pws.get_best_resumes('Job Name: ' + name + '. Job Description: ' + description)
|
59 |
+
response = gr.Markdown('<p>' + resumes_output + '</p>')
|
60 |
+
return [response, gr.Row.update(visible=True),
|
61 |
+
gr.Textbox(label="Input", info="", lines=1, placeholder="Ask the chatbot.", interactive=True, scale=1),
|
62 |
+
gr.Button("SEND", interactive=True, scale=1)]
|
63 |
+
except Exception as e:
|
64 |
+
response = gr.Markdown('<p>An error occurred. Please try again.</p>')
|
65 |
+
return [response, gr.Row.update(visible=True),
|
66 |
+
gr.Textbox(label="Input", info="", lines=1, placeholder="Please process resumes", interactive=False, scale=1),
|
67 |
+
gr.Button("SEND", interactive=False, scale=1)]
|
68 |
+
|
69 |
+
|
70 |
+
def extract_jobs(filepath):
|
71 |
+
try:
|
72 |
+
description = r"""You are a document information extraction assistant that extract job names and job description
|
73 |
+
precisely and accurately from a provided file."""
|
74 |
+
|
75 |
+
instructions = r"""You will be provided a document that contains information about one or more job positions.
|
76 |
+
You will extract all of these job positions and return them in a JSON Array. Each item in the JSON Array will
|
77 |
+
have a field "job_name" for the name of the job position and "job_description" for the description and
|
78 |
+
requirements of the job position. Your response
|
79 |
+
should only be a JSON Array with all items."""
|
80 |
+
|
81 |
+
file_streams = [open(filepath, "rb")]
|
82 |
+
vector_store = client.beta.vector_stores.create(
|
83 |
+
name="Job Description File",
|
84 |
+
)
|
85 |
+
|
86 |
+
file_batch = client.beta.vector_stores.file_batches.upload_and_poll(
|
87 |
+
vector_store_id=vector_store.id,
|
88 |
+
files=file_streams
|
89 |
+
)
|
90 |
+
|
91 |
+
assistant = client.beta.assistants.create(
|
92 |
+
name="Job Description Extract Assistant",
|
93 |
+
description=description,
|
94 |
+
instructions=instructions,
|
95 |
+
model="gpt-4o",
|
96 |
+
tools=[{"type": "file_search"}],
|
97 |
+
tool_resources={"file_search": {"vector_store_ids": [vector_store.id]}},
|
98 |
+
temperature=0
|
99 |
+
)
|
100 |
+
thread = client.beta.threads.create()
|
101 |
+
|
102 |
+
messages = client.beta.threads.messages.create(
|
103 |
+
thread_id=thread.id,
|
104 |
+
role="user",
|
105 |
+
content="Please extract all job descriptions"
|
106 |
+
)
|
107 |
+
|
108 |
+
run = client.beta.threads.runs.create_and_poll(
|
109 |
+
thread_id=thread.id,
|
110 |
+
assistant_id=assistant.id,
|
111 |
+
)
|
112 |
+
|
113 |
+
if run.status == 'completed':
|
114 |
+
messages = client.beta.threads.messages.list(
|
115 |
+
thread_id=thread.id
|
116 |
+
)
|
117 |
+
|
118 |
+
messages = client.beta.threads.messages.list(
|
119 |
+
thread_id=thread.id
|
120 |
+
)
|
121 |
+
print(messages.data[0].content[0].text.value)
|
122 |
+
json_array = messages.data[0].content[0].text.value
|
123 |
+
json_array = re.sub(r"^```json", "", json_array)
|
124 |
+
json_array = re.sub(r"```$", "", json_array)
|
125 |
+
json_array = re.sub(r"【.*?】", "", json_array)
|
126 |
+
print(json_array)
|
127 |
+
print(json.loads(json_array))
|
128 |
+
|
129 |
+
for job in json.loads(json_array):
|
130 |
+
job_names.append(job['job_name'])
|
131 |
+
job_descriptions.append(job['job_description'])
|
132 |
+
return [gr.Markdown("""<p><center>Extraction completed successfully</center></p>"""),
|
133 |
+
gr.Dropdown(label='Select Job', choices=job_names + ['Custom'], allow_custom_value=True)]
|
134 |
+
except Exception as e:
|
135 |
+
return [gr.Markdown("""<p><center>Parsing failed. Error message: """ + repr(e) + """</center></p>"""),
|
136 |
+
gr.Dropdown(label='Select Job', choices=appfun.job_names + ['Custom'], allow_custom_value=True)]
|
137 |
+
|
138 |
+
|
139 |
+
def my_chatbot(user_input, history):
|
140 |
+
text = ""
|
141 |
+
history = history or []
|
142 |
+
output = client_pws.query_assistant(user_input)
|
143 |
+
history.append((user_input, output))
|
144 |
+
|
145 |
+
return history, history, text
|
|
|
|
|
|
|
|
|
|