Spaces:
Running
Running
File size: 2,571 Bytes
ff601d3 cfeaec7 ff601d3 1d144f7 cfeaec7 ff601d3 cfeaec7 ff601d3 cfeaec7 1d144f7 cfeaec7 1d144f7 cfeaec7 ff601d3 cfeaec7 1d144f7 ff601d3 1d144f7 cfeaec7 1d144f7 cfeaec7 1d144f7 ff601d3 cfeaec7 ff601d3 1d144f7 ff601d3 cfeaec7 ff601d3 1d144f7 ff601d3 1d144f7 ff601d3 1d144f7 ff601d3 1d144f7 ff601d3 1d144f7 ff601d3 1d144f7 ff601d3 1d144f7 ff601d3 1d144f7 ff601d3 1d144f7 ff601d3 1d144f7 ff601d3 1d144f7 ff601d3 1d144f7 ff601d3 1d144f7 ff601d3 1d144f7 ff601d3 1d144f7 ff601d3 1d144f7 ff601d3 1d144f7 ff601d3 cfeaec7 1d144f7 ff601d3 1d144f7 ff601d3 1d144f7 ff601d3 1d144f7 ff601d3 1d144f7 cfeaec7 ff601d3 1d144f7 | 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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | from dash import Dash, html, dcc, Input, Output
import plotly.graph_objects as go
from radon.complexity import cc_visit
import dash_bootstrap_components as dbc
app = Dash(__name__, external_stylesheets=[dbc.themes.CYBORG])
server = app.server
def analyze_code(code):
if not code:
return 0,"Paste code to analyze",0
try:
results = cc_visit(code)
complexity = sum([r.complexity for r in results])
except:
complexity = 1
if complexity == 0:
complexity = 1
quality = max(100-(complexity*4),40)
if complexity > 15:
suggestion = "Code is highly complex. Reduce nested loops and split functions."
elif complexity > 8:
suggestion = "Moderate complexity. Try simplifying logic."
else:
suggestion = "Code quality looks good."
return quality, suggestion, complexity
def create_gauge(score):
fig = go.Figure(go.Indicator(
mode="gauge+number",
value=score,
title={'text':"Quality Score"},
gauge={
'axis':{'range':[0,100]},
'bar':{'color':"lime"},
'steps':[
{'range':[0,50],'color':"red"},
{'range':[50,75],'color':"orange"},
{'range':[75,100],'color':"green"}
]
}
))
fig.update_layout(template="plotly_dark")
return fig
app.layout = dbc.Container([
html.H1("AI Code Quality Analyzer Dashboard",
style={'textAlign':'center','marginBottom':'30px'}),
dbc.Row([
dbc.Col([
html.H4("Enter Python Code"),
dcc.Textarea(
id="code_input",
placeholder="Paste your Python code here...",
style={
'width':'100%',
'height':'350px',
'backgroundColor':'#111',
'color':'white',
'border':'1px solid gray'
}
),
html.Br(),
dbc.Button(
"Analyze Code",
id="analyze_btn",
color="primary",
style={'width':'100%'}
)
], width=6),
dbc.Col([
html.H4("Analysis Results"),
dbc.Card([
dbc.CardBody([
html.H5(id="suggestion"),
html.Br(),
dcc.Graph(id="quality_graph"),
html.H5(id="complexity_score")
])
])
], width=6)
])
], fluid=True)
@app.callback(
Output("suggestion","children"),
Output("quality_graph","figure"),
Output("complexity_score","children"),
Input("analyze_btn","n_clicks"),
Input("code_input","value")
)
def update_output(n_clicks, code):
quality, suggestion, complexity = analyze_code(code)
fig = create_gauge(quality)
return (
"AI Suggestion: " + suggestion,
fig,
"Code Complexity Score: " + str(complexity)
)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860) |