Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -21,11 +21,13 @@ llm = Llama(
|
|
| 21 |
def smart_response(message, history):
|
| 22 |
|
| 23 |
# --- SMART ROUTER ---
|
| 24 |
-
|
|
|
|
| 25 |
is_opinion = any(t in message.lower() for t in triggers)
|
| 26 |
|
| 27 |
if is_opinion:
|
| 28 |
print(f"🧠 OPINION MODE: {message}")
|
|
|
|
| 29 |
prompt = f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.
|
| 30 |
|
| 31 |
### Instruction:
|
|
@@ -35,28 +37,37 @@ def smart_response(message, history):
|
|
| 35 |
"""
|
| 36 |
else:
|
| 37 |
print(f"ℹ️ CHAT MODE: {message}")
|
| 38 |
-
#
|
| 39 |
prompt = f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.
|
| 40 |
|
| 41 |
### Instruction:
|
| 42 |
-
Answer the following question concisely
|
|
|
|
| 43 |
|
| 44 |
### Response:
|
| 45 |
"""
|
| 46 |
|
| 47 |
-
# --- GENERATION
|
| 48 |
stream = llm(
|
| 49 |
prompt,
|
| 50 |
max_tokens=512,
|
| 51 |
temperature=0.7,
|
| 52 |
-
stop=["</s>", "###", "Instruction:"
|
| 53 |
-
stream=True,
|
| 54 |
echo=False
|
| 55 |
)
|
| 56 |
|
| 57 |
partial_message = ""
|
| 58 |
for output in stream:
|
| 59 |
token = output['choices'][0]['text']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
partial_message += token
|
| 61 |
yield partial_message
|
| 62 |
|
|
|
|
| 21 |
def smart_response(message, history):
|
| 22 |
|
| 23 |
# --- SMART ROUTER ---
|
| 24 |
+
# We check if the user is asking for an opinion/analysis vs a simple fact
|
| 25 |
+
triggers = ["should", "opinion", "think", "good", "bad", "pros", "cons", "benefit", "risk", "impact", "why", "compare", "difference"]
|
| 26 |
is_opinion = any(t in message.lower() for t in triggers)
|
| 27 |
|
| 28 |
if is_opinion:
|
| 29 |
print(f"🧠 OPINION MODE: {message}")
|
| 30 |
+
# Use the format the model was fine-tuned on to trigger the Dual behavior
|
| 31 |
prompt = f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.
|
| 32 |
|
| 33 |
### Instruction:
|
|
|
|
| 37 |
"""
|
| 38 |
else:
|
| 39 |
print(f"ℹ️ CHAT MODE: {message}")
|
| 40 |
+
# STRONG OVERRIDE: We explicitly tell the model strictly NOT to use framing.
|
| 41 |
prompt = f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.
|
| 42 |
|
| 43 |
### Instruction:
|
| 44 |
+
You are a factual assistant. Answer the following question directly and concisely. Do NOT use positive or negative framing.
|
| 45 |
+
Question: {message}
|
| 46 |
|
| 47 |
### Response:
|
| 48 |
"""
|
| 49 |
|
| 50 |
+
# --- GENERATION ---
|
| 51 |
stream = llm(
|
| 52 |
prompt,
|
| 53 |
max_tokens=512,
|
| 54 |
temperature=0.7,
|
| 55 |
+
stop=["</s>", "###", "Instruction:"],
|
| 56 |
+
stream=True,
|
| 57 |
echo=False
|
| 58 |
)
|
| 59 |
|
| 60 |
partial_message = ""
|
| 61 |
for output in stream:
|
| 62 |
token = output['choices'][0]['text']
|
| 63 |
+
|
| 64 |
+
# --- SAFETY NET ---
|
| 65 |
+
# If the model tries to output the headers in Chat Mode, we hide them.
|
| 66 |
+
# This acts as a filter for the "overfitting" behavior.
|
| 67 |
+
if not is_opinion:
|
| 68 |
+
if "**POSITIVE FRAMING:**" in token or "**NEGATIVE FRAMING:**" in token:
|
| 69 |
+
continue # Skip printing these specific tokens
|
| 70 |
+
|
| 71 |
partial_message += token
|
| 72 |
yield partial_message
|
| 73 |
|