File size: 4,511 Bytes
1c8df3f
 
ef6c9f2
1c8df3f
7ebdf88
1c8df3f
ef6c9f2
7ebdf88
 
 
 
 
bf7c36c
7ebdf88
ef6c9f2
b0e001d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18781f8
 
b0e001d
 
 
 
 
 
 
 
 
 
 
 
99cbce9
ef6c9f2
18781f8
 
 
 
 
 
 
 
ef6c9f2
99cbce9
18781f8
99cbce9
b0e001d
 
 
1c8df3f
 
b0e001d
 
 
1c8df3f
b0e001d
 
 
 
 
 
 
 
 
 
 
 
1c8df3f
b0e001d
 
1c8df3f
b0e001d
 
1c8df3f
b0e001d
 
6640a62
b0e001d
 
 
 
 
 
 
40a0e4d
b0e001d
 
 
 
 
 
 
40a0e4d
b0e001d
 
 
99cbce9
18781f8
99cbce9
 
b0e001d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40a0e4d
b0e001d
 
 
 
 
 
 
 
 
 
 
 
ebf2f2d
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
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")