Spaces:
Running
Running
| 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) | |
| 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) |