-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
96 lines (78 loc) · 3.35 KB
/
app.py
File metadata and controls
96 lines (78 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import os
import pandas as pd
from flask import Flask, render_template, request, jsonify
import google.generativeai as genai
app = Flask(__name__)
# --- CONFIGURATION ---
API_KEY = "AIzaSyDY_kZ-RREc_fmq_qoxhBR0jabG7TWFZqU"
genai.configure(api_key=API_KEY)
# stable model use panrom
model = genai.GenerativeModel('gemini-pro')
UPLOAD_FOLDER = 'uploads'
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
# --- GEMINI DETAILED REASONING LOGIC ---
def get_detailed_analysis(status, sensitive_col):
"""Google Gemini vazhiya detailed technical reasoning & steps vaangum"""
if status == "Fair":
return ("No disparity detected.", "The dataset is balanced. Maintain current data collection standards.")
# User-ah attract panna professional prompt
prompt = f"""
Act as an Ethical AI Auditor. Bias has been detected in the '{sensitive_col}' column of a dataset.
1. Give a 1-sentence executive summary of why this is a risk.
2. Provide 3 highly technical mitigation steps (e.g., Reweighing, Disparate Impact Remover).
Be professional and concise.
"""
try:
response = model.generate_content(prompt)
full_text = response.text
# Reasoning-ahyum Mitigation-ahyum split panrom
lines = full_text.split('\n')
reasoning = lines[0] if lines else "Systemic risk detected in sensitive attributes."
mitigation = "\n".join(lines[1:]) if len(lines) > 1 else "Perform re-sampling and fairness auditing."
return reasoning, mitigation
except Exception as e:
# Network error vantha fallback logic (PPT-kku safe)
reasoning = f"Statistical disparity found in {sensitive_col} distribution."
mitigation = "1. Apply AI Fairness 360 Reweighing.\n2. Use Adversarial Debiasing.\n3. Conduct regular disparate impact testing."
return reasoning, mitigation
# --- ROUTES ---
@app.route('/')
def index():
return render_template('index.html', result=None)
@app.route('/analyze', methods=['POST'])
def analyze():
if 'file' not in request.files:
return render_template('index.html', error="No file uploaded")
file = request.files['file']
filepath = os.path.join(UPLOAD_FOLDER, file.filename)
file.save(filepath)
try:
df = pd.read_csv(filepath)
sensitive_columns = ['Gender', 'Race', 'Age', 'Ethnicity', 'Education']
found_col = next((col for col in sensitive_columns if col in df.columns), None)
if found_col:
status = "Systemic Bias Detected"
color = "#ef4444" # Red
score = 35
sensitive_col = found_col
else:
status = "Fair"
color = "#10b981" # Green
score = 98
sensitive_col = "None"
# Gemini kitta irunthu detailed data vaanguroom
reasoning, mitigation = get_detailed_analysis(status, sensitive_col)
res = {
"status": status,
"color": color,
"score": score,
"reason": reasoning,
"alternative": mitigation,
"chart_data": '{"labels": ["Group A", "Group B"], "values": [65, 35]}'
}
return render_template('index.html', result=res)
except Exception as e:
return render_template('index.html', error=f"Error: {str(e)}")
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)