saif12165 commited on
Commit
24d69a1
·
verified ·
1 Parent(s): 7fde736

updated app.py 2

Browse files
Files changed (1) hide show
  1. app.py +29 -30
app.py CHANGED
@@ -1,4 +1,4 @@
1
- # app.py - CodeLab Stage 3: Semantic Analysis - Enhanced Version
2
  import gradio as gr
3
  import torch
4
  from transformers import AutoTokenizer, AutoModel, T5ForConditionalGeneration, T5Tokenizer
@@ -120,18 +120,17 @@ class SemanticAnalyzer:
120
  except Exception as e:
121
  logger.error(f"❌ Error in CodeT5 analysis: {str(e)}")
122
  return self._fallback_analysis(code)
123
-
124
 
125
  def _fallback_analysis(self, code: str) -> Dict[str, Any]:
126
- """Fallback analysis when AI models fail"""
127
- lines_count = len(code.split('\n'))
128
- return {
129
- 'code_summary': f'Python function with {lines_count} lines',
130
- 'logic_patterns': self.extract_logic_patterns_enhanced(code),
131
- 'approach_analysis': self.analyze_approach_enhanced(code),
132
- 'complexity_analysis': self.analyze_complexity_enhanced(code),
133
- 'semantic_quality': self.assess_semantic_quality(code)
134
- }
135
 
136
  def extract_logic_patterns_enhanced(self, code: str) -> List[str]:
137
  """Enhanced logical pattern extraction"""
@@ -152,10 +151,10 @@ class SemanticAnalyzer:
152
  if 'return' in code: patterns.append('return_statement')
153
 
154
  # Advanced patterns with regex
155
- if re.search(r'for\\s+\\w+\\s+in\\s+range', code): patterns.append('indexed_iteration')
156
- if re.search(r'for\\s+\\w+\\s+in\\s+enumerate', code): patterns.append('indexed_enumeration')
157
- if re.search(r'if\\s+.*[<>]=?.*:', code): patterns.append('comparison_logic')
158
- if re.search(r'\\[.*\\]', code): patterns.append('list_operations')
159
 
160
  # Error handling patterns
161
  if 'try:' in code or 'except' in code: patterns.append('error_handling')
@@ -191,7 +190,7 @@ class SemanticAnalyzer:
191
  # Advanced approaches
192
  elif 'enumerate' in code:
193
  return 'enumerated_iteration_approach'
194
- elif re.search(r'def\\s+\\w+.*def\\s+\\w+', code):
195
  return 'nested_function_approach'
196
  else:
197
  return 'custom_logic_approach'
@@ -214,7 +213,7 @@ class SemanticAnalyzer:
214
  return 'O(1)'
215
 
216
  def estimate_space_complexity(code):
217
- if 'sorted(' in code or re.search(r'\\[.*for.*\\]', code):
218
  return 'O(n)'
219
  elif '[' in code and ']' in code:
220
  return 'O(n)'
@@ -236,7 +235,7 @@ class SemanticAnalyzer:
236
  }
237
 
238
  # Readability assessment
239
- lines = code.split('\\n')
240
  total_score = 10
241
 
242
  # Check for comments or docstrings
@@ -245,7 +244,7 @@ class SemanticAnalyzer:
245
  total_score += 1
246
 
247
  # Check for meaningful variable names
248
- if re.search(r'\\b(max_val|min_val|result|answer|total)\\b', code):
249
  quality_metrics['best_practices'].append('meaningful_variables')
250
  total_score += 1
251
 
@@ -279,7 +278,7 @@ class SemanticAnalyzer:
279
  # Pattern-based solution generation (more reliable than AI generation)
280
  if 'max' in question_lower and 'min' not in question_lower:
281
  return {
282
- 'code': 'def find_max(numbers):\\n """Find maximum value in a list"""\\n if not numbers:\\n return None\\n return max(numbers)',
283
  'explanation': 'Optimal solution using built-in max() function with input validation',
284
  'approach': 'builtin_optimized',
285
  'complexity': {'time': 'O(n)', 'space': 'O(1)'},
@@ -288,7 +287,7 @@ class SemanticAnalyzer:
288
  }
289
  elif 'min' in question_lower and 'max' not in question_lower:
290
  return {
291
- 'code': 'def find_min(numbers):\\n """Find minimum value in a list"""\\n if not numbers:\\n return None\\n return min(numbers)',
292
  'explanation': 'Optimal solution using built-in min() function with input validation',
293
  'approach': 'builtin_optimized',
294
  'complexity': {'time': 'O(n)', 'space': 'O(1)'},
@@ -297,7 +296,7 @@ class SemanticAnalyzer:
297
  }
