Spaces:
Running
Running
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -11,6 +11,22 @@ import os
|
|
| 11 |
# Configuration
|
| 12 |
MODEL_ID = "amkyawdev/mm-coder-agent-v1-combined"
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
# Global variables to store model and tokenizer
|
| 15 |
model = None
|
| 16 |
tokenizer = None
|
|
@@ -42,7 +58,10 @@ def generate_code(prompt, max_tokens=512, temperature=0.7):
|
|
| 42 |
if model is None:
|
| 43 |
load_model()
|
| 44 |
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
with torch.no_grad():
|
| 48 |
outputs = model.generate(
|
|
@@ -56,8 +75,11 @@ def generate_code(prompt, max_tokens=512, temperature=0.7):
|
|
| 56 |
|
| 57 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 58 |
|
| 59 |
-
# Remove the prompt
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
| 61 |
response = response[len(prompt):].strip()
|
| 62 |
|
| 63 |
return response
|
|
|
|
| 11 |
# Configuration
|
| 12 |
MODEL_ID = "amkyawdev/mm-coder-agent-v1-combined"
|
| 13 |
|
| 14 |
+
# System prompt for better responses
|
| 15 |
+
SYSTEM_PROMPT = """You are MM Coder Agent v1, a professional AI coding assistant.
|
| 16 |
+
|
| 17 |
+
Guidelines:
|
| 18 |
+
1. Write clean, efficient code with proper imports
|
| 19 |
+
2. Be concise and practical
|
| 20 |
+
3. Use idiomatic code for the target language
|
| 21 |
+
4. Include error handling when needed
|
| 22 |
+
5. Prioritize security and best practices
|
| 23 |
+
|
| 24 |
+
Output format:
|
| 25 |
+
- Start with code directly
|
| 26 |
+
- Use code blocks with language tags
|
| 27 |
+
- Add brief explanation only if complex
|
| 28 |
+
- Always verify your code works"""
|
| 29 |
+
|
| 30 |
# Global variables to store model and tokenizer
|
| 31 |
model = None
|
| 32 |
tokenizer = None
|
|
|
|
| 58 |
if model is None:
|
| 59 |
load_model()
|
| 60 |
|
| 61 |
+
# Format prompt with system instruction
|
| 62 |
+
formatted_prompt = f"{SYSTEM_PROMPT}\n\nUser: {prompt}\nAssistant:"
|
| 63 |
+
|
| 64 |
+
inputs = tokenizer(formatted_prompt, return_tensors="pt").to(model.device)
|
| 65 |
|
| 66 |
with torch.no_grad():
|
| 67 |
outputs = model.generate(
|
|
|
|
| 75 |
|
| 76 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 77 |
|
| 78 |
+
# Remove the prompt and system prompt from response
|
| 79 |
+
prefix_to_remove = f"{SYSTEM_PROMPT}\n\nUser: {prompt}\nAssistant:"
|
| 80 |
+
if response.startswith(prefix_to_remove):
|
| 81 |
+
response = response[len(prefix_to_remove):].strip()
|
| 82 |
+
elif response.startswith(prompt):
|
| 83 |
response = response[len(prompt):].strip()
|
| 84 |
|
| 85 |
return response
|