File size: 7,480 Bytes
f0b6d9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import pandas as pd
import subprocess
import os
from groq import Groq
import json

GROQ_API_KEY = os.getenv("GROQ_API_KEY")


if GROQ_API_KEY is None:
    raise RuntimeError("GROQ_API_KEY environment variable not set.")

client = Groq(api_key=GROQ_API_KEY)

# **Streamlit App Header**
st.title("Welcome to the Shortlisting App")

st.markdown("""
Through this application, your organization can streamline the hiring process for any open position. 
Simply import candidate resumes in CSV format and upload them here.
By providing a Job Description, our system evaluates the candidates and outputs the top 5 best candidates for further review.
""")


st.subheader("Job Description Builder")
ad_creator = st.text_area("Insert Details regarding the role you want to hire for. This includes: Company Name, Location, Role Name, Key Qualifications, etc.", height=100)


def save_user_input(input_text,filename):
    """Save user input to a JSON file."""
    data = {"input": input_text}
    if os.path.exists(filename):
        with open(filename, "r") as file:
            existing_data = json.load(file)
    else:
        existing_data = []

    existing_data.append(data)

    with open(filename, "w") as file:
        json.dump(existing_data, file, indent=4)


def create_job_description(job_ad):
    """Generate the Job Description using the Groq API."""
    try:
        # save user input into JSON
        save_user_input(job_ad, "user_input_jobAdBuilder.json")

        # JobAdBuilder.py ausführen
        result = subprocess.run(
            ["python", "JobAdBuilder.py", job_ad],
            capture_output=True,
            text=True,
            check=True
        )
        return result.stdout
    except subprocess.CalledProcessError as e:
        st.error(f"Error running JobAdBuilder.py: {e.stderr}")
        return None
    
if st.button("Save and Build Job Description"):
    if ad_creator.strip():
        output = create_job_description(ad_creator)
        if output:
            st.success("Job description successfully built!")
            st.subheader("Builder Output:")
            adjusted_output = st.text_area("Adjust the Job Description if needed:", output, height= 500)
            if st.button("Save Adjusted Job Description"):
                with open("AdjustedJobDescription.json", "w") as file:
                    json.dump({"adjusted_output": adjusted_output}, file, indent=4)
                st.success("Adjusted Job Description saved successfully!")
    else:
        st.error("The Job Description field cannot be empty!")


# **Job Description Input**
st.subheader("Job Description Analyzer")
job_description = st.text_area("Insert the Job Description here:", height=500)


def save_and_analyze_job_description(job_description):
    """Analyze the Job Description using the Groq API."""
    try:
        # save user input
        save_user_input(job_description, "user_input_JobDescriptionAnalyzer.json")

        # JobDescription.py ausführen
        result = subprocess.run(
            ["python", "JobDescription.py", job_description],
            capture_output=True,
            text=True,
            check=True
        )
        return result.stdout
    except subprocess.CalledProcessError as e:
        st.error(f"Error running JobDescription.py: {e.stderr}")
        return None


def display_analysis_result(analysis_result):
    """Display the analysis result a structured format."""
    st.markdown(f"**Job Title:** {analysis_result.get('jobTitle', '')}")
    st.markdown(f"**Company:** {analysis_result.get('company', '')}")
    st.markdown(f"**Location:** {analysis_result.get('location', '')}")
    st.markdown("**Key Responsibilities:**")
    for responsibility in analysis_result.get('keyResponsibilities', []):
        st.markdown(f"- {responsibility}")
    st.markdown("**Required Skills:**")
    for skill in analysis_result.get('requiredSkills', []):
        st.markdown(f"- {skill}")
    st.markdown("**Preferred Qualifications:**")
    for qualification in analysis_result.get('preferredQualifications', []):
        st.markdown(f"- {qualification}")


if st.button("Save and Analyze Job Description"):
    if job_description.strip():
        output = save_and_analyze_job_description(job_description)
        if output:
            st.success("Job description successfully analyzed!")
            st.subheader("Analyzer Output:")
            try:
                analysis_result = json.loads(output)
                display_analysis_result(analysis_result)
                #Save the analysis result to a JSON file for later use
                if os.path.exists("JobAnalyzed.json"):
                    with open("JobAnalyzed.json", "r") as file:
                        existing_data = json.load(file)
                else:
                    existing_data = []

                existing_data.append(analysis_result)

                with open("JobAnalyzed.json", "w") as file:
                    json.dump(existing_data, file, indent=4)

                adjusted_output = st.text_area("Adjust the Analyzed Job Description if needed:", json.dumps(analysis_result, indent=4), height= 500)
                if st.button("Save Adjusted Analyzed Job Description"):
                    if os.path.exists("AdjustedJobAnalyzed.json"):
                        with open("AdjustedJobAnalyzed.json", "r") as file:
                            existing_data = json.load(file)
                    else:
                        existing_adjusted_data = []

                    existing_adjusted_data.append({"adjusted_output": adjusted_output})

                    with open("AdjustedJobAnalyzed.json", "w") as file:
                        json.dump(existing_adjusted_data, file, indent=4)
                    st.success("Adjusted Analyzed Job Description saved!")
            except json.JSONDecodeError:
                st.text(output)
    else:
        st.error("The Job Description field cannot be empty!")

# **CSV Upload**
st.subheader("CSV Upload")
resume_list = st.file_uploader("Upload candidate resumes in CSV format:", type=["csv"])

def process_csv_file(csv_file):
    """Laden und Verarbeiten der CSV-Datei."""
    try:
        df = pd.read_csv(csv_file)
        st.success("CSV file successfully uploaded!")
        st.dataframe(df.head())  # show first rows for checking
        return df
    except Exception as e:
        st.error(f"Error loading CSV file: {e}")
        return None

if resume_list:
    df = process_csv_file(resume_list)

# **CSV Processing with bestFit.py**
if st.button("Find the best candidates for the position."):
    if resume_list:
        csv_file_path = "uploaded_file.csv"
        with open(csv_file_path, "wb") as file:
            file.write(resume_list.getbuffer())
        
        try:
            # Führe das Skript aus
            result = subprocess.run(
                ["python", "BestFit.py", csv_file_path],
                capture_output=True,
                text=True,
                check=True
            )
            st.subheader("Output from bestFit.py:")
            try:
                analysis_result = json.loads(result.stdout)
                st.json(analysis_result)
            except json.JSONDecodeError:
                st.text(result.stdout)
        except subprocess.CalledProcessError as e:
            st.error(f"Error running bestFit.py: {e.stderr}")
    else:
        st.error("Please upload a CSV file before processing!")