pratikshahp commited on
Commit
cbb6156
·
verified ·
1 Parent(s): 2c0698c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -0
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 = {row["Employee"].strip(): row["Sentiment"] for _, row in df.iterrows()}
32
+
33
+ employee_name = hr_query.strip()
34
+ sentiment = employees_data.get(employee_name)
35
+ if sentiment:
36
+ return predict_attrition_risk(employee_name, sentiment)
37
+ else:
38
+ return f"{employee_name}: No records found for this employee."
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()
49
+
50
+ process_button.click(process_csv, inputs=file_input, outputs=[df_state, process_message])
51
+ analyze_button.click(analyze_attrition_with_llm, inputs=[df_state, hr_input], outputs=output_text)
52
+
53
+ demo.launch(share=True)