Update app.py
Browse files
app.py
CHANGED
|
@@ -62,22 +62,28 @@ def generate_response(user_input):
|
|
| 62 |
response = chat_session.send_message(user_input)
|
| 63 |
return response.text
|
| 64 |
except Exception as e:
|
| 65 |
-
return f"Error: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
def optimize_code(code):
|
| 68 |
-
#
|
|
|
|
|
|
|
| 69 |
try:
|
| 70 |
-
tree = ast.parse(
|
| 71 |
-
|
| 72 |
-
|
| 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
|
| 81 |
|
| 82 |
# Run pylint for additional suggestions
|
| 83 |
with open("temp_code.py", "w") as file:
|
|
@@ -94,19 +100,6 @@ def fetch_from_github(query):
|
|
| 94 |
return response.json()['items'][:5] # Return top 5 results
|
| 95 |
return []
|
| 96 |
|
| 97 |
-
def interact_with_api(api_url):
|
| 98 |
-
response = requests.get(api_url)
|
| 99 |
-
return response.json()
|
| 100 |
-
|
| 101 |
-
def train_ml_model(code_data):
|
| 102 |
-
df = pd.DataFrame(code_data)
|
| 103 |
-
X = df.drop('target', axis=1)
|
| 104 |
-
y = df['target']
|
| 105 |
-
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
|
| 106 |
-
model = RandomForestClassifier(n_estimators=100, max_depth=10)
|
| 107 |
-
model.fit(X_train, y_train)
|
| 108 |
-
return model
|
| 109 |
-
|
| 110 |
def analyze_code_quality(code):
|
| 111 |
# Tokenize and encode the code
|
| 112 |
inputs = tokenizer(code, return_tensors="pt", truncation=True, max_length=512, padding="max_length")
|
|
@@ -250,14 +243,15 @@ if st.button("Generate Advanced Code"):
|
|
| 250 |
else:
|
| 251 |
with st.spinner("Generating and analyzing code..."):
|
| 252 |
completed_text = generate_response(prompt)
|
| 253 |
-
if "Error" in completed_text:
|
| 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
|
| 260 |
-
st.code(
|
|
|
|
| 261 |
else:
|
| 262 |
quality_score = analyze_code_quality(optimized_code)
|
| 263 |
st.success(f"Code generated and optimized successfully! Quality Score: {quality_score:.2f}")
|
|
|
|
| 62 |
response = chat_session.send_message(user_input)
|
| 63 |
return response.text
|
| 64 |
except Exception as e:
|
| 65 |
+
return f"Error in generating response: {str(e)}"
|
| 66 |
+
|
| 67 |
+
def validate_and_fix_code(code):
|
| 68 |
+
lines = code.split('\n')
|
| 69 |
+
fixed_lines = []
|
| 70 |
+
for line in lines:
|
| 71 |
+
# Check for unterminated string literals
|
| 72 |
+
if line.count('"') % 2 != 0 and line.count("'") % 2 != 0:
|
| 73 |
+
line += '"' # Add a closing quote if needed
|
| 74 |
+
fixed_lines.append(line)
|
| 75 |
+
return '\n'.join(fixed_lines)
|
| 76 |
|
| 77 |
def optimize_code(code):
|
| 78 |
+
# Validate and fix the code first
|
| 79 |
+
fixed_code = validate_and_fix_code(code)
|
| 80 |
+
|
| 81 |
try:
|
| 82 |
+
tree = ast.parse(fixed_code)
|
| 83 |
+
# Placeholder for actual optimization logic
|
| 84 |
+
optimized_code = fixed_code
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
except SyntaxError as e:
|
| 86 |
+
return fixed_code, f"SyntaxError: {str(e)}"
|
| 87 |
|
| 88 |
# Run pylint for additional suggestions
|
| 89 |
with open("temp_code.py", "w") as file:
|
|
|
|
| 100 |
return response.json()['items'][:5] # Return top 5 results
|
| 101 |
return []
|
| 102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
def analyze_code_quality(code):
|
| 104 |
# Tokenize and encode the code
|
| 105 |
inputs = tokenizer(code, return_tensors="pt", truncation=True, max_length=512, padding="max_length")
|
|
|
|
| 243 |
else:
|
| 244 |
with st.spinner("Generating and analyzing code..."):
|
| 245 |
completed_text = generate_response(prompt)
|
| 246 |
+
if "Error in generating response" in completed_text:
|
| 247 |
st.error(completed_text)
|
| 248 |
else:
|
| 249 |
optimized_code, lint_results = optimize_code(completed_text)
|
| 250 |
|
| 251 |
if "SyntaxError" in lint_results:
|
| 252 |
+
st.warning(f"Syntax error detected in the generated code. Attempting to fix...")
|
| 253 |
+
st.code(optimized_code)
|
| 254 |
+
st.info("Please review the code above. It may contain errors or be incomplete.")
|
| 255 |
else:
|
| 256 |
quality_score = analyze_code_quality(optimized_code)
|
| 257 |
st.success(f"Code generated and optimized successfully! Quality Score: {quality_score:.2f}")
|