Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,11 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import subprocess
|
|
|
|
|
|
|
| 3 |
from huggingface_hub import hf_hub_download
|
| 4 |
|
| 5 |
# --- 1. Setup & 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 Model (GGUF) ---
|
|
@@ -27,7 +28,23 @@ 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": "You are a helpful assistant. Answer as concisely as possible, usually in 1–3 sentences.",
|
|
@@ -62,7 +79,6 @@ def chat_fn(message, history, max_new_tokens, style):
|
|
| 62 |
temperature = 0.7
|
| 63 |
top_p = 0.9
|
| 64 |
repetition_penalty = 1.1
|
| 65 |
-
|
| 66 |
prompt = build_prompt(message, history, style)
|
| 67 |
|
| 68 |
output = llm(
|
|
@@ -75,8 +91,7 @@ def chat_fn(message, history, max_new_tokens, style):
|
|
| 75 |
)
|
| 76 |
return output["choices"][0]["text"].strip()
|
| 77 |
|
| 78 |
-
# ---
|
| 79 |
-
|
| 80 |
winter_theme = gr.themes.Soft(
|
| 81 |
primary_hue="blue",
|
| 82 |
neutral_hue="slate",
|
|
@@ -88,55 +103,55 @@ winter_theme = gr.themes.Soft(
|
|
| 88 |
button_primary_text_color="#000000",
|
| 89 |
)
|
| 90 |
|
| 91 |
-
#
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
background: url('Cute-Christmas-Background-edit-online-1.jpg') no-repeat center center fixed !important;
|
| 97 |
background-size: cover !important;
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
-
/*
|
| 101 |
-
.gradio-container {
|
| 102 |
background: transparent !important;
|
| 103 |
-
|
| 104 |
-
}
|
| 105 |
|
| 106 |
-
/*
|
| 107 |
-
.group, .form, .bubble-wrap {
|
| 108 |
background: rgba(15, 23, 42, 0.75) !important;
|
| 109 |
border: 2px solid #FFFFFF !important;
|
| 110 |
border-radius: 12px !important;
|
| 111 |
backdrop-filter: blur(4px);
|
| 112 |
box-shadow: 0 4px 15px rgba(0,0,0,0.5);
|
| 113 |
-
}
|
| 114 |
|
| 115 |
-
/*
|
| 116 |
-
.user-message {
|
| 117 |
background-color: rgba(255, 255, 255, 0.2) !important;
|
| 118 |
border: 1px solid #FFFFFF !important;
|
| 119 |
color: #FFFFFF !important;
|
| 120 |
-
}
|
| 121 |
-
.bot-message {
|
| 122 |
background-color: rgba(0, 0, 0, 0.6) !important;
|
| 123 |
border: 1px solid #A0A0A0 !important;
|
| 124 |
color: #E0E0E0 !important;
|
| 125 |
-
}
|
| 126 |
|
| 127 |
-
/*
|
| 128 |
-
textarea, input {
|
| 129 |
background-color: rgba(0, 0, 0, 0.5) !important;
|
| 130 |
border: 1px solid #FFFFFF !important;
|
| 131 |
color: white !important;
|
| 132 |
-
}
|
| 133 |
-
label, span, p, .prose {
|
| 134 |
color: #FFFFFF !important;
|
| 135 |
text-shadow: 1px 1px 2px black;
|
| 136 |
-
}
|
| 137 |
|
| 138 |
-
|
| 139 |
-
footer {visibility: hidden}
|
| 140 |
"""
|
| 141 |
|
| 142 |
max_new_tokens_slider = gr.Slider(
|
|
@@ -148,6 +163,7 @@ style_radio = gr.Radio(
|
|
| 148 |
label="Answer style",
|
| 149 |
)
|
| 150 |
|
|
|
|
| 151 |
demo = gr.ChatInterface(
|
| 152 |
fn=chat_fn,
|
| 153 |
title="❄️ WinterChat Lab 2 ❄️",
|
|
@@ -156,9 +172,10 @@ demo = gr.ChatInterface(
|
|
| 156 |
additional_inputs_accordion="Settings",
|
| 157 |
)
|
| 158 |
|
| 159 |
-
# Apply
|
| 160 |
demo.theme = winter_theme
|
| 161 |
demo.css = custom_css
|
| 162 |
|
| 163 |
if __name__ == "__main__":
|
| 164 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import subprocess
|
| 3 |
+
import base64
|
| 4 |
+
import os
|
| 5 |
from huggingface_hub import hf_hub_download
|
| 6 |
|
| 7 |
# --- 1. Setup & Install ---
|
| 8 |
subprocess.run("pip install -q 'llama_cpp_python==0.3.15'", shell=True, check=False)
|
|
|
|
| 9 |
from llama_cpp import Llama
|
| 10 |
|
| 11 |
# --- 2. Load Model (GGUF) ---
|
|
|
|
| 28 |
use_mlock=False,
|
| 29 |
)
|
| 30 |
|
| 31 |
+
# --- 3. Image Handling (The Fix) ---
|
| 32 |
+
def encode_image(image_path):
|
| 33 |
+
"""
|
| 34 |
+
Reads the local image and converts it to a base64 string.
|
| 35 |
+
This ensures it loads in the CSS without 404 errors.
|
| 36 |
+
"""
|
| 37 |
+
if not os.path.exists(image_path):
|
| 38 |
+
return "" # Fail silently if file missing
|
| 39 |
+
with open(image_path, "rb") as image_file:
|
| 40 |
+
encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
|
| 41 |
+
return f"data:image/jpeg;base64,{encoded_string}"
|
| 42 |
+
|
| 43 |
+
# Exact filename from your screenshot
|
| 44 |
+
IMG_FILENAME = "Cute-Christmas-Background-edit-online-1.jpg"
|
| 45 |
+
bg_image_data = encode_image(IMG_FILENAME)
|
| 46 |
+
|
| 47 |
+
# --- 4. Prompts & Logic ---
|
| 48 |
STYLE_SYSTEM_PROMPTS = {
|
| 49 |
"Default": "You are a helpful, polite assistant.",
|
| 50 |
"Short answer": "You are a helpful assistant. Answer as concisely as possible, usually in 1–3 sentences.",
|
|
|
|
| 79 |
temperature = 0.7
|
| 80 |
top_p = 0.9
|
| 81 |
repetition_penalty = 1.1
|
|
|
|
| 82 |
prompt = build_prompt(message, history, style)
|
| 83 |
|
| 84 |
output = llm(
|
|
|
|
| 91 |
)
|
| 92 |
return output["choices"][0]["text"].strip()
|
| 93 |
|
| 94 |
+
# --- 5. Theme & CSS ---
|
|
|
|
| 95 |
winter_theme = gr.themes.Soft(
|
| 96 |
primary_hue="blue",
|
| 97 |
neutral_hue="slate",
|
|
|
|
| 103 |
button_primary_text_color="#000000",
|
| 104 |
)
|
| 105 |
|
| 106 |
+
# We inject the base64 image data directly into the URL()
|
| 107 |
+
custom_css = f"""
|
| 108 |
+
/* Apply Background to the main app container */
|
| 109 |
+
.gradio-container {{
|
| 110 |
+
background-image: url('{bg_image_data}') !important;
|
|
|
|
| 111 |
background-size: cover !important;
|
| 112 |
+
background-position: center center !important;
|
| 113 |
+
background-attachment: fixed !important;
|
| 114 |
+
background-repeat: no-repeat !important;
|
| 115 |
+
}}
|
| 116 |
|
| 117 |
+
/* Ensure the main content area is transparent so bg shows */
|
| 118 |
+
.gradio-container > .main {{
|
| 119 |
background: transparent !important;
|
| 120 |
+
}}
|
|
|
|
| 121 |
|
| 122 |
+
/* Dark Glassy Look for Chat and Settings */
|
| 123 |
+
.group, .form, .bubble-wrap {{
|
| 124 |
background: rgba(15, 23, 42, 0.75) !important;
|
| 125 |
border: 2px solid #FFFFFF !important;
|
| 126 |
border-radius: 12px !important;
|
| 127 |
backdrop-filter: blur(4px);
|
| 128 |
box-shadow: 0 4px 15px rgba(0,0,0,0.5);
|
| 129 |
+
}}
|
| 130 |
|
| 131 |
+
/* Chat Bubbles */
|
| 132 |
+
.user-message {{
|
| 133 |
background-color: rgba(255, 255, 255, 0.2) !important;
|
| 134 |
border: 1px solid #FFFFFF !important;
|
| 135 |
color: #FFFFFF !important;
|
| 136 |
+
}}
|
| 137 |
+
.bot-message {{
|
| 138 |
background-color: rgba(0, 0, 0, 0.6) !important;
|
| 139 |
border: 1px solid #A0A0A0 !important;
|
| 140 |
color: #E0E0E0 !important;
|
| 141 |
+
}}
|
| 142 |
|
| 143 |
+
/* Inputs and Text */
|
| 144 |
+
textarea, input {{
|
| 145 |
background-color: rgba(0, 0, 0, 0.5) !important;
|
| 146 |
border: 1px solid #FFFFFF !important;
|
| 147 |
color: white !important;
|
| 148 |
+
}}
|
| 149 |
+
label, span, p, .prose {{
|
| 150 |
color: #FFFFFF !important;
|
| 151 |
text-shadow: 1px 1px 2px black;
|
| 152 |
+
}}
|
| 153 |
|
| 154 |
+
footer {{visibility: hidden}}
|
|
|
|
| 155 |
"""
|
| 156 |
|
| 157 |
max_new_tokens_slider = gr.Slider(
|
|
|
|
| 163 |
label="Answer style",
|
| 164 |
)
|
| 165 |
|
| 166 |
+
# Instantiate
|
| 167 |
demo = gr.ChatInterface(
|
| 168 |
fn=chat_fn,
|
| 169 |
title="❄️ WinterChat Lab 2 ❄️",
|
|
|
|
| 172 |
additional_inputs_accordion="Settings",
|
| 173 |
)
|
| 174 |
|
| 175 |
+
# Apply configuration
|
| 176 |
demo.theme = winter_theme
|
| 177 |
demo.css = custom_css
|
| 178 |
|
| 179 |
if __name__ == "__main__":
|
| 180 |
+
# allowed_paths=["."] is a backup, but base64 should solve it.
|
| 181 |
+
demo.launch(allowed_paths=["."])
|