Wessym commited on
Commit
3cdc66d
·
verified ·
1 Parent(s): fb9dfb2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -54
app.py CHANGED
@@ -2,11 +2,10 @@ import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import os
4
 
5
- # ============================================================
6
- # CONFIG
7
- # ============================================================
8
- REPO_ID = "Wessym/prompt-refiner-mistral7b-Q4_K_M-GGUF"
9
- FILENAME = "prompt-refiner-mistral7b-Q4_K_M.gguf"
10
 
11
  SYSTEM_PROMPT = (
12
  'You are an expert Prompt Engineer. '
@@ -28,65 +27,35 @@ SYSTEM_PROMPT = (
28
  'Always respond in the same language as the input.'
29
  )
30
 
31
- # ============================================================
32
- # CHARGEMENT DU MODELE (une seule fois au démarrage)
33
- # ============================================================
34
- print("Chargement du modèle...")
35
- model_path = hf_hub_download(
36
- repo_id = REPO_ID,
37
- filename = FILENAME,
38
- token = os.environ.get("HF_TOKEN"),
39
- )
40
-
41
- llm = Llama(
42
- model_path = model_path,
43
- n_ctx = 2048,
44
- n_threads = 4,
45
- n_gpu_layers = 0,
46
- verbose = False,
47
- )
48
- print("Modèle chargé ✅")
49
-
50
- # ============================================================
51
- # FONCTION PRINCIPALE
52
- # ============================================================
53
  def refine_prompt(user_input: str) -> str:
54
  if not user_input.strip():
55
  return "❌ Veuillez entrer un prompt."
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
- prompt = f"<s>[INST] {SYSTEM_PROMPT}\n\nUser input to process: {user_input} [/INST]"
58
-
59
- response = llm(
60
- prompt,
61
- max_tokens = 512,
62
- temperature = 0.3,
63
- top_p = 0.9,
64
- repeat_penalty = 1.1,
65
- stop = ["</s>", "[INST]"],
66
- )
67
- return response["choices"][0]["text"].strip()
68
-
69
- # ============================================================
70
- # INTERFACE GRADIO
71
- # ============================================================
72
  with gr.Blocks(title="Prompt Refiner") as demo:
73
  gr.Markdown("# 🚀 Prompt Refiner\nEntre ton prompt et il sera structuré automatiquement.")
74
-
75
  with gr.Row():
76
  input_box = gr.Textbox(label="Ton prompt", placeholder="Ex: Crée un script YouTube sur l'IA...", lines=3)
77
  output_box = gr.Textbox(label="Prompt raffiné", lines=10)
78
-
79
  btn = gr.Button("Raffiner ✨", variant="primary")
80
  btn.click(fn=refine_prompt, inputs=input_box, outputs=output_box)
81
 
82
- gr.Examples(
83
- examples=[
84
- ["Create a YouTube script about AI for beginners"],
85
- ["Rédige un article de blog sur le marketing digital"],
86
- ["bonjour je mappelle wassim"],
87
- ["qsdqsdqsdqs"],
88
- ],
89
- inputs=input_box,
90
- )
91
 
92
- demo.launch()
 
 
 
 
2
  from huggingface_hub import InferenceClient
3
  import os
4
 
5
+ HF_TOKEN = os.environ.get("HF_TOKEN")
6
+ MODEL_ID = "Wessym/prompt-refiner-mistral7b"
7
+
8
+ client = InferenceClient(model=MODEL_ID, token=HF_TOKEN)
 
9
 
10
  SYSTEM_PROMPT = (
11
  'You are an expert Prompt Engineer. '
 
27
  'Always respond in the same language as the input.'
28
  )
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  def refine_prompt(user_input: str) -> str:
31
  if not user_input.strip():
32
  return "❌ Veuillez entrer un prompt."
33
+ try:
34
+ messages = [
35
+ {"role": "system", "content": SYSTEM_PROMPT},
36
+ {"role": "user", "content": f"User input to process: {user_input}"}
37
+ ]
38
+ response = client.chat_completion(
39
+ messages = messages,
40
+ max_tokens = 512,
41
+ temperature = 0.3,
42
+ )
43
+ return response.choices[0].message.content.strip()
44
+ except Exception as e:
45
+ return f"❌ Erreur: {str(e)}"
46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  with gr.Blocks(title="Prompt Refiner") as demo:
48
  gr.Markdown("# 🚀 Prompt Refiner\nEntre ton prompt et il sera structuré automatiquement.")
 
49
  with gr.Row():
50
  input_box = gr.Textbox(label="Ton prompt", placeholder="Ex: Crée un script YouTube sur l'IA...", lines=3)
51
  output_box = gr.Textbox(label="Prompt raffiné", lines=10)
 
52
  btn = gr.Button("Raffiner ✨", variant="primary")
53
  btn.click(fn=refine_prompt, inputs=input_box, outputs=output_box)
54
 
55
+ demo.launch()
56
+ ```
 
 
 
 
 
 
 
57
 
58
+ Et le `requirements.txt` doit contenir seulement :
59
+ ```
60
+ gradio
61
+ huggingface_hub