roneymatusp commited on
Commit
90da011
·
verified ·
1 Parent(s): 2e807b9

Upload british_prompt_optimizer.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. british_prompt_optimizer.py +250 -0
british_prompt_optimizer.py ADDED
@@ -0,0 +1,250 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ BRITISH PROMPT OPTIMIZER - PRODUCTION VERSION
4
+ Aceita QUALQUER idioma e SEMPRE retorna prompt otimizado em inglês britânico
5
+ """
6
+
7
+ from ollama import Client
8
+ import json
9
+ import logging
10
+ from datetime import datetime
11
+ from pathlib import Path
12
+
13
+ # Configuração
14
+ OLLAMA_API_KEY = "08d341f2a47745999691ebbde61a3374.cz1ftaVA-TViLz3vwGZBAFLm"
15
+ MODEL = "gpt-oss:20b"
16
+
17
+ # Sistema para OTIMIZAÇÃO DE PROMPTS
18
+ SYSTEM_PROMPT = """You are a PROMPT OPTIMIZER for a British International School.
19
+
20
+ YOUR TASK:
21
+ 1. Take ANY input (Portuguese, American English, any language)
22
+ 2. ALWAYS output an OPTIMIZED PROMPT in British English
23
+ 3. The output should be a clear, educational prompt suitable for British school levels
24
+
25
+ OPTIMIZATION RULES:
26
+ - Convert to British spelling: colour, centre, analyse, organise, behaviour, favourite
27
+ - Add British educational context: Pre-Prep, Prep, Junior, IGCSE, IBDP
28
+ - Make the prompt clear and specific
29
+ - Include learning objectives when relevant
30
+ - Use British pedagogical terminology
31
+
32
+ EXAMPLES:
33
+ Input: "como ensinar matemática?"
34
+ Output: "Design a mathematics lesson plan for Year 5 pupils covering fractions and decimals, incorporating visual aids and hands-on activities aligned with the National Curriculum."
35
+
36
+ Input: "teach photosynthesis"
37
+ Output: "Create an engaging IGCSE Biology lesson on photosynthesis, including practical experiments, diagrams, and assessment strategies suitable for Year 10 pupils."
38
+
39
+ ALWAYS OUTPUT ONLY THE OPTIMIZED PROMPT, NOTHING ELSE."""
40
+
41
+ # Setup logging
42
+ logging.basicConfig(
43
+ level=logging.INFO,
44
+ format='%(asctime)s - %(levelname)s - %(message)s'
45
+ )
46
+ logger = logging.getLogger(__name__)
47
+
48
+ class BritishPromptOptimizer:
49
+ def __init__(self):
50
+ self.client = Client(
51
+ host="https://ollama.com",
52
+ headers={'Authorization': OLLAMA_API_KEY}
53
+ )
54
+ self.model = MODEL
55
+
56
+ def optimize_prompt(self, user_input):
57
+ """Optimize ANY input to British English educational prompt"""
58
+
59
+ try:
60
+ # Generate optimized prompt
61
+ response = self.client.chat(
62
+ model=self.model,
63
+ messages=[
64
+ {'role': 'system', 'content': SYSTEM_PROMPT},
65
+ {'role': 'user', 'content': user_input}
66
+ ],
67
+ options={
68
+ 'temperature': 0.7,
69
+ 'top_p': 0.9,
70
+ 'max_tokens': 200
71
+ }
72
+ )
73
+
74
+ optimized = response['message']['content'].strip()
75
+
76
+ # Ensure British spelling
77
+ optimized = self.enforce_british_spelling(optimized)
78
+
79
+ return optimized
80
+
81
+ except Exception as e:
82
+ logger.error(f"Error optimizing prompt: {e}")
83
+ return "Create an engaging lesson plan for British pupils incorporating active learning strategies."
84
+
85
+ def enforce_british_spelling(self, text):
86
+ """Ensure British spelling in output"""
87
+ replacements = {
88
+ "color": "colour",
89
+ "center": "centre",
90
+ "analyze": "analyse",
91
+ "organize": "organise",
92
+ "behavior": "behaviour",
93
+ "favor": "favour",
94
+ "honor": "honour",
95
+ "theater": "theatre",
96
+ "meter": "metre",
97
+ "fiber": "fibre",
98
+ "defense": "defence",
99
+ "license": "licence",
100
+ "practice" + " (noun)": "practice",
101
+ "practise" + " (verb)": "practise"
102
+ }
103
+
104
+ for american, british in replacements.items():
105
+ text = text.replace(american, british)
106
+ text = text.replace(american.capitalize(), british.capitalize())
107
+
108
+ return text
109
+
110
+ def interactive_mode(self):
111
+ """Run interactive optimizer"""
112
+ print("\n🎓 BRITISH PROMPT OPTIMIZER")
113
+ print("="*60)
114
+ print("Enter ANY prompt in ANY language")
115
+ print("I will optimize it for British education")
116
+ print("Type 'quit' to exit\n")
117
+
118
+ while True:
119
+ user_input = input("\nOriginal prompt: ").strip()
120
+
121
+ if user_input.lower() in ['quit', 'exit', 'bye']:
122
+ print("\nCheerio! Have a brilliant day!")
123
+ break
124
+
125
+ if not user_input:
126
+ continue
127
+
128
+ print("\nOptimized prompt: ", end="", flush=True)
129
+ optimized = self.optimize_prompt(user_input)
130
+ print(optimized)
131
+
132
+ def batch_optimize(self):
133
+ """Optimize multiple prompts"""
134
+ test_prompts = [
135
+ "como fazer uma aula boa?",
136
+ "teach math to kids",
137
+ "explicar fotossíntese",
138
+ "homework ideas",
139
+ "avaliação de alunos",
140
+ "create a science project",
141
+ "português para crianças",
142
+ "classroom management tips",
143
+ "ideias para educação física",
144
+ "how to teach Shakespeare"
145
+ ]
146
+
147
+ print("\n🔄 BATCH OPTIMIZATION")
148
+ print("="*60)
149
+
150
+ results = []
151
+
152
+ for i, prompt in enumerate(test_prompts, 1):
153
+ print(f"\n{i}. Original: {prompt}")
154
+ optimized = self.optimize_prompt(prompt)
155
+ print(f" Optimized: {optimized}")
156
+
157
+ results.append({
158
+ "original": prompt,
159
+ "optimized": optimized,
160
+ "timestamp": datetime.now().isoformat()
161
+ })
162
+
163
+ # Save results
164
+ output_dir = Path("prompt_optimization_results")
165
+ output_dir.mkdir(exist_ok=True)
166
+
167
+ output_file = output_dir / f"batch_optimization_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
168
+ with open(output_file, 'w', encoding='utf-8') as f:
169
+ json.dump(results, f, indent=2, ensure_ascii=False)
170
+
171
+ print(f"\n📁 Results saved to: {output_file}")
172
+
173
+ def optimize_from_file(self, input_file, output_file):
174
+ """Optimize prompts from JSONL file"""
175
+ print(f"\n📂 Processing file: {input_file}")
176
+
177
+ optimized_count = 0
178
+
179
+ with open(input_file, 'r', encoding='utf-8') as f_in:
180
+ with open(output_file, 'w', encoding='utf-8') as f_out:
181
+ for line in f_in:
182
+ try:
183
+ data = json.loads(line.strip())
184
+
185
+ # Extract user message
186
+ if 'messages' in data:
187
+ for msg in data['messages']:
188
+ if msg['role'] == 'user':
189
+ original = msg['content']
190
+ optimized = self.optimize_prompt(original)
191
+
192
+ # Save optimized version
193
+ output_data = {
194
+ "original": original,
195
+ "optimized": optimized,
196
+ "timestamp": datetime.now().isoformat()
197
+ }
198
+
199
+ f_out.write(json.dumps(output_data, ensure_ascii=False) + '\n')
200
+ optimized_count += 1
201
+
202
+ if optimized_count % 100 == 0:
203
+ print(f" Processed: {optimized_count}")
204
+
205
+ except Exception as e:
206
+ logger.error(f"Error processing line: {e}")
207
+ continue
208
+
209
+ print(f"\n✅ Optimized {optimized_count} prompts")
210
+ print(f"📁 Saved to: {output_file}")
211
+
212
+ def main():
213
+ optimizer = BritishPromptOptimizer()
214
+
215
+ print("🎓 BRITISH PROMPT OPTIMIZER - PRODUCTION")
216
+ print("="*60)
217
+ print("1. Interactive mode (type prompts)")
218
+ print("2. Batch test (10 examples)")
219
+ print("3. Process file")
220
+ print("4. Quick test")
221
+
222
+ choice = input("\nSelect mode (1-4): ").strip()
223
+
224
+ if choice == "1":
225
+ optimizer.interactive_mode()
226
+ elif choice == "2":
227
+ optimizer.batch_optimize()
228
+ elif choice == "3":
229
+ input_file = input("Input file path: ").strip()
230
+ output_file = input("Output file path: ").strip()
231
+ if Path(input_file).exists():
232
+ optimizer.optimize_from_file(input_file, output_file)
233
+ else:
234
+ print("File not found!")
235
+ elif choice == "4":
236
+ # Quick test
237
+ test_prompts = [
238
+ "como fazer uma aula boa?",
239
+ "teach photosynthesis",
240
+ "avaliação para matemática"
241
+ ]
242
+ print("\n🧪 QUICK TEST")
243
+ for prompt in test_prompts:
244
+ print(f"\nOriginal: {prompt}")
245
+ print(f"Optimized: {optimizer.optimize_prompt(prompt)}")
246
+ else:
247
+ print("Invalid choice")
248
+
249
+ if __name__ == "__main__":
250
+ main()