File size: 12,115 Bytes
4db8ed6
 
 
3e96d3c
4db8ed6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5185793
4db8ed6
 
 
 
 
 
 
 
 
 
 
 
 
 
5185793
425caa5
4db8ed6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ba35a1f
5185793
 
 
8328a3e
5185793
4db8ed6
 
 
 
 
 
 
 
 
 
 
 
 
 
5185793
6f550fc
3e96d3c
4db8ed6
 
 
 
 
 
 
 
 
 
5185793
 
3e96d3c
 
4db8ed6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5185793
4db8ed6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
import os
import json
from typing import List
from urllib.parse import quote
import logging
import gradio as gr
import pandas as pd
from server import cv_processor, job_processor, applicant_evaluator

logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)


logo_base64 = cv_processor.encode_base64(
    "static/AIRecruiterAgent.png"
)

def evaluate_applicants(
    cv_files: List[str],
    job_description: str,
    #progress=gr.Progress(visible=True, label="Evaluating Applicants...")
) -> pd.DataFrame:
    """
    Evaluate applicants' CVs against the job description.

    Parameters
    ----------
    cv_files: List[str]
        List of CV file paths to evaluate.
    job_description: str
        The job description text to evaluate against.

    Returns
    -------
        pd.DataFrame: DataFrame containing evaluation results with match scores and reasoning.
    """
    # TODO: Add progress bar support with batch processing
    # TODO: Add error handling for file processing and evaluation
    # if not cv_files:
    #     gr.Error("Please upload applicants CV files in PDF format.")
    # if not job_description:
    #     gr.Error("Please provide the job description text for evaluation.")
    
    # Get job annotation
    logger.info("Getting job annotation from job description.")
    job_annotation = job_processor.get_job_content(job_description)
           
    evaluation_res = []
    for cv_file in cv_files:
        # Get CV annotation
        logger.info("Getting cv annotation from CV file: %s", cv_file.name)
        cv_annotation = cv_processor.get_cv_content(cv_file.name)
        
        # Evaluate the applicant against the job description
        logger.info("Evaluating applicant CV against job description.")
        res = applicant_evaluator.evaluate_applicant(
            cv_annotation["cv"]["annotation"], 
            job_annotation["job"]["annotation"]
        )
        evaluation = json.loads(res["evaluation"])
        cv_base64 = cv_processor.encode_base64(cv_file.name)

        score = float(evaluation["match_score"])
        if score >= 0.8:
            match_labels = "Strong Matched"
        elif score >= 0.5:
            match_labels = "Partially Matched"
        else:
            match_labels = "Not Matched"
        evaluation_res.append(
            {
                "Applicant": os.path.basename(cv_file.name),
                "Match Score": evaluation["match_score"],
                "Match Labels": match_labels,
                "Match Reasoning": evaluation["match_reasoning"],
                "CV Base64": cv_base64,
                "CV Url": f"gradio_api/file={cv_file.name}"
            }
        )
    #logger.info(f"Evaluation results: {response}")
    evaluation_res = sorted(
        evaluation_res, 
        key=lambda d: d['Match Score'], 
        reverse=True
    )
    return pd.DataFrame.from_records(evaluation_res)


