File size: 2,843 Bytes
d051039
 
9351e2e
d051039
1be0657
9351e2e
 
 
 
 
d051039
1be0657
9351e2e
d051039
9351e2e
c63666e
 
9351e2e
d051039
 
9351e2e
d051039
f340f2e
 
 
 
 
 
 
 
 
 
 
 
9351e2e
 
f340f2e
9351e2e
1be0657
f340f2e
 
 
1be0657
 
d051039
f340f2e
 
 
 
 
1be0657
d051039
f340f2e
 
 
 
a33411c
d051039
991b2a9
fa636c4
 
 
 
 
 
 
f340f2e
571c45a
f340f2e
 
fa636c4
a33411c
1be0657
4cc5b9b
 
024fe47
571c45a
d051039
 
f340f2e
 
 
 
 
d051039
9351e2e
 
 
d051039
bde323a
d051039
 
 
024fe47
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import gradio as gr
from llama_cpp import Llama
from huggingface_hub import hf_hub_download

# 1. Download Model
print("⬇️ Downloading model...")
model_path = hf_hub_download(
    repo_id="XY26/dual-frame-mistral-7",
    filename="mistral-7b-v0.3.Q4_K_M.gguf"
)

# 2. Load Engine
print("⚙️ Loading engine...")
llm = Llama(
    model_path=model_path,
    n_ctx=4096,
    n_threads=2,
    verbose=False
)

def smart_response(message, history):
    
    # --- 1. EXPANDED TRIGGERS ---
    # We add common decision words (English & French) to ensure we catch everything.
    triggers = [
        # English
        "should", "opinion", "think", "good", "bad", "pros", "cons", 
        "benefit", "risk", "impact", "why", "better", "safe", "worth", 
        "buy", "choose", "prefer", "best",
        # French
        "sain", "dangereux", "mieux", "avis", "pensez", "effet", 
        "faut", "acheter", "choisir", "bien", "mal", "pourquoi"
    ]
    
    is_opinion = any(t in message.lower() for t in triggers)

    # --- 2. RESPONSE PRIMING ---
    if is_opinion:
        print(f"🧠 OPINION MODE: {message}")
        
        # We start writing the response FOR the model.
        # By adding "**POSITIVE FRAMING:**" at the end, the model HAS to fill it in.
        prompt = f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.

### Instruction:
Analyze the following topic. You must provide the answer in two distinct frames:
1. POSITIVE FRAMING (Arguments For / Benefits)
2. NEGATIVE FRAMING (Arguments Against / Risks)

Topic: {message}

### Response:
**POSITIVE FRAMING:**"""
        
        # We remove "POSITIVE FRAMING" from the output later so it doesn't print twice
        prefix_to_add = "**POSITIVE FRAMING:**"
        
    else:
        print(f"ℹ️ CHAT MODE: {message}")
        prompt = f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.

### Instruction:
You are a helpful assistant. Answer the following question concisely and directly: {message}

### Response:
"""
        prefix_to_add = ""

    # --- 3. GENERATION ---
    stop_tokens = ["</s>", "###", "User:", "Instruction:"]

    output = llm(
        prompt,
        max_tokens=512,
        temperature=0.7,
        stop=stop_tokens,
        echo=False
    )
    
    text = output['choices'][0]['text'].strip()
    
    # If we used priming, we need to attach the prefix back to the start
    final_response = prefix_to_add + " " + text
    return final_response

# 3. Launch Interface
demo = gr.ChatInterface(
    fn=smart_response,
    title="Dual-Mind Mistral 🧠",
    description="Ask a Fact (Normal Chat) or an Opinion (Dual Perspectives)."
)

if __name__ == "__main__":
    demo.queue().launch(server_name="0.0.0.0", server_port=7860)