Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -33,7 +33,6 @@ if model_path:
|
|
| 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": "You are a helpful, polite assistant.",
|
| 39 |
"Short answer": "Answer as concisely as possible, usually in 1–3 sentences.",
|
|
@@ -43,13 +42,17 @@ STYLE_SYSTEM_PROMPTS = {
|
|
| 43 |
|
| 44 |
def _extract_text(content):
|
| 45 |
if isinstance(content, list):
|
| 46 |
-
return "\n".join(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
return str(content)
|
| 48 |
|
| 49 |
def chat_fn(message, history, max_new_tokens, style):
|
| 50 |
-
if not llm:
|
|
|
|
| 51 |
|
| 52 |
-
# Select the specific system prompt based on the button chosen
|
| 53 |
system_prompt = STYLE_SYSTEM_PROMPTS.get(style, STYLE_SYSTEM_PROMPTS["Default"])
|
| 54 |
|
| 55 |
prompt = f"System: {system_prompt}\nConversation:\n"
|
|
@@ -57,12 +60,13 @@ def chat_fn(message, history, max_new_tokens, style):
|
|
| 57 |
role = msg.get("role")
|
| 58 |
txt = _extract_text(msg.get("content", ""))
|
| 59 |
if txt:
|
| 60 |
-
if role == "user":
|
| 61 |
-
|
|
|
|
|
|
|
| 62 |
|
| 63 |
prompt += f"User: {message}\nAssistant:"
|
| 64 |
|
| 65 |
-
# Default internal values for randomness
|
| 66 |
output = llm(
|
| 67 |
prompt,
|
| 68 |
max_tokens=int(max_new_tokens),
|
|
@@ -74,7 +78,6 @@ def chat_fn(message, history, max_new_tokens, style):
|
|
| 74 |
|
| 75 |
# --- 4. UI Controls ---
|
| 76 |
|
| 77 |
-
# Slider for length
|
| 78 |
max_new_tokens_slider = gr.Slider(
|
| 79 |
minimum=16,
|
| 80 |
maximum=256,
|
|
@@ -83,21 +86,34 @@ max_new_tokens_slider = gr.Slider(
|
|
| 83 |
label="Max Response Length"
|
| 84 |
)
|
| 85 |
|
| 86 |
-
# The "Buttons" at the bottom for Style
|
| 87 |
style_radio = gr.Radio(
|
| 88 |
choices=["Default", "Short answer", "Detailed explanation", "Step-by-step reasoning"],
|
| 89 |
value="Detailed explanation",
|
| 90 |
label="Answer Style"
|
| 91 |
)
|
| 92 |
|
| 93 |
-
# --- 5.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
demo = gr.ChatInterface(
|
| 95 |
fn=chat_fn,
|
| 96 |
title="Lab 2 – Fine-tuned GGUF model",
|
| 97 |
description="Chat with the fine-tuned Llama model. Use the controls below to change the response style.",
|
| 98 |
additional_inputs=[max_new_tokens_slider, style_radio],
|
| 99 |
additional_inputs_accordion="Controls",
|
|
|
|
| 100 |
)
|
| 101 |
|
| 102 |
if __name__ == "__main__":
|
| 103 |
-
demo.launch()
|
|
|
|
| 33 |
)
|
| 34 |
|
| 35 |
# --- 3. Style / System Prompts ---
|
|
|
|
| 36 |
STYLE_SYSTEM_PROMPTS = {
|
| 37 |
"Default": "You are a helpful, polite assistant.",
|
| 38 |
"Short answer": "Answer as concisely as possible, usually in 1–3 sentences.",
|
|
|
|
| 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 |
role = msg.get("role")
|
| 61 |
txt = _extract_text(msg.get("content", ""))
|
| 62 |
if txt:
|
| 63 |
+
if role == "user":
|
| 64 |
+
prompt += f"User: {txt}\n"
|
| 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),
|
|
|
|
| 78 |
|
| 79 |
# --- 4. UI Controls ---
|
| 80 |
|
|
|
|
| 81 |
max_new_tokens_slider = gr.Slider(
|
| 82 |
minimum=16,
|
| 83 |
maximum=256,
|
|
|
|
| 86 |
label="Max Response Length"
|
| 87 |
)
|
| 88 |
|
|
|
|
| 89 |
style_radio = gr.Radio(
|
| 90 |
choices=["Default", "Short answer", "Detailed explanation", "Step-by-step reasoning"],
|
| 91 |
value="Detailed explanation",
|
| 92 |
label="Answer Style"
|
| 93 |
)
|
| 94 |
|
| 95 |
+
# --- 4.5. Background CSS ---
|
| 96 |
+
|
| 97 |
+
BACKGROUND_URL = "https://images.template.net/269645/Cute-Christmas-Background-edit-online-1.jpg"
|
| 98 |
+
|
| 99 |
+
custom_css = f"""
|
| 100 |
+
.gradio-container {{
|
| 101 |
+
background-image: url('{BACKGROUND_URL}');
|
| 102 |
+
background-repeat: no-repeat;
|
| 103 |
+
background-size: cover;
|
| 104 |
+
background-position: center;
|
| 105 |
+
}}
|
| 106 |
+
"""
|
| 107 |
+
|
| 108 |
+
# --- 5. Launch App (with background) ---
|
| 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 |
+
css=custom_css, # <- here
|
| 116 |
)
|
| 117 |
|
| 118 |
if __name__ == "__main__":
|
| 119 |
+
demo.launch()
|