def df_select_callback(df: pd.DataFrame, evt: gr.SelectData):
    selected_row = evt.row_value
    if not selected_row:
        return "No row selected.", ""

    match_score = selected_row[1]  # .get('Match Score', 'N/A')
    match_labels = selected_row[2]  # .get('Match Labels', 'N/A')
    match_reasoning = selected_row[3]  # .get('Match Reasoning', 'N/A')
    
    cv_base64 = selected_row[4]  # .get('CV Base64', '')
    pdf_url = selected_row[5]  # .get('CV Url', '')
    pdf_encoded_url = f"https://agents-mcp-hackathon-airecruiteragent.hf.space/{pdf_url}"
    
    if cv_base64:
        pdf_encoded = gr.HTML(
            """
            <!-- The Modal -->
            <div id="myModal" class="modal" style="display: none;">

            <!-- Modal content -->
            <div class="modal-content">
                <div class="modal-header">
                <span class="close" onclick="hide_pdfviewer()">&times;</span>
                <h2>Candidate CV</h2>
                </div>
                <div class="modal-body">
                
                    <div id="my-pdf" class="pdfobject-container" style="height: 100%">
                    <object data="data:application/pdf;base64,{cv_base64}" type="application/pdf" style="height: 100%; width: 100%;">
                        <iframe src="https://docs.google.com/viewer?url={pdf_encoded_url}&embedded=true" style="height: 100%; width: 100%;" frameborder="0" allow="accelerometer; ambient-light-sensor; autoplay; battery; camera; document-domain; encrypted-media; fullscreen; geolocation; gyroscope; layout-animations; legacy-image-formats; magnetometer; microphone; midi; oversized-images; payment; picture-in-picture; publickey-credentials-get; sync-xhr; usb; vr ; wake-lock; xr-spatial-tracking" sandbox="allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"></iframe>
                    </object>
                    </div>
                </div>
                <div class="modal-footer">
                
                </div>
            </div>

            </div>
            
            """.format(
                title=selected_row[0],
                cv_base64=cv_base64,
                pdf_url=pdf_url, 
                pdf_encoded_url=pdf_encoded_url
            ),
            label="CV PDF Viewer",
            elem_id="pdf_viewer",
            min_height="0px",
            max_height="100%",
            visible=True,
        )
    else:
        pdf_encoded = gr.HTML(
            "", 
            label="CV PDF Viewer",
            elem_id="pdf_viewer", 
            min_height="0px",
            max_height="100%",
            visible=False,
        )

    return match_reasoning, pdf_encoded


head = """
<script src="https://unpkg.com/[email protected]/pdfobject.min.js"></script>
<script type="text/javascript">
    function hide_pdfviewer(){
    document.getElementById('myModal').style.display="none";
    }

    function show_pdfviewer(){
    document.getElementById('myModal').style.display="block";
    }
</script>
"""

css = """
.logo-container {
  width:100%;
  height:auto;
  padding:1%;
}

.logo-img {
  margin-left:2%;
  float:left; 
  height:40px;
  width:40px;    
} 

/* The Modal (background) */
.modal {
  display: none;
  position: fixed; 
  z-index: 1000; /* Sit on top */
  padding-top: 100px; 
  left: 0;
  top: 0;
  width: 100%; 
  height: 100%; 
  overflow: auto; /* Enable scroll if needed */
  background-color: rgb(0,0,0); /* Fallback color */
  background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}

/* Modal Content */
.modal-content {
  position: relative;
  background-color: #fefefe;
  margin: auto;
  padding: 0;
  border: 1px solid #888;
  width: 80%;
  height: 90%;
  overflow: auto;
  box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
  -webkit-animation-name: animatetop;
  -webkit-animation-duration: 0.4s;
  animation-name: animatetop;
  animation-duration: 0.4s
}

/* Add Animation */
@-webkit-keyframes animatetop {
  from {top:-300px; opacity:0} 
  to {top:0; opacity:1}
}

@keyframes animatetop {
  from {top:-300px; opacity:0}
  to {top:0; opacity:1}
}

/* The Close Button */
.close {
  color: white;
  float: right;
  font-size: 28px;
  font-weight: bold;
}

.close:hover,
.close:focus {
  color: #000;
  text-decoration: none;
  cursor: pointer;
}

.modal-header {
  padding: 2px 16px;
  background-color: #5cb85c;
  color: white;
}

.modal-body {
  padding: 2px 16px;
  height: 90%;
}

.modal-footer {
  padding: 2px 16px;
  background-color: #5cb85c;
  color: white;
}
"""



