Download tools/code_tools.py from kokabtak/kokb1: direct link, hf CLI and curl.
- Browser
- Download file 6.91 kB
-
https://huggingface.co/kokabtak/kokb1/resolve/main/tools/code_tools.py
- Command line
-
hf download hf://kokabtak/kokb1/tools/code_tools.py
-
curl -L -o code_tools.py https://huggingface.co/kokabtak/kokb1/resolve/main/tools/code_tools.py
6.91 kB
| """ | |
| Code Generation Tools for KokB1 - Multi-language code generation | |
| Developed by Kokab Tak (kokabtak) | |
| Supports: Python, JavaScript, Java, C++, Go, Rust, and more | |
| """ | |
| from typing import Dict, List, Optional | |
| import re | |
| class CodeGeneratorTool: | |
| """Tool for generating code in multiple programming languages""" | |
| def __init__(self): | |
| self.language_templates = { | |
| 'python': self._python_template, | |
| 'javascript': self._javascript_template, | |
| 'java': self._java_template, | |
| 'cpp': self._cpp_template, | |
| 'go': self._go_template, | |
| 'rust': self._rust_template, | |
| } | |
| def _python_template(self, task: str, function_name: str = "solve") -> str: | |
| """Generate Python code template""" | |
| return f'''""" | |
| {task} | |
| Generated by kokb1 - Kokab Tak AI Model | |
| """ | |
| def {function_name}(): | |
| """ | |
| Solve the given task | |
| """ | |
| # TODO: Implement your solution here | |
| pass | |
| if __name__ == "__main__": | |
| result = {function_name}() | |
| print(f"Result: {{result}}") | |
| ''' | |
| def _javascript_template(self, task: str, function_name: str = "solve") -> str: | |
| """Generate JavaScript code template""" | |
| return f'''/** | |
| * {task} | |
| * Generated by kokb1 - Kokab Tak AI Model | |
| */ | |
| function {function_name}() {{ | |
| // TODO: Implement your solution here | |
| return null; | |
| }} | |
| // Example usage | |
| const result = {function_name}(); | |
| console.log(`Result: ${{result}}`); | |
| module.exports = {{ {function_name} }}; | |
| ''' | |
| def _java_template(self, task: str, class_name: str = "Solution") -> str: | |
| """Generate Java code template""" | |
| return f'''/** | |
| * {task} | |
| * Generated by kokb1 - Kokab Tak AI Model | |
| */ | |
| public class {class_name} {{ | |
| public static Object solve() {{ | |
| // TODO: Implement your solution here | |
| return null; | |
| }} | |
| public static void main(String[] args) {{ | |
| Object result = solve(); | |
| System.out.println("Result: " + result); | |
| }} | |
| }} | |
| ''' | |
| def _cpp_template(self, task: str) -> str: | |
| """Generate C++ code template""" | |
| return f'''/* | |
| * {task} | |
| * Generated by kokb1 - Kokab Tak AI Model | |
| */ | |
| #include <iostream> | |
| #include <string> | |
| using namespace std; | |
| string solve() {{ | |
| // TODO: Implement your solution here | |
| return ""; | |
| }} | |
| int main() {{ | |
| string result = solve(); | |
| cout << "Result: " << result << endl; | |
| return 0; | |
| }} | |
| ''' | |
| def _go_template(self, task: str) -> str: | |
| """Generate Go code template""" | |
| return f'''/* | |
| * {task} | |
| * Generated by kokb1 - Kokab Tak AI Model | |
| */ | |
| package main | |
| import "fmt" | |
| func solve() string {{ | |
| // TODO: Implement your solution here | |
| return "" | |
| }} | |
| func main() {{ | |
| result := solve() | |
| fmt.Printf("Result: %s\\n", result) | |
| }} | |
| ''' | |
| def _rust_template(self, task: str) -> str: | |
| """Generate Rust code template""" | |
| return f'''/* | |
| * {task} | |
| * Generated by kokb1 - Kokab Tak AI Model | |
| */ | |
| fn solve() -> String {{ | |
| // TODO: Implement your solution here | |
| String::from("") | |
| }} | |
| fn main() {{ | |
| let result = solve(); | |
| println!("Result: {{}}", result); | |
| }} | |
| ''' | |
| def generate_code(self, task: str, language: str = 'python', **kwargs) -> str: | |
| """Generate code for a given task in specified language""" | |
| template_func = self.language_templates.get(language.lower()) | |
| if not template_func: | |
| return f"# Language '{language}' not supported yet. Try: python, javascript, java, cpp, go, rust" | |
| return template_func(task, **kwargs) | |
| def detect_language(self, code: str) -> str: | |
| """Detect programming language from code""" | |
| if 'def ' in code and 'import ' in code: | |
| return 'python' | |
| elif 'function ' in code or 'const ' in code or 'let ' in code: | |
| return 'javascript' | |
| elif 'public class' in code and 'public static void main' in code: | |
| return 'java' | |
| elif '#include' in code and 'using namespace' in code: | |
| return 'cpp' | |
| elif 'package main' in code and 'func main()' in code: | |
| return 'go' | |
| elif 'fn main()' in code and 'let ' in code: | |
| return 'rust' | |
| else: | |
| return 'unknown' | |
| class CodeAnalyzerTool: | |
| """Tool for analyzing and explaining code""" | |
| def __init__(self): | |
| self.common_patterns = { | |
| 'loop': r'(for|while)\s*[\(\{]', | |
| 'condition': r'if\s*[\(\{]', | |
| 'function': r'(def|function|fn)\s+\w+', | |
| 'class': r'class\s+\w+', | |
| 'import': r'(import|from|require)\s+', | |
| } | |
| def analyze(self, code: str) -> Dict: | |
| """Analyze code structure""" | |
| analysis = { | |
| 'language': CodeGeneratorTool().detect_language(code), | |
| 'has_loops': bool(re.search(self.common_patterns['loop'], code)), | |
| 'has_conditions': bool(re.search(self.common_patterns['condition'], code)), | |
| 'has_functions': bool(re.search(self.common_patterns['function'], code)), | |
| 'has_classes': bool(re.search(self.common_patterns['class'], code)), | |
| 'has_imports': bool(re.search(self.common_patterns['import'], code)), | |
| 'line_count': len(code.split('\n')), | |
| 'char_count': len(code), | |
| } | |
| return analysis | |
| def explain(self, code: str) -> str: | |
| """Provide explanation of code""" | |
| analysis = self.analyze(code) | |
| explanation = f"This code is written in {analysis['language']}.\n" | |
| explanation += f"It has {analysis['line_count']} lines and {analysis['char_count']} characters.\n\n" | |
| if analysis['has_functions']: | |
| explanation += "✓ Contains functions\n" | |
| if analysis['has_loops']: | |
| explanation += "✓ Contains loops\n" | |
| if analysis['has_conditions']: | |
| explanation += "✓ Contains conditional statements\n" | |
| if analysis['has_classes']: | |
| explanation += "✓ Contains class definitions\n" | |
| if analysis['has_imports']: | |
| explanation += "✓ Contains imports/dependencies\n" | |
| return explanation | |
| if __name__ == "__main__": | |
| # Test code generation | |
| generator = CodeGeneratorTool() | |
| print("Testing Code Generation...") | |
| print("Python:") | |
| print(generator.generate_code("Calculate fibonacci sequence", "python")) | |
| print("\n" + "="*50 + "\n") | |
| print("JavaScript:") | |
| print(generator.generate_code("Create a simple counter", "javascript")) | |
| print("\n" + "="*50 + "\n") | |
| # Test code analysis | |
| analyzer = CodeAnalyzerTool() | |
| sample_code = ''' | |
| def fibonacci(n): | |
| if n <= 1: | |
| return n | |
| else: | |
| return fibonacci(n-1) + fibonacci(n-2) | |
| for i in range(10): | |
| print(fibonacci(i)) | |
| ''' | |
| print("Testing Code Analysis...") | |
| print(analyzer.explain(sample_code)) | |