mpara commited on
Commit
1c93e67
·
verified ·
1 Parent(s): 0989140

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +169 -0
app.py ADDED
@@ -0,0 +1,169 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # Import libraries
3
+ import gradio as gr
4
+ from transformers import AutoTokenizer, AutoModel
5
+ from sentence_transformers import SentenceTransformer, util
6
+ from pygments import highlight
7
+ from pygments.lexers import guess_lexer, TextLexer
8
+ from pygments.formatters import HtmlFormatter
9
+ import networkx as nx
10
+ import torch
11
+
12
+ # Load the CodeBERT model
13
+ model = SentenceTransformer('microsoft/codebert-base')
14
+
15
+ # Build the knowledge graph
16
+ knowledge_graph = nx.DiGraph()
17
+ concepts = {
18
+ 'Binary Search': ['Arrays', 'Search Algorithms'],
19
+ 'Quick Sort': ['Arrays', 'Sorting Algorithms', 'Recursion'],
20
+ 'Merge Sort': ['Arrays', 'Sorting Algorithms', 'Recursion'],
21
+ 'Dynamic Programming': ['Recursion', 'Algorithms'],
22
+ 'Linked List': ['Pointers', 'Data Structures'],
23
+ 'Hash Tables': ['Arrays', 'Data Structures'],
24
+ 'Graphs': ['Trees', 'Data Structures'],
25
+ 'Trees': ['Recursion', 'Data Structures'],
26
+ 'Recursion': ['Functions', 'Mathematical Induction'],
27
+ 'Object-Oriented Programming': ['Classes', 'Inheritance', 'Polymorphism'],
28
+ 'Concurrency': ['Threads', 'Synchronization'],
29
+ # Add more concepts as needed
30
+ }
31
+
32
+ for concept, prereqs in concepts.items():
33
+ for prereq in prereqs:
34
+ knowledge_graph.add_edge(concept, prereq)
35
+
36
+ # Define functions to get prerequisites
37
+ def get_all_prerequisites(concept):
38
+ return list(nx.descendants(knowledge_graph, concept))
39
+
40
+ # Define the code analysis function
41
+ def analyze_code(code_snippet):
42
+ # Generate code embedding
43
+ code_embedding = model.encode(code_snippet, convert_to_tensor=True)
44
+
45
+ # Prepare concept embeddings
46
+ concept_names = list(knowledge_graph.nodes())
47
+ concept_embeddings = model.encode(concept_names, convert_to_tensor=True)
48
+
49
+ # Compute cosine similarities
50
+ cosine_scores = util.cos_sim(code_embedding, concept_embeddings)[0]
51
+
52
+ # Threshold for concept matching
53
+ threshold = 0.4 # Adjust as needed
54
+ matched_concepts = []
55
+ for i, score in enumerate(cosine_scores):
56
+ if score > threshold:
57
+ matched_concepts.append((concept_names[i], float(score)))
58
+
59
+ if matched_concepts:
60
+ # Sort concepts by similarity score
61
+ matched_concepts.sort(key=lambda x: x[1], reverse=True)
62
+ # Get top concepts
63
+ top_concepts = [concept for concept, score in matched_concepts]
64
+
65
+ return {
66
+ 'concepts': top_concepts
67
+ }
68
+ else:
69
+ return {
70
+ 'concepts': []
71
+ }
72
+
73
+ # Highlight code syntax
74
+ def highlight_code(code_snippet):
75
+ try:
76
+ lexer = guess_lexer(code_snippet)
77
+ except Exception:
78
+ lexer = TextLexer()
79
+ formatter = HtmlFormatter(style='friendly', full=True, noclasses=True)
80
+ highlighted_code = highlight(code_snippet, lexer, formatter)
81
+ return highlighted_code
82
+
83
+ # Learning resources mapping
84
+ resource_mapping = {
85
+ # Main concepts
86
+ 'Binary Search': '[Binary Search Algorithm](https://www.geeksforgeeks.org/binary-search/)',
87
+ 'Quick Sort': '[Quick Sort Algorithm](https://www.tutorialspoint.com/data_structures_algorithms/quick_sort_algorithm.htm)',
88
+ 'Merge Sort': '[Merge Sort Algorithm](https://www.programiz.com/dsa/merge-sort)',
89
+ 'Dynamic Programming': '[Dynamic Programming](https://www.geeksforgeeks.org/dynamic-programming/)',
90
+ 'Linked List': '[Linked Lists](https://www.geeksforgeeks.org/data-structures/linked-list/)',
91
+ 'Hash Tables': '[Hash Tables](https://www.programiz.com/dsa/hash-table)',
92
+ 'Graphs': '[Graph Data Structure](https://www.geeksforgeeks.org/graph-data-structure-and-algorithms/)',
93
+ 'Trees': '[Tree Data Structure](https://www.programiz.com/dsa/trees)',
94
+ 'Object-Oriented Programming': '[OOP Concepts](https://www.javatpoint.com/python-oops-concepts)',
95
+ 'Concurrency': '[Concurrency in Programming](https://www.geeksforgeeks.org/concurrency-in-operating-system/)',
96
+ # Prerequisites
97
+ 'Arrays': '[Arrays Tutorial](https://www.geeksforgeeks.org/arrays-in-c-cpp/)',
98
+ 'Search Algorithms': '[Search Algorithms](https://www.tutorialspoint.com/data_structures_algorithms/search_algorithms.htm)',
99
+ 'Sorting Algorithms': '[Sorting Algorithms](https://www.programiz.com/dsa/sorting-algorithm)',
100
+ 'Recursion': '[Understanding Recursion](https://www.freecodecamp.org/news/recursion-in-programming/)',
101
+ 'Pointers': '[Pointers in C/C++](https://www.geeksforgeeks.org/pointers-in-c-and-c-set-1-introduction-arithmetic-and-array/)',
102
+ 'Data Structures': '[Data Structures Course](https://www.coursera.org/learn/data-structures)',
103
+ 'Algorithms': '[Algorithms Course](https://www.edx.org/course/algorithm-design-and-analysis)',
104
+ 'Functions': '[Functions in Programming](https://www.programiz.com/python-programming/function)',
105
+ 'Mathematical Induction': '[Mathematical Induction](https://brilliant.org/wiki/mathematical-induction/)',
106
+ 'Classes': '[Classes and Objects](https://www.learncpp.com/cpp-tutorial/classes-and-class-members/)',
107
+ 'Inheritance': '[Inheritance in OOP](https://www.javatpoint.com/cpp-inheritance)',
108
+ 'Polymorphism': '[Polymorphism in OOP](https://www.tutorialspoint.com/cplusplus/cpp_polymorphism.htm)',
109
+ 'Threads': '[Multithreading](https://www.geeksforgeeks.org/multithreading-in-cpp/)',
110
+ 'Synchronization': '[Thread Synchronization](https://www.ibm.com/docs/en/zos/2.1.0?topic=programming-thread-synchronization)',
111
+ # Add more resources as needed
112
+ }
113
+
114
+ # Define the main app function
115
+ def CodeIQ_app(code_snippet, high_level_only):
116
+ analysis = analyze_code(code_snippet)
117
+ concepts = analysis['concepts']
118
+
119
+ if not concepts:
120
+ return "No programming concepts identified in the provided code snippet.", "", ""
121
+
122
+ # Get resources
123
+ resources = []
124
+ if high_level_only:
125
+ # Show main concepts as prerequisites
126
+ for concept in concepts:
127
+ resource = resource_mapping.get(concept, 'Resource not available.')
128
+ resources.append(f"- **{concept}**: {resource}")
129
+ else:
130
+ # Show all underlying prerequisites
131
+ prerequisites = set()
132
+ for concept in concepts:
133
+ prereqs = get_all_prerequisites(concept)
134
+ prerequisites.update(prereqs)
135
+ for prereq in prerequisites:
136
+ resource = resource_mapping.get(prereq, 'Resource not available.')
137
+ resources.append(f"- **{prereq}**: {resource}")
138
+
139
+ # Generate highlighted code
140
+ highlighted = highlight_code(code_snippet)
141
+
142
+ # Prepare outputs
143
+ concepts_output = ', '.join(concepts)
144
+ prerequisites_output = '\n'.join(resources)
145
+
146
+ return highlighted, concepts_output, prerequisites_output
147
+
148
+ # Create the Gradio interface
149
+ interface = gr.Interface(
150
+ fn=CodeIQ_app,
151
+ inputs=[
152
+ gr.Textbox(lines=20, label="Code Snippet"),
153
+ gr.Checkbox(label="Show only main concepts as prerequisites", value=True)
154
+ ],
155
+ outputs=[
156
+ gr.HTML(label="Highlighted Code"),
157
+ gr.Textbox(label="Identified Concepts"),
158
+ gr.Markdown(label="Prerequisites and Resources")
159
+ ],
160
+ title="CodeIQ - Advanced Code Prerequisite Analyzer",
161
+ description="""
162
+ **Enter a code snippet to identify programming concepts and prerequisites.**
163
+
164
+ CodeIQ analyzes your code, highlights the syntax, and provides you with the concepts used and the prerequisites needed to understand those concepts. Use the checkbox to select whether you want to see only the main concepts as prerequisites or all underlying prerequisites. You'll also receive curated resources to help you learn.
165
+ """
166
+ )
167
+
168
+ # Launch the app
169
+ interface.launch(debug=True)