dmurali commited on
Commit
dbbd6a7
1 Parent(s): cdce7a3

Update app_funtions.py

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