298
  elif 'sum' in question_lower or 'total' in question_lower:
299
  return {
300
- 'code': 'def calculate_sum(numbers):\\n """Calculate sum of numbers in a list"""\\n return sum(numbers)',
301
  'explanation': 'Optimal solution using built-in sum() function',
302
  'approach': 'builtin_optimized',
303
  'complexity': {'time': 'O(n)', 'space': 'O(1)'},
@@ -357,7 +356,7 @@ class SemanticAnalyzer:
357
  def _template_solution(self, question_text: str) -> Dict[str, Any]:
358
  """Template-based fallback solution"""
359
  return {
360
- 'code': 'def solution(data):\\n """Template solution"""\\n # Implementation needed\\n return data[0] if data else None',
361
  'explanation': 'Template solution - implementation needed based on specific requirements',
362
  'approach': 'template_fallback',
363
  'complexity': 'O(1)',
@@ -475,8 +474,8 @@ class SemanticAnalyzer:
475
  def _clean_code_for_analysis(self, code: str) -> str:
476
  """Clean code for better analysis"""
477
  # Remove excessive whitespace
478
- lines = [line.strip() for line in code.split('\\n') if line.strip()]
479
- return '\\n'.join(lines)
480
 
481
  # Initialize the analyzer (with lazy loading)
482
  analyzer = None
@@ -701,7 +700,7 @@ demo = gr.Interface(
701
  label="Student Code",
702
  placeholder="Enter Python code here...",
703
  lines=12,
704
- value="def find_max(numbers):\\n max_val = numbers[0]\\n for num in numbers:\\n if num > max_val:\\n max_val = num\\n return max_val"
705
  ),
706
  gr.Textbox(
707
  label="Question Text",
@@ -719,24 +718,24 @@ demo = gr.Interface(
719
  lines=25,
720
  show_copy_button=True
721
  ),
722
- title="🧠 CodeLab Semantic Analysis - Stage 3 (Enhanced)",
723
  description="""
724
  Advanced semantic analysis using CodeBERT and CodeT5 models for educational code evaluation.
725
  This system analyzes code semantics, generates optimal solutions, and provides educational insights.
726
  """,
727
  examples=[
728
  [
729
- "def find_max(numbers):\\n return max(numbers)",
730
  "Find the maximum number in a list",
731
  True
732
  ],
733
  [
734
- "def find_min(arr):\\n minimum = arr[0]\\n for i in range(1, len(arr)):\\n if arr[i] < minimum:\\n minimum = arr[i]\\n return minimum",
735
  "Find the minimum number in an array",
736
  True
737
  ],
738
  [
739
- "def calculate_sum(nums):\\n total = 0\\n for num in nums:\\n total += num\\n return total",
740
  "Calculate the sum of all numbers in a list",
741
  True
742
  ]
 
1
+ # app.py - CodeLab Stage 3: Semantic Analysis - Fixed Version
2
  import gradio as gr
3
  import torch
4
  from transformers import AutoTokenizer, AutoModel, T5ForConditionalGeneration, T5Tokenizer
 
120
  except Exception as e:
121
  logger.error(f"❌ Error in CodeT5 analysis: {str(e)}")
122
  return self._fallback_analysis(code)
 
123
 
124
  def _fallback_analysis(self, code: str) -> Dict[str, Any]:
125
+ """Fallback analysis when AI models fail"""
126
+ lines_count = len(code.split('\n'))
127
+ return {
128
+ 'code_summary': f'Python function with {lines_count} lines',
129
+ 'logic_patterns': self.extract_logic_patterns_enhanced(code),
130
+ 'approach_analysis': self.analyze_approach_enhanced(code),
131
+ 'complexity_analysis': self.analyze_complexity_enhanced(code),
132
+ 'semantic_quality': self.assess_semantic_quality(code)
133
+ }
134
 
135
  def extract_logic_patterns_enhanced(self, code: str) -> List[str]:
136
  """Enhanced logical pattern extraction"""
 
151
  if 'return' in code: patterns.append('return_statement')
152
 
153
  # Advanced patterns with regex
154
+ if re.search(r'for\s+\w+\s+in\s+range', code): patterns.append('indexed_iteration')
155
+ if re.search(r'for\s+\w+\s+in\s+enumerate', code): patterns.append('indexed_enumeration')
156
+ if re.search(r'if\s+.*[<>]=?.*:', code): patterns.append('comparison_logic')
157
+ if re.search(r'\[.*\]', code): patterns.append('list_operations')
158
 
159
  # Error handling patterns
160
  if 'try:' in code or 'except' in code: patterns.append('error_handling')
 
190
  # Advanced approaches
191
  elif 'enumerate' in code:
192
  return 'enumerated_iteration_approach'
193
+ elif re.search(r'def\s+\w+.*def\s+\w+', code):
194
  return 'nested_function_approach'
195
  else:
196
  return 'custom_logic_approach'
 
213
  return 'O(1)'
214
 
215
  def estimate_space_complexity(code):
216
+ if 'sorted(' in code or re.search(r'\[.*for.*\]', code):
217
  return 'O(n)'
218
  elif '[' in code and ']' in code:
219
  return 'O(n)'
 
235
  }
236
 
237
  # Readability assessment
238
+ lines = code.split('\n')
239
  total_score = 10
240
 
241
  # Check for comments or docstrings
 
244
  total_score += 1
245
 
246
  # Check for meaningful variable names
247
+ if re.search(r'\b(max_val|min_val|result|answer|total)\b', code):
248
  quality_metrics['best_practices'].append('meaningful_variables')
249
  total_score += 1
250
 
 
278
  # Pattern-based solution generation (more reliable than AI generation)
279
  if 'max' in question_lower and 'min' not in question_lower:
280
  return {
281
+ 'code': 'def find_max(numbers):\n """Find maximum value in a list"""\n if not numbers:\n return None\n return max(numbers)',
282
  'explanation': 'Optimal solution using built-in max() function with input validation',
283
  'approach': 'builtin_optimized',
284
  'complexity': {'time': 'O(n)', 'space': 'O(1)'},
 
287
  }
288
  elif 'min' in question_lower and 'max' not in question_lower:
289
  return {
290
+ 'code': 'def find_min(numbers):\n """Find minimum value in a list"""\n if not numbers:\n return None\n return min(numbers)',
291
  'explanation': 'Optimal solution using built-in min() function with input validation',
292
  'approach': 'builtin_optimized',
293
  'complexity': {'time': 'O(n)', 'space': 'O(1)'},
 
296
  }
297
  elif 'sum' in question_lower or 'total' in question_lower:
298
  return {
299
+ 'code': 'def calculate_sum(numbers):\n """Calculate sum of numbers in a list"""\n return sum(numbers)',
300
  'explanation': 'Optimal solution using built-in sum() function',
301
  'approach': 'builtin_optimized',
302
  'complexity': {'time': 'O(n)', 'space': 'O(1)'},
 
356
  def _template_solution(self, question_text: str) -> Dict[str, Any]:
357
  """Template-based fallback solution"""
358
  return {
359
+ 'code': 'def solution(data):\n """Template solution"""\n # Implementation needed\n return data[0] if data else None',
360
  'explanation': 'Template solution - implementation needed based on specific requirements',
361
  'approach': 'template_fallback',
362
  'complexity': 'O(1)',
 
474
  def _clean_code_for_analysis(self, code: str) -> str:
475
  """Clean code for better analysis"""
476
  # Remove excessive whitespace
477
+ lines = [line.strip() for line in code.split('\n') if line.strip()]
478
+ return '\n'.join(lines)
479
 
480
  # Initialize the analyzer (with lazy loading)
481
  analyzer = None
 
700
  label="Student Code",
701
  placeholder="Enter Python code here...",
702
  lines=12,
703
+ value="def find_max(numbers):\n max_val = numbers[0]\n for num in numbers:\n if num > max_val:\n max_val = num\n return max_val"
704
  ),
705
  gr.Textbox(
706
  label="Question Text",
 
718
  lines=25,
719
  show_copy_button=True
720
  ),
721
+ title="🧠 CodeLab Semantic Analysis - Stage 3 (Fixed)",
722
  description="""
723
  Advanced semantic analysis using CodeBERT and CodeT5 models for educational code evaluation.
724
  This system analyzes code semantics, generates optimal solutions, and provides educational insights.
725
  """,
726
  examples=[
727
  [
728
+ "def find_max(numbers):\n return max(numbers)",
729
  "Find the maximum number in a list",
730
  True
731
  ],
732
  [
733
+ "def find_min(arr):\n minimum = arr[0]\n for i in range(1, len(arr)):\n if arr[i] < minimum:\n minimum = arr[i]\n return minimum",
734
  "Find the minimum number in an array",
735
  True
736
  ],
737
  [
738
+ "def calculate_sum(nums):\n total = 0\n for num in nums:\n total += num\n return total",
739
  "Calculate the sum of all numbers in a list",
740
  True
741
  ]