Update app.py
Browse files
app.py
CHANGED
|
@@ -30,19 +30,48 @@ def analyze_attrition_with_llm(df_dict, hr_query):
|
|
| 30 |
df = df_dict["df"]
|
| 31 |
employees_data = {row["Employee"].strip(): row["Sentiment"] for _, row in df.iterrows()}
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
with gr.Blocks() as demo:
|
| 41 |
gr.Markdown("<h1>AI-Driven Employee Attrition Risk Analysis</h1>")
|
| 42 |
file_input = gr.File(label="Upload Employee Feedback CSV", file_types=[".csv"])
|
| 43 |
process_button = gr.Button("Process CSV")
|
| 44 |
process_message = gr.Markdown()
|
| 45 |
-
hr_input = gr.Textbox(label="Employee Name")
|
| 46 |
analyze_button = gr.Button("Check Attrition Risk")
|
| 47 |
output_text = gr.Markdown()
|
| 48 |
df_state = gr.State()
|
|
|
|
| 30 |
df = df_dict["df"]
|
| 31 |
employees_data = {row["Employee"].strip(): row["Sentiment"] for _, row in df.iterrows()}
|
| 32 |
|
| 33 |
+
# LLM function calling
|
| 34 |
+
prompt = f"HR Query: {hr_query}\nEmployees Data: {json.dumps(employees_data, indent=2)}"
|
| 35 |
+
response = client.chat.completions.create(
|
| 36 |
+
model="gpt-4-turbo",
|
| 37 |
+
messages=[{"role": "user", "content": prompt}],
|
| 38 |
+
functions=[
|
| 39 |
+
{
|
| 40 |
+
"name": "predict_attrition_risk",
|
| 41 |
+
"description": "Predicts attrition risk based on sentiment.",
|
| 42 |
+
"parameters": {
|
| 43 |
+
"type": "object",
|
| 44 |
+
"properties": {
|
| 45 |
+
"employee_name": {"type": "string", "description": "Employee's name"},
|
| 46 |
+
"sentiment": {"type": "string", "description": "Extracted sentiment"}
|
| 47 |
+
},
|
| 48 |
+
"required": ["employee_name", "sentiment"]
|
| 49 |
+
}
|
| 50 |
+
}
|
| 51 |
+
],
|
| 52 |
+
function_call="auto"
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
message = response.choices[0].message
|
| 56 |
+
if message.function_call:
|
| 57 |
+
try:
|
| 58 |
+
function_call = json.loads(message.function_call.arguments)
|
| 59 |
+
employee_name = function_call.get("employee_name")
|
| 60 |
+
sentiment = function_call.get("sentiment")
|
| 61 |
+
|
| 62 |
+
if employee_name and sentiment:
|
| 63 |
+
return predict_attrition_risk(employee_name, sentiment)
|
| 64 |
+
except Exception as e:
|
| 65 |
+
return f"❌ Error processing LLM function call: {str(e)}"
|
| 66 |
+
|
| 67 |
+
return "🤖 I'm sorry, but I can only answer queries related to employee attrition risk."
|
| 68 |
|
| 69 |
with gr.Blocks() as demo:
|
| 70 |
gr.Markdown("<h1>AI-Driven Employee Attrition Risk Analysis</h1>")
|
| 71 |
file_input = gr.File(label="Upload Employee Feedback CSV", file_types=[".csv"])
|
| 72 |
process_button = gr.Button("Process CSV")
|
| 73 |
process_message = gr.Markdown()
|
| 74 |
+
hr_input = gr.Textbox(label="Employee Name or HR Query")
|
| 75 |
analyze_button = gr.Button("Check Attrition Risk")
|
| 76 |
output_text = gr.Markdown()
|
| 77 |
df_state = gr.State()
|