Spaces:
Runtime error
Runtime error
File size: 9,435 Bytes
6574073 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
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 |