alperugurcan commited on
Commit
5ef67ca
·
verified ·
1 Parent(s): dd2156e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -0
app.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ import gradio as gr
3
+ import pandas as pd
4
+ from xgboost import XGBClassifier
5
+ import pickle
6
+
7
+ # Load model
8
+ model = pickle.load(open('xgboost_model.pkl', 'rb'))
9
+
10
+ def predict_defects(loc, complexity, operators, operands, comments):
11
+ """
12
+ Simplified prediction using only key metrics
13
+ """
14
+ # Create a complete input data with reasonable defaults
15
+ input_data = pd.DataFrame([[
16
+ loc, # loc
17
+ complexity, # v(g)
18
+ complexity, # ev(g)
19
+ complexity, # iv(g)
20
+ operators + operands, # n
21
+ operators * operands, # v
22
+ 0.5, # l (default)
23
+ 2.0, # d (default)
24
+ 100, # i (default)
25
+ 200, # e (default)
26
+ 0.2, # b (default)
27
+ 30, # t (default)
28
+ loc, # lOCode
29
+ comments, # lOComment
30
+ loc * 0.1, # lOBlank
31
+ comments, # locCodeAndComment
32
+ operators, # uniq_Op
33
+ operands, # uniq_Opnd
34
+ operators * 2, # total_Op
35
+ operands * 2, # total_Opnd
36
+ complexity # branchCount
37
+ ]], columns=['loc', 'v(g)', 'ev(g)', 'iv(g)', 'n', 'v',
38
+ 'l', 'd', 'i', 'e', 'b', 't', 'lOCode',
39
+ 'lOComment', 'lOBlank', 'locCodeAndComment',
40
+ 'uniq_Op', 'uniq_Opnd', 'total_Op',
41
+ 'total_Opnd', 'branchCount'])
42
+
43
+ # Make prediction
44
+ prediction = model.predict_proba(input_data)[0][1]
45
+
46
+ return f"Defect Probability: {prediction:.2%}"
47
+
48
+ # Gradio interface
49
+ iface = gr.Interface(
50
+ fn=predict_defects,
51
+ inputs=[
52
+ gr.Slider(minimum=1, maximum=1000, value=100,
53
+ label="Lines of Code", info="Total number of code lines"),
54
+ gr.Slider(minimum=1, maximum=50, value=5,
55
+ label="Code Complexity", info="How complex is your code"),
56
+ gr.Slider(minimum=1, maximum=100, value=20,
57
+ label="Unique Operators", info="Number of different operators (+, -, *, etc.)"),
58
+ gr.Slider(minimum=1, maximum=100, value=20,
59
+ label="Unique Operands", info="Number of different variables and constants"),
60
+ gr.Slider(minimum=0, maximum=500, value=50,
61
+ label="Comment Lines", info="Number of comment lines"),
62
+ ],
63
+ outputs="text",
64
+ title="Software Defect Predictor",
65
+ description="Predict the probability of software defects based on code metrics",
66
+ theme="soft"
67
+ )
68
+
69
+ if __name__ == "__main__":
70
+ iface.launch()