# Create the Gradio interface
with gr.Blocks(head=head, css=css) as demo_app:
    # Title section
    with gr.Row():
        gr.HTML(
            """
            <div style="text-align:center; margin-bottom: 10px;">
            <div class="logo-container" style="display: inline-block; text-align: center;">
                <img src="data:image/png;base64,{logo_base64}" alt="AI Recruiter Agent" style="height: 100px; width: auto; " class="logo-img"> 
                <h1 style="color: #4A90E2; font-size: 2.5em;">
                    AI Recruiter Agent
                </h1>
                <h3>Revolutionizing recruitment with AI-driven insights.</h3>
            </div>
            
            </div>
        """.format(logo_base64=logo_base64)  # Assuming logo_base64 is defined elsewhere
        )

    with gr.Row():
        # Input section for resumes and job description
        with gr.Column(scale=1):
            gr.Markdown("### Upload Resumes and Job Description")
            gr.Markdown(
                "Upload multiple resumes in PDF format and provide the job description text for evaluation."
            )

            cv_files = gr.Files(
                file_count="multiple",
                file_types=[".pdf"],  # , ".docx", ".txt"
                label="Upload Candidate Resume files",
                height="150px",
            )
            job_description = gr.TextArea(
                placeholder="Enter Job Description text here...",
                label="Job Description",
                lines=12,
                max_lines=12,
            )
            with gr.Row():
                # Buttons for starting over and submitting
                start_over_button = gr.Button("Start Over", variant="secondary")
                submit_button = gr.Button("Match Applicants", variant="primary")

        # Output section for evaluation results
        with gr.Column(scale=1):
            gr.Markdown("### Evaluation Results")
            gr.Markdown(
                "Click on the results to view detailed evaluation of each applicant against the job description."
            )
            # Output area for evaluation results
            result_df = gr.DataFrame(
                headers=[
                    "Applicant",
                    "Match Score",
                    "Match Labels",
                ],
                #label="Evaluation Results",
                elem_id="myResult",
                type="pandas",
                interactive=False,
                show_row_numbers=True,
                column_widths=["50%", "25%", "25%", "0%", "0%", "0%"],
                wrap=False,
            )
            # output = gr.JSON(label="Evaluation Results")
            result_detail_textbox = gr.Textbox(
                label="Detailed Evaluation",
                placeholder="Click on a row to see detailed evaluation.",
                lines=8,
                max_lines=8,
            )
            show_pdf_button = gr.Button(
                "Show Selected Candidate CV", 
                variant="secondary", 
                elem_id="show_pdf_button",
            )

            pdf_viewer = gr.HTML(
                "", 
                label="CV PDF Viewer",
                elem_id="pdf_viewer", 
                min_height="0px",
                max_height="100%",
                visible=False,
            )

    # Button click handlers
    def reset_fields():
        return (
            [], "", [], "",  
            gr.HTML(
                "", 
                label="CV PDF Viewer",
                elem_id="pdf_viewer", 
                min_height="0px",
                max_height="100%",
                visible=False,
            )
            
        )

    # Reset function to clear inputs and outputs
    start_over_button.click(
        fn=reset_fields, 
        inputs=None, 
        outputs=[
            cv_files, 
            job_description, 
            result_df, 
            result_detail_textbox,
            pdf_viewer
        ]
    )
    submit_button.click(
        fn=evaluate_applicants, inputs=[cv_files, job_description], outputs=[result_df]
    )
    result_df.select(
        fn=df_select_callback,
        inputs=[result_df],
        outputs=[result_detail_textbox, pdf_viewer],
    )

    show_pdf_button.click(
        fn=lambda: gr.update(visible=True), 
        inputs=None, 
        outputs=pdf_viewer,
        js="show_pdfviewer()",
    )

# Launch the interface and MCP server
if __name__ == "__main__":
    demo_app.launch(
        mcp_server=True,
        allowed_paths=["static"]
    )