pratikshahp commited on
Commit
3290fd4
·
verified ·
1 Parent(s): b2a0fc5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -10
app.py CHANGED
@@ -19,9 +19,12 @@ def process_csv(file):
19
  df["Sentiment"] = df["Feedback"].apply(lambda x: pipe(x)[0]["label"])
20
  return {"df": df}, "✅ CSV processed!"
21
 
22
- def predict_attrition_risk(employee_name: str, sentiment: str):
23
  risk_mapping = {"positive": "Low Risk", "neutral": "Medium Risk", "negative": "High Risk"}
24
- return f"{employee_name}: {risk_mapping.get(sentiment.lower(), 'Unknown Sentiment')}"
 
 
 
25
 
26
  def analyze_attrition_with_llm(df_dict, hr_query):
27
  if df_dict is None or "df" not in df_dict:
@@ -29,38 +32,47 @@ def analyze_attrition_with_llm(df_dict, hr_query):
29
 
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
 
 
19
  df["Sentiment"] = df["Feedback"].apply(lambda x: pipe(x)[0]["label"])
20
  return {"df": df}, "✅ CSV processed!"
21
 
22
+ def predict_attrition_risk(employee_name: str, sentiment: str, explanation: str):
23
  risk_mapping = {"positive": "Low Risk", "neutral": "Medium Risk", "negative": "High Risk"}
24
+ risk_level = risk_mapping.get(sentiment.lower(), "Unknown Sentiment")
25
+
26
+ return f"**{employee_name}**: {risk_level} risk\n\n📝 {explanation}"
27
+
28
 
29
  def analyze_attrition_with_llm(df_dict, hr_query):
30
  if df_dict is None or "df" not in df_dict:
 
32
 
33
  df = df_dict["df"]
34
  employees_data = {row["Employee"].strip(): row["Sentiment"] for _, row in df.iterrows()}
35
+
36
+ # LLM function calling with enhanced prompt
37
+ prompt = f"""
38
+ HR Query: {hr_query}
39
+ Employees Data: {json.dumps(employees_data, indent=2)}
40
+
41
+ Based on the sentiment of employee feedback, predict their attrition risk as High, Medium, or Low.
42
+ Additionally, provide a **short summary (~50 words)** explaining why this risk level was assigned.
43
+ """
44
 
 
 
45
  response = client.chat.completions.create(
46
  model="gpt-4-turbo",
47
  messages=[{"role": "user", "content": prompt}],
48
  functions=[
49
  {
50
  "name": "predict_attrition_risk",
51
+ "description": "Predicts attrition risk based on sentiment and explains the reasoning.",
52
  "parameters": {
53
  "type": "object",
54
  "properties": {
55
  "employee_name": {"type": "string", "description": "Employee's name"},
56
+ "sentiment": {"type": "string", "description": "Extracted sentiment"},
57
+ "explanation": {"type": "string", "description": "Short reasoning for the risk level (50 words)."}
58
  },
59
+ "required": ["employee_name", "sentiment", "explanation"]
60
  }
61
  }
62
  ],
63
  function_call="auto"
64
  )
65
+
66
  message = response.choices[0].message
67
  if message.function_call:
68
  try:
69
  function_call = json.loads(message.function_call.arguments)
70
  employee_name = function_call.get("employee_name")
71
  sentiment = function_call.get("sentiment")
72
+ explanation = function_call.get("explanation")
73
+
74
  if employee_name and sentiment:
75
+ return f"**{employee_name}**: {sentiment} risk\n\n📝 {explanation}"
76
  except Exception as e:
77
  return f"❌ Error processing LLM function call: {str(e)}"
78