Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,63 +1,72 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from llama_cpp import Llama
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
# Load the
|
|
|
|
| 9 |
llm = Llama(
|
| 10 |
-
model_path=
|
| 11 |
-
n_ctx=
|
| 12 |
-
n_threads=2
|
|
|
|
| 13 |
)
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 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 |
-
#
|
| 27 |
-
if
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
### Instruction:
|
| 32 |
{}
|
| 33 |
### Response:
|
| 34 |
"""
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
else:
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
prompt = system_prompt.format(message)
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
max_tokens=
|
| 48 |
-
|
| 49 |
-
|
| 50 |
)
|
| 51 |
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
#
|
| 55 |
-
|
| 56 |
-
fn=
|
| 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 |
-
|
|
|
|
| 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)
|