Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,107 +1,92 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
from transformers import
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
print(
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
"{
|
| 30 |
-
"
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
"{
|
| 34 |
-
"
|
| 35 |
-
"{{ content }}"
|
| 36 |
-
"{% endfor %}"
|
| 37 |
-
"{% if add_generation_prompt %}"
|
| 38 |
-
"{{ '<|start_header_id|>assistant<|end_header_id|>\n\n' }}"
|
| 39 |
-
"{% endif %}"
|
| 40 |
)
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
except Exception as e:
|
| 45 |
-
print(f"Error loading model. Ensure you have a valid HF_TOKEN and access to the gated repo. Error: {e}")
|
| 46 |
-
raise e
|
| 47 |
|
| 48 |
-
# ---
|
| 49 |
def respond(
|
| 50 |
message,
|
| 51 |
history: list[dict],
|
| 52 |
-
system_message_dummy,
|
| 53 |
max_tokens,
|
| 54 |
temperature,
|
| 55 |
top_p,
|
| 56 |
repetition_penalty,
|
| 57 |
style_mode,
|
| 58 |
):
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
#
|
| 67 |
-
|
| 68 |
-
history = history[-10:]
|
| 69 |
-
|
| 70 |
-
# Build messages
|
| 71 |
-
messages = [{"role": "system", "content": system_prompt}]
|
| 72 |
-
for turn in history:
|
| 73 |
messages.append({"role": turn['role'], "content": turn['content']})
|
|
|
|
|
|
|
| 74 |
messages.append({"role": "user", "content": message})
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
add_generation_prompt=True
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
]
|
| 87 |
-
|
| 88 |
-
# Generate
|
| 89 |
-
outputs = model.generate(
|
| 90 |
-
input_ids,
|
| 91 |
-
max_new_tokens=int(max_tokens),
|
| 92 |
-
eos_token_id=terminators,
|
| 93 |
temperature=float(temperature),
|
| 94 |
top_p=float(top_p),
|
| 95 |
-
|
| 96 |
-
|
|
|
|
| 97 |
)
|
| 98 |
|
| 99 |
-
|
| 100 |
-
decoded_response = tokenizer.decode(response, skip_special_tokens=True)
|
| 101 |
-
return decoded_response
|
| 102 |
|
| 103 |
-
# ---
|
| 104 |
-
# (Kept identical to previous, just updated title)
|
| 105 |
chatbot = gr.ChatInterface(
|
| 106 |
respond,
|
| 107 |
type="messages",
|
|
@@ -111,13 +96,18 @@ chatbot = gr.ChatInterface(
|
|
| 111 |
gr.Slider(minimum=0.1, maximum=2.0, value=0.7, label="Temperature"),
|
| 112 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.9, label="Top-p"),
|
| 113 |
gr.Slider(minimum=1.0, maximum=2.0, value=1.1, step=0.05, label="Repetition Penalty"),
|
| 114 |
-
gr.Dropdown(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
],
|
| 116 |
)
|
| 117 |
|
| 118 |
with gr.Blocks() as demo:
|
| 119 |
-
gr.Markdown("# Chat
|
| 120 |
-
gr.
|
|
|
|
| 121 |
chatbot.render()
|
| 122 |
|
| 123 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from llama_cpp import Llama
|
| 3 |
+
from transformers import AutoTokenizer
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
MODEL_REPO = "simonper/Llama-3.2-1B-bnb-4bit_untrained_gguf_4bit"
|
| 7 |
+
MODEL_FILE = "Llama-3.2-1B.Q4_K_M.gguf"
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
TOKENIZER_ID = "chthees/lora_model_full_finetome-tokenizer"
|
| 11 |
+
|
| 12 |
+
print("Loading Tokenizer...")
|
| 13 |
+
tokenizer = AutoTokenizer.from_pretrained(TOKENIZER_ID)
|
| 14 |
+
|
| 15 |
+
print("Loading Model...")
|
| 16 |
+
llm = Llama.from_pretrained(
|
| 17 |
+
repo_id=MODEL_REPO,
|
| 18 |
+
filename=MODEL_FILE,
|
| 19 |
+
n_ctx=2048,
|
| 20 |
+
n_threads=2,
|
| 21 |
+
verbose=False
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
# --- SYSTEM PROMPT LOGIC ---
|
| 25 |
+
def get_system_prompt(style_mode):
|
| 26 |
+
base_instruction = "You are a helpful and intelligent AI assistant."
|
| 27 |
|
| 28 |
+
prompts = {
|
| 29 |
+
"Normal": f"{base_instruction} Answer clearly and concisely.",
|
| 30 |
+
"Professional": (
|
| 31 |
+
f"{base_instruction} You are a senior corporate executive. "
|
| 32 |
+
"Your tone is strictly professional, polite, and business-oriented."
|
| 33 |
+
),
|
| 34 |
+
"Shakespeare": (
|
| 35 |
+
f"{base_instruction} You are William Shakespeare. "
|
| 36 |
+
"Speak only in Early Modern English (thee, thou, hath). Be poetic and dramatic."
|
| 37 |
+
),
|
| 38 |
+
"Funny/Ironic": (
|
| 39 |
+
f"{base_instruction} You are a sarcastic comedian. "
|
| 40 |
+
"Wrap your answers in dry humor, irony, and witty remarks."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
)
|
| 42 |
+
}
|
| 43 |
+
return prompts.get(style_mode, prompts["Normal"])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
# --- CORE RESPONSE FUNCTION ---
|
| 46 |
def respond(
|
| 47 |
message,
|
| 48 |
history: list[dict],
|
| 49 |
+
system_message_dummy,
|
| 50 |
max_tokens,
|
| 51 |
temperature,
|
| 52 |
top_p,
|
| 53 |
repetition_penalty,
|
| 54 |
style_mode,
|
| 55 |
):
|
| 56 |
+
messages = []
|
| 57 |
+
|
| 58 |
+
# Add System Persona
|
| 59 |
+
system_prompt = get_system_prompt(style_mode)
|
| 60 |
+
messages.append({"role": "system", "content": system_prompt})
|
| 61 |
+
|
| 62 |
+
# Add Conversation History
|
| 63 |
+
# We slice to the last 10 turns to keep the context window manageable
|
| 64 |
+
for turn in history[-10:]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
messages.append({"role": turn['role'], "content": turn['content']})
|
| 66 |
+
|
| 67 |
+
# Add Current User Message
|
| 68 |
messages.append({"role": "user", "content": message})
|
| 69 |
|
| 70 |
+
prompt_str = tokenizer.apply_chat_template(
|
| 71 |
+
messages,
|
| 72 |
+
tokenize=False,
|
| 73 |
+
add_generation_prompt=True
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
# 3. Generate Response
|
| 77 |
+
output = llm(
|
| 78 |
+
prompt_str,
|
| 79 |
+
max_tokens=int(max_tokens),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
temperature=float(temperature),
|
| 81 |
top_p=float(top_p),
|
| 82 |
+
repeat_penalty=float(repetition_penalty),
|
| 83 |
+
stop=[tokenizer.eos_token, "<|eot_id|>"],
|
| 84 |
+
echo=False
|
| 85 |
)
|
| 86 |
|
| 87 |
+
return output["choices"][0]["text"].strip()
|
|
|
|
|
|
|
| 88 |
|
| 89 |
+
# --- GUI SETUP ---
|
|
|
|
| 90 |
chatbot = gr.ChatInterface(
|
| 91 |
respond,
|
| 92 |
type="messages",
|
|
|
|
| 96 |
gr.Slider(minimum=0.1, maximum=2.0, value=0.7, label="Temperature"),
|
| 97 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.9, label="Top-p"),
|
| 98 |
gr.Slider(minimum=1.0, maximum=2.0, value=1.1, step=0.05, label="Repetition Penalty"),
|
| 99 |
+
gr.Dropdown(
|
| 100 |
+
choices=["Normal", "Professional", "Shakespeare", "Funny/Ironic"],
|
| 101 |
+
value="Normal",
|
| 102 |
+
label="Choose the Style / Tone"
|
| 103 |
+
)
|
| 104 |
],
|
| 105 |
)
|
| 106 |
|
| 107 |
with gr.Blocks() as demo:
|
| 108 |
+
gr.Markdown("# Styled Chat Bot")
|
| 109 |
+
with gr.Sidebar():
|
| 110 |
+
gr.LoginButton()
|
| 111 |
chatbot.render()
|
| 112 |
|
| 113 |
if __name__ == "__main__":
|