XY26 commited on
Commit
9351e2e
·
verified ·
1 Parent(s): 5a44f3a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -39
app.py CHANGED
@@ -1,63 +1,72 @@
1
  import gradio as gr
2
  from llama_cpp import Llama
 
3
 
4
- # --- 1. SETUP: Load the Model ---
5
- model_id = "XY26/dual-frame-mistral-7"
6
- filename = "mistral-7b-v0.3.Q4_K_M.gguf"
 
 
 
7
 
8
- # Load the model (CPU Optimized)
 
9
  llm = Llama(
10
- model_path=gr.utils.download_from_hub(model_id, filename),
11
- n_ctx=2048, # Context window
12
- n_threads=2 # CPU threads (Keep low for free tier)
 
13
  )
14
 
15
- # --- 2. THE SMART LOGIC (The Router) ---
16
- def is_opinion_question(text):
17
- """
18
- Simple keyword detection to guess if a user wants an opinion or a fact.
19
- """
20
- triggers = ["should", "opinion", "think", "good", "bad", "pros", "cons", "benefit", "risk", "impact", "effect", "analyze", "perspectives"]
21
- text_lower = text.lower()
22
- return any(trigger in text_lower for trigger in triggers)
23
-
24
- def generate_response(message, history):
25
 
26
- # DECISION TIME: Fact vs Opinion
27
- if is_opinion_question(message):
28
- # MODE A: DUAL FRAMING (Inject the special prompt)
29
- print(f"Detected OPINION: {message}")
30
- system_prompt = """Below is an instruction that describes a task. Write a response that appropriately completes the request.
 
 
 
 
31
  ### Instruction:
32
  {}
33
  ### Response:
34
  """
35
- prompt = system_prompt.format(message)
 
 
 
36
 
37
  else:
38
- # MODE B: FACTUAL (Inject a strict 'normal' prompt)
39
- print(f"Detected FACT: {message}")
40
- system_prompt = """[INST] You are a helpful assistant. Answer the following question concisely and accurately. Do not use positive/negative framing headers.
41
- Question: {} [/INST]"""
42
- prompt = system_prompt.format(message)
43
 
44
- # Generate
45
- output = llm(
46
- prompt,
47
- max_tokens=512,
48
- stop=["</s>", "[/INST]"],
49
- echo=False
50
  )
51
 
52
- return output['choices'][0]['text']
 
 
 
 
 
53
 
54
- # --- 3. THE INTERFACE ---
55
- interface = gr.ChatInterface(
56
- fn=generate_response,
57
  title="Dual-Mind Mistral 🧠",
58
  description="Ask a factual question (e.g., 'Capital of France') for a normal answer.\nAsk a complex question (e.g., 'Should I use AI?') to get Dual Perspectives.",
59
  examples=["What is the capital of France?", "Should schools ban smartphones?", "Is nuclear energy safe?", "1 + 1 = ?"],
60
  )
61
 
62
  if __name__ == "__main__":
63
- interface.launch()
 
1
  import gradio as gr
2
  from llama_cpp import Llama
3
+ from huggingface_hub import hf_hub_download
4
 
5
+ # 1. Download the Model
6
+ print("⬇️ Downloading model...")
7
+ model_path = hf_hub_download(
8
+ repo_id="XY26/dual-frame-mistral-7",
9
+ filename="mistral-7b-v0.3.Q4_K_M.gguf"
10
+ )
11
 
12
+ # 2. Load the Engine
13
+ print("⚙️ Loading engine...")
14
  llm = Llama(
15
+ model_path=model_path,
16
+ n_ctx=4096, # Context window
17
+ n_threads=2, # CPU threads
18
+ verbose=False
19
  )
20
 
21
+ def smart_response(message, history):
22
+ messages = []
 
 
 
 
 
 
 
 
23
 
24
+ # --- SMART ROUTER LOGIC ---
25
+ # We check if the user is asking for an opinion/debate vs a simple fact
26
+ triggers = ["should", "opinion", "think", "good", "bad", "pros", "cons", "benefit", "risk", "impact"]
27
+ is_opinion = any(t in message.lower() for t in triggers)
28
+
29
+ if is_opinion:
30
+ print(f"🧠 Detected OPINION: {message}")
31
+ # Force the Dual Framing prompt
32
+ system_content = """Below is an instruction that describes a task. Write a response that appropriately completes the request.
33
  ### Instruction:
34
  {}
35
  ### Response:
36
  """
37
+ # For this model format, we inject the instruction into the system prompt structure or user message
38
+ # We will format it as a single user message for simplicity with this model
39
+ final_prompt = system_content.format(message)
40
+ messages.append({"role": "user", "content": final_prompt})
41
 
42
  else:
43
+ print(f"ℹ️ Detected FACT: {message}")
44
+ # Standard helpful assistant behavior
45
+ messages.append({"role": "system", "content": "You are a helpful assistant. Answer concisely."})
46
+ messages.append({"role": "user", "content": message})
 
47
 
48
+ # --- GENERATION ---
49
+ stream = llm.create_chat_completion(
50
+ messages=messages,
51
+ max_tokens=1024,
52
+ stream=True,
53
+ temperature=0.7
54
  )
55
 
56
+ partial_message = ""
57
+ for chunk in stream:
58
+ delta = chunk['choices'][0]['delta']
59
+ if 'content' in delta:
60
+ partial_message += delta['content']
61
+ yield partial_message
62
 
63
+ # 3. Launch Interface
64
+ demo = gr.ChatInterface(
65
+ fn=smart_response,
66
  title="Dual-Mind Mistral 🧠",
67
  description="Ask a factual question (e.g., 'Capital of France') for a normal answer.\nAsk a complex question (e.g., 'Should I use AI?') to get Dual Perspectives.",
68
  examples=["What is the capital of France?", "Should schools ban smartphones?", "Is nuclear energy safe?", "1 + 1 = ?"],
69
  )
70
 
71
  if __name__ == "__main__":
72
+ demo.queue().launch(server_name="0.0.0.0", server_port=7860)