# Import libraries import gradio as gr from transformers import AutoTokenizer, AutoModel from sentence_transformers import SentenceTransformer, util from pygments import highlight from pygments.lexers import guess_lexer, TextLexer from pygments.formatters import HtmlFormatter import networkx as nx import torch # Load the CodeBERT model model = SentenceTransformer('microsoft/codebert-base') # Build the knowledge graph knowledge_graph = nx.DiGraph() concepts = { 'Binary Search': ['Arrays', 'Search Algorithms'], 'Quick Sort': ['Arrays', 'Sorting Algorithms', 'Recursion'], 'Merge Sort': ['Arrays', 'Sorting Algorithms', 'Recursion'], 'Dynamic Programming': ['Recursion', 'Algorithms'], 'Linked List': ['Pointers', 'Data Structures'], 'Hash Tables': ['Arrays', 'Data Structures'], 'Graphs': ['Trees', 'Data Structures'], 'Trees': ['Recursion', 'Data Structures'], 'Recursion': ['Functions', 'Mathematical Induction'], 'Object-Oriented Programming': ['Classes', 'Inheritance', 'Polymorphism'], 'Concurrency': ['Threads', 'Synchronization'], # Add more concepts as needed } for concept, prereqs in concepts.items(): for prereq in prereqs: knowledge_graph.add_edge(concept, prereq) # Define functions to get prerequisites def get_all_prerequisites(concept): return list(nx.descendants(knowledge_graph, concept)) # Define the code analysis function def analyze_code(code_snippet): # Generate code embedding code_embedding = model.encode(code_snippet, convert_to_tensor=True) # Prepare concept embeddings concept_names = list(knowledge_graph.nodes()) concept_embeddings = model.encode(concept_names, convert_to_tensor=True) # Compute cosine similarities cosine_scores = util.cos_sim(code_embedding, concept_embeddings)[0] # Threshold for concept matching threshold = 0.4 # Adjust as needed matched_concepts = [] for i, score in enumerate(cosine_scores): if score > threshold: matched_concepts.append((concept_names[i], float(score))) if matched_concepts: # Sort concepts by similarity score matched_concepts.sort(key=lambda x: x[1], reverse=True) # Get top concepts top_concepts = [concept for concept, score in matched_concepts] return { 'concepts': top_concepts } else: return { 'concepts': [] } # Highlight code syntax def highlight_code(code_snippet): try: lexer = guess_lexer(code_snippet) except Exception: lexer = TextLexer() formatter = HtmlFormatter(style='friendly', full=True, noclasses=True) highlighted_code = highlight(code_snippet, lexer, formatter) return highlighted_code # Learning resources mapping resource_mapping = { # Main concepts 'Binary Search': '[Binary Search Algorithm](https://www.geeksforgeeks.org/binary-search/)', 'Quick Sort': '[Quick Sort Algorithm](https://www.tutorialspoint.com/data_structures_algorithms/quick_sort_algorithm.htm)', 'Merge Sort': '[Merge Sort Algorithm](https://www.programiz.com/dsa/merge-sort)', 'Dynamic Programming': '[Dynamic Programming](https://www.geeksforgeeks.org/dynamic-programming/)', 'Linked List': '[Linked Lists](https://www.geeksforgeeks.org/data-structures/linked-list/)', 'Hash Tables': '[Hash Tables](https://www.programiz.com/dsa/hash-table)', 'Graphs': '[Graph Data Structure](https://www.geeksforgeeks.org/graph-data-structure-and-algorithms/)', 'Trees': '[Tree Data Structure](https://www.programiz.com/dsa/trees)', 'Object-Oriented Programming': '[OOP Concepts](https://www.javatpoint.com/python-oops-concepts)', 'Concurrency': '[Concurrency in Programming](https://www.geeksforgeeks.org/concurrency-in-operating-system/)', # Prerequisites 'Arrays': '[Arrays Tutorial](https://www.geeksforgeeks.org/arrays-in-c-cpp/)', 'Search Algorithms': '[Search Algorithms](https://www.tutorialspoint.com/data_structures_algorithms/search_algorithms.htm)', 'Sorting Algorithms': '[Sorting Algorithms](https://www.programiz.com/dsa/sorting-algorithm)', 'Recursion': '[Understanding Recursion](https://www.freecodecamp.org/news/recursion-in-programming/)', 'Pointers': '[Pointers in C/C++](https://www.geeksforgeeks.org/pointers-in-c-and-c-set-1-introduction-arithmetic-and-array/)', 'Data Structures': '[Data Structures Course](https://www.coursera.org/learn/data-structures)', 'Algorithms': '[Algorithms Course](https://www.edx.org/course/algorithm-design-and-analysis)', 'Functions': '[Functions in Programming](https://www.programiz.com/python-programming/function)', 'Mathematical Induction': '[Mathematical Induction](https://brilliant.org/wiki/mathematical-induction/)', 'Classes': '[Classes and Objects](https://www.learncpp.com/cpp-tutorial/classes-and-class-members/)', 'Inheritance': '[Inheritance in OOP](https://www.javatpoint.com/cpp-inheritance)', 'Polymorphism': '[Polymorphism in OOP](https://www.tutorialspoint.com/cplusplus/cpp_polymorphism.htm)', 'Threads': '[Multithreading](https://www.geeksforgeeks.org/multithreading-in-cpp/)', 'Synchronization': '[Thread Synchronization](https://www.ibm.com/docs/en/zos/2.1.0?topic=programming-thread-synchronization)', # Add more resources as needed } # Define the main app function def CodeIQ_app(code_snippet, high_level_only): analysis = analyze_code(code_snippet) concepts = analysis['concepts'] if not concepts: return "No programming concepts identified in the provided code snippet.", "", "" # Get resources resources = [] if high_level_only: # Show main concepts as prerequisites for concept in concepts: resource = resource_mapping.get(concept, 'Resource not available.') resources.append(f"- **{concept}**: {resource}") else: # Show all underlying prerequisites prerequisites = set() for concept in concepts: prereqs = get_all_prerequisites(concept) prerequisites.update(prereqs) for prereq in prerequisites: resource = resource_mapping.get(prereq, 'Resource not available.') resources.append(f"- **{prereq}**: {resource}") # Generate highlighted code highlighted = highlight_code(code_snippet) # Prepare outputs concepts_output = ', '.join(concepts) prerequisites_output = '\n'.join(resources) return highlighted, concepts_output, prerequisites_output # Create the Gradio interface interface = gr.Interface( fn=CodeIQ_app, inputs=[ gr.Textbox(lines=20, label="Code Snippet"), gr.Checkbox(label="Show only main concepts as prerequisites", value=True) ], outputs=[ gr.HTML(label="Highlighted Code"), gr.Textbox(label="Identified Concepts"), gr.Markdown(label="Prerequisites and Resources") ], title="CodeIQ - Advanced Code Prerequisite Analyzer", description=""" **Enter a code snippet to identify programming concepts and prerequisites.** 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. """ ) # Launch the app interface.launch(debug=True)