Spaces:
Sleeping
Sleeping
| ''' | |
| This is the main file of the app. This file contains the code for the streamlit app. | |
| ''' | |
| import time | |
| import datetime | |
| import base64 | |
| import streamlit as st | |
| from streamlit_chat import message | |
| from job_description_generator import predict_job_description, get_job_description_conversation | |
| from job_description_fixer import fix_job_description, get_job_description_fixer_conversation | |
| from interview_questions_generator import (predict_interview_question, | |
| get_interview_questions_conversation) | |
| from cover_letter_generator import get_cover_letter | |
| from top_accomplishment_generator import get_accomplishments | |
| from constants import PROMPT_VERSION | |
| conversation = get_job_description_conversation() | |
| if 'generator_conversation' not in st.session_state: | |
| with open("./conversation.txt", "a", encoding='utf-8') as f: | |
| #add a horizontal line | |
| f.write("--------------------------------------------------\n") | |
| #add the date | |
| f.write(f"Conversation on {datetime.datetime.now()}, prompt_{PROMPT_VERSION}: \n\n") | |
| st.session_state['generator_conversation'] = conversation | |
| fixer_conversation = get_job_description_fixer_conversation() | |
| if 'fixer_conversation' not in st.session_state: | |
| st.session_state['fixer_conversation'] = fixer_conversation | |
| st.session_state['response'] = {'history': [], 'prediction': ''} | |
| interview_questions_conversation = get_interview_questions_conversation() | |
| if 'interview_questions_conversation' not in st.session_state: | |
| st.session_state['interview_questions_conversation'] = interview_questions_conversation | |
| def get_downloadable_conversation(input_text, response): | |
| ''' | |
| Downloads the conversation to a text file. | |
| ''' | |
| conversation_to_save = f"Conversation with JobGPT on {datetime.datetime.now()}, prompt_{PROMPT_VERSION}: \n\n" | |
| for historical_message in response['history']: | |
| conversation_to_save = conversation_to_save + historical_message + "\n" | |
| conversation_to_save = conversation_to_save + f"Human: {input_text} \n" | |
| conversation_to_save = conversation_to_save + f"JobGPT: {response['prediction']} \n" | |
| conversation_to_save = conversation_to_save + "----------------------------------------\n" | |
| return conversation_to_save | |
| def message_writer(input_text, response): | |
| ''' | |
| Writes the messages to the chat window. | |
| ''' | |
| messages = [] | |
| current_message = "" | |
| current_is_user = True | |
| for historical_message in response['history']: | |
| if "human" in historical_message.lower(): | |
| messages.append([current_message, current_is_user]) | |
| current_message = historical_message.replace("Human:", "") | |
| current_is_user = True | |
| elif "JobGPT" in historical_message: | |
| messages.append([current_message, current_is_user]) | |
| current_message = historical_message.replace("JobGPT:", "") | |
| current_is_user = False | |
| else: | |
| current_message = current_message + "\n" + historical_message | |
| messages.append([current_message, current_is_user]) | |
| for message_to_send, is_user in messages: | |
| if message_to_send.strip() != "": | |
| message(message_to_send, is_user=is_user) | |
| message(input_text, is_user=True) | |
| message(response['prediction'], is_user=False) | |
| return 0 | |
| def setup(): | |
| """ | |
| Streamlit related setup. This has to be run for each page. | |
| """ | |
| hide_streamlit_style = """ | |
| <style> | |
| #MainMenu {visibility: hidden;} | |
| footer {visibility: hidden;} | |
| </style> | |
| """ | |
| st.markdown(hide_streamlit_style, unsafe_allow_html=True) | |
| def main(): | |
| ''' | |
| Main function of the app. | |
| ''' | |
| setup() | |
| #create a sidebar where you can select your page | |
| st.sidebar.title("JobGPT") | |
| st.sidebar.markdown("---") | |
| #selector | |
| page = st.sidebar.selectbox( | |
| "Select a page", ["Home", | |
| "Job Description Generator", | |
| "Job Description Fixer", | |
| "Cover Letter Generator", | |
| "Interview questions generator", | |
| "Accomplishments Generator"]) | |
| if page == "Home": | |
| st.title("JobGPT") | |
| st.write("Select a page in the sidebar to get started.") | |
| st.write("### Available options:") | |
| st.write("1. Job Description Generator") | |
| st.write("2. Job Description Fixer") | |
| st.write("3. Cover Letter Generator") | |
| st.write("4. Interview Questions Generator") | |
| st.write("5. Accomplishments Generator") | |
| st.markdown("---") | |
| elif page == "Job Description Generator": | |
| container_one = st.container() | |
| container_one.title("A Job Description Generating Chatbot") | |
| container_one.markdown( | |
| "JobGPT is a chatbot that generates job descriptions. \ | |
| This is built just for demo purpose." | |
| ) | |
| input_text = container_one.text_area( | |
| "Prompt", | |
| "Hi, can you please help me generate an unbiased job description?") | |
| button = container_one.button("Send") | |
| st.sidebar.markdown("---") | |
| st.sidebar.markdown("Click on `new chat` to start a new chat. \ | |
| History will be cleared and you'll lose access to current chat." | |
| ) | |
| clear_session = st.sidebar.button("New Chat") | |
| if clear_session: | |
| with open("./conversation.txt", "a", encoding='utf-8') as f: | |
| #add a horizontal line | |
| f.write("--------------------------------------------------\n") | |
| #add the date | |
| f.write(f"Conversation on {datetime.datetime.now()}, prompt_{PROMPT_VERSION}: \n\n") | |
| st.session_state['generator_conversation'] = conversation | |
| container_one.markdown("---") | |
| initial_message = "Hello, how can I help you?" | |
| message(initial_message) | |
| #download_button = st.sidebar.button("Download Conversation") | |
| if button: | |
| response = predict_job_description(input_text, | |
| st.session_state['generator_conversation']) | |
| message_writer(input_text, response) | |
| st.session_state['response'] = response | |
| conversation_to_save = get_downloadable_conversation( | |
| input_text, st.session_state['response']) | |
| #write to conversation.txt | |
| with open("./conversation.txt", "a", encoding='utf-8') as f: | |
| f.write(f"HUMAN: {input_text}\n") | |
| f.write(f"BOT: {response['prediction']}\n") | |
| #download the conversation | |
| b64 = base64.b64encode(conversation_to_save.encode()).decode() | |
| href = f'<a href="data:file/txt;base64,{b64}" download="{datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")}-conversation.txt">Download conversation</a>' | |
| st.sidebar.markdown(href, unsafe_allow_html=True) | |
| elif page == "Job Description Fixer": | |
| container_two = st.container() | |
| container_two.title("A Job Description Fixing Chatbot") | |
| container_two.markdown( | |
| "JobGPT is a chatbot that fixes job descriptions. This is built just for demo purpose." | |
| ) | |
| input_text = container_two.text_area( | |
| "Prompt", | |
| "Hi, can you please help me fix my job description? It's biased.") | |
| button = container_two.button("Send") | |
| st.sidebar.markdown("---") | |
| st.sidebar.markdown("Click on `new chat` to start a new chat. \ | |
| History will be cleared and you'll lose access to current chat." | |
| ) | |
| clear_session = st.sidebar.button("New Chat") | |
| if clear_session: | |
| st.session_state['fixer_conversation'] = fixer_conversation | |
| container_two.markdown("---") | |
| initial_message = "Hello, how can I help you?" | |
| message(initial_message) | |
| if button: | |
| response = fix_job_description( | |
| input_text, st.session_state['fixer_conversation']) | |
| message_writer(input_text, response) | |
| st.session_state['response'] = response | |
| conversation_to_save = get_downloadable_conversation( | |
| input_text, st.session_state['response']) | |
| #download the conversation | |
| b64 = base64.b64encode(conversation_to_save.encode()).decode() | |
| href = f'<a href="data:file/txt;base64,{b64}" download="{datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")}-conversation.txt">Download conversation</a>' | |
| st.sidebar.markdown(href, unsafe_allow_html=True) | |
| elif page == "Cover Letter Generator": | |
| container_three = st.container() | |
| container_three.title("A Cover Letter Generating Chatbot") | |
| container_three.markdown( "JobGPT is a chatbot that generates cover letters. \ | |
| This is built just for demo purpose.") | |
| container_three.markdown("---") | |
| uploaded_files = container_three.file_uploader("Upload your resume", type=["pdf", "txt", "docx"], accept_multiple_files=True) | |
| if uploaded_files is not None and uploaded_files != []: | |
| counter = 0 | |
| for uploaded_file in uploaded_files: | |
| file_extension = uploaded_file.name.split(".")[-1] | |
| with open(f"./documents/resume_{counter}.{file_extension}", "wb") as file_io: | |
| file_io.write(uploaded_file.getbuffer()) | |
| counter += 1 | |
| with st.spinner('Uploading...'): | |
| time.sleep(1) | |
| container_three.success('Uploaded!') | |
| container_three.markdown("---") | |
| form = container_three.form(key='my_form') | |
| title = form.text_input("Job Title (required)", placeholder="VP of Engineering") | |
| company = form.text_input("Company Name (required)", placeholder="Google") | |
| more_info = form.text_area("More Info", | |
| help="Add more info about you or the job in natural language", | |
| placeholder="I am a software engineer with 5 years of experience. The job focuses on building a new product in healthcare sector.") | |
| submit_button = form.form_submit_button(label='Submit') | |
| if submit_button: | |
| if title == "": | |
| st.error("Please enter a job title") | |
| elif company == "": | |
| st.error("Please enter a company name") | |
| else: | |
| with st.spinner('Generating...'): | |
| cover_letter = get_cover_letter(title, company, more_info, "./documents") | |
| container_three.markdown("---") | |
| container_three.markdown("### Cover Letter:") | |
| container_three.write(cover_letter) | |
| elif page == "Accomplishments Generator": | |
| container_three = st.container() | |
| container_three.title("An Accomplishments Generating Chatbot") | |
| container_three.markdown( "JobGPT is a chatbot that generates Accomplishments from resume. \ | |
| This is built just for demo purpose.") | |
| container_three.markdown("---") | |
| uploaded_file = container_three.file_uploader("Upload your resume", type=["pdf"]) | |
| if uploaded_file is not None: | |
| with open("resume.pdf", "wb") as file_io: | |
| file_io.write(uploaded_file.getbuffer()) | |
| with st.spinner('Uploading...'): | |
| time.sleep(1) | |
| container_three.success('Uploaded!') | |
| container_three.markdown("---") | |
| form = container_three.form(key='my_form') | |
| title = form.text_input("Job Title (required)", placeholder="VP of Engineering") | |
| company = form.text_input("Company Name (required)", placeholder="Google") | |
| job_description = form.text_area("Job Description", | |
| help="Paste the job description you are applying for", | |
| placeholder="We are looking for a software engineer with 5 years of experience. The job focuses on building a new product in healthcare sector") | |
| submit_button = form.form_submit_button(label='Submit') | |
| if submit_button: | |
| if title == "": | |
| st.error("Please enter a job title") | |
| elif company == "": | |
| st.error("Please enter a company name") | |
| elif job_description == "": | |
| st.error("Please enter a job description") | |
| else: | |
| with st.spinner('Generating...'): | |
| accomplishments = get_accomplishments(title, company, job_description, "resume.pdf") | |
| container_three.markdown("---") | |
| container_three.markdown("### Top Accomplishments for the given job:") | |
| container_three.write(accomplishments) | |
| elif page == "Interview questions generator": | |
| container_two = st.container() | |
| container_two.title("An Interview Questions Generating Chatbot") | |
| container_two.markdown( | |
| "JobGPT is a chatbot that generates interview questions.\ | |
| This is built just for demo purpose." | |
| ) | |
| input_text = container_two.text_area( | |
| "Prompt", | |
| "Hi, can you please help me generate interview questions?") | |
| button = container_two.button("Send") | |
| st.sidebar.markdown("---") | |
| st.sidebar.markdown("Click on `new chat` to start a new chat. \ | |
| History will be cleared and you'll lose access to current chat." | |
| ) | |
| clear_session = st.sidebar.button("New Chat") | |
| if clear_session: | |
| st.session_state['interview_questions_conversation'] = interview_questions_conversation | |
| container_two.markdown("---") | |
| initial_message = "Hello, how can I help you?" | |
| message(initial_message) | |
| if button: | |
| response = predict_interview_question( | |
| input_text, st.session_state['interview_questions_conversation']) | |
| message_writer(input_text, response) | |
| st.session_state['response'] = response | |
| conversation_to_save = get_downloadable_conversation( | |
| input_text, st.session_state['response']) | |
| #download the conversation | |
| b64 = base64.b64encode(conversation_to_save.encode()).decode() | |
| href = f'<a href="data:file/txt;base64,{b64}" download="{datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")}-conversation.txt">Download conversation</a>' | |
| st.sidebar.markdown(href, unsafe_allow_html=True) | |
| main() | |