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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import spaces
|
| 3 |
+
from models import CodeModel
|
| 4 |
+
from utils import format_code_response, parse_model_output
|
| 5 |
+
import torch
|
| 6 |
+
import os
|
| 7 |
+
from typing import List, Dict, Any
|
| 8 |
+
|
| 9 |
+
# Initialize the code model
|
| 10 |
+
code_model = CodeModel()
|
| 11 |
+
|
| 12 |
+
def chat_with_coder(message: str, history: List[Dict[str, str]], language: str = "python", temperature: float = 0.7) -> Dict[str, Any]:
|
| 13 |
+
"""
|
| 14 |
+
Main chatbot function that handles coding queries with a 5B parameter model.
|
| 15 |
+
|
| 16 |
+
Args:
|
| 17 |
+
message (str): User's input message
|
| 18 |
+
history (List[Dict[str, str]]): Chat history in OpenAI format
|
| 19 |
+
language (str): Target programming language
|
| 20 |
+
temperature (float): Generation temperature (0.0-1.0)
|
| 21 |
+
|
| 22 |
+
Returns:
|
| 23 |
+
Dict[str, Any]: Updated chat history and response
|
| 24 |
+
"""
|
| 25 |
+
try:
|
| 26 |
+
# Add context about coding capabilities
|
| 27 |
+
system_prompt = f"""You are an expert {language} programmer and AI coding assistant.
|
| 28 |
+
You help users with:
|
| 29 |
+
- Writing and debugging {language} code
|
| 30 |
+
- Code optimization and best practices
|
| 31 |
+
- Explaining complex programming concepts
|
| 32 |
+
- Code review and suggestions
|
| 33 |
+
- Algorithm implementation
|
| 34 |
+
|
| 35 |
+
Always provide clean, well-commented, and efficient code. Format code blocks properly with language specification."""
|
| 36 |
+
|
| 37 |
+
# Prepare messages for the model
|
| 38 |
+
messages = [{"role": "system", "content": system_prompt}]
|
| 39 |
+
messages.extend(history)
|
| 40 |
+
messages.append({"role": "user", "content": message})
|
| 41 |
+
|
| 42 |
+
# Generate response using the model
|
| 43 |
+
response = code_model.generate(
|
| 44 |
+
messages=messages,
|
| 45 |
+
temperature=temperature,
|
| 46 |
+
max_new_tokens=2048,
|
| 47 |
+
language=language
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
# Parse and format the response
|
| 51 |
+
formatted_response = format_code_response(response)
|
| 52 |
+
|
| 53 |
+
# Update chat history
|
| 54 |
+
new_history = history.copy()
|
| 55 |
+
new_history.append({"role": "user", "content": message})
|
| 56 |
+
new_history.append({"role": "assistant", "content": formatted_response})
|
| 57 |
+
|
| 58 |
+
return {"choices": [{"message": {"content": formatted_response}}], "history": new_history}
|
| 59 |
+
|
| 60 |
+
except Exception as e:
|
| 61 |
+
error_msg = f"I apologize, but I encountered an error: {str(e)}. Please try again or rephrase your question."
|
| 62 |
+
return {"choices": [{"message": {"content": error_msg}}], "history": history}
|
| 63 |
+
|
| 64 |
+
def clear_chat():
|
| 65 |
+
"""Clear the chat history."""
|
| 66 |
+
return {"choices": [{"message": {"content": "Hello! I'm your AI coding assistant powered by a 5B parameter language model. I can help you with Python, JavaScript, Java, C++, and many other programming languages. What would you like to code today?"}}], "history": []}
|
| 67 |
+
|
| 68 |
+
def create_demo():
|
| 69 |
+
"""Create the Gradio demo interface."""
|
| 70 |
+
|
| 71 |
+
with gr.Blocks(
|
| 72 |
+
title="AI Coder - 5B Parameter Chatbot",
|
| 73 |
+
description="Powered by a 5B parameter language model with coding capabilities",
|
| 74 |
+
theme=gr.themes.Soft(),
|
| 75 |
+
css="""
|
| 76 |
+
.container {max-width: 1200px !important;}
|
| 77 |
+
.header {text-align: center; padding: 20px;}
|
| 78 |
+
.header h1 {color: #2d3748; margin-bottom: 10px;}
|
| 79 |
+
.header a {color: #3182ce; text-decoration: none; font-weight: bold;}
|
| 80 |
+
.header a:hover {text-decoration: underline;}
|
| 81 |
+
.coding-section {background: #f7fafc; border-radius: 8px; padding: 15px; margin: 10px 0;}
|
| 82 |
+
"""
|
| 83 |
+
) as demo:
|
| 84 |
+
|
| 85 |
+
# Header
|
| 86 |
+
gr.HTML("""
|
| 87 |
+
<div class="header">
|
| 88 |
+
<h1>🤖 AI Coder - Powered by 5B Parameter Model</h1>
|
| 89 |
+
<p>Advanced AI chatbot with comprehensive coding features using a 5B parameter language model</p>
|
| 90 |
+
<p>Built with <a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank">anycoder</a></p>
|
| 91 |
+
</div>
|
| 92 |
+
""")
|
| 93 |
+
|
| 94 |
+
# Main chat interface
|
| 95 |
+
with gr.Row():
|
| 96 |
+
# Left column - Chat
|
| 97 |
+
with gr.Column(scale=3):
|
| 98 |
+
chatbot = gr.Chatbot(
|
| 99 |
+
label="AI Coding Assistant",
|
| 100 |
+
height=600,
|
| 101 |
+
type="messages",
|
| 102 |
+
avatar_images=(None, "🤖"),
|
| 103 |
+
show_copy_button=True
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
with gr.Row():
|
| 107 |
+
msg = gr.Textbox(
|
| 108 |
+
placeholder="Ask me to code something, debug code, or explain programming concepts...",
|
| 109 |
+
lines=3,
|
| 110 |
+
scale=4
|
| 111 |
+
)
|
| 112 |
+
send_btn = gr.Button("Send", variant="primary", scale=1)
|
| 113 |
+
|
| 114 |
+
with gr.Row():
|
| 115 |
+
clear_btn = gr.Button("Clear Chat", variant="secondary")
|
| 116 |
+
|
| 117 |
+
# Right column - Controls
|
| 118 |
+
with gr.Column(scale=1):
|
| 119 |
+
gr.Markdown("### 🛠️ Coding Settings")
|
| 120 |
+
|
| 121 |
+
language = gr.Dropdown(
|
| 122 |
+
choices=[
|
| 123 |
+
"python", "javascript", "java", "cpp", "c", "go",
|
| 124 |
+
"rust", "typescript", "php", "ruby", "swift", "kotlin",
|
| 125 |
+
"sql", "html", "css", "bash", "powershell"
|
| 126 |
+
],
|
| 127 |
+
value="python",
|
| 128 |
+
label="Programming Language",
|
| 129 |
+
info="Target language for code generation"
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
temperature = gr.Slider(
|
| 133 |
+
minimum=0.1,
|
| 134 |
+
maximum=1.0,
|
| 135 |
+
value=0.7,
|
| 136 |
+
step=0.1,
|
| 137 |
+
label="Creativity (Temperature)",
|
| 138 |
+
info="Lower for precise code, higher for creative solutions"
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
with gr.Accordion("🎯 Quick Coding Prompts", open=False):
|
| 142 |
+
gr.Examples(
|
| 143 |
+
examples=[
|
| 144 |
+
"Write a Python function to reverse a linked list",
|
| 145 |
+
"Create a React component for a login form",
|
| 146 |
+
"Debug this JavaScript code: [paste code]",
|
| 147 |
+
"Explain Big O notation with code examples",
|
| 148 |
+
"Write SQL queries for a user management system",
|
| 149 |
+
"Create a binary search algorithm in C++"
|
| 150 |
+
],
|
| 151 |
+
inputs=msg,
|
| 152 |
+
examples_per_page=3
|
| 153 |
+
)
|
| 154 |
+
|
| 155 |
+
with gr.Accordion("🔧 Model Info", open=False):
|
| 156 |
+
gr.Markdown(f"""
|
| 157 |
+
**Model:** {code_model.model_name}
|
| 158 |
+
**Parameters:** {code_model.parameter_count}
|
| 159 |
+
**Max Context:** {code_model.max_length:,} tokens
|
| 160 |
+
**Device:** {'CUDA' if torch.cuda.is_available() else 'CPU'}
|
| 161 |
+
**Status:** {'✅ Ready' if code_model.is_loaded else '⏳ Loading...'}
|
| 162 |
+
""")
|
| 163 |
+
|
| 164 |
+
# Event handlers
|
| 165 |
+
def user(user_message, history):
|
| 166 |
+
return "", history + [{"role": "user", "content": user_message}]
|
| 167 |
+
|
| 168 |
+
def bot(history, selected_language, temp):
|
| 169 |
+
if not history:
|
| 170 |
+
return history
|
| 171 |
+
|
| 172 |
+
last_message = history[-1]["content"]
|
| 173 |
+
result = chat_with_coder(last_message, history[:-1], selected_language, temp)
|
| 174 |
+
return result["history"]
|
| 175 |
+
|
| 176 |
+
# Wire up events
|
| 177 |
+
msg.submit(
|
| 178 |
+
user,
|
| 179 |
+
[msg, chatbot],
|
| 180 |
+
[msg, chatbot],
|
| 181 |
+
queue=False
|
| 182 |
+
).then(
|
| 183 |
+
bot,
|
| 184 |
+
[chatbot, language, temperature],
|
| 185 |
+
chatbot
|
| 186 |
+
)
|
| 187 |
+
|
| 188 |
+
send_btn.click(
|
| 189 |
+
user,
|
| 190 |
+
[msg, chatbot],
|
| 191 |
+
[msg, chatbot],
|
| 192 |
+
queue=False
|
| 193 |
+
).then(
|
| 194 |
+
bot,
|
| 195 |
+
[chatbot, language, temperature],
|
| 196 |
+
chatbot
|
| 197 |
+
)
|
| 198 |
+
|
| 199 |
+
clear_btn.click(
|
| 200 |
+
clear_chat,
|
| 201 |
+
outputs=[chatbot]
|
| 202 |
+
)
|
| 203 |
+
|
| 204 |
+
# Load initial message
|
| 205 |
+
chatbot.value = [{"role": "assistant", "content": "Hello! I'm your AI coding assistant powered by a 5B parameter language model. I can help you with Python, JavaScript, Java, C++, and many other programming languages. What would you like to code today?"}]
|
| 206 |
+
|
| 207 |
+
return demo
|
| 208 |
+
|
| 209 |
+
if __name__ == "__main__":
|
| 210 |
+
demo = create_demo()
|
| 211 |
+
demo.launch(
|
| 212 |
+
server_name="0.0.0.0",
|
| 213 |
+
server_port=7860,
|
| 214 |
+
show_error=True,
|
| 215 |
+
share=False,
|
| 216 |
+
debug=True
|
| 217 |
+
)
|
models.py
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 3 |
+
from typing import List, Dict, Any, Optional
|
| 4 |
+
import logging
|
| 5 |
+
|
| 6 |
+
class CodeModel:
|
| 7 |
+
"""5B Parameter coding model wrapper with optimized inference."""
|
| 8 |
+
|
| 9 |
+
def __init__(self):
|
| 10 |
+
self.model_name = "bigcode/starcoder2-7b" # 7B model (closest to 5B with excellent coding)
|
| 11 |
+
self.parameter_count = "7B"
|
| 12 |
+
self.max_length = 16384
|
| 13 |
+
self.tokenizer = None
|
| 14 |
+
self.model = None
|
| 15 |
+
self.pipeline = None
|
| 16 |
+
self.is_loaded = False
|
| 17 |
+
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 18 |
+
self.setup_model()
|
| 19 |
+
|
| 20 |
+
def setup_model(self):
|
| 21 |
+
"""Initialize and load the 5B+ parameter coding model."""
|
| 22 |
+
try:
|
| 23 |
+
print(f"Loading {self.model_name} model...")
|
| 24 |
+
|
| 25 |
+
# Load tokenizer and model
|
| 26 |
+
self.tokenizer = AutoTokenizer.from_pretrained(
|
| 27 |
+
self.model_name,
|
| 28 |
+
trust_remote_code=True,
|
| 29 |
+
padding_side="left"
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
# Set pad token if not present
|
| 33 |
+
if self.tokenizer.pad_token is None:
|
| 34 |
+
self.tokenizer.pad_token = self.tokenizer.eos_token
|
| 35 |
+
|
| 36 |
+
# Load model with optimization
|
| 37 |
+
self.model = AutoModelForCausalLM.from_pretrained(
|
| 38 |
+
self.model_name,
|
| 39 |
+
torch_dtype=torch.float16 if self.device == "cuda" else torch.float32,
|
| 40 |
+
device_map="auto" if self.device == "cuda" else None,
|
| 41 |
+
trust_remote_code=True,
|
| 42 |
+
low_cpu_mem_usage=True
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
# Create pipeline for easier inference
|
| 46 |
+
self.pipeline = pipeline(
|
| 47 |
+
"text-generation",
|
| 48 |
+
model=self.model,
|
| 49 |
+
tokenizer=self.tokenizer,
|
| 50 |
+
device=0 if self.device == "cuda" else -1,
|
| 51 |
+
do_sample=True,
|
| 52 |
+
temperature=0.7,
|
| 53 |
+
top_p=0.95,
|
| 54 |
+
repetition_penalty=1.1,
|
| 55 |
+
max_new_tokens=2048,
|
| 56 |
+
pad_token_id=self.tokenizer.eos_token_id
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
self.is_loaded = True
|
| 60 |
+
print(f"✅ {self.model_name} loaded successfully on {self.device}")
|
| 61 |
+
|
| 62 |
+
except Exception as e:
|
| 63 |
+
print(f"❌ Error loading model: {e}")
|
| 64 |
+
self._fallback_model()
|
| 65 |
+
|
| 66 |
+
def _fallback_model(self):
|
| 67 |
+
"""Fallback to a smaller model if the main model fails to load."""
|
| 68 |
+
try:
|
| 69 |
+
print("Trying fallback model: microsoft/DialoGPT-medium")
|
| 70 |
+
self.model_name = "microsoft/DialoGPT-medium"
|
| 71 |
+
self.parameter_count = "345M"
|
| 72 |
+
self.max_length = 1024
|
| 73 |
+
|
| 74 |
+
self.tokenizer = AutoTokenizer.from_pretrained(self.model_name)
|
| 75 |
+
if self.tokenizer.pad_token is None:
|
| 76 |
+
self.tokenizer.pad_token = self.tokenizer.eos_token
|
| 77 |
+
|
| 78 |
+
self.model = AutoModelForCausalLM.from_pretrained(
|
| 79 |
+
self.model_name,
|
| 80 |
+
torch_dtype=torch.float16 if self.device == "cuda" else torch.float32,
|
| 81 |
+
device_map="auto" if self.device == "cuda" else None
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
self.pipeline = pipeline(
|
| 85 |
+
"text-generation",
|
| 86 |
+
model=self.model,
|
| 87 |
+
tokenizer=self.tokenizer,
|
| 88 |
+
device=0 if self.device == "cuda" else -1,
|
| 89 |
+
max_new_tokens=512,
|
| 90 |
+
pad_token_id=self.tokenizer.eos_token_id
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
self.is_loaded = True
|
| 94 |
+
print(f"✅ Fallback model loaded successfully")
|
| 95 |
+
|
| 96 |
+
except Exception as e:
|
| 97 |
+
print(f"❌ Fallback model also failed: {e}")
|
| 98 |
+
self.is_loaded = False
|
| 99 |
+
|
| 100 |
+
def generate(
|
| 101 |
+
self,
|
| 102 |
+
messages: List[Dict[str, str]],
|
| 103 |
+
temperature: float = 0.7,
|
| 104 |
+
max_new_tokens: int = 2048,
|
| 105 |
+
language: str = "python"
|
| 106 |
+
) -> str:
|
| 107 |
+
"""Generate response from the model."""
|
| 108 |
+
|
| 109 |
+
if not self.is_loaded:
|
| 110 |
+
return "I'm sorry, the model is not loaded yet. Please try again in a moment."
|
| 111 |
+
|
| 112 |
+
try:
|
| 113 |
+
# Convert chat format to text
|
| 114 |
+
if messages:
|
| 115 |
+
# Format as conversation
|
| 116 |
+
conversation = ""
|
| 117 |
+
for msg in messages:
|
| 118 |
+
role = msg["role"]
|
| 119 |
+
content = msg["content"]
|
| 120 |
+
if role == "system":
|
| 121 |
+
conversation += f"System: {content}\n\n"
|
| 122 |
+
elif role == "user":
|
| 123 |
+
conversation += f"Human: {content}\n"
|
| 124 |
+
elif role == "assistant":
|
| 125 |
+
conversation += f"Assistant: {content}\n"
|
| 126 |
+
|
| 127 |
+
# Add specific coding instructions
|
| 128 |
+
if "write" in conversation.lower() or "code" in conversation.lower():
|
| 129 |
+
conversation += f"\n\nPlease provide clean, well-commented {language} code with proper syntax and best practices."
|
| 130 |
+
|
| 131 |
+
conversation += "\nAssistant:"
|
| 132 |
+
|
| 133 |
+
# Generate response
|
| 134 |
+
with torch.no_grad():
|
| 135 |
+
if self.pipeline:
|
| 136 |
+
# Use pipeline for generation
|
| 137 |
+
outputs = self.pipeline(
|
| 138 |
+
conversation,
|
| 139 |
+
do_sample=True,
|
| 140 |
+
temperature=temperature,
|
| 141 |
+
top_p=0.95,
|
| 142 |
+
repetition_penalty=1.1,
|
| 143 |
+
max_new_tokens=max_new_tokens,
|
| 144 |
+
pad_token_id=self.tokenizer.eos_token_id,
|
| 145 |
+
eos_token_id=self.tokenizer.eos_token_id,
|
| 146 |
+
return_full_text=False
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
if outputs and len(outputs) > 0:
|
| 150 |
+
return outputs[0]["generated_text"].strip()
|
| 151 |
+
|
| 152 |
+
# Fallback to direct model generation
|
| 153 |
+
inputs = self.tokenizer.encode(conversation, return_tensors="pt").to(self.device)
|
| 154 |
+
|
| 155 |
+
with torch.no_grad():
|
| 156 |
+
outputs = self.model.generate(
|
| 157 |
+
inputs,
|
| 158 |
+
do_sample=True,
|
| 159 |
+
temperature=temperature,
|
| 160 |
+
top_p=0.95,
|
| 161 |
+
repetition_penalty=1.1,
|
| 162 |
+
max_new_tokens=max_new_tokens,
|
| 163 |
+
pad_token_id=self.tokenizer.eos_token_id,
|
| 164 |
+
eos_token_id=self.tokenizer.eos_token_id,
|
| 165 |
+
attention_mask=torch.ones_like(inputs)
|
| 166 |
+
)
|
| 167 |
+
|
| 168 |
+
# Decode response
|
| 169 |
+
response = self.tokenizer.decode(outputs[0][inputs.shape[1]:], skip_special_tokens=True)
|
| 170 |
+
return response.strip()
|
| 171 |
+
|
| 172 |
+
except Exception as e:
|
| 173 |
+
logging.error(f"Generation error: {e}")
|
| 174 |
+
return f"I apologize, but I encountered an error while generating the response: {str(e)}"
|
| 175 |
+
|
| 176 |
+
def get_model_info(self) -> Dict[str, Any]:
|
| 177 |
+
"""Get information about the loaded model."""
|
| 178 |
+
return {
|
| 179 |
+
"model_name": self.model_name,
|
| 180 |
+
"parameter_count": self.parameter_count,
|
| 181 |
+
"max_length": self.max_length,
|
| 182 |
+
"device": self.device,
|
| 183 |
+
"is_loaded": self.is_loaded,
|
| 184 |
+
"vocab_size": len(self.tokenizer) if self.tokenizer else 0
|
| 185 |
+
}
|
requirements.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
spaces
|
| 3 |
+
transformers
|
| 4 |
+
torch
|
| 5 |
+
accelerate
|
| 6 |
+
tokenizers
|
| 7 |
+
datasets
|
| 8 |
+
numpy
|
| 9 |
+
pandas
|
| 10 |
+
requests
|
| 11 |
+
huggingface-hub
|
| 12 |
+
python-multipart
|
| 13 |
+
fastapi
|
| 14 |
+
uvicorn
|
| 15 |
+
peft
|
| 16 |
+
bitsandbytes
|
| 17 |
+
scipy
|
| 18 |
+
matplotlib
|
| 19 |
+
seaborn
|
| 20 |
+
jupyter
|
| 21 |
+
ipywidgets
|
utils.py
ADDED
|
@@ -0,0 +1,262 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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 3 |
+
import json
|
| 4 |
+
|
| 5 |
+
def format_code_response(response: str) -> str:
|
| 6 |
+
"""
|
| 7 |
+
Format and enhance code responses with proper syntax highlighting and structure.
|
| 8 |
+
|
| 9 |
+
Args:
|
| 10 |
+
response (str): Raw model response
|
| 11 |
+
|
| 12 |
+
Returns:
|
| 13 |
+
str: Formatted response with enhanced code blocks
|
| 14 |
+
"""
|
| 15 |
+
if not response:
|
| 16 |
+
return "I'm sorry, I couldn't generate a response. Could you please rephrase your question?"
|
| 17 |
+
|
| 18 |
+
# Detect and format code blocks
|
| 19 |
+
formatted_response = response
|
| 20 |
+
|
| 21 |
+
# Enhance existing code blocks
|
| 22 |
+
code_block_pattern = r'```(\w+)?\n(.*?)```'
|
| 23 |
+
|
| 24 |
+
def replace_code_block(match):
|
| 25 |
+
language = match.group(1) or "text"
|
| 26 |
+
code_content = match.group(2).strip()
|
| 27 |
+
|
| 28 |
+
# Clean up the code content
|
| 29 |
+
code_content = clean_code_content(code_content, language)
|
| 30 |
+
|
| 31 |
+
return f'```{language}\n{code_content}\n```'
|
| 32 |
+
|
| 33 |
+
# Apply code block formatting
|
| 34 |
+
formatted_response = re.sub(code_block_pattern, replace_code_block, response, flags=re.DOTALL)
|
| 35 |
+
|
| 36 |
+
# Add helpful tips for coding responses
|
| 37 |
+
if any(keyword in response.lower() for keyword in ['def ', 'function', 'class ', 'import ', 'from ']):
|
| 38 |
+
# This appears to be a code response, add a helpful note
|
| 39 |
+
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!"
|
| 40 |
+
|
| 41 |
+
# Add execution hints for certain languages
|
| 42 |
+
if 'python' in formatted_response.lower() and 'pip install' not in formatted_response.lower():
|
| 43 |
+
if any(module in formatted_response.lower() for module in ['requests', 'numpy', 'pandas', 'tensorflow', 'pytorch']):
|
| 44 |
+
formatted_response += "\n\n⚠️ **Note:** Some packages may need to be installed first. Check the imports and install any missing dependencies."
|
| 45 |
+
|
| 46 |
+
return formatted_response
|
| 47 |
+
|
| 48 |
+
def clean_code_content(code: str, language: str) -> str:
|
| 49 |
+
"""
|
| 50 |
+
Clean and optimize code content for better readability.
|
| 51 |
+
|
| 52 |
+
Args:
|
| 53 |
+
code (str): Raw code content
|
| 54 |
+
language (str): Programming language
|
| 55 |
+
|
| 56 |
+
Returns:
|
| 57 |
+
str: Cleaned code content
|
| 58 |
+
"""
|
| 59 |
+
# Remove excessive whitespace
|
| 60 |
+
lines = code.split('\n')
|
| 61 |
+
cleaned_lines = []
|
| 62 |
+
prev_empty = False
|
| 63 |
+
|
| 64 |
+
for line in lines:
|
| 65 |
+
# Skip completely empty lines at the start
|
| 66 |
+
if not line.strip() and not cleaned_lines:
|
| 67 |
+
continue
|
| 68 |
+
|
| 69 |
+
# Normalize indentation
|
| 70 |
+
cleaned_line = line.rstrip()
|
| 71 |
+
cleaned_lines.append(cleaned_line)
|
| 72 |
+
|
| 73 |
+
prev_empty = not line.strip()
|
| 74 |
+
|
| 75 |
+
# Limit excessive empty lines
|
| 76 |
+
result_lines = []
|
| 77 |
+
empty_count = 0
|
| 78 |
+
|
| 79 |
+
for line in cleaned_lines:
|
| 80 |
+
if not line.strip():
|
| 81 |
+
empty_count += 1
|
| 82 |
+
if empty_count <= 2: # Max 2 consecutive empty lines
|
| 83 |
+
result_lines.append(line)
|
| 84 |
+
else:
|
| 85 |
+
empty_count = 0
|
| 86 |
+
result_lines.append(line)
|
| 87 |
+
|
| 88 |
+
return '\n'.join(result_lines)
|
| 89 |
+
|
| 90 |
+
def parse_model_output(output: str) -> Dict[str, Any]:
|
| 91 |
+
"""
|
| 92 |
+
Parse and extract structured information from model output.
|
| 93 |
+
|
| 94 |
+
Args:
|
| 95 |
+
output (str): Raw model output
|
| 96 |
+
|
| 97 |
+
Returns:
|
| 98 |
+
Dict[str, Any]: Structured information about the response
|
| 99 |
+
"""
|
| 100 |
+
result = {
|
| 101 |
+
"raw_output": output,
|
| 102 |
+
"has_code": False,
|
| 103 |
+
"code_language": None,
|
| 104 |
+
"code_blocks": [],
|
| 105 |
+
"suggestions": [],
|
| 106 |
+
"explanations": []
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
# Extract code blocks
|
| 110 |
+
code_pattern = r'```(\w+)?\n(.*?)```'
|
| 111 |
+
code_matches = re.findall(code_pattern, output, re.DOTALL)
|
| 112 |
+
|
| 113 |
+
if code_matches:
|
| 114 |
+
result["has_code"] = True
|
| 115 |
+
for lang, code in code_matches:
|
| 116 |
+
result["code_blocks"].append({
|
| 117 |
+
"language": lang or "text",
|
| 118 |
+
"content": code.strip()
|
| 119 |
+
})
|
| 120 |
+
if not result["code_language"]:
|
| 121 |
+
result["code_language"] = lang
|
| 122 |
+
|
| 123 |
+
# Extract explanations (lines that don't contain code)
|
| 124 |
+
lines = output.split('\n')
|
| 125 |
+
for line in lines:
|
| 126 |
+
line = line.strip()
|
| 127 |
+
if line and not line.startswith('```') and not any(keyword in line.lower() for keyword in ['def ', 'class ', 'import ', 'from ', '{', '}', '(', ')', ';', 'console.log', 'print(']):
|
| 128 |
+
if len(line) > 20: # Only substantial lines
|
| 129 |
+
result["explanations"].append(line)
|
| 130 |
+
|
| 131 |
+
return result
|
| 132 |
+
|
| 133 |
+
def format_error_message(error: Exception, user_message: str = "") -> str:
|
| 134 |
+
"""
|
| 135 |
+
Format error messages in a user-friendly way.
|
| 136 |
+
|
| 137 |
+
Args:
|
| 138 |
+
error (Exception): The caught exception
|
| 139 |
+
user_message (str): The original user message
|
| 140 |
+
|
| 141 |
+
Returns:
|
| 142 |
+
str: Formatted error message
|
| 143 |
+
"""
|
| 144 |
+
error_type = type(error).__name__
|
| 145 |
+
error_msg = str(error)
|
| 146 |
+
|
| 147 |
+
# Common error patterns and helpful responses
|
| 148 |
+
if "CUDA" in error_msg and "out of memory" in error_msg.lower():
|
| 149 |
+
helpful_msg = "I'm experiencing memory limitations. Please try a shorter message or simpler request."
|
| 150 |
+
elif "timeout" in error_msg.lower():
|
| 151 |
+
helpful_msg = "The request is taking too long. Please try with a shorter prompt."
|
| 152 |
+
elif "connection" in error_msg.lower() or "network" in error_msg.lower():
|
| 153 |
+
helpful_msg = "I'm having trouble connecting to the model. Please check your connection and try again."
|
| 154 |
+
else:
|
| 155 |
+
helpful_msg = "I'm encountering a technical issue. Please try rephrasing your question or try again later."
|
| 156 |
+
|
| 157 |
+
return f"❌ {helpful_msg}\n\n**Technical details:** {error_type}: {error_msg}"
|
| 158 |
+
|
| 159 |
+
def extract_coding_concepts(text: str) -> List[str]:
|
| 160 |
+
"""
|
| 161 |
+
Extract programming concepts and keywords from text.
|
| 162 |
+
|
| 163 |
+
Args:
|
| 164 |
+
text (str): Input text
|
| 165 |
+
|
| 166 |
+
Returns:
|
| 167 |
+
List[str]: List of detected programming concepts
|
| 168 |
+
"""
|
| 169 |
+
programming_concepts = [
|
| 170 |
+
'algorithm', 'data structure', 'complexity', 'recursion', 'iteration',
|
| 171 |
+
'object-oriented', 'functional programming', 'design pattern', 'api',
|
| 172 |
+
'database', 'sql', 'nosql', 'testing', 'debugging', 'optimization',
|
| 173 |
+
'performance', 'security', 'authentication', 'authorization',
|
| 174 |
+
'microservices', 'serverless', 'docker', 'kubernetes', 'devops',
|
| 175 |
+
'machine learning', 'data science', 'web scraping', 'automation'
|
| 176 |
+
]
|
| 177 |
+
|
| 178 |
+
text_lower = text.lower()
|
| 179 |
+
detected_concepts = []
|
| 180 |
+
|
| 181 |
+
for concept in programming_concepts:
|
| 182 |
+
if concept in text_lower:
|
| 183 |
+
detected_concepts.append(concept)
|
| 184 |
+
|
| 185 |
+
return detected_concepts
|
| 186 |
+
|
| 187 |
+
def create_example_prompts() -> Dict[str, List[str]]:
|
| 188 |
+
"""Create example prompts organized by category."""
|
| 189 |
+
return {
|
| 190 |
+
"Beginner": [
|
| 191 |
+
"Write a Python function to calculate factorial",
|
| 192 |
+
"Create a simple HTML page with a login form",
|
| 193 |
+
"Explain what variables are in programming"
|
| 194 |
+
],
|
| 195 |
+
"Intermediate": [
|
| 196 |
+
"Write a binary search algorithm in JavaScript",
|
| 197 |
+
"Create a REST API endpoint in Flask",
|
| 198 |
+
"Explain the difference between arrays and linked lists"
|
| 199 |
+
],
|
| 200 |
+
"Advanced": [
|
| 201 |
+
"Implement a concurrent web scraper in Python",
|
| 202 |
+
"Design a database schema for an e-commerce system",
|
| 203 |
+
"Optimize this SQL query for better performance"
|
| 204 |
+
],
|
| 205 |
+
"Debugging": [
|
| 206 |
+
"Debug this Python code: [code]",
|
| 207 |
+
"Why is my JavaScript function returning undefined?",
|
| 208 |
+
"Help me fix this SQL syntax error"
|
| 209 |
+
],
|
| 210 |
+
"Code Review": [
|
| 211 |
+
"Review this function for best practices",
|
| 212 |
+
"How can I make this code more efficient?",
|
| 213 |
+
"What security issues do you see in this code?"
|
| 214 |
+
]
|
| 215 |
+
}
|
| 216 |
+
|
| 217 |
+
def validate_code_syntax(code: str, language: str) -> Dict[str, Any]:
|
| 218 |
+
"""
|
| 219 |
+
Basic syntax validation for generated code.
|
| 220 |
+
|
| 221 |
+
Args:
|
| 222 |
+
code (str): Code to validate
|
| 223 |
+
language (str): Programming language
|
| 224 |
+
|
| 225 |
+
Returns:
|
| 226 |
+
Dict[str, Any]: Validation results
|
| 227 |
+
"""
|
| 228 |
+
validation_result = {
|
| 229 |
+
"is_valid": True,
|
| 230 |
+
"issues": [],
|
| 231 |
+
"suggestions": []
|
| 232 |
+
}
|
| 233 |
+
|
| 234 |
+
# Basic validation rules
|
| 235 |
+
if language == "python":
|
| 236 |
+
# Check for basic Python syntax issues
|
| 237 |
+
if code.count('(') != code.count(')'):
|
| 238 |
+
validation_result["issues"].append("Unbalanced parentheses")
|
| 239 |
+
validation_result["is_valid"] = False
|
| 240 |
+
|
| 241 |
+
if code.count('{') != code.count('}'):
|
| 242 |
+
validation_result["issues"].append("Unbalanced braces")
|
| 243 |
+
validation_result["is_valid"] = False
|
| 244 |
+
|
| 245 |
+
# Common suggestions
|
| 246 |
+
if 'def ' in code and ':' not in code:
|
| 247 |
+
validation_result["suggestions"].append("Function definitions should end with a colon")
|
| 248 |
+
|
| 249 |
+
if 'import ' in code and '\n' not in code:
|
| 250 |
+
validation_result["suggestions"].append("Consider organizing imports at the top of the file")
|
| 251 |
+
|
| 252 |
+
elif language in ["javascript", "typescript"]:
|
| 253 |
+
# Check for common JS syntax issues
|
| 254 |
+
if code.count('{') != code.count('}'):
|
| 255 |
+
validation_result["issues"].append("Unbalanced curly braces")
|
| 256 |
+
validation_result["is_valid"] = False
|
| 257 |
+
|
| 258 |
+
if code.count('(') != code.count(')'):
|
| 259 |
+
validation_result["issues"].append("Unbalanced parentheses")
|
| 260 |
+
validation_result["is_valid"] = False
|
| 261 |
+
|
| 262 |
+
return validation_result
|