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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import re
|
| 4 |
+
|
| 5 |
+
class PromptOptimizer:
|
| 6 |
+
def __init__(self):
|
| 7 |
+
self.complexity_patterns = {
|
| 8 |
+
'context': r'(contexte|situation|cadre)',
|
| 9 |
+
'specifics': r'(détails|spécifiquement|précisément)',
|
| 10 |
+
'requirements': r'(requis|nécessaire|obligatoire)',
|
| 11 |
+
'constraints': r'(contrainte|limite|restriction)'
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
def analyze_prompt(self, prompt: str) -> dict:
|
| 15 |
+
"""Analyse la structure et la qualité du prompt."""
|
| 16 |
+
metrics = {
|
| 17 |
+
'longueur': len(prompt),
|
| 18 |
+
'complexité': self._calculate_complexity(prompt),
|
| 19 |
+
'clarté': self._evaluate_clarity(prompt),
|
| 20 |
+
'structure': self._analyze_structure(prompt)
|
| 21 |
+
}
|
| 22 |
+
return metrics
|
| 23 |
+
|
| 24 |
+
def optimize_prompt(self, prompt: str, mode: str = 'expert', complexity: int = 50) -> str:
|
| 25 |
+
"""Optimise le prompt selon le mode et le niveau de complexité souhaités."""
|
| 26 |
+
# Analyse initiale
|
| 27 |
+
analysis = self.analyze_prompt(prompt)
|
| 28 |
+
|
| 29 |
+
# Structure de base optimisée
|
| 30 |
+
template = {
|
| 31 |
+
'expert': """Instructions détaillées:
|
| 32 |
+
{prompt}
|
| 33 |
+
|
| 34 |
+
Paramètres d'exécution:
|
| 35 |
+
- Niveau de détail requis: {complexity}%
|
| 36 |
+
- Mode d'analyse: Expert
|
| 37 |
+
- Format attendu: Structuré et exhaustif
|
| 38 |
+
|
| 39 |
+
Exigences spécifiques:
|
| 40 |
+
1. Analyse approfondie du contexte
|
| 41 |
+
2. Identification des points clés
|
| 42 |
+
3. Propositions d'optimisation
|
| 43 |
+
4. Justification des choix
|
| 44 |
+
|
| 45 |
+
Contraintes:
|
| 46 |
+
- Réponse précise et argumentée
|
| 47 |
+
- Exemples concrets
|
| 48 |
+
- Solutions applicables""",
|
| 49 |
+
'creative': """Contexte créatif:
|
| 50 |
+
{prompt}
|
| 51 |
+
|
| 52 |
+
Paramètres d'inspiration:
|
| 53 |
+
- Niveau d'innovation: {complexity}%
|
| 54 |
+
- Approche: Créative
|
| 55 |
+
- Style: Libre et innovant
|
| 56 |
+
|
| 57 |
+
Axes de développement:
|
| 58 |
+
1. Exploration des possibilités
|
| 59 |
+
2. Innovation conceptuelle
|
| 60 |
+
3. Associations créatives
|
| 61 |
+
4. Perspectives uniques
|
| 62 |
+
|
| 63 |
+
Encouragements:
|
| 64 |
+
- Pensée latérale
|
| 65 |
+
- Solutions originales
|
| 66 |
+
- Concepts novateurs""",
|
| 67 |
+
'technical': """Spécifications techniques:
|
| 68 |
+
{prompt}
|
| 69 |
+
|
| 70 |
+
Paramètres techniques:
|
| 71 |
+
- Niveau de précision: {complexity}%
|
| 72 |
+
- Approche: Technique
|
| 73 |
+
- Documentation: Détaillée
|
| 74 |
+
|
| 75 |
+
Points d'attention:
|
| 76 |
+
1. Faisabilité technique
|
| 77 |
+
2. Optimisation des ressources
|
| 78 |
+
3. Standards de qualité
|
| 79 |
+
4. Tests et validation
|
| 80 |
+
|
| 81 |
+
Exigences:
|
| 82 |
+
- Documentation technique
|
| 83 |
+
- Métriques de performance
|
| 84 |
+
- Recommandations d'implémentation"""
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
# Sélection et personnalisation du template
|
| 88 |
+
optimized = template[mode].format(
|
| 89 |
+
prompt=prompt,
|
| 90 |
+
complexity=complexity
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
return optimized
|
| 94 |
+
|
| 95 |
+
def _calculate_complexity(self, prompt: str) -> float:
|
| 96 |
+
"""Calcule la complexité du prompt basée sur plusieurs facteurs."""
|
| 97 |
+
score = 0
|
| 98 |
+
# Vérifie la présence de patterns de complexité
|
| 99 |
+
for pattern in self.complexity_patterns.values():
|
| 100 |
+
if re.search(pattern, prompt.lower()):
|
| 101 |
+
score += 0.25
|
| 102 |
+
|
| 103 |
+
# Analyse la structure des phrases
|
| 104 |
+
sentences = prompt.split('.')
|
| 105 |
+
avg_length = sum(len(s.split()) for s in sentences) / len(sentences)
|
| 106 |
+
score += min(avg_length / 20, 0.5) # Normalise à max 0.5
|
| 107 |
+
|
| 108 |
+
return min(score, 1.0) # Score final entre 0 et 1
|
| 109 |
+
|
| 110 |
+
def _evaluate_clarity(self, prompt: str) -> float:
|
| 111 |
+
"""Évalue la clarté du prompt."""
|
| 112 |
+
clarity_score = 0
|
| 113 |
+
|
| 114 |
+
# Vérifie la présence de marqueurs de structure
|
| 115 |
+
if re.search(r'(premièrement|ensuite|enfin)', prompt.lower()):
|
| 116 |
+
clarity_score += 0.3
|
| 117 |
+
|
| 118 |
+
# Vérifie la ponctuation appropriée
|
| 119 |
+
if re.search(r'[.?!]', prompt):
|
| 120 |
+
clarity_score += 0.2
|
| 121 |
+
|
| 122 |
+
# Vérifie la présence de mots de liaison
|
| 123 |
+
if re.search(r'(car|donc|ainsi|cependant)', prompt.lower()):
|
| 124 |
+
clarity_score += 0.3
|
| 125 |
+
|
| 126 |
+
return min(clarity_score, 1.0)
|
| 127 |
+
|
| 128 |
+
def _analyze_structure(self, prompt: str) -> dict:
|
| 129 |
+
"""Analyse la structure du prompt."""
|
| 130 |
+
return {
|
| 131 |
+
'paragraphes': len(prompt.split('\n\n')),
|
| 132 |
+
'phrases': len(prompt.split('.')),
|
| 133 |
+
'mots': len(prompt.split())
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
def create_interface():
|
| 137 |
+
optimizer = PromptOptimizer()
|
| 138 |
+
|
| 139 |
+
def process_prompt(prompt, mode, complexity):
|
| 140 |
+
analysis = optimizer.analyze_prompt(prompt)
|
| 141 |
+
optimized = optimizer.optimize_prompt(prompt, mode, complexity)
|
| 142 |
+
return {
|
| 143 |
+
'analyse': analysis,
|
| 144 |
+
'prompt_optimisé': optimized
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
# Interface Gradio
|
| 148 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 149 |
+
gr.Markdown("# Optimiseur de Prompts IA")
|
| 150 |
+
|
| 151 |
+
with gr.Row():
|
| 152 |
+
with gr.Column():
|
| 153 |
+
input_prompt = gr.Textbox(
|
| 154 |
+
label="Prompt Initial",
|
| 155 |
+
placeholder="Entrez votre prompt ici...",
|
| 156 |
+
lines=5
|
| 157 |
+
)
|
| 158 |
+
mode = gr.Radio(
|
| 159 |
+
choices=["expert", "creative", "technical"],
|
| 160 |
+
label="Mode",
|
| 161 |
+
value="expert"
|
| 162 |
+
)
|
| 163 |
+
complexity = gr.Slider(
|
| 164 |
+
minimum=0,
|
| 165 |
+
maximum=100,
|
| 166 |
+
value=50,
|
| 167 |
+
label="Niveau de Complexité"
|
| 168 |
+
)
|
| 169 |
+
optimize_btn = gr.Button("Optimiser")
|
| 170 |
+
|
| 171 |
+
with gr.Column():
|
| 172 |
+
output = gr.JSON(label="Résultats")
|
| 173 |
+
|
| 174 |
+
optimize_btn.click(
|
| 175 |
+
fn=process_prompt,
|
| 176 |
+
inputs=[input_prompt, mode, complexity],
|
| 177 |
+
outputs=[output]
|
| 178 |
+
)
|
| 179 |
+
|
| 180 |
+
return demo
|
| 181 |
+
|
| 182 |
+
if __name__ == "__main__":
|
| 183 |
+
demo = create_interface()
|
| 184 |
+
demo.launch()
|