Spaces:
Runtime error
Runtime error
File size: 9,743 Bytes
bf7b232 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
import re
import pdfkit
import streamlit as st
from src.chatbot.chatgpt import Chatgpt
from src.utils import parse_pdf, build_html_resume
section_examples = {'summary': 'I have passion for new tech',
'workExperience': 'Tell about my ability to lead projects',
'education': 'Describe my degree type in more details'}
openai_key_info = 'https://platform.openai.com/account/api-keys'
def list_section(section_name, section_data):
description_key = 'description'
item_keys = list(section_data[0].keys())
item_keys.remove(description_key)
for item_id, section_item in enumerate(section_data):
cols = st.columns(len(item_keys))
for col, key in zip(cols, item_keys):
col.text_input(key, section_item[key], key=f'{section_name}_{item_id}_{key}')
st.text_area(description_key, section_item[description_key], key=f'{section_name}_{item_id}_{description_key}')
recruiter_subsection(section_name, section_example=section_examples[section_name], item_id=item_id)
st.markdown('***')
def skills_section(section_name, skills_data):
num_columns = 3
for skills_row in range(0, len(skills_data), num_columns):
cols = st.columns([3, 1] * num_columns)
skills_row_names = skills_data[skills_row: skills_row + num_columns]
for item_id, skill in enumerate(skills_row_names):
skill_id = skills_row + item_id
cols[item_id * 2].text_input(' ', value=skill, key=f'{section_name}_{skill_id}', label_visibility='hidden')
cols[item_id * 2 + 1].markdown('## ')
if cols[item_id * 2 + 1].button('x', key=f'{section_name}_{skill_id}_remove_skill'):
_remove_skill(skill_id, skills_data)
skill_subsection(section_name)
st.markdown('***')
def _remove_skill(skill_id, skills_data):
del skills_data[skill_id]
st.experimental_rerun()
def skill_subsection(section_name, item_id=0):
key = f'{section_name}_{item_id}_add_skill'
cols = st.columns([12, 1])
new_skill = cols[0].text_input("Add skill", key=key)
cols[1].markdown('##')
clicked = cols[1].button("\+")
if clicked and new_skill:
st.write(new_skill)
st.session_state['resume_data'][section_name].append(new_skill)
st.write(st.session_state['resume_data'][section_name])
st.experimental_rerun()
def recruiter_subsection(section_name, section_example, item_id=0):
with st.container():
cols = st.columns([3, 10], gap='small')
cols[0].write('\n')
cols[0].write('\n')
button_clicked = cols[0].button("Auto Section Improve", key=f'{section_name}_{item_id}_improve_auto')
trigger_key = 'Add a special request'
user_request_template = f"{trigger_key} to the bot here... e.g. {section_example}."
user_request = cols[1].text_input("section_example", value=user_request_template,
key=f'{section_name}_{item_id}_improve_manual', label_visibility='hidden')
if button_clicked:
user_request = '' if trigger_key in user_request else user_request
section_key = get_item_key(section_name, item_id)
section_text = st.session_state[section_key]
new_section_text = st.session_state['chatbot'].improve_section(section_text, user_request)
update_resume_data(new_section_text, section_name, item_id)
st.experimental_rerun()
def get_item_key(section_name, item_id=0):
section_key = ''
if section_name in ['workExperience', 'education']:
key = 'description'
section_key = f'{section_name}_{item_id}_{key}'
elif section_name == 'summary':
section_key = f'{section_name}'
return section_key
def update_resume_data(text_input, section_name, item_id=0):
if section_name in ['workExperience', 'education']:
key = 'description'
st.session_state['resume_data'][section_name][item_id][key] = text_input
elif section_name == 'summary':
section_key = f'{section_name}'
st.session_state['resume_data'][section_key] = text_input
def summary_section(section_name, summary_data):
st.text_area(section_name, summary_data, key=f'{section_name}', label_visibility='hidden')
recruiter_subsection(section_name, section_examples[section_name])
def contact_info_section(section_name, info_data):
for key, value in info_data.items():
if value:
st.text_input(key.title(), value, key=f'{section_name}_{key}')
st.markdown('***')
def header():
st.text_input('name', st.session_state.resume_data['name'], key="name")
st.text_input('title', st.session_state.resume_data['title'], key="title")
def body():
section_dict = {'contactInfo': contact_info_section, 'summary': summary_section, 'workExperience': list_section,
'education': list_section, 'skills': skills_section}
tabs_names = [key_to_tab_name(key) for key in section_dict.keys()]
tabs = st.tabs(tabs_names)
for tab, key in zip(tabs, section_dict):
section_func = section_dict[key]
with tab:
section_func(key, st.session_state['resume_data'][key])
def key_to_tab_name(input_string):
return re.sub(r'([A-Z])', r' \1', input_string).strip().title()
def sidebar():
with st.sidebar:
uploaded_file = st.file_uploader('Upload PDF Resume', type="PDF")
if uploaded_file and _is_new_file(uploaded_file) and is_chatbot_loaded():
_init_resume(uploaded_file)
if is_data_loaded() and is_chatbot_loaded():
st.button("Improve More", on_click=_improve_more)
st.download_button('Download PDF', file_name='out.pdf', mime="application/json", data=download_pdf())
def download_pdf():
resume_data = format_resume_data()
html_resume = build_html_resume(resume_data)
options = {'page-size': 'A4', 'margin-top': '0.75in', 'margin-right': '0.75in', 'margin-bottom': '0.75in',
'margin-left': '0.75in', 'encoding': "UTF-8", 'no-outline': None}
return pdfkit.from_string(html_resume, options=options, css='src/css/main.css')
def _improve_more():
print("Improving resume")
st.session_state['resume_data'] = st.session_state['chatbot'].improve_resume(st.session_state['resume_data'])
def _init_chatbot():
cols = st.columns([6, 1, 1])
api_key = cols[0].text_input("Enter OpenAI API key")
cols[1].markdown("#")
api_submit = cols[1].button("Submit")
cols[2].markdown("#")
get_info = cols[2].button("Get key")
if get_info:
st.info(f"Get your key at: {openai_key_info}")
if api_submit:
if Chatgpt.validate_api(api_key):
st.session_state['chatbot'] = Chatgpt(api_key)
st.experimental_rerun()
else:
st.error("Not valid API key - try again...")
def is_chatbot_loaded():
return st.session_state.get('chatbot')
def _is_new_file(uploaded_file):
return uploaded_file.id != st.session_state.get('file_id', '')
def _init_resume(uploaded_file):
resume_data = parse_pdf(uploaded_file)
st.session_state['resume_data'] = st.session_state['chatbot'].improve_resume(resume_data)
st.session_state['file_id'] = uploaded_file.id
st.experimental_rerun()
def format_resume_data():
current_state = st.session_state
resume_data = {}
contact_info = {}
work_experience = []
education = []
skills = []
resume_data['name'] = current_state.get('name', '')
resume_data['title'] = current_state.get('title', '')
contact_info_keys = ['linkedin', 'github', 'email', 'address']
for key in contact_info_keys:
contact_info[key] = current_state.get(f'contactInfo_{key}', '')
resume_data['contactInfo'] = contact_info
resume_data['summary'] = current_state.get('summary', '')
work_experience_keys = ['workExperience_{}_title', 'workExperience_{}_company', 'workExperience_{}_dates',
'workExperience_{}_description']
education_keys = ['education_{}_degree', 'education_{}_school', 'education_{}_dates', 'education_{}_description']
total_work_experience = count_entries(st.session_state, 'workExperience')
total_education = count_entries(st.session_state, 'education')
for i in range(total_work_experience):
work_experience.append(
{key.split('_')[2]: current_state.get(key.format(i), '') for key in work_experience_keys})
for i in range(total_education):
education.append({key.split('_')[2]: current_state.get(key.format(i), '') for key in education_keys})
resume_data['workExperience'] = work_experience
resume_data['education'] = education
total_skills = count_entries(st.session_state, 'skills')
for i in range(total_skills):
skill_key = f'skills_{i}'
skills.append(current_state.get(skill_key, ''))
resume_data['skills'] = skills
return resume_data
def count_entries(input_dict, entry_type):
max_index = max([int(key.split("_")[1]) for key in input_dict.keys() if key.startswith(f"{entry_type}_")],
default=0)
return max_index + 1
def title():
st.title("ChatCV - AI Resume Builder")
def upload_resume_header():
st.success("Upload PDF Resume - Let the magic begin...")
def is_data_loaded():
return st.session_state.get('resume_data')
def _main():
title()
if is_chatbot_loaded():
sidebar()
if is_data_loaded():
header()
body()
else:
upload_resume_header()
else:
_init_chatbot()
if __name__ == '__main__':
_main()
# bootstrap 4 collapse example
|