Artificial-superintelligence commited on
Commit
ae1ac19
Β·
verified Β·
1 Parent(s): e8d028b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +149 -51
app.py CHANGED
@@ -3,7 +3,6 @@ import google.generativeai as genai
3
  import requests
4
  import subprocess
5
  import os
6
- import pylint
7
  import pandas as pd
8
  import numpy as np
9
  from sklearn.model_selection import train_test_split
@@ -67,15 +66,18 @@ def generate_response(user_input):
67
 
68
  def optimize_code(code):
69
  # Use abstract syntax tree for advanced code analysis
70
- tree = ast.parse(code)
71
- analyzer = CodeAnalyzer()
72
- analyzer.visit(tree)
73
-
74
- # Apply code transformations based on analysis
75
- transformer = CodeTransformer(analyzer.get_optimizations())
76
- optimized_tree = transformer.visit(tree)
77
-
78
- optimized_code = ast.unparse(optimized_tree)
 
 
 
79
 
80
  # Run pylint for additional suggestions
81
  with open("temp_code.py", "w") as file:
@@ -122,31 +124,119 @@ def analyze_code_quality(code):
122
  return prediction.item() # Return the probability of needing improvement
123
 
124
  def visualize_code_structure(code):
125
- tree = ast.parse(code)
126
- graph = nx.DiGraph()
127
-
128
- def add_nodes_edges(node, parent=None):
129
- node_id = id(node)
130
- graph.add_node(node_id, label=type(node).__name__)
131
- if parent:
132
- graph.add_edge(id(parent), node_id)
133
- for child in ast.iter_child_nodes(node):
134
- add_nodes_edges(child, node)
135
-
136
- add_nodes_edges(tree)
137
-
138
- plt.figure(figsize=(12, 8))
139
- pos = nx.spring_layout(graph)
140
- nx.draw(graph, pos, with_labels=True, node_color='lightblue', node_size=1000, font_size=8, font_weight='bold')
141
- labels = nx.get_node_attributes(graph, 'label')
142
- nx.draw_networkx_labels(graph, pos, labels, font_size=6)
143
-
144
- return plt
 
 
 
145
 
146
  # Streamlit UI setup
147
  st.set_page_config(page_title="Advanced AI Code Assistant", page_icon="πŸš€", layout="wide")
148
 
149
- # ... (keep the existing CSS styles) ...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
 
151
  st.markdown('<div class="main-container">', unsafe_allow_html=True)
152
  st.title("πŸš€ Advanced AI Code Assistant")
@@ -164,27 +254,35 @@ if st.button("Generate Advanced Code"):
164
  st.error(completed_text)
165
  else:
166
  optimized_code, lint_results = optimize_code(completed_text)
167
- quality_score = analyze_code_quality(optimized_code)
168
-
169
- st.success(f"Code generated and optimized successfully! Quality Score: {quality_score:.2f}")
170
-
171
- st.markdown('<div class="output-container">', unsafe_allow_html=True)
172
- st.markdown('<div class="code-block">', unsafe_allow_html=True)
173
- st.code(optimized_code)
174
- st.markdown('</div>', unsafe_allow_html=True)
175
-
176
- with st.expander("View Code Structure Visualization"):
177
- st.pyplot(visualize_code_structure(optimized_code))
178
-
179
- with st.expander("View Lint Results"):
180
- st.text(lint_results)
181
-
182
- with st.expander("Fetch Similar Code from GitHub"):
183
- github_results = fetch_from_github(prompt)
184
- for item in github_results:
185
- st.markdown(f"[{item['name']}]({item['html_url']})")
186
 
