Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import gradio as gr
|
|
| 2 |
from llama_cpp import Llama
|
| 3 |
from huggingface_hub import hf_hub_download
|
| 4 |
|
| 5 |
-
# 1. Download
|
| 6 |
print("⬇️ Downloading model...")
|
| 7 |
model_path = hf_hub_download(
|
| 8 |
repo_id="XY26/dual-frame-mistral-7",
|
|
@@ -13,8 +13,8 @@ model_path = hf_hub_download(
|
|
| 13 |
print("⚙️ Loading engine...")
|
| 14 |
llm = Llama(
|
| 15 |
model_path=model_path,
|
| 16 |
-
n_ctx=4096,
|
| 17 |
-
n_threads=2,
|
| 18 |
verbose=False
|
| 19 |
)
|
| 20 |
|
|
@@ -22,26 +22,24 @@ def smart_response(message, history):
|
|
| 22 |
messages = []
|
| 23 |
|
| 24 |
# --- SMART ROUTER LOGIC ---
|
| 25 |
-
#
|
| 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 |
-
#
|
| 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
|
| 45 |
messages.append({"role": "system", "content": "You are a helpful assistant. Answer concisely."})
|
| 46 |
messages.append({"role": "user", "content": message})
|
| 47 |
|
|
@@ -50,7 +48,9 @@ def smart_response(message, history):
|
|
| 50 |
messages=messages,
|
| 51 |
max_tokens=1024,
|
| 52 |
stream=True,
|
| 53 |
-
temperature=0.7
|
|
|
|
|
|
|
| 54 |
)
|
| 55 |
|
| 56 |
partial_message = ""
|
|
@@ -64,8 +64,7 @@ def smart_response(message, history):
|
|
| 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__":
|
|
|
|
| 2 |
from llama_cpp import Llama
|
| 3 |
from huggingface_hub import hf_hub_download
|
| 4 |
|
| 5 |
+
# 1. Download Your Mistral Model (Corrected ID)
|
| 6 |
print("⬇️ Downloading model...")
|
| 7 |
model_path = hf_hub_download(
|
| 8 |
repo_id="XY26/dual-frame-mistral-7",
|
|
|
|
| 13 |
print("⚙️ Loading engine...")
|
| 14 |
llm = Llama(
|
| 15 |
model_path=model_path,
|
| 16 |
+
n_ctx=4096,
|
| 17 |
+
n_threads=2,
|
| 18 |
verbose=False
|
| 19 |
)
|
| 20 |
|
|
|
|
| 22 |
messages = []
|
| 23 |
|
| 24 |
# --- SMART ROUTER LOGIC ---
|
| 25 |
+
# Check if user wants an Opinion (Dual Frame) or Fact (Standard)
|
| 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 |
+
# Inject the Fine-Tuned Prompt Format
|
| 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 |
final_prompt = system_content.format(message)
|
| 38 |
messages.append({"role": "user", "content": final_prompt})
|
| 39 |
|
| 40 |
else:
|
| 41 |
print(f"ℹ️ Detected FACT: {message}")
|
| 42 |
+
# Standard Prompt
|
| 43 |
messages.append({"role": "system", "content": "You are a helpful assistant. Answer concisely."})
|
| 44 |
messages.append({"role": "user", "content": message})
|
| 45 |
|
|
|
|
| 48 |
messages=messages,
|
| 49 |
max_tokens=1024,
|
| 50 |
stream=True,
|
| 51 |
+
temperature=0.7,
|
| 52 |
+
# CRITICAL FIX: Stop tokens prevent the AI from talking to itself
|
| 53 |
+
stop=["</s>", "User:", "###", "Instruction:", "<|endoftext|>"]
|
| 54 |
)
|
| 55 |
|
| 56 |
partial_message = ""
|
|
|
|
| 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 |
)
|
| 69 |
|
| 70 |
if __name__ == "__main__":
|