alperugurcan's picture
Create app.py
5ef67ca verified
raw
history blame
2.52 kB
# app.py
import gradio as gr
import pandas as pd
from xgboost import XGBClassifier
import pickle
# Load model
model = pickle.load(open('xgboost_model.pkl', 'rb'))
def predict_defects(loc, complexity, operators, operands, comments):
"""
Simplified prediction using only key metrics
"""
# Create a complete input data with reasonable defaults
input_data = pd.DataFrame([[
loc, # loc
complexity, # v(g)
complexity, # ev(g)
complexity, # iv(g)
operators + operands, # n
operators * operands, # v
0.5, # l (default)
2.0, # d (default)
100, # i (default)
200, # e (default)
0.2, # b (default)
30, # t (default)
loc, # lOCode
comments, # lOComment
loc * 0.1, # lOBlank
comments, # locCodeAndComment
operators, # uniq_Op
operands, # uniq_Opnd
operators * 2, # total_Op
operands * 2, # total_Opnd
complexity # branchCount
]], columns=['loc', 'v(g)', 'ev(g)', 'iv(g)', 'n', 'v',
'l', 'd', 'i', 'e', 'b', 't', 'lOCode',
'lOComment', 'lOBlank', 'locCodeAndComment',
'uniq_Op', 'uniq_Opnd', 'total_Op',
'total_Opnd', 'branchCount'])
# Make prediction
prediction = model.predict_proba(input_data)[0][1]
return f"Defect Probability: {prediction:.2%}"
# Gradio interface
iface = gr.Interface(
fn=predict_defects,
inputs=[
gr.Slider(minimum=1, maximum=1000, value=100,
label="Lines of Code", info="Total number of code lines"),
gr.Slider(minimum=1, maximum=50, value=5,
label="Code Complexity", info="How complex is your code"),
gr.Slider(minimum=1, maximum=100, value=20,
label="Unique Operators", info="Number of different operators (+, -, *, etc.)"),
gr.Slider(minimum=1, maximum=100, value=20,
label="Unique Operands", info="Number of different variables and constants"),
gr.Slider(minimum=0, maximum=500, value=50,
label="Comment Lines", info="Number of comment lines"),
],
outputs="text",
title="Software Defect Predictor",
description="Predict the probability of software defects based on code metrics",
theme="soft"
)
if __name__ == "__main__":
iface.launch()