Spaces:
Runtime error
Runtime error
| import re | |
| from typing import Dict, List, Any, Optional | |
| import json | |
| def format_code_response(response: str) -> str: | |
| """ | |
| Format and enhance code responses with proper syntax highlighting and structure. | |
| Args: | |
| response (str): Raw model response | |
| Returns: | |
| str: Formatted response with enhanced code blocks | |
| """ | |
| if not response: | |
| return "I'm sorry, I couldn't generate a response. Could you please rephrase your question?" | |
| # Detect and format code blocks | |
| formatted_response = response | |
| # Enhance existing code blocks | |
| code_block_pattern = r'```(\w+)?\n(.*?)```' | |
| def replace_code_block(match): | |
| language = match.group(1) or "text" | |
| code_content = match.group(2).strip() | |
| # Clean up the code content | |
| code_content = clean_code_content(code_content, language) | |
| return f'```{language}\n{code_content}\n```' | |
| # Apply code block formatting | |
| formatted_response = re.sub(code_block_pattern, replace_code_block, response, flags=re.DOTALL) | |
| # Add helpful tips for coding responses | |
| if any(keyword in response.lower() for keyword in ['def ', 'function', 'class ', 'import ', 'from ']): | |
| # This appears to be a code response, add a helpful note | |
| formatted_response += "\n\n💡 **Tip:** You can copy this code directly and use it in your project. Don't forget to install any required dependencies!" | |
| # Add execution hints for certain languages | |
| if 'python' in formatted_response.lower() and 'pip install' not in formatted_response.lower(): | |
| if any(module in formatted_response.lower() for module in ['requests', 'numpy', 'pandas', 'tensorflow', 'pytorch']): | |
| formatted_response += "\n\n⚠️ **Note:** Some packages may need to be installed first. Check the imports and install any missing dependencies." | |
| return formatted_response | |
| def clean_code_content(code: str, language: str) -> str: | |
| """ | |
| Clean and optimize code content for better readability. | |
| Args: | |
| code (str): Raw code content | |
| language (str): Programming language | |
| Returns: | |
| str: Cleaned code content | |
| """ | |
| # Remove excessive whitespace | |
| lines = code.split('\n') | |
| cleaned_lines = [] | |
| prev_empty = False | |
| for line in lines: | |
| # Skip completely empty lines at the start | |
| if not line.strip() and not cleaned_lines: | |
| continue | |
| # Normalize indentation | |
| cleaned_line = line.rstrip() | |
| cleaned_lines.append(cleaned_line) | |
| prev_empty = not line.strip() | |
| # Limit excessive empty lines | |
| result_lines = [] | |
| empty_count = 0 | |
| for line in cleaned_lines: | |
| if not line.strip(): | |
| empty_count += 1 | |
| if empty_count <= 2: # Max 2 consecutive empty lines | |
| result_lines.append(line) | |
| else: | |
| empty_count = 0 | |
| result_lines.append(line) | |
| return '\n'.join(result_lines) | |
| def parse_model_output(output: str) -> Dict[str, Any]: | |
| """ | |
| Parse and extract structured information from model output. | |
| Args: | |
| output (str): Raw model output | |
| Returns: | |
| Dict[str, Any]: Structured information about the response | |
| """ | |
| result = { | |
| "raw_output": output, | |
| "has_code": False, | |
| "code_language": None, | |
| "code_blocks": [], | |
| "suggestions": [], | |
| "explanations": [] | |
| } | |
| # Extract code blocks | |
| code_pattern = r'```(\w+)?\n(.*?)```' | |
| code_matches = re.findall(code_pattern, output, re.DOTALL) | |
| if code_matches: | |
| result["has_code"] = True | |
| for lang, code in code_matches: | |
| result["code_blocks"].append({ | |
| "language": lang or "text", | |
| "content": code.strip() | |
| }) | |
| if not result["code_language"]: | |
| result["code_language"] = lang | |
| # Extract explanations (lines that don't contain code) | |
| lines = output.split('\n') | |
| for line in lines: | |
| line = line.strip() | |
| if line and not line.startswith('```') and not any(keyword in line.lower() for keyword in ['def ', 'class ', 'import ', 'from ', '{', '}', '(', ')', ';', 'console.log', 'print(']): | |
| if len(line) > 20: # Only substantial lines | |
| result["explanations"].append(line) | |
| return result | |
| def format_error_message(error: Exception, user_message: str = "") -> str: | |
| """ | |
| Format error messages in a user-friendly way. | |
| Args: | |
| error (Exception): The caught exception | |
| user_message (str): The original user message | |
| Returns: | |
| str: Formatted error message | |
| """ | |
| error_type = type(error).__name__ | |
| error_msg = str(error) | |
| # Common error patterns and helpful responses | |
| if "CUDA" in error_msg and "out of memory" in error_msg.lower(): | |
| helpful_msg = "I'm experiencing memory limitations. Please try a shorter message or simpler request." | |
| elif "timeout" in error_msg.lower(): | |
| helpful_msg = "The request is taking too long. Please try with a shorter prompt." | |
| elif "connection" in error_msg.lower() or "network" in error_msg.lower(): | |
| helpful_msg = "I'm having trouble connecting to the model. Please check your connection and try again." | |
| else: | |
| helpful_msg = "I'm encountering a technical issue. Please try rephrasing your question or try again later." | |
| return f"❌ {helpful_msg}\n\n**Technical details:** {error_type}: {error_msg}" | |
| def extract_coding_concepts(text: str) -> List[str]: | |
| """ | |
| Extract programming concepts and keywords from text. | |
| Args: | |
| text (str): Input text | |
| Returns: | |
| List[str]: List of detected programming concepts | |
| """ | |
| programming_concepts = [ | |
| 'algorithm', 'data structure', 'complexity', 'recursion', 'iteration', | |
| 'object-oriented', 'functional programming', 'design pattern', 'api', | |
| 'database', 'sql', 'nosql', 'testing', 'debugging', 'optimization', | |
| 'performance', 'security', 'authentication', 'authorization', | |
| 'microservices', 'serverless', 'docker', 'kubernetes', 'devops', | |
| 'machine learning', 'data science', 'web scraping', 'automation' | |
| ] | |
| text_lower = text.lower() | |
| detected_concepts = [] | |
| for concept in programming_concepts: | |
| if concept in text_lower: | |
| detected_concepts.append(concept) | |
| return detected_concepts | |
| def create_example_prompts() -> Dict[str, List[str]]: | |
| """Create example prompts organized by category.""" | |
| return { | |
| "Beginner": [ | |
| "Write a Python function to calculate factorial", | |
| "Create a simple HTML page with a login form", | |
| "Explain what variables are in programming" | |
| ], | |
| "Intermediate": [ | |
| "Write a binary search algorithm in JavaScript", | |
| "Create a REST API endpoint in Flask", | |
| "Explain the difference between arrays and linked lists" | |
| ], | |
| "Advanced": [ | |
| "Implement a concurrent web scraper in Python", | |
| "Design a database schema for an e-commerce system", | |
| "Optimize this SQL query for better performance" | |
| ], | |
| "Debugging": [ | |
| "Debug this Python code: [code]", | |
| "Why is my JavaScript function returning undefined?", | |
| "Help me fix this SQL syntax error" | |
| ], | |
| "Code Review": [ | |
| "Review this function for best practices", | |
| "How can I make this code more efficient?", | |
| "What security issues do you see in this code?" | |
| ] | |
| } | |
| def validate_code_syntax(code: str, language: str) -> Dict[str, Any]: | |
| """ | |
| Basic syntax validation for generated code. | |
| Args: | |
| code (str): Code to validate | |
| language (str): Programming language | |
| Returns: | |
| Dict[str, Any]: Validation results | |
| """ | |
| validation_result = { | |
| "is_valid": True, | |
| "issues": [], | |
| "suggestions": [] | |
| } | |
| # Basic validation rules | |
| if language == "python": | |
| # Check for basic Python syntax issues | |
| if code.count('(') != code.count(')'): | |
| validation_result["issues"].append("Unbalanced parentheses") | |
| validation_result["is_valid"] = False | |
| if code.count('{') != code.count('}'): | |
| validation_result["issues"].append("Unbalanced braces") | |
| validation_result["is_valid"] = False | |
| # Common suggestions | |
| if 'def ' in code and ':' not in code: | |
| validation_result["suggestions"].append("Function definitions should end with a colon") | |
| if 'import ' in code and '\n' not in code: | |
| validation_result["suggestions"].append("Consider organizing imports at the top of the file") | |
| elif language in ["javascript", "typescript"]: | |
| # Check for common JS syntax issues | |
| if code.count('{') != code.count('}'): | |
| validation_result["issues"].append("Unbalanced curly braces") | |
| validation_result["is_valid"] = False | |
| if code.count('(') != code.count(')'): | |
| validation_result["issues"].append("Unbalanced parentheses") | |
| validation_result["is_valid"] = False | |
| return validation_result |