Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -22,25 +22,16 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 22 |
)
|
| 23 |
model.eval()
|
| 24 |
|
| 25 |
-
# Hardcoded system prompt
|
| 26 |
SYSTEM_PROMPT = "You are a helpful, polite assistant. Give clear and structured explanations."
|
| 27 |
|
| 28 |
def build_prompt(message, history):
|
| 29 |
-
"""
|
| 30 |
-
Builds the prompt for the model.
|
| 31 |
-
Parses history and adds the fixed system prompt.
|
| 32 |
-
"""
|
| 33 |
messages = []
|
| 34 |
-
|
| 35 |
-
# Add the fixed system prompt
|
| 36 |
messages.append({"role": "system", "content": SYSTEM_PROMPT})
|
| 37 |
|
| 38 |
-
# Process history
|
| 39 |
for msg in history:
|
| 40 |
role = msg.get("role")
|
| 41 |
content = msg.get("content", "")
|
| 42 |
-
|
| 43 |
-
# Handle if content is a list of blocks (Gradio 6 specific) or string
|
| 44 |
if isinstance(content, list):
|
| 45 |
texts = []
|
| 46 |
for block in content:
|
|
@@ -55,7 +46,6 @@ def build_prompt(message, history):
|
|
| 55 |
if text and role in ("user", "assistant", "system"):
|
| 56 |
messages.append({"role": role, "content": text})
|
| 57 |
|
| 58 |
-
# Add current user message
|
| 59 |
messages.append({"role": "user", "content": message})
|
| 60 |
|
| 61 |
prompt = tokenizer.apply_chat_template(
|
|
@@ -64,13 +54,10 @@ def build_prompt(message, history):
|
|
| 64 |
return prompt
|
| 65 |
|
| 66 |
def chat_fn(message, history, max_new_tokens):
|
| 67 |
-
# build_prompt no longer needs 'style'
|
| 68 |
prompt = build_prompt(message, history)
|
| 69 |
-
|
| 70 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 71 |
|
| 72 |
-
# Default
|
| 73 |
-
# You can adjust these manually here if you want different behavior
|
| 74 |
temperature = 0.7
|
| 75 |
top_p = 0.9
|
| 76 |
repetition_penalty = 1.1
|
|
@@ -95,7 +82,6 @@ def chat_fn(message, history, max_new_tokens):
|
|
| 95 |
return generated
|
| 96 |
|
| 97 |
# --- Nature Theme Configuration ---
|
| 98 |
-
# customized Soft theme with Earth tones
|
| 99 |
nature_theme = gr.themes.Soft(
|
| 100 |
primary_hue="green",
|
| 101 |
secondary_hue="emerald",
|
|
@@ -103,11 +89,10 @@ nature_theme = gr.themes.Soft(
|
|
| 103 |
).set(
|
| 104 |
body_background_fill="#f5f7f5",
|
| 105 |
block_background_fill="rgba(255, 255, 255, 0.9)",
|
| 106 |
-
button_primary_background_fill="#2E7D32",
|
| 107 |
border_color_primary="#4CAF50",
|
| 108 |
)
|
| 109 |
|
| 110 |
-
# Custom CSS for a background image (Nature/Forest)
|
| 111 |
custom_css = """
|
| 112 |
.gradio-container {
|
| 113 |
background: url('https://images.unsplash.com/photo-1441974231531-c6227db76b6e?q=80&w=2560&auto=format&fit=crop') no-repeat center center fixed;
|
|
@@ -116,20 +101,22 @@ custom_css = """
|
|
| 116 |
footer {visibility: hidden}
|
| 117 |
"""
|
| 118 |
|
| 119 |
-
# Only the Token Slider remains
|
| 120 |
max_new_tokens_slider = gr.Slider(
|
| 121 |
minimum=16, maximum=512, value=128, step=8, label="Response Length (Tokens)",
|
| 122 |
)
|
| 123 |
|
|
|
|
| 124 |
demo = gr.ChatInterface(
|
| 125 |
fn=chat_fn,
|
| 126 |
title="🌿 NatureChat Lab 2",
|
| 127 |
description="Chat with the fine-tuned Llama model. Relax and enjoy the view.",
|
| 128 |
additional_inputs=[max_new_tokens_slider],
|
| 129 |
-
additional_inputs_accordion="Settings"
|
| 130 |
-
theme=nature_theme,
|
| 131 |
-
css=custom_css
|
| 132 |
)
|
| 133 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
if __name__ == "__main__":
|
| 135 |
demo.launch()
|
|
|
|
| 22 |
)
|
| 23 |
model.eval()
|
| 24 |
|
| 25 |
+
# Hardcoded system prompt
|
| 26 |
SYSTEM_PROMPT = "You are a helpful, polite assistant. Give clear and structured explanations."
|
| 27 |
|
| 28 |
def build_prompt(message, history):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
messages = []
|
|
|
|
|
|
|
| 30 |
messages.append({"role": "system", "content": SYSTEM_PROMPT})
|
| 31 |
|
|
|
|
| 32 |
for msg in history:
|
| 33 |
role = msg.get("role")
|
| 34 |
content = msg.get("content", "")
|
|
|
|
|
|
|
| 35 |
if isinstance(content, list):
|
| 36 |
texts = []
|
| 37 |
for block in content:
|
|
|
|
| 46 |
if text and role in ("user", "assistant", "system"):
|
| 47 |
messages.append({"role": role, "content": text})
|
| 48 |
|
|
|
|
| 49 |
messages.append({"role": "user", "content": message})
|
| 50 |
|
| 51 |
prompt = tokenizer.apply_chat_template(
|
|
|
|
| 54 |
return prompt
|
| 55 |
|
| 56 |
def chat_fn(message, history, max_new_tokens):
|
|
|
|
| 57 |
prompt = build_prompt(message, history)
|
|
|
|
| 58 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 59 |
|
| 60 |
+
# Default values for removed sliders
|
|
|
|
| 61 |
temperature = 0.7
|
| 62 |
top_p = 0.9
|
| 63 |
repetition_penalty = 1.1
|
|
|
|
| 82 |
return generated
|
| 83 |
|
| 84 |
# --- Nature Theme Configuration ---
|
|
|
|
| 85 |
nature_theme = gr.themes.Soft(
|
| 86 |
primary_hue="green",
|
| 87 |
secondary_hue="emerald",
|
|
|
|
| 89 |
).set(
|
| 90 |
body_background_fill="#f5f7f5",
|
| 91 |
block_background_fill="rgba(255, 255, 255, 0.9)",
|
| 92 |
+
button_primary_background_fill="#2E7D32",
|
| 93 |
border_color_primary="#4CAF50",
|
| 94 |
)
|
| 95 |
|
|
|
|
| 96 |
custom_css = """
|
| 97 |
.gradio-container {
|
| 98 |
background: url('https://images.unsplash.com/photo-1441974231531-c6227db76b6e?q=80&w=2560&auto=format&fit=crop') no-repeat center center fixed;
|
|
|
|
| 101 |
footer {visibility: hidden}
|
| 102 |
"""
|
| 103 |
|
|
|
|
| 104 |
max_new_tokens_slider = gr.Slider(
|
| 105 |
minimum=16, maximum=512, value=128, step=8, label="Response Length (Tokens)",
|
| 106 |
)
|
| 107 |
|
| 108 |
+
# Instantiate ChatInterface WITHOUT theme/css arguments initially to avoid TypeError
|
| 109 |
demo = gr.ChatInterface(
|
| 110 |
fn=chat_fn,
|
| 111 |
title="🌿 NatureChat Lab 2",
|
| 112 |
description="Chat with the fine-tuned Llama model. Relax and enjoy the view.",
|
| 113 |
additional_inputs=[max_new_tokens_slider],
|
| 114 |
+
additional_inputs_accordion="Settings"
|
|
|
|
|
|
|
| 115 |
)
|
| 116 |
|
| 117 |
+
# Apply theme and CSS manually after instantiation
|
| 118 |
+
demo.theme = nature_theme
|
| 119 |
+
demo.css = custom_css
|
| 120 |
+
|
| 121 |
if __name__ == "__main__":
|
| 122 |
demo.launch()
|