Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,13 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from llama_cpp import Llama
|
| 3 |
|
| 4 |
-
# --- 1. MODELL LADEN ---
|
| 5 |
llm = Llama.from_pretrained(
|
| 6 |
repo_id="simonper/Llama-3.2-1B-bnb-4bit_finetome-100k_gguf_3epochs_4bit",
|
| 7 |
filename="Llama-3.2-1B.Q4_K_M.gguf",
|
| 8 |
n_ctx=2048,
|
| 9 |
-
n_threads=2,
|
| 10 |
)
|
| 11 |
|
| 12 |
-
# --- 2. HELPER: PROMPT BAUEN ---
|
| 13 |
def build_prompt(system_message: str, history: list[dict], user_message: str) -> str:
|
| 14 |
lines = []
|
| 15 |
if system_message:
|
|
@@ -25,7 +23,7 @@ def build_prompt(system_message: str, history: list[dict], user_message: str) ->
|
|
| 25 |
lines.append("Assistant:")
|
| 26 |
return "\n".join(lines)
|
| 27 |
|
| 28 |
-
|
| 29 |
def respond(
|
| 30 |
message,
|
| 31 |
history: list[dict[str, str]],
|
|
@@ -33,42 +31,39 @@ def respond(
|
|
| 33 |
max_tokens,
|
| 34 |
temperature,
|
| 35 |
top_p,
|
| 36 |
-
repetition_penalty,
|
| 37 |
style_mode,
|
| 38 |
):
|
| 39 |
|
| 40 |
-
#
|
| 41 |
base_instruction = (
|
| 42 |
-
"
|
| 43 |
-
"
|
| 44 |
)
|
| 45 |
|
| 46 |
context = ""
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
| 51 |
elif style_mode == "Shakespeare":
|
| 52 |
-
context = "
|
| 53 |
-
elif style_mode == "
|
| 54 |
-
context = "
|
| 55 |
-
elif style_mode == "Lustig/Ironisch":
|
| 56 |
-
context = "Formuliere die Eingabe lustig und ironisch."
|
| 57 |
else:
|
| 58 |
-
context = "
|
| 59 |
|
| 60 |
-
final_system = f"{base_instruction} {context}
|
| 61 |
|
| 62 |
-
# --- B. Prompt bauen ---
|
| 63 |
prompt = build_prompt(final_system, history, message)
|
| 64 |
|
| 65 |
-
# --- C. Modell aufrufen ---
|
| 66 |
output = llm(
|
| 67 |
prompt,
|
| 68 |
max_tokens=int(max_tokens),
|
| 69 |
temperature=float(temperature),
|
| 70 |
top_p=float(top_p),
|
| 71 |
-
repeat_penalty=float(repetition_penalty),
|
| 72 |
stop=["User:", "System:"],
|
| 73 |
echo=False
|
| 74 |
)
|
|
@@ -84,26 +79,23 @@ chatbot = gr.ChatInterface(
|
|
| 84 |
additional_inputs=[
|
| 85 |
gr.Textbox(value="", label="System Prompt (Hidden)", visible=False),
|
| 86 |
|
| 87 |
-
# Bestehende Slider
|
| 88 |
gr.Slider(minimum=1, maximum=2048, value=512, label="Max Tokens"),
|
| 89 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, label="Temperature"),
|
| 90 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, label="Top-p"),
|
|
|
|
| 91 |
|
| 92 |
-
#
|
| 93 |
-
# Standard 1.0 = Keine Strafe. 1.2 ist meist ein guter Wert für Llama.
|
| 94 |
-
gr.Slider(minimum=1.0, maximum=2.0, value=1.2, step=0.05, label="Repetition Penalty"),
|
| 95 |
-
|
| 96 |
-
# Style Dropdown
|
| 97 |
gr.Dropdown(
|
| 98 |
-
choices=["Normal", "
|
| 99 |
value="Normal",
|
| 100 |
-
label="
|
| 101 |
)
|
| 102 |
],
|
| 103 |
)
|
| 104 |
|
| 105 |
with gr.Blocks() as demo:
|
| 106 |
-
|
|
|
|
| 107 |
with gr.Sidebar():
|
| 108 |
gr.LoginButton()
|
| 109 |
chatbot.render()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from llama_cpp import Llama
|
| 3 |
|
|
|
|
| 4 |
llm = Llama.from_pretrained(
|
| 5 |
repo_id="simonper/Llama-3.2-1B-bnb-4bit_finetome-100k_gguf_3epochs_4bit",
|
| 6 |
filename="Llama-3.2-1B.Q4_K_M.gguf",
|
| 7 |
n_ctx=2048,
|
| 8 |
+
n_threads=2,
|
| 9 |
)
|
| 10 |
|
|
|
|
| 11 |
def build_prompt(system_message: str, history: list[dict], user_message: str) -> str:
|
| 12 |
lines = []
|
| 13 |
if system_message:
|
|
|
|
| 23 |
lines.append("Assistant:")
|
| 24 |
return "\n".join(lines)
|
| 25 |
|
| 26 |
+
|
| 27 |
def respond(
|
| 28 |
message,
|
| 29 |
history: list[dict[str, str]],
|
|
|
|
| 31 |
max_tokens,
|
| 32 |
temperature,
|
| 33 |
top_p,
|
| 34 |
+
repetition_penalty,
|
| 35 |
style_mode,
|
| 36 |
):
|
| 37 |
|
| 38 |
+
# Translated instruction
|
| 39 |
base_instruction = (
|
| 40 |
+
"You are a friendly ChatBot that answers questions and can hold conversations. "
|
| 41 |
+
"Please always answer in one of the following styles: "
|
| 42 |
)
|
| 43 |
|
| 44 |
context = ""
|
| 45 |
+
# Logic keys updated to match the English Dropdown choices below
|
| 46 |
+
if style_mode == "Professional Email":
|
| 47 |
+
context = "Formulate the answer extremely politely and professionally (Business English)."
|
| 48 |
+
elif style_mode == "Gen-Z / Slang":
|
| 49 |
+
context = "Formulate the answer in Gen-Z slang (use words like 'cringe', 'wild', 'sus', emojis)."
|
| 50 |
elif style_mode == "Shakespeare":
|
| 51 |
+
context = "Formulate the answer in old-fashioned, poetic English."
|
| 52 |
+
elif style_mode == "Funny/Ironic":
|
| 53 |
+
context = "Formulate the answer in a funny and ironic way."
|
|
|
|
|
|
|
| 54 |
else:
|
| 55 |
+
context = "Answer normally."
|
| 56 |
|
| 57 |
+
final_system = f"{base_instruction} {context}"
|
| 58 |
|
|
|
|
| 59 |
prompt = build_prompt(final_system, history, message)
|
| 60 |
|
|
|
|
| 61 |
output = llm(
|
| 62 |
prompt,
|
| 63 |
max_tokens=int(max_tokens),
|
| 64 |
temperature=float(temperature),
|
| 65 |
top_p=float(top_p),
|
| 66 |
+
repeat_penalty=float(repetition_penalty),
|
| 67 |
stop=["User:", "System:"],
|
| 68 |
echo=False
|
| 69 |
)
|
|
|
|
| 79 |
additional_inputs=[
|
| 80 |
gr.Textbox(value="", label="System Prompt (Hidden)", visible=False),
|
| 81 |
|
|
|
|
| 82 |
gr.Slider(minimum=1, maximum=2048, value=512, label="Max Tokens"),
|
| 83 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, label="Temperature"),
|
| 84 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, label="Top-p"),
|
| 85 |
+
gr.Slider(minimum=1.0, maximum=2.0, value=1.3, step=0.05, label="Repetition Penalty"),
|
| 86 |
|
| 87 |
+
# Translated Dropdown Options
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
gr.Dropdown(
|
| 89 |
+
choices=["Normal", "Professional Email", "Gen-Z / Slang", "Shakespeare", "Passive-Aggressive", "Funny/Ironic"],
|
| 90 |
value="Normal",
|
| 91 |
+
label="Choose the Style / Tone"
|
| 92 |
)
|
| 93 |
],
|
| 94 |
)
|
| 95 |
|
| 96 |
with gr.Blocks() as demo:
|
| 97 |
+
# Translated Title
|
| 98 |
+
gr.Markdown("# Advanced Chat Bot")
|
| 99 |
with gr.Sidebar():
|
| 100 |
gr.LoginButton()
|
| 101 |
chatbot.render()
|