Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,12 +2,13 @@ import gradio as gr
|
|
| 2 |
import subprocess
|
| 3 |
from huggingface_hub import hf_hub_download
|
| 4 |
|
| 5 |
-
# 1. Install
|
|
|
|
| 6 |
subprocess.run("pip install -q 'llama_cpp_python==0.3.15'", shell=True, check=False)
|
| 7 |
|
| 8 |
from llama_cpp import Llama
|
| 9 |
|
| 10 |
-
# 2. Load GGUF
|
| 11 |
MODEL_REPO = "Jeppcode/ScalableLab2"
|
| 12 |
GGUF_FILENAME = "model-q4_k_m.gguf"
|
| 13 |
|
|
@@ -27,69 +28,45 @@ llm = Llama(
|
|
| 27 |
use_mlock=False,
|
| 28 |
)
|
| 29 |
|
| 30 |
-
# 3.
|
| 31 |
STYLE_SYSTEM_PROMPTS = {
|
| 32 |
"Default": "You are a helpful, polite assistant.",
|
| 33 |
-
"Short answer":
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
"Detailed explanation": (
|
| 37 |
-
"You are a helpful teaching assistant. Give clear, structured and detailed explanations, "
|
| 38 |
-
"often with bullet points or numbered steps when useful."
|
| 39 |
-
),
|
| 40 |
-
"Step-by-step reasoning": (
|
| 41 |
-
"You are a careful problem solver. Think step by step and explain your reasoning clearly "
|
| 42 |
-
"before giving the final answer."
|
| 43 |
-
),
|
| 44 |
}
|
| 45 |
|
| 46 |
-
def
|
| 47 |
if isinstance(content, list):
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
if isinstance(block, dict) and block.get("type") == "text"
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
texts.append(str(block))
|
| 54 |
-
return "\n".join(t for t in texts if t)
|
| 55 |
-
else:
|
| 56 |
-
return str(content)
|
| 57 |
|
| 58 |
def build_prompt(message, history, style):
|
| 59 |
system_prompt = STYLE_SYSTEM_PROMPTS.get(style, STYLE_SYSTEM_PROMPTS["Default"])
|
| 60 |
-
prompt_parts = []
|
| 61 |
-
prompt_parts.append(f"System: {system_prompt}\n")
|
| 62 |
-
prompt_parts.append("Conversation:\n")
|
| 63 |
|
| 64 |
for msg in history or []:
|
| 65 |
role = msg.get("role")
|
| 66 |
-
content =
|
| 67 |
-
if
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
prompt_parts.append(f"User: {content}\n")
|
| 71 |
-
elif role == "assistant":
|
| 72 |
-
prompt_parts.append(f"Assistant: {content}\n")
|
| 73 |
-
elif role == "system":
|
| 74 |
-
prompt_parts.append(f"System (previous): {content}\n")
|
| 75 |
|
| 76 |
prompt_parts.append(f"User: {message}\n")
|
| 77 |
prompt_parts.append("Assistant:")
|
| 78 |
return "".join(prompt_parts)
|
| 79 |
|
| 80 |
def chat_fn(message, history, max_new_tokens, style):
|
| 81 |
-
#
|
| 82 |
temperature = 0.7
|
| 83 |
top_p = 0.9
|
| 84 |
repetition_penalty = 1.1
|
| 85 |
|
| 86 |
prompt = build_prompt(message, history, style)
|
| 87 |
|
| 88 |
-
# Simple logic for temperature
|
| 89 |
-
if temperature <= 0.0:
|
| 90 |
-
temperature = 0.0
|
| 91 |
-
top_p = 1.0
|
| 92 |
-
|
| 93 |
output = llm(
|
| 94 |
prompt,
|
| 95 |
max_tokens=int(max_new_tokens),
|
|
@@ -98,105 +75,94 @@ def chat_fn(message, history, max_new_tokens, style):
|
|
| 98 |
repeat_penalty=repetition_penalty,
|
| 99 |
stop=["User:", "Assistant:", "System:", "Conversation:"],
|
| 100 |
)
|
| 101 |
-
|
| 102 |
-
return reply
|
| 103 |
|
| 104 |
-
# --- Christmas Theme Configuration ---
|
| 105 |
|
| 106 |
-
#
|
| 107 |
-
|
| 108 |
-
primary_hue="
|
| 109 |
-
secondary_hue="green",
|
| 110 |
neutral_hue="slate",
|
| 111 |
).set(
|
| 112 |
body_background_fill="transparent",
|
| 113 |
-
block_background_fill="rgba(
|
| 114 |
-
border_color_primary="#
|
| 115 |
-
button_primary_background_fill="#
|
| 116 |
-
button_primary_text_color="
|
|
|
|
| 117 |
)
|
| 118 |
|
| 119 |
-
#
|
| 120 |
custom_css = """
|
| 121 |
-
/* Background:
|
| 122 |
.gradio-container {
|
| 123 |
-
background: url('https://images.unsplash.com/photo-
|
| 124 |
background-size: cover;
|
| 125 |
}
|
| 126 |
|
| 127 |
-
/*
|
| 128 |
.gradio-container > .main {
|
| 129 |
background: transparent !important;
|
| 130 |
}
|
| 131 |
|
| 132 |
-
/*
|
| 133 |
-
.bubble-wrap {
|
| 134 |
-
background: rgba(
|
| 135 |
-
border: 2px solid #
|
| 136 |
-
border-radius:
|
|
|
|
|
|
|
| 137 |
}
|
| 138 |
|
| 139 |
-
/*
|
| 140 |
.user-message {
|
| 141 |
-
background-color:
|
| 142 |
-
|
| 143 |
-
|
| 144 |
}
|
| 145 |
|
| 146 |
-
/* Bot Message - Christmas Green */
|
| 147 |
.bot-message {
|
| 148 |
-
background-color:
|
| 149 |
-
|
| 150 |
-
|
| 151 |
}
|
| 152 |
|
| 153 |
-
/*
|
| 154 |
-
|
| 155 |
-
background: rgba(
|
| 156 |
-
border:
|
| 157 |
-
|
| 158 |
-
padding: 10px;
|
| 159 |
}
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
color: #3E2723 !important; /* Dark chocolate text for readability */
|
| 164 |
-
font-weight: bold;
|
| 165 |
}
|
| 166 |
|
|
|
|
| 167 |
footer {visibility: hidden}
|
| 168 |
"""
|
| 169 |
|
| 170 |
-
#
|
| 171 |
max_new_tokens_slider = gr.Slider(
|
| 172 |
-
minimum=16, maximum=256, value=64, step=8, label="Max
|
| 173 |
)
|
| 174 |
-
|
| 175 |
style_radio = gr.Radio(
|
| 176 |
-
choices=[
|
| 177 |
-
"Default",
|
| 178 |
-
"Short answer",
|
| 179 |
-
"Detailed explanation",
|
| 180 |
-
"Step-by-step reasoning",
|
| 181 |
-
],
|
| 182 |
value="Detailed explanation",
|
| 183 |
label="Answer style",
|
| 184 |
)
|
| 185 |
|
| 186 |
-
#
|
| 187 |
demo = gr.ChatInterface(
|
| 188 |
fn=chat_fn,
|
| 189 |
-
title="
|
| 190 |
-
description="Chat with the fine-tuned model.
|
| 191 |
-
additional_inputs=[
|
| 192 |
-
|
| 193 |
-
style_radio,
|
| 194 |
-
],
|
| 195 |
-
additional_inputs_accordion="Holiday Settings",
|
| 196 |
)
|
| 197 |
|
| 198 |
-
# Apply
|
| 199 |
-
demo.theme =
|
| 200 |
demo.css = custom_css
|
| 201 |
|
| 202 |
if __name__ == "__main__":
|
|
|
|
| 2 |
import subprocess
|
| 3 |
from huggingface_hub import hf_hub_download
|
| 4 |
|
| 5 |
+
# --- 1. Setup & Install ---
|
| 6 |
+
# Install llama-cpp-python in runtime (prevents build errors in Spaces)
|
| 7 |
subprocess.run("pip install -q 'llama_cpp_python==0.3.15'", shell=True, check=False)
|
| 8 |
|
| 9 |
from llama_cpp import Llama
|
| 10 |
|
| 11 |
+
# --- 2. Load Model (GGUF) ---
|
| 12 |
MODEL_REPO = "Jeppcode/ScalableLab2"
|
| 13 |
GGUF_FILENAME = "model-q4_k_m.gguf"
|
| 14 |
|
|
|
|
| 28 |
use_mlock=False,
|
| 29 |
)
|
| 30 |
|
| 31 |
+
# --- 3. Prompts & Logic ---
|
| 32 |
STYLE_SYSTEM_PROMPTS = {
|
| 33 |
"Default": "You are a helpful, polite assistant.",
|
| 34 |
+
"Short answer": "You are a helpful assistant. Answer as concisely as possible, usually in 1–3 sentences.",
|
| 35 |
+
"Detailed explanation": "You are a helpful teaching assistant. Give clear, structured and detailed explanations.",
|
| 36 |
+
"Step-by-step reasoning": "You are a careful problem solver. Think step by step and explain your reasoning clearly.",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
}
|
| 38 |
|
| 39 |
+
def _extract_text(content):
|
| 40 |
if isinstance(content, list):
|
| 41 |
+
return "\n".join(
|
| 42 |
+
block.get("text", "") for block in content
|
| 43 |
+
if isinstance(block, dict) and block.get("type") == "text"
|
| 44 |
+
)
|
| 45 |
+
return str(content)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
def build_prompt(message, history, style):
|
| 48 |
system_prompt = STYLE_SYSTEM_PROMPTS.get(style, STYLE_SYSTEM_PROMPTS["Default"])
|
| 49 |
+
prompt_parts = [f"System: {system_prompt}\n", "Conversation:\n"]
|
|
|
|
|
|
|
| 50 |
|
| 51 |
for msg in history or []:
|
| 52 |
role = msg.get("role")
|
| 53 |
+
content = _extract_text(msg.get("content", ""))
|
| 54 |
+
if content:
|
| 55 |
+
if role == "user": prompt_parts.append(f"User: {content}\n")
|
| 56 |
+
elif role == "assistant": prompt_parts.append(f"Assistant: {content}\n")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
prompt_parts.append(f"User: {message}\n")
|
| 59 |
prompt_parts.append("Assistant:")
|
| 60 |
return "".join(prompt_parts)
|
| 61 |
|
| 62 |
def chat_fn(message, history, max_new_tokens, style):
|
| 63 |
+
# Hardcoded values for the hidden sliders
|
| 64 |
temperature = 0.7
|
| 65 |
top_p = 0.9
|
| 66 |
repetition_penalty = 1.1
|
| 67 |
|
| 68 |
prompt = build_prompt(message, history, style)
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
output = llm(
|
| 71 |
prompt,
|
| 72 |
max_tokens=int(max_new_tokens),
|
|
|
|
| 75 |
repeat_penalty=repetition_penalty,
|
| 76 |
stop=["User:", "Assistant:", "System:", "Conversation:"],
|
| 77 |
)
|
| 78 |
+
return output["choices"][0]["text"].strip()
|
|
|
|
| 79 |
|
| 80 |
+
# --- 4. Winter/Christmas Theme Configuration ---
|
| 81 |
|
| 82 |
+
# We create a base theme, but most work is done in CSS
|
| 83 |
+
winter_theme = gr.themes.Soft(
|
| 84 |
+
primary_hue="blue", # Ice blue accents
|
|
|
|
| 85 |
neutral_hue="slate",
|
| 86 |
).set(
|
| 87 |
body_background_fill="transparent",
|
| 88 |
+
block_background_fill="rgba(10, 20, 40, 0.7)", # Dark Blue Glass
|
| 89 |
+
border_color_primary="#FFFFFF", # Explicit White Border
|
| 90 |
+
button_primary_background_fill="#FFFFFF", # White buttons
|
| 91 |
+
button_primary_text_color="#000000",
|
| 92 |
+
text_color_subdued="#E0E0E0",
|
| 93 |
)
|
| 94 |
|
| 95 |
+
# Custom CSS for the "Perfect" Look
|
| 96 |
custom_css = """
|
| 97 |
+
/* 1. Background Image: Snowy Winter Forest */
|
| 98 |
.gradio-container {
|
| 99 |
+
background: url('https://images.unsplash.com/photo-1477601263568-180e2c6d046e?q=80&w=2560&auto=format&fit=crop') no-repeat center center fixed;
|
| 100 |
background-size: cover;
|
| 101 |
}
|
| 102 |
|
| 103 |
+
/* 2. Transparency */
|
| 104 |
.gradio-container > .main {
|
| 105 |
background: transparent !important;
|
| 106 |
}
|
| 107 |
|
| 108 |
+
/* 3. The Chat & Settings Blocks - "Frosty Glass" with White Borders */
|
| 109 |
+
.group, .form, .bubble-wrap {
|
| 110 |
+
background: rgba(15, 23, 42, 0.75) !important; /* Dark Blue/Grey Glass */
|
| 111 |
+
border: 2px solid #FFFFFF !important; /* THE WHITE BORDER */
|
| 112 |
+
border-radius: 12px !important;
|
| 113 |
+
backdrop-filter: blur(4px); /* Slight blur behind the glass */
|
| 114 |
+
box-shadow: 0 4px 15px rgba(0,0,0,0.5);
|
| 115 |
}
|
| 116 |
|
| 117 |
+
/* 4. Chat Bubbles */
|
| 118 |
.user-message {
|
| 119 |
+
background-color: rgba(255, 255, 255, 0.2) !important; /* Icy White transparency */
|
| 120 |
+
border: 1px solid #FFFFFF !important;
|
| 121 |
+
color: #FFFFFF !important;
|
| 122 |
}
|
| 123 |
|
|
|
|
| 124 |
.bot-message {
|
| 125 |
+
background-color: rgba(0, 0, 0, 0.6) !important; /* Darker for contrast */
|
| 126 |
+
border: 1px solid #A0A0A0 !important;
|
| 127 |
+
color: #E0E0E0 !important;
|
| 128 |
}
|
| 129 |
|
| 130 |
+
/* 5. Inputs and Text */
|
| 131 |
+
textarea, input {
|
| 132 |
+
background-color: rgba(0, 0, 0, 0.5) !important;
|
| 133 |
+
border: 1px solid #FFFFFF !important;
|
| 134 |
+
color: white !important;
|
|
|
|
| 135 |
}
|
| 136 |
+
label, span, p {
|
| 137 |
+
color: #FFFFFF !important;
|
| 138 |
+
text-shadow: 1px 1px 2px black; /* Makes text readable on snow */
|
|
|
|
|
|
|
| 139 |
}
|
| 140 |
|
| 141 |
+
/* Hide Footer */
|
| 142 |
footer {visibility: hidden}
|
| 143 |
"""
|
| 144 |
|
| 145 |
+
# Only Max Tokens + Style
|
| 146 |
max_new_tokens_slider = gr.Slider(
|
| 147 |
+
minimum=16, maximum=256, value=64, step=8, label="Max Response Length",
|
| 148 |
)
|
|
|
|
| 149 |
style_radio = gr.Radio(
|
| 150 |
+
choices=["Default", "Short answer", "Detailed explanation"],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
value="Detailed explanation",
|
| 152 |
label="Answer style",
|
| 153 |
)
|
| 154 |
|
| 155 |
+
# Init App
|
| 156 |
demo = gr.ChatInterface(
|
| 157 |
fn=chat_fn,
|
| 158 |
+
title="❄️ WinterChat Lab 2 ❄️",
|
| 159 |
+
description="Stay frosty. Chat with the fine-tuned model.",
|
| 160 |
+
additional_inputs=[max_new_tokens_slider, style_radio],
|
| 161 |
+
additional_inputs_accordion="Settings",
|
|
|
|
|
|
|
|
|
|
| 162 |
)
|
| 163 |
|
| 164 |
+
# Apply Visuals manually
|
| 165 |
+
demo.theme = winter_theme
|
| 166 |
demo.css = custom_css
|
| 167 |
|
| 168 |
if __name__ == "__main__":
|