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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -26
app.py CHANGED
@@ -23,7 +23,7 @@ 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):
@@ -32,29 +32,21 @@ def analyze_attrition_with_llm(df_dict, hr_query):
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
  }
@@ -63,20 +55,9 @@ def analyze_attrition_with_llm(df_dict, hr_query):
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
-
79
- return "🤖 I'm sorry, but I can only answer queries related to employee attrition risk."
80
 
81
  with gr.Blocks() as demo:
82
  gr.Markdown("<h1>AI-Driven Employee Attrition Risk Analysis</h1>")
 
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} \n\n📝 {explanation}"
27
 
28
 
29
  def analyze_attrition_with_llm(df_dict, hr_query):
 
32
 
33
  df = df_dict["df"]
34
  employees_data = {row["Employee"].strip(): row["Sentiment"] for _, row in df.iterrows()}
 
 
 
 
 
 
 
 
 
35
 
36
+ prompt = f"HR Query: {hr_query}\nEmployees Data: {json.dumps(employees_data, indent=2)}"
37
  response = client.chat.completions.create(
38
+ model="gpt-4o-mini",
39
  messages=[{"role": "user", "content": prompt}],
40
  functions=[
41
  {
42
  "name": "predict_attrition_risk",
43
+ "description": "Predicts attrition risk based on sentiment and provides explanation.",
44
  "parameters": {
45
  "type": "object",
46
  "properties": {
47
  "employee_name": {"type": "string", "description": "Employee's name"},
48
  "sentiment": {"type": "string", "description": "Extracted sentiment"},
49
+ "explanation": {"type": "string", "description": "Brief explanation"}
50
  },
51
  "required": ["employee_name", "sentiment", "explanation"]
52
  }
 
55
  function_call="auto"
56
  )
57
 
58
+ # Directly return the full response as a formatted string
59
+ return response.choices[0].message.content if response.choices[0].message.content else "🤖 No response generated."
 
 
 
 
 
60
 
 
 
 
 
 
 
61
 
62
  with gr.Blocks() as demo:
63
  gr.Markdown("<h1>AI-Driven Employee Attrition Risk Analysis</h1>")