Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,69 +1,49 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
model_id,
|
| 11 |
-
dtype=torch.float32,
|
| 12 |
-
device_map="cpu",
|
| 13 |
-
low_cpu_mem_usage=True
|
| 14 |
)
|
| 15 |
|
| 16 |
def stream_chat(message, history):
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
conversation = f"System: {system_message}\n"
|
| 21 |
|
| 22 |
for msg in history:
|
| 23 |
-
role =
|
| 24 |
-
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
pad_token_id=tokenizer.eos_token_id
|
| 38 |
)
|
| 39 |
|
| 40 |
-
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
| 41 |
-
thread.start()
|
| 42 |
-
|
| 43 |
partial_text = ""
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
partial_text += new_text
|
| 49 |
-
|
| 50 |
-
# Check if the model is drifting into reasoning
|
| 51 |
-
if any(trigger in partial_text for trigger in stop_triggers):
|
| 52 |
-
# Clean up the output to remove the trigger word itself
|
| 53 |
-
for trigger in stop_triggers:
|
| 54 |
-
if trigger in partial_text:
|
| 55 |
-
partial_text = partial_text.split(trigger)[0]
|
| 56 |
-
yield partial_text.strip()
|
| 57 |
-
break
|
| 58 |
-
|
| 59 |
-
yield partial_text.strip()
|
| 60 |
|
| 61 |
with gr.Blocks() as demo:
|
| 62 |
gr.ChatInterface(
|
| 63 |
fn=stream_chat,
|
| 64 |
type="messages",
|
| 65 |
-
title="
|
| 66 |
-
description="
|
| 67 |
)
|
| 68 |
|
| 69 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from llama_cpp import Llama
|
| 3 |
+
|
| 4 |
+
# Initialize the model
|
| 5 |
+
# n_ctx is the context window; 2048 is plenty for fast chat
|
| 6 |
+
llm = Llama(
|
| 7 |
+
model_path="llama3.2-1b-Uncensored-Q4_K_M.gguf",
|
| 8 |
+
n_ctx=2048,
|
| 9 |
+
n_threads=4 # Optimized for Hugging Face Free Tier CPU
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
)
|
| 11 |
|
| 12 |
def stream_chat(message, history):
|
| 13 |
+
# Llama 3 Prompt Format - Strict Directives
|
| 14 |
+
prompt = f"<|begin_of_text|><|start_header_id|>system<|end_header_id|>\n\n" \
|
| 15 |
+
f"You are a direct AI. Respond immediately. No thinking. No internal monologue.<|eot_id|>"
|
|
|
|
| 16 |
|
| 17 |
for msg in history:
|
| 18 |
+
role = msg['role']
|
| 19 |
+
content = msg['content']
|
| 20 |
+
prompt += f"<|start_header_id|>{role}<|end_header_id|>\n\n{content}<|eot_id|>"
|
| 21 |
|
| 22 |
+
prompt += f"<|start_header_id|>user<|end_header_id|>\n\n{message}<|eot_id|>" \
|
| 23 |
+
f"<|start_header_id|>assistant<|end_header_id|>\n\n"
|
| 24 |
+
|
| 25 |
+
# Streaming generation
|
| 26 |
+
stream = llm(
|
| 27 |
+
prompt,
|
| 28 |
+
max_tokens=512,
|
| 29 |
+
stop=["<|eot_id|>", "User:", "Assistant:"],
|
| 30 |
+
stream=True,
|
| 31 |
+
temperature=0, # GREEDY: Absolute directness, no 'wandering' thoughts
|
| 32 |
+
repeat_penalty=1.2
|
|
|
|
| 33 |
)
|
| 34 |
|
|
|
|
|
|
|
|
|
|
| 35 |
partial_text = ""
|
| 36 |
+
for output in stream:
|
| 37 |
+
token = output["choices"][0]["text"]
|
| 38 |
+
partial_text += token
|
| 39 |
+
yield partial_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
with gr.Blocks() as demo:
|
| 42 |
gr.ChatInterface(
|
| 43 |
fn=stream_chat,
|
| 44 |
type="messages",
|
| 45 |
+
title="LLAMA-3.2-1B UNCENSORED (GGUF)",
|
| 46 |
+
description="Running on Llama.cpp for maximum CPU speed. No-thinking mode active."
|
| 47 |
)
|
| 48 |
|
| 49 |
if __name__ == "__main__":
|