Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -46,49 +46,27 @@ class CodeCopilot:
|
|
| 46 |
except Exception as e:
|
| 47 |
return f"Processing Error: {str(e)}"
|
| 48 |
|
| 49 |
-
import ast
|
| 50 |
-
|
| 51 |
def analyze_code_patterns(self, text):
|
|
|
|
|
|
|
| 52 |
patterns = {
|
| 53 |
-
'function_def':
|
| 54 |
-
'class_def':
|
| 55 |
-
'loop':
|
| 56 |
-
'conditional':
|
| 57 |
}
|
| 58 |
-
try:
|
| 59 |
-
tree = ast.parse(text)
|
| 60 |
-
for node in ast.walk(tree):
|
| 61 |
-
if isinstance(node, ast.FunctionDef):
|
| 62 |
-
patterns['function_def'] += 1
|
| 63 |
-
elif isinstance(node, ast.ClassDef):
|
| 64 |
-
patterns['class_def'] += 1
|
| 65 |
-
elif isinstance(node, (ast.For, ast.While)):
|
| 66 |
-
patterns['loop'] += 1
|
| 67 |
-
elif isinstance(node, ast.If):
|
| 68 |
-
patterns['conditional'] += 1
|
| 69 |
-
except Exception as e:
|
| 70 |
-
print(f"AST parse error: {e}")
|
| 71 |
-
|
| 72 |
return patterns
|
| 73 |
-
|
| 74 |
|
| 75 |
def generate_suggestions(self, patterns):
|
|
|
|
| 76 |
suggestions = []
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
if patterns['
|
| 82 |
-
suggestions.append("
|
| 83 |
-
|
| 84 |
-
if patterns['conditional'] >= 2:
|
| 85 |
-
suggestions.append("β Consider simplifying conditionals using design patterns or helper functions.")
|
| 86 |
-
|
| 87 |
-
if sum(patterns.values()) == 0:
|
| 88 |
-
suggestions.append("π§ No recognizable code patterns found. Try pasting a complete code block.")
|
| 89 |
-
|
| 90 |
-
return "\n".join(suggestions)
|
| 91 |
-
|
| 92 |
|
| 93 |
def process_input(self, user_input):
|
| 94 |
"""Process user input and generate response"""
|
|
@@ -144,7 +122,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="π€ AI Code Copilot") as demo:
|
|
| 144 |
output_text = gr.Markdown()
|
| 145 |
# Display suggestions
|
| 146 |
gr.Markdown("**Suggestions:**")
|
| 147 |
-
|
| 148 |
# Display pattern analysis
|
| 149 |
gr.Markdown("**Pattern Analysis:**")
|
| 150 |
pattern_display = gr.Dataframe(
|
|
@@ -155,7 +133,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="π€ AI Code Copilot") as demo:
|
|
| 155 |
)
|
| 156 |
|
| 157 |
# Processing function for both button and enter
|
| 158 |
-
def
|
| 159 |
response, patterns, sugg = copilot.process_input(user_input)
|
| 160 |
pattern_df = pd.DataFrame({
|
| 161 |
"Pattern": list(patterns.keys()),
|
|
@@ -164,14 +142,14 @@ with gr.Blocks(theme=gr.themes.Soft(), title="π€ AI Code Copilot") as demo:
|
|
| 164 |
return response, sugg, pattern_df
|
| 165 |
|
| 166 |
submit_btn.click(
|
| 167 |
-
fn=
|
| 168 |
inputs=input_text,
|
| 169 |
-
outputs=[output_text,
|
| 170 |
)
|
| 171 |
input_text.submit(
|
| 172 |
-
fn=
|
| 173 |
inputs=input_text,
|
| 174 |
-
outputs=[output_text,
|
| 175 |
)
|
| 176 |
|
| 177 |
if __name__ == "__main__":
|
|
|
|
| 46 |
except Exception as e:
|
| 47 |
return f"Processing Error: {str(e)}"
|
| 48 |
|
|
|
|
|
|
|
| 49 |
def analyze_code_patterns(self, text):
|
| 50 |
+
"""Analyze text for coding patterns"""
|
| 51 |
+
sentences = sent_tokenize(text)
|
| 52 |
patterns = {
|
| 53 |
+
'function_def': sum(1 for s in sentences if 'def ' in s),
|
| 54 |
+
'class_def': sum(1 for s in sentences if 'class ' in s),
|
| 55 |
+
'loop': sum(1 for s in sentences if any(word in s for word in ['for ', 'while ', 'loop'])),
|
| 56 |
+
'conditional': sum(1 for s in sentences if any(word in s for word in ['if ', 'else ', 'elif ']))
|
| 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 into smaller functions or using a class structure.")
|
| 65 |
+
if patterns['loop'] > 2:
|
| 66 |
+
suggestions.append("π You might benefit from list comprehensions or map/filter functions.")
|
| 67 |
+
if patterns['conditional'] > 3:
|
| 68 |
+
suggestions.append("β Complex conditionals might be simplified using polymorphism or strategy pattern.")
|
| 69 |
+
return "\n".join(suggestions) if suggestions else "No specific suggestions at this time."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
def process_input(self, user_input):
|
| 72 |
"""Process user input and generate response"""
|
|
|
|
| 122 |
output_text = gr.Markdown()
|
| 123 |
# Display suggestions
|
| 124 |
gr.Markdown("**Suggestions:**")
|
| 125 |
+
suggestions_output = gr.Markdown()
|
| 126 |
# Display pattern analysis
|
| 127 |
gr.Markdown("**Pattern Analysis:**")
|
| 128 |
pattern_display = gr.Dataframe(
|
|
|
|
| 133 |
)
|
| 134 |
|
| 135 |
# Processing function for both button and enter
|
| 136 |
+
def process_input_wrapper(user_input):
|
| 137 |
response, patterns, sugg = copilot.process_input(user_input)
|
| 138 |
pattern_df = pd.DataFrame({
|
| 139 |
"Pattern": list(patterns.keys()),
|
|
|
|
| 142 |
return response, sugg, pattern_df
|
| 143 |
|
| 144 |
submit_btn.click(
|
| 145 |
+
fn=process_input_wrapper,
|
| 146 |
inputs=input_text,
|
| 147 |
+
outputs=[output_text, suggestions_output, pattern_display]
|
| 148 |
)
|
| 149 |
input_text.submit(
|
| 150 |
+
fn=process_input_wrapper,
|
| 151 |
inputs=input_text,
|
| 152 |
+
outputs=[output_text, suggestions_output, pattern_display]
|
| 153 |
)
|
| 154 |
|
| 155 |
if __name__ == "__main__":
|