XY26 commited on
Commit
f340f2e
·
verified ·
1 Parent(s): fa636c4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -14
app.py CHANGED
@@ -20,27 +20,43 @@ llm = Llama(
20
 
21
  def smart_response(message, history):
22
 
23
- # --- SMART ROUTER ---
24
- triggers = ["should", "opinion", "think", "good", "bad", "pros", "cons", "benefit", "risk", "impact", "why"]
 
 
 
 
 
 
 
 
 
 
25
  is_opinion = any(t in message.lower() for t in triggers)
26
 
 
27
  if is_opinion:
28
  print(f"🧠 OPINION MODE: {message}")
29
- # DUAL FRAME PROMPT
30
- # We use the standard instruction format the model learned during fine-tuning.
 
31
  prompt = f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.
32
 
33
  ### Instruction:
34
- {message}
 
 
 
 
35
 
36
  ### Response:
37
- """
 
 
 
38
 
39
  else:
40
  print(f"ℹ️ CHAT MODE: {message}")
41
- # FACTUAL PROMPT
42
- # CRITICAL FIX: We use the SAME format (Alpaca), but we add a specific
43
- # instruction telling the model to be "concise" and "factual".
44
  prompt = f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.
45
 
46
  ### Instruction:
@@ -48,11 +64,10 @@ You are a helpful assistant. Answer the following question concisely and directl
48
 
49
  ### Response:
50
  """
 
51
 
52
- # --- GENERATION ---
53
- # We add "###" to the stop tokens. This cuts off the model immediately
54
- # if it tries to start generating "Solution 1" or a new "### Instruction".
55
- stop_tokens = ["</s>", "###", "Solution"]
56
 
57
  output = llm(
58
  prompt,
@@ -62,7 +77,11 @@ You are a helpful assistant. Answer the following question concisely and directl
62
  echo=False
63
  )
64
 
65
- return output['choices'][0]['text'].strip()
 
 
 
 
66
 
67
  # 3. Launch Interface
68
  demo = gr.ChatInterface(
 
20
 
21
  def smart_response(message, history):
22
 
23
+ # --- 1. EXPANDED TRIGGERS ---
24
+ # We add common decision words (English & French) to ensure we catch everything.
25
+ triggers = [
26
+ # English
27
+ "should", "opinion", "think", "good", "bad", "pros", "cons",
28
+ "benefit", "risk", "impact", "why", "better", "safe", "worth",
29
+ "buy", "choose", "prefer", "best",
30
+ # French
31
+ "sain", "dangereux", "mieux", "avis", "pensez", "effet",
32
+ "faut", "acheter", "choisir", "bien", "mal", "pourquoi"
33
+ ]
34
+
35
  is_opinion = any(t in message.lower() for t in triggers)
36
 
37
+ # --- 2. RESPONSE PRIMING ---
38
  if is_opinion:
39
  print(f"🧠 OPINION MODE: {message}")
40
+
41
+ # We start writing the response FOR the model.
42
+ # By adding "**POSITIVE FRAMING:**" at the end, the model HAS to fill it in.
43
  prompt = f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.
44
 
45
  ### Instruction:
46
+ Analyze the following topic. You must provide the answer in two distinct frames:
47
+ 1. POSITIVE FRAMING (Arguments For / Benefits)
48
+ 2. NEGATIVE FRAMING (Arguments Against / Risks)
49
+
50
+ Topic: {message}
51
 
52
  ### Response:
53
+ **POSITIVE FRAMING:**"""
54
+
55
+ # We remove "POSITIVE FRAMING" from the output later so it doesn't print twice
56
+ prefix_to_add = "**POSITIVE FRAMING:**"
57
 
58
  else:
59
  print(f"ℹ️ CHAT MODE: {message}")
 
 
 
60
  prompt = f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.
61
 
62
  ### Instruction:
 
64
 
65
  ### Response:
66
  """
67
+ prefix_to_add = ""
68
 
69
+ # --- 3. GENERATION ---
70
+ stop_tokens = ["</s>", "###", "User:", "Instruction:"]
 
 
71
 
72
  output = llm(
73
  prompt,
 
77
  echo=False
78
  )
79
 
80
+ text = output['choices'][0]['text'].strip()
81
+
82
+ # If we used priming, we need to attach the prefix back to the start
83
+ final_response = prefix_to_add + " " + text
84
+ return final_response
85
 
86
  # 3. Launch Interface
87
  demo = gr.ChatInterface(