187
- st.markdown('</div>', unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
188
 
189
  st.markdown("""
190
  <div style='text-align: center; margin-top: 2rem; color: #4a5568;'>
 
3
  import requests
4
  import subprocess
5
  import os
 
6
  import pandas as pd
7
  import numpy as np
8
  from sklearn.model_selection import train_test_split
 
66
 
67
  def optimize_code(code):
68
  # Use abstract syntax tree for advanced code analysis
69
+ try:
70
+ tree = ast.parse(code)
71
+ analyzer = CodeAnalyzer()
72
+ analyzer.visit(tree)
73
+
74
+ # Apply code transformations based on analysis
75
+ transformer = CodeTransformer(analyzer.get_optimizations())
76
+ optimized_tree = transformer.visit(tree)
77
+
78
+ optimized_code = ast.unparse(optimized_tree)
79
+ except SyntaxError as e:
80
+ return code, f"SyntaxError: {str(e)}"
81
 
82
  # Run pylint for additional suggestions
83
  with open("temp_code.py", "w") as file:
 
124
  return prediction.item() # Return the probability of needing improvement
125
 
126
  def visualize_code_structure(code):
127
+ try:
128
+ tree = ast.parse(code)
129
+ graph = nx.DiGraph()
130
+
131
+ def add_nodes_edges(node, parent=None):
132
+ node_id = id(node)
133
+ graph.add_node(node_id, label=type(node).__name__)
134
+ if parent:
135
+ graph.add_edge(id(parent), node_id)
136
+ for child in ast.iter_child_nodes(node):
137
+ add_nodes_edges(child, node)
138
+
139
+ add_nodes_edges(tree)
140
+
141
+ plt.figure(figsize=(12, 8))
142
+ pos = nx.spring_layout(graph)
143
+ nx.draw(graph, pos, with_labels=True, node_color='lightblue', node_size=1000, font_size=8, font_weight='bold')
144
+ labels = nx.get_node_attributes(graph, 'label')
145
+ nx.draw_networkx_labels(graph, pos, labels, font_size=6)
146
+
147
+ return plt
148
+ except SyntaxError:
149
+ return None
150
 
151
  # Streamlit UI setup
152
  st.set_page_config(page_title="Advanced AI Code Assistant", page_icon="πŸš€", layout="wide")
153
 
154
+ st.markdown("""
155
+ <style>
156
+ @import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;600;700&display=swap');
157
+
158
+ body {
159
+ font-family: 'Inter', sans-serif;
160
+ background-color: #f0f4f8;
161
+ color: #1a202c;
162
+ }
163
+ .stApp {
164
+ max-width: 1200px;
165
+ margin: 0 auto;
166
+ padding: 2rem;
167
+ }
168
+ .main-container {
169
+ background: #ffffff;
170
+ border-radius: 16px;
171
+ padding: 2rem;
172
+ box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
173
+ }
174
+ h1 {
175
+ font-size: 2.5rem;
176
+ font-weight: 700;
177
+ color: #2d3748;
178
+ text-align: center;
179
+ margin-bottom: 1rem;
180
+ }
181
+ .subtitle {
182
+ font-size: 1.1rem;
183
+ text-align: center;
184
+ color: #4a5568;
185
+ margin-bottom: 2rem;
186
+ }
187
+ .stTextArea textarea {
188
+ border: 2px solid #e2e8f0;
189
+ border-radius: 8px;
190
+ font-size: 1rem;
191
+ padding: 0.75rem;
192
+ transition: all 0.3s ease;
193
+ }
194
+ .stTextArea textarea:focus {
195
+ border-color: #4299e1;
196
+ box-shadow: 0 0 0 3px rgba(66, 153, 225, 0.5);
197
+ }
198
+ .stButton button {
199
+ background-color: #4299e1;
200
+ color: white;
201
+ border: none;
202
+ border-radius: 8px;
203
+ font-size: 1.1rem;
204
+ font-weight: 600;
205
+ padding: 0.75rem 2rem;
206
+ transition: all 0.3s ease;
207
+ width: 100%;
208
+ }
209
+ .stButton button:hover {
210
+ background-color: #3182ce;
211
+ }
212
+ .output-container {
213
+ background: #f7fafc;
214
+ border-radius: 8px;
215
+ padding: 1rem;
216
+ margin-top: 2rem;
217
+ }
218
+ .code-block {
219
+ background-color: #2d3748;
220
+ color: #e2e8f0;
221
+ font-family: 'Fira Code', monospace;
222
+ font-size: 0.9rem;
223
+ border-radius: 8px;
224
+ padding: 1rem;
225
+ margin-top: 1rem;
226
+ overflow-x: auto;
227
+ }
228
+ .stAlert {
229
+ background-color: #ebf8ff;
230
+ color: #2b6cb0;
231
+ border-radius: 8px;
232
+ border: none;
233
+ padding: 0.75rem 1rem;
234
+ }
235
+ .stSpinner {
236
+ color: #4299e1;
237
+ }
238
+ </style>
239
+ """, unsafe_allow_html=True)
240
 
241
  st.markdown('<div class="main-container">', unsafe_allow_html=True)
242
  st.title("πŸš€ Advanced AI Code Assistant")
 
254
  st.error(completed_text)
255
  else:
256
  optimized_code, lint_results = optimize_code(completed_text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
257
 
258
+ if "SyntaxError" in lint_results:
259
+ st.warning(f"Syntax error detected: {lint_results}")
260
+ st.code(completed_text)
261
+ else:
262
+ quality_score = analyze_code_quality(optimized_code)
263
+ st.success(f"Code generated and optimized successfully! Quality Score: {quality_score:.2f}")
264
+
265
+ st.markdown('<div class="output-container">', unsafe_allow_html=True)
266
+ st.markdown('<div class="code-block">', unsafe_allow_html=True)
267
+ st.code(optimized_code)
268
+ st.markdown('</div>', unsafe_allow_html=True)
269
+
270
+ visualization = visualize_code_structure(optimized_code)
271
+ if visualization:
272
+ with st.expander("View Code Structure Visualization"):
273
+ st.pyplot(visualization)
274
+ else:
275
+ st.warning("Unable to generate code structure visualization due to syntax errors.")
276
+
277
+ with st.expander("View Lint Results"):
278
+ st.text(lint_results)
279
+
280
+ with st.expander("Fetch Similar Code from GitHub"):
281
+ github_results = fetch_from_github(prompt)
282
+ for item in github_results:
283
+ st.markdown(f"[{item['name']}]({item['html_url']})")
284
+
285
+ st.markdown('</div>', unsafe_allow_html=True)
286
 
287
  st.markdown("""
288
  <div style='text-align: center; margin-top: 2rem; color: #4a5568;'>