Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
+
|
| 5 |
+
MODEL_ID = "Jeppcode/ScalableLab2"
|
| 6 |
+
SUBFOLDER = "merged-model-fp16"
|
| 7 |
+
|
| 8 |
+
print(f"Loading model {MODEL_ID}/{SUBFOLDER} ...")
|
| 9 |
+
|
| 10 |
+
# Tokenizer
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 12 |
+
MODEL_ID, subfolder=SUBFOLDER,
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
# Model – fp16 and optimized for CPU
|
| 16 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 17 |
+
MODEL_ID,
|
| 18 |
+
subfolder=SUBFOLDER,
|
| 19 |
+
dtype=torch.float16,
|
| 20 |
+
low_cpu_mem_usage=True,
|
| 21 |
+
device_map="cpu",
|
| 22 |
+
)
|
| 23 |
+
model.eval()
|
| 24 |
+
|
| 25 |
+
# Hardcoded system prompt since we removed the style selector
|
| 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:
|
| 47 |
+
if isinstance(block, dict) and block.get("type") == "text":
|
| 48 |
+
texts.append(block.get("text", ""))
|
| 49 |
+
else:
|
| 50 |
+
texts.append(str(block))
|
| 51 |
+
text = "\n".join(t for t in texts if t)
|
| 52 |
+
else:
|
| 53 |
+
text = str(content)
|
| 54 |
+
|
| 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(
|
| 62 |
+
messages, tokenize=False, add_generation_prompt=True,
|
| 63 |
+
)
|
| 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 hardcoded values for the removed sliders
|
| 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
|
| 77 |
+
|
| 78 |
+
gen_kwargs = {
|
| 79 |
+
**inputs,
|
| 80 |
+
"max_new_tokens": int(max_new_tokens),
|
| 81 |
+
"pad_token_id": tokenizer.eos_token_id,
|
| 82 |
+
"eos_token_id": tokenizer.eos_token_id,
|
| 83 |
+
"repetition_penalty": float(repetition_penalty),
|
| 84 |
+
"do_sample": True,
|
| 85 |
+
"temperature": float(temperature),
|
| 86 |
+
"top_p": float(top_p),
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
with torch.no_grad():
|
| 90 |
+
outputs = model.generate(**gen_kwargs)
|
| 91 |
+
generated = tokenizer.decode(
|
| 92 |
+
outputs[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True,
|
| 93 |
+
).strip()
|
| 94 |
+
|
| 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",
|
| 102 |
+
neutral_hue="stone",
|
| 103 |
+
).set(
|
| 104 |
+
body_background_fill="#f5f7f5",
|
| 105 |
+
block_background_fill="rgba(255, 255, 255, 0.9)",
|
| 106 |
+
button_primary_background_fill="#2E7D32", # Forest Green
|
| 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;
|
| 114 |
+
background-size: cover;
|
| 115 |
+
}
|
| 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()
|