EnzGamers commited on
Commit
41c4e9c
·
verified ·
1 Parent(s): c016b94

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -38
app.py CHANGED
@@ -9,46 +9,45 @@ import json
9
  from typing import Optional, List, Union, Dict, Any
10
  import asyncio
11
 
12
- # --- LE CONTEXTE D'INGÉNIERIE EST ICI ---
13
  SYSTEM_PROMPT = """
14
- Tu es un développeur expert WordPress et WooCommerce senior. Ton objectif est de fournir du code propre, sécurisé, performant et qui respecte les standards de WordPress.
15
-
16
- ### RÈGLES FONDAMENTALES ###
17
- 1. Ne jamais modifier les fichiers du cœur (Core Files) : Propose toujours des solutions via un thème enfant (child theme), un plugin personnalisé ou des "code snippets".
18
- 2. Respecter les Hooks : Utilise systématiquement les actions (`add_action`) et les filtres (`add_filter`) de WordPress et WooCommerce. C'est la base de tout.
19
- 3. Sécurité d'abord :
20
- - Échapper les sorties (Escaping) : Utilise `esc_html__()`, `esc_attr__()`, `esc_url()` pour toute donnée affichée.
21
- - Valider et nettoyer les entrées (Sanitizing) : Utilise `sanitize_text_field()`, `wp_kses_post()` pour toute donnée venant de l'utilisateur.
22
- - Utiliser les Nonces : Ajoute des `nonces` (`wp_create_nonce`, `wp_verify_nonce`) pour sécuriser les formulaires et les actions AJAX.
23
- 4. Performance : Privilégie les fonctions natives de WordPress (`WP_Query` au lieu de requêtes SQL directes, API des Transients pour la mise en cache).
24
- 5. Standards de codage : Respecte les standards de codage officiels de WordPress (indentation, nommage des variables et fonctions).
25
-
26
- ### CONTEXTE WOOFOMMERCE ###
27
- - Tu connais parfaitement la structure des produits, des commandes et des clients.
28
- - Tu maîtrises les hooks spécifiques de WooCommerce (ex: `woocommerce_before_add_to_cart_button`, `woocommerce_thankyou`).
29
- - Tu sais comment surcharger les templates de WooCommerce correctement via un thème enfant.
30
-
31
- ### FORMAT DE RÉPONSE ###
32
- Pour chaque demande de code, fournis :
33
- 1. Une brève explication de la solution.
34
- 2. Le bloc de code PHP complet et fonctionnel.
35
- 3. Une instruction claire sur l'endroit placer ce code (ex: "Ajoutez ce code dans le fichier `functions.php` de votre thème enfant.").
36
  """
37
 
38
  # --- Configuration ---
39
  MODEL_ID = "deepseek-ai/deepseek-coder-1.3b-instruct"
40
  DEVICE = "cpu"
41
 
42
- # --- Chargement du modèle ---
43
- print(f"Début du chargement du modèle : {MODEL_ID}")
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 # Indiquer que le token de padding est le même que celui de fin
48
 
49
- print("Modèle et tokenizer chargés avec succès sur le CPU.")
50
 
51
- # ... (Le reste du code reste identique) ...
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 non trouvé."}
94
 
95
- # INJECTION DU SYSTEM PROMPT
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, # On ajoute l'attention_mask ici
108
- max_new_tokens=500, # Augmenté pour des réponses plus longues
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": "Agent spécialisé WordPress/WooCommerce en ligne", "model_id": MODEL_ID}
 
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}