Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -57,17 +57,30 @@ class CodeCopilot:
|
|
| 57 |
}
|
| 58 |
return patterns
|
| 59 |
|
| 60 |
-
def generate_suggestions(self, patterns):
|
| 61 |
-
"""Generate suggestions based on detected patterns"""
|
| 62 |
suggestions = []
|
|
|
|
| 63 |
if patterns['function_def'] > 3:
|
| 64 |
-
suggestions.append("π Consider breaking down
|
|
|
|
| 65 |
if patterns['loop'] > 2:
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
if patterns['conditional'] > 3:
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
def process_input(self, user_input):
|
| 72 |
"""Process user input and generate response"""
|
| 73 |
patterns = self.analyze_code_patterns(user_input)
|
|
@@ -84,7 +97,7 @@ class CodeCopilot:
|
|
| 84 |
"""
|
| 85 |
|
| 86 |
response = self.get_blackbox_response(prompt)
|
| 87 |
-
suggestions = self.generate_suggestions(patterns)
|
| 88 |
|
| 89 |
self.chat_history.append((user_input, response))
|
| 90 |
|
|
|
|
| 57 |
}
|
| 58 |
return patterns
|
| 59 |
|
| 60 |
+
def generate_suggestions(self, text, patterns):
|
| 61 |
+
"""Generate suggestions based on detected patterns and actual code"""
|
| 62 |
suggestions = []
|
| 63 |
+
|
| 64 |
if patterns['function_def'] > 3:
|
| 65 |
+
suggestions.append("π Your code has many functions. Consider breaking down responsibilities or using classes to organize them.")
|
| 66 |
+
|
| 67 |
if patterns['loop'] > 2:
|
| 68 |
+
if 'range' in text:
|
| 69 |
+
suggestions.append("π Detected multiple loops using `range`. You can optimize using Python's `enumerate()` or list comprehensions.")
|
| 70 |
+
else:
|
| 71 |
+
suggestions.append("π You might benefit from list comprehensions or using `map`/`filter` functions.")
|
| 72 |
+
|
| 73 |
if patterns['conditional'] > 3:
|
| 74 |
+
if 'elif' in text:
|
| 75 |
+
suggestions.append("β Your code has multiple conditional branches. Consider using a dictionary-based dispatch or polymorphism.")
|
| 76 |
+
else:
|
| 77 |
+
suggestions.append("π§ Simplify conditionals with clearer boolean logic or helper functions.")
|
| 78 |
+
|
| 79 |
+
if not suggestions:
|
| 80 |
+
suggestions.append("β
Your code looks clean! No major structural improvements detected.")
|
| 81 |
+
|
| 82 |
+
return "\n".join(suggestions)
|
| 83 |
+
|
| 84 |
def process_input(self, user_input):
|
| 85 |
"""Process user input and generate response"""
|
| 86 |
patterns = self.analyze_code_patterns(user_input)
|
|
|
|
| 97 |
"""
|
| 98 |
|
| 99 |
response = self.get_blackbox_response(prompt)
|
| 100 |
+
suggestions = self.generate_suggestions(user_input, patterns)
|
| 101 |
|
| 102 |
self.chat_history.append((user_input, response))
|
| 103 |
|