Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from openai import OpenAI
|
| 6 |
+
from transformers import pipeline
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
|
| 9 |
+
load_dotenv()
|
| 10 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
| 11 |
+
client = OpenAI(api_key=api_key)
|
| 12 |
+
|
| 13 |
+
pipe = pipeline("text-classification", model="cardiffnlp/twitter-roberta-base-sentiment-latest")
|
| 14 |
+
|
| 15 |
+
def process_csv(file):
|
| 16 |
+
df = pd.read_csv(file)
|
| 17 |
+
if "Feedback" not in df.columns or "Employee" not in df.columns:
|
| 18 |
+
return None, "❌ Error: CSV must contain 'Employee' and 'Feedback' columns."
|
| 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:
|
| 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=[{"role": "user", "content": prompt}],
|
| 42 |
+
functions=[
|
| 43 |
+
{
|
| 44 |
+
"name": "predict_attrition_risk",
|
| 45 |
+
"description": "Predicts attrition risk based on sentiment.",
|
| 46 |
+
"parameters": {
|
| 47 |
+
"type": "object",
|
| 48 |
+
"properties": {
|
| 49 |
+
"employee_name": {"type": "string"},
|
| 50 |
+
"sentiment": {"type": "string"}
|
| 51 |
+
},
|
| 52 |
+
"required": ["employee_name", "sentiment"]
|
| 53 |
+
}
|
| 54 |
+
}
|
| 55 |
+
],
|
| 56 |
+
function_call="auto"
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
message = response.choices[0].message
|
| 60 |
+
|
| 61 |
+
if hasattr(message, "function_call") and message.function_call:
|
| 62 |
+
try:
|
| 63 |
+
function_calls = json.loads(message.function_call.arguments)
|
| 64 |
+
results = []
|
| 65 |
+
for call in function_calls if isinstance(function_calls, list) else [function_calls]:
|
| 66 |
+
employee_name = call.get("employee_name")
|
| 67 |
+
sentiment = call.get("sentiment")
|
| 68 |
+
|
| 69 |
+
if not any(emp["employee_name"] == employee_name for emp in employees_data):
|
| 70 |
+
return "No records found for this employee."
|
| 71 |
+
|
| 72 |
+
results.append(predict_attrition_risk(employee_name, sentiment))
|
| 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 HR-related questions about employee sentiment and attrition risk."
|
| 78 |
+
|
| 79 |
+
with gr.Blocks() as demo:
|
| 80 |
+
gr.Markdown("<h1>AI-Driven Employee Attrition Risk Analysis</h1>")
|
| 81 |
+
file_input = gr.File(label="Upload Employee Feedback CSV", file_types=[".csv"])
|
| 82 |
+
process_button = gr.Button("Process CSV")
|
| 83 |
+
process_message = gr.Markdown()
|
| 84 |
+
hr_input = gr.Textbox(label="HR Query")
|
| 85 |
+
analyze_button = gr.Button("Ask HR Query")
|
| 86 |
+
output_text = gr.Markdown()
|
| 87 |
+
df_state = gr.State()
|
| 88 |
+
|
| 89 |
+
process_button.click(process_csv, inputs=file_input, outputs=[df_state, process_message])
|
| 90 |
+
analyze_button.click(analyze_attrition_with_llm, inputs=[df_state, hr_input], outputs=output_text)
|
| 91 |
+
|
| 92 |
+
demo.launch(share=True)
|