Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -33,26 +33,38 @@ if model_path:
|
|
| 33 |
)
|
| 34 |
|
| 35 |
# --- 3. Style / System Prompts ---
|
|
|
|
| 36 |
STYLE_SYSTEM_PROMPTS = {
|
| 37 |
-
"Default":
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
}
|
| 42 |
|
| 43 |
def _extract_text(content):
|
| 44 |
if isinstance(content, list):
|
| 45 |
-
return "\n".join(
|
| 46 |
-
b.get("text", "")
|
| 47 |
-
for b in content
|
| 48 |
-
if isinstance(b, dict) and b.get("type") == "text"
|
| 49 |
-
)
|
| 50 |
return str(content)
|
| 51 |
|
| 52 |
def chat_fn(message, history, max_new_tokens, style):
|
| 53 |
-
if not llm:
|
| 54 |
-
return "Error: Model not loaded."
|
| 55 |
|
|
|
|
| 56 |
system_prompt = STYLE_SYSTEM_PROMPTS.get(style, STYLE_SYSTEM_PROMPTS["Default"])
|
| 57 |
|
| 58 |
prompt = f"System: {system_prompt}\nConversation:\n"
|
|
@@ -60,13 +72,12 @@ def chat_fn(message, history, max_new_tokens, style):
|
|
| 60 |
role = msg.get("role")
|
| 61 |
txt = _extract_text(msg.get("content", ""))
|
| 62 |
if txt:
|
| 63 |
-
if role == "user":
|
| 64 |
-
|
| 65 |
-
elif role == "assistant":
|
| 66 |
-
prompt += f"Assistant: {txt}\n"
|
| 67 |
|
| 68 |
prompt += f"User: {message}\nAssistant:"
|
| 69 |
|
|
|
|
| 70 |
output = llm(
|
| 71 |
prompt,
|
| 72 |
max_tokens=int(max_new_tokens),
|
|
@@ -76,48 +87,32 @@ def chat_fn(message, history, max_new_tokens, style):
|
|
| 76 |
)
|
| 77 |
return output["choices"][0]["text"].strip()
|
| 78 |
|
| 79 |
-
# --- 4.
|
| 80 |
-
BACKGROUND_URL = "https://images.template.net/269645/Cute-Christmas-Background-edit-online-1.jpg"
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
.gradio-container .block, .gradio-container .panel {{
|
| 91 |
-
background-color: rgba(255, 255, 255, 0.85);
|
| 92 |
-
}}
|
| 93 |
-
"""
|
| 94 |
|
| 95 |
-
#
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
with gr.Accordion("Controls", open=True):
|
| 103 |
-
max_new_tokens_slider = gr.Slider(
|
| 104 |
-
minimum=16,
|
| 105 |
-
maximum=256,
|
| 106 |
-
value=64,
|
| 107 |
-
step=8,
|
| 108 |
-
label="Max Response Length",
|
| 109 |
-
)
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
additional_inputs=[max_new_tokens_slider, style_radio],
|
| 120 |
-
)
|
| 121 |
|
| 122 |
if __name__ == "__main__":
|
| 123 |
-
demo.launch()
|
|
|
|
| 33 |
)
|
| 34 |
|
| 35 |
# --- 3. Style / System Prompts ---
|
| 36 |
+
# These are the "Buttons" logic to change how the AI behaves
|
| 37 |
STYLE_SYSTEM_PROMPTS = {
|
| 38 |
+
"Default": (
|
| 39 |
+
"You are a helpful, polite AI assistant. "
|
| 40 |
+
),
|
| 41 |
+
"Short answer": (
|
| 42 |
+
"Answer as briefly as possible, usually in 1-3 sentences. "
|
| 43 |
+
"Give only the core information needed to answer the question. "
|
| 44 |
+
"Do not add extra explanations, lists, or examples unless the user asks for more detail."
|
| 45 |
+
),
|
| 46 |
+
"Detailed explanation": (
|
| 47 |
+
"Give a clear, structured, and detailed explanation. "
|
| 48 |
+
"Break your answer into short paragraphs or bullet points when helpful. "
|
| 49 |
+
"Explain what, how, and why, but avoid unnecessary repetition or filler."
|
| 50 |
+
),
|
| 51 |
+
"Step-by-step reasoning": (
|
| 52 |
+
"Solve the problem step by step. "
|
| 53 |
+
"First restate the task in your own words, then explain your reasoning in numbered steps, "
|
| 54 |
+
"and finally give a short final answer at the end. "
|
| 55 |
+
"Keep the reasoning easy to follow and avoid unrelated digressions."
|
| 56 |
+
),
|
| 57 |
}
|
| 58 |
|
| 59 |
def _extract_text(content):
|
| 60 |
if isinstance(content, list):
|
| 61 |
+
return "\n".join(b.get("text", "") for b in content if isinstance(b, dict) and b.get("type") == "text")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
return str(content)
|
| 63 |
|
| 64 |
def chat_fn(message, history, max_new_tokens, style):
|
| 65 |
+
if not llm: return "Error: Model not loaded."
|
|
|
|
| 66 |
|
| 67 |
+
# Select the specific system prompt based on the button chosen
|
| 68 |
system_prompt = STYLE_SYSTEM_PROMPTS.get(style, STYLE_SYSTEM_PROMPTS["Default"])
|
| 69 |
|
| 70 |
prompt = f"System: {system_prompt}\nConversation:\n"
|
|
|
|
| 72 |
role = msg.get("role")
|
| 73 |
txt = _extract_text(msg.get("content", ""))
|
| 74 |
if txt:
|
| 75 |
+
if role == "user": prompt += f"User: {txt}\n"
|
| 76 |
+
elif role == "assistant": prompt += f"Assistant: {txt}\n"
|
|
|
|
|
|
|
| 77 |
|
| 78 |
prompt += f"User: {message}\nAssistant:"
|
| 79 |
|
| 80 |
+
# Default internal values for randomness
|
| 81 |
output = llm(
|
| 82 |
prompt,
|
| 83 |
max_tokens=int(max_new_tokens),
|
|
|
|
| 87 |
)
|
| 88 |
return output["choices"][0]["text"].strip()
|
| 89 |
|
| 90 |
+
# --- 4. UI Controls ---
|
|
|
|
| 91 |
|
| 92 |
+
# Slider for length
|
| 93 |
+
max_new_tokens_slider = gr.Slider(
|
| 94 |
+
minimum=16,
|
| 95 |
+
maximum=256,
|
| 96 |
+
value=64,
|
| 97 |
+
step=8,
|
| 98 |
+
label="Max Response Length"
|
| 99 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
+
# The "Buttons" at the bottom for Style
|
| 102 |
+
style_radio = gr.Radio(
|
| 103 |
+
choices=["Default", "Short answer", "Detailed explanation", "Step-by-step reasoning"],
|
| 104 |
+
value="Detailed explanation",
|
| 105 |
+
label="Answer Style"
|
| 106 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
+
# --- 5. Launch App (Clean / No Theme) ---
|
| 109 |
+
demo = gr.ChatInterface(
|
| 110 |
+
fn=chat_fn,
|
| 111 |
+
title="Lab 2 – Fine-tuned GGUF model",
|
| 112 |
+
description="Chat with the fine-tuned Llama model. Use the controls below to change the response style.",
|
| 113 |
+
additional_inputs=[max_new_tokens_slider, style_radio],
|
| 114 |
+
additional_inputs_accordion="Controls",
|
| 115 |
+
)
|
|
|
|
|
|
|
| 116 |
|
| 117 |
if __name__ == "__main__":
|
| 118 |
+
demo.launch()
|