JobDescription / app.py
arif670's picture
Update app.py
ebf2f2d verified
import streamlit as st
from docx import Document
from openai import OpenAI
import io
import os
# Initialize OpenAI client
api_key = os.getenv("OPENAI_API_KEY") # Read API key from environment variable
if not api_key:
st.error("OpenAI API key not found. Please set the OPENAI_API_KEY environment variable.")
st.stop()
# Initialize the OpenAI client without any additional arguments
client = OpenAI(api_key=api_key)
# Department and Role mapping
DEPARTMENT_ROLES = {
"Construction": [
"Site Engineer",
"Project Manager",
"Construction Worker",
"Safety Officer",
"Quantity Surveyor"
],
"Design": [
"Architect",
"Interior Designer",
"CAD Technician",
"Structural Engineer"
],
"Management": [
"Operations Manager",
"Site Supervisor",
"Contracts Manager",
"Planning Manager"
]
}
# Function to generate JD using OpenAI
def generate_jd_with_openai(department, role):
prompt = f"""
Create a detailed professional job description for a {role} in the {department} department of a construction company.
Include the following sections:
1. Job Summary
2. Key Responsibilities
3. Qualifications and Skills
4. Experience Requirements
5. Benefits and Perks
Make it professional and detailed, suitable for a construction company.
"""
try:
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a professional HR consultant specializing in construction industry job descriptions."},
{"role": "user", "content": prompt}
],
temperature=0.7,
max_tokens=1000
)
return response.choices[0].message.content
except Exception as e:
st.error(f"Error generating job description: {str(e)}")
return None
# Function to create Word document
def create_word_document(role, content):
doc = Document()
# Add title
title = doc.add_heading(f"Job Description: {role}", 0)
title.alignment = 1 # Center alignment
# Add content
for line in content.split('\n'):
if line.strip().startswith(('1.', '2.', '3.', '4.', '5.')):
heading = doc.add_heading(line.strip(), level=1)
else:
para = doc.add_paragraph(line)
# Save to bytes buffer
buffer = io.BytesIO()
doc.save(buffer)
buffer.seek(0)
return buffer
# Streamlit UI
st.set_page_config(page_title="Construction JD Generator", layout="wide")
st.title("πŸ—οΈ AI-Powered Job Description Generator")
st.subheader("Create professional job descriptions for your construction company")
# Create two columns for department and role selection
col1, col2 = st.columns(2)
with col1:
department = st.selectbox(
"Select Department",
list(DEPARTMENT_ROLES.keys()),
index=0,
help="Choose the department for the position"
)
with col2:
role = st.selectbox(
"Select Role",
DEPARTMENT_ROLES[department],
index=0,
help="Choose the specific role"
)
# Generate JD button
if st.button("✨ Generate Job Description"):
with st.spinner("Generating professional job description using AI..."):
# Generate JD content
jd_content = generate_jd_with_openai(department, role)
if jd_content:
# Create Word document
doc_buffer = create_word_document(role, jd_content)
# Show preview
st.success("Job Description Generated Successfully!")
st.markdown("### Preview:")
st.markdown(jd_content)
# Download button
st.download_button(
label="πŸ“₯ Download Job Description",
data=doc_buffer,
file_name=f"{role.replace(' ', '_')}_JD.docx",
mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document",
help="Download the job description in Word format"
)
# Instructions
st.markdown("""
### Instructions:
1. Select the department from the first dropdown
2. Choose the specific role from the second dropdown
3. Click 'Generate Job Description'
4. Review the generated description
5. Download the Word document
""")
# Footer
st.markdown("---")
st.markdown("Built with ❀️ using Streamlit and OpenAI")