iammahmads commited on
Commit
9c51460
Β·
verified Β·
1 Parent(s): 290a7a4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -42
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': 0,
54
- 'class_def': 0,
55
- 'loop': 0,
56
- 'conditional': 0
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
- if patterns['function_def'] >= 1:
79
- suggestions.append("πŸ” Consider organizing related functions into classes.")
80
-
81
- if patterns['loop'] >= 1:
82
- suggestions.append("πŸ”„ Try using list comprehensions or built-in functions like `map()` where appropriate.")
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
- suggestions = gr.Markdown()
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 process_input(user_input):
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=process_input,
168
  inputs=input_text,
169
- outputs=[output_text, suggestions, pattern_display]
170
  )
171
  input_text.submit(
172
- fn=process_input,
173
  inputs=input_text,
174
- outputs=[output_text, suggestions, pattern_display]
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__":