Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -9,46 +9,45 @@ import json
|
|
9 |
from typing import Optional, List, Union, Dict, Any
|
10 |
import asyncio
|
11 |
|
12 |
-
# ---
|
13 |
SYSTEM_PROMPT = """
|
14 |
-
|
15 |
-
|
16 |
-
###
|
17 |
-
1.
|
18 |
-
2.
|
19 |
-
3.
|
20 |
-
-
|
21 |
-
-
|
22 |
-
-
|
23 |
-
4. Performance
|
24 |
-
5. Standards
|
25 |
-
|
26 |
-
###
|
27 |
-
-
|
28 |
-
-
|
29 |
-
-
|
30 |
-
|
31 |
-
### FORMAT
|
32 |
-
|
33 |
-
1.
|
34 |
-
2.
|
35 |
-
3.
|
36 |
"""
|
37 |
|
38 |
# --- Configuration ---
|
39 |
MODEL_ID = "deepseek-ai/deepseek-coder-1.3b-instruct"
|
40 |
DEVICE = "cpu"
|
41 |
|
42 |
-
# ---
|
43 |
-
print(f"
|
44 |
model = AutoModelForCausalLM.from_pretrained(MODEL_ID, torch_dtype=torch.bfloat16, device_map=DEVICE)
|
45 |
-
# CORRECTION DU WARNING : On configure le tokenizer correctement
|
46 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, padding_side='left')
|
47 |
-
tokenizer.pad_token = tokenizer.eos_token
|
48 |
|
49 |
-
print("
|
50 |
|
51 |
-
# ... (
|
52 |
|
53 |
app = FastAPI()
|
54 |
|
@@ -90,22 +89,20 @@ async def create_chat_completion(request: ChatCompletionRequest):
|
|
90 |
elif isinstance(last_message.content, str):
|
91 |
user_prompt = last_message.content
|
92 |
|
93 |
-
if not user_prompt: return {"error": "Prompt
|
94 |
|
95 |
-
#
|
96 |
messages_for_model = [
|
97 |
{'role': 'system', 'content': SYSTEM_PROMPT},
|
98 |
{'role': 'user', 'content': user_prompt}
|
99 |
]
|
100 |
|
101 |
-
# CORRECTION DU WARNING : On passe l'attention_mask
|
102 |
inputs = tokenizer.apply_chat_template(messages_for_model, add_generation_prompt=True, return_tensors="pt").to(DEVICE)
|
103 |
|
104 |
-
# Génération de la réponse complète
|
105 |
outputs = model.generate(
|
106 |
-
inputs,
|
107 |
-
attention_mask=inputs.attention_mask,
|
108 |
-
max_new_tokens=500,
|
109 |
do_sample=True,
|
110 |
temperature=0.1,
|
111 |
top_k=50,
|
@@ -113,7 +110,7 @@ async def create_chat_completion(request: ChatCompletionRequest):
|
|
113 |
num_return_sequences=1,
|
114 |
eos_token_id=tokenizer.eos_token_id
|
115 |
)
|
116 |
-
response_text = tokenizer.decode(outputs[0][len(inputs[0]):], skip_special_tokens=True)
|
117 |
|
118 |
async def stream_generator():
|
119 |
response_id = f"chatcmpl-{uuid.uuid4()}"
|
@@ -132,4 +129,4 @@ async def create_chat_completion(request: ChatCompletionRequest):
|
|
132 |
|
133 |
@app.get("/")
|
134 |
def root():
|
135 |
-
return {"status": "
|
|
|
9 |
from typing import Optional, List, Union, Dict, Any
|
10 |
import asyncio
|
11 |
|
12 |
+
# --- THE ENGINEERING CONTEXT IS HERE (IN ENGLISH) ---
|
13 |
SYSTEM_PROMPT = """
|
14 |
+
You are a senior expert WordPress and WooCommerce developer. Your goal is to provide code that is clean, secure, high-performance, and follows WordPress standards.
|
15 |
+
|
16 |
+
### FUNDAMENTAL RULES ###
|
17 |
+
1. **Never Modify Core Files:** Always provide solutions via a child theme, a custom plugin, or code snippets.
|
18 |
+
2. **Respect Hooks:** Systematically use WordPress and WooCommerce actions (`add_action`) and filters (`add_filter`). This is the foundation of everything.
|
19 |
+
3. **Security First:**
|
20 |
+
- **Escape Output:** Use `esc_html__()`, `esc_attr__()`, `esc_url()` for any displayed data.
|
21 |
+
- **Sanitize Input:** Use `sanitize_text_field()`, `wp_kses_post()` for any data coming from the user.
|
22 |
+
- **Use Nonces:** Add nonces (`wp_create_nonce`, `wp_verify_nonce`) to secure forms and AJAX actions.
|
23 |
+
4. **Performance:** Prioritize native WordPress functions (`WP_Query` instead of direct SQL queries, Transients API for caching).
|
24 |
+
5. **Coding Standards:** Follow the official WordPress coding standards (indentation, variable and function naming).
|
25 |
+
|
26 |
+
### WOOCOMMERCE CONTEXT ###
|
27 |
+
- You have a perfect understanding of the product, order, and customer structure.
|
28 |
+
- You master the specific WooCommerce hooks (e.g., `woocommerce_before_add_to_cart_button`, `woocommerce_thankyou`).
|
29 |
+
- You know how to override WooCommerce templates correctly via a child theme.
|
30 |
+
|
31 |
+
### RESPONSE FORMAT ###
|
32 |
+
For each code request, provide:
|
33 |
+
1. A brief explanation of the solution.
|
34 |
+
2. The complete and functional PHP code block.
|
35 |
+
3. A clear instruction on where to place this code (e.g., "Add this code to your child theme's `functions.php` file.").
|
36 |
"""
|
37 |
|
38 |
# --- Configuration ---
|
39 |
MODEL_ID = "deepseek-ai/deepseek-coder-1.3b-instruct"
|
40 |
DEVICE = "cpu"
|
41 |
|
42 |
+
# --- Model Loading ---
|
43 |
+
print(f"Loading model: {MODEL_ID}")
|
44 |
model = AutoModelForCausalLM.from_pretrained(MODEL_ID, torch_dtype=torch.bfloat16, device_map=DEVICE)
|
|
|
45 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, padding_side='left')
|
46 |
+
tokenizer.pad_token = tokenizer.eos_token
|
47 |
|
48 |
+
print("Model and tokenizer loaded successfully on CPU.")
|
49 |
|
50 |
+
# ... (The rest of the code remains the same) ...
|
51 |
|
52 |
app = FastAPI()
|
53 |
|
|
|
89 |
elif isinstance(last_message.content, str):
|
90 |
user_prompt = last_message.content
|
91 |
|
92 |
+
if not user_prompt: return {"error": "Prompt not found."}
|
93 |
|
94 |
+
# INJECTING THE SYSTEM PROMPT
|
95 |
messages_for_model = [
|
96 |
{'role': 'system', 'content': SYSTEM_PROMPT},
|
97 |
{'role': 'user', 'content': user_prompt}
|
98 |
]
|
99 |
|
|
|
100 |
inputs = tokenizer.apply_chat_template(messages_for_model, add_generation_prompt=True, return_tensors="pt").to(DEVICE)
|
101 |
|
|
|
102 |
outputs = model.generate(
|
103 |
+
inputs.input_ids,
|
104 |
+
attention_mask=inputs.attention_mask,
|
105 |
+
max_new_tokens=500,
|
106 |
do_sample=True,
|
107 |
temperature=0.1,
|
108 |
top_k=50,
|
|
|
110 |
num_return_sequences=1,
|
111 |
eos_token_id=tokenizer.eos_token_id
|
112 |
)
|
113 |
+
response_text = tokenizer.decode(outputs[0][len(inputs.input_ids[0]):], skip_special_tokens=True)
|
114 |
|
115 |
async def stream_generator():
|
116 |
response_id = f"chatcmpl-{uuid.uuid4()}"
|
|
|
129 |
|
130 |
@app.get("/")
|
131 |
def root():
|
132 |
+
return {"status": "Specialized WordPress/WooCommerce Agent is online", "model_id": MODEL_ID}
|