Update app.py
Browse files
app.py
CHANGED
|
@@ -28,17 +28,15 @@ def analyze_attrition_with_llm(df_dict, hr_query):
|
|
| 28 |
return "❌ Error: No processed data. Upload a CSV first."
|
| 29 |
|
| 30 |
df = df_dict["df"]
|
| 31 |
-
employees_data = [
|
| 32 |
-
{"employee_name": row["Employee"], "sentiment": row["Sentiment"], "feedback": row["Feedback"]}
|
| 33 |
-
for _, row in df.iterrows()
|
| 34 |
-
]
|
| 35 |
-
|
| 36 |
-
prompt = f"HR asked: '{hr_query}'. Here is the employee sentiment data:\n{json.dumps(employees_data, indent=2)}\n"
|
| 37 |
-
prompt += "If an employee is not found, return 'No records found for this employee.' If the question is irrelevant, refuse to answer politely."
|
| 38 |
|
|
|
|
| 39 |
response = client.chat.completions.create(
|
| 40 |
model="gpt-4-turbo",
|
| 41 |
-
messages=[
|
|
|
|
|
|
|
|
|
|
| 42 |
functions=[
|
| 43 |
{
|
| 44 |
"name": "predict_attrition_risk",
|
|
@@ -46,8 +44,8 @@ def analyze_attrition_with_llm(df_dict, hr_query):
|
|
| 46 |
"parameters": {
|
| 47 |
"type": "object",
|
| 48 |
"properties": {
|
| 49 |
-
"employee_name": {"type": "string"},
|
| 50 |
-
"sentiment": {"type": "string"}
|
| 51 |
},
|
| 52 |
"required": ["employee_name", "sentiment"]
|
| 53 |
}
|
|
@@ -57,24 +55,20 @@ def analyze_attrition_with_llm(df_dict, hr_query):
|
|
| 57 |
)
|
| 58 |
|
| 59 |
message = response.choices[0].message
|
| 60 |
-
|
| 61 |
-
if hasattr(message, "function_call") and message.function_call:
|
| 62 |
try:
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
return "\n".join(results)
|
| 74 |
-
except Exception:
|
| 75 |
-
return "No records found for this employee."
|
| 76 |
|
| 77 |
-
return "I'm sorry, but I can only answer
|
| 78 |
|
| 79 |
with gr.Blocks() as demo:
|
| 80 |
gr.Markdown("<h1>AI-Driven Employee Attrition Risk Analysis</h1>")
|
|
|
|
| 28 |
return "❌ Error: No processed data. Upload a CSV first."
|
| 29 |
|
| 30 |
df = df_dict["df"]
|
| 31 |
+
employees_data = {row["Employee"].strip(): row["Sentiment"] for _, row in df.iterrows()}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
# Use GPT-4-turbo to analyze the HR query
|
| 34 |
response = client.chat.completions.create(
|
| 35 |
model="gpt-4-turbo",
|
| 36 |
+
messages=[
|
| 37 |
+
{"role": "system", "content": "You are an HR assistant. Only respond to queries about employee attrition risk based on sentiment. If the query is irrelevant, reply with an apology."},
|
| 38 |
+
{"role": "user", "content": hr_query}
|
| 39 |
+
],
|
| 40 |
functions=[
|
| 41 |
{
|
| 42 |
"name": "predict_attrition_risk",
|
|
|
|
| 44 |
"parameters": {
|
| 45 |
"type": "object",
|
| 46 |
"properties": {
|
| 47 |
+
"employee_name": {"type": "string", "description": "Employee's name"},
|
| 48 |
+
"sentiment": {"type": "string", "description": "Extracted sentiment"}
|
| 49 |
},
|
| 50 |
"required": ["employee_name", "sentiment"]
|
| 51 |
}
|
|
|
|
| 55 |
)
|
| 56 |
|
| 57 |
message = response.choices[0].message
|
| 58 |
+
if hasattr(message, "function_call") and message.function_call is not None:
|
|
|
|
| 59 |
try:
|
| 60 |
+
function_call = json.loads(message.function_call.arguments)
|
| 61 |
+
employee_name = function_call.get("employee_name")
|
| 62 |
+
sentiment = employees_data.get(employee_name)
|
| 63 |
+
|
| 64 |
+
if sentiment:
|
| 65 |
+
return predict_attrition_risk(employee_name, sentiment)
|
| 66 |
+
else:
|
| 67 |
+
return f"{employee_name}: No records found for this employee."
|
| 68 |
+
except Exception as e:
|
| 69 |
+
return f"❌ Error processing LLM function call: {str(e)}"
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
+
return "🤖 I'm sorry, but I can only answer queries related to employee attrition risk."
|
| 72 |
|
| 73 |
with gr.Blocks() as demo:
|
| 74 |
gr.Markdown("<h1>AI-Driven Employee Attrition Risk Analysis</h1>")
|