Spaces:
Sleeping
Sleeping
File size: 10,614 Bytes
9f9aaaf 38b1b0b 9f9aaaf 38b1b0b 9f9aaaf 38b1b0b 9f9aaaf 38b1b0b 9f9aaaf 38b1b0b 9f9aaaf 38b1b0b 9f9aaaf 38b1b0b 9f9aaaf 38b1b0b 9f9aaaf 38b1b0b 9f9aaaf 38b1b0b 9f9aaaf 38b1b0b 9f9aaaf 38b1b0b 9f9aaaf 38b1b0b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
# Load the model
MODEL_NAME = "DarwinAnim8or/TinyRP"
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, torch_dtype=torch.float16, device_map="auto")
# Sample character presets
SAMPLE_CHARACTERS = {
"Custom Character": "",
"Adventurous Knight": "You are Sir Gareth, a brave and noble knight on a quest to save the kingdom. You speak with honor and courage, always ready to help those in need. You carry an enchanted sword and have a loyal horse named Thunder.",
"Mysterious Wizard": "You are Eldara, an ancient and wise wizard who speaks in riddles and knows secrets of the mystical arts. You live in a tower filled with magical books and potions. You are helpful but often cryptic in your responses.",
"Friendly Tavern Keeper": "You are Bram, a cheerful tavern keeper who loves telling stories and meeting new travelers. Your tavern 'The Dancing Dragon' is a warm, welcoming place. You know all the local gossip and always have a tale to share.",
"Curious Scientist": "You are Dr. Maya Chen, a brilliant scientist who is fascinated by discovery and invention. You're enthusiastic about explaining complex concepts in simple ways and always looking for new experiments to try.",
"Space Explorer": "You are Captain Nova, a fearless space explorer who has traveled to distant galaxies. You pilot the starship 'Wanderer' and have encountered many alien species. You're brave, curious, and always ready for the next adventure.",
"Fantasy Princess": "You are Princess Lyra, kind-hearted royalty who cares deeply about her people. You're intelligent, diplomatic, and skilled in both politics and magic. You often sneak out of the castle to help citizens in need."
}
def generate_response(
message,
history: list[tuple[str, str]],
character_description,
max_tokens,
temperature,
top_p,
repetition_penalty,
use_chatml_format
):
# Prepare the conversation
if use_chatml_format and character_description.strip():
# Use ChatML format with character as system message
conversation = f"<|im_start|>system\n{character_description}<|im_end|>\n"
# Add conversation history
for user_msg, assistant_msg in history:
if user_msg:
conversation += f"<|im_start|>user\n{user_msg}<|im_end|>\n"
if assistant_msg:
conversation += f"<|im_start|>assistant\n{assistant_msg}<|im_end|>\n"
# Add current message
conversation += f"<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n"
else:
# Simple format
if character_description.strip():
conversation = f"{character_description}\n\n"
else:
conversation = ""
# Add conversation history
for user_msg, assistant_msg in history:
if user_msg:
conversation += f"Human: {user_msg}\n"
if assistant_msg:
conversation += f"Assistant: {assistant_msg}\n"
# Add current message
conversation += f"Human: {message}\nAssistant:"
# Tokenize
inputs = tokenizer.encode(conversation, return_tensors="pt", truncation=True, max_length=1024-max_tokens)
# Generate
with torch.no_grad():
outputs = model.generate(
inputs,
max_new_tokens=max_tokens,
temperature=temperature,
top_p=top_p,
repetition_penalty=repetition_penalty,
do_sample=True,
pad_token_id=tokenizer.eos_token_id,
eos_token_id=tokenizer.eos_token_id,
)
# Decode response
full_response = tokenizer.decode(outputs[0], skip_special_tokens=True)
# Extract just the new response
if use_chatml_format:
# Split on the last assistant tag
response = full_response.split("<|im_start|>assistant\n")[-1]
# Remove any trailing end tags
response = response.replace("<|im_end|>", "").strip()
else:
# Split on the last "Assistant:"
response = full_response.split("Assistant:")[-1].strip()
return response
def load_character_preset(character_name):
"""Load a character preset"""
return SAMPLE_CHARACTERS.get(character_name, "")
# Custom CSS for better styling
css = """
.character-card {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
border-radius: 15px;
padding: 20px;
margin: 10px 0;
color: white;
}
.title-text {
text-align: center;
font-size: 2.5em;
font-weight: bold;
background: linear-gradient(45deg, #667eea, #764ba2);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
margin-bottom: 20px;
}
.parameter-box {
background: #f8f9fa;
border-radius: 10px;
padding: 15px;
margin: 10px 0;
}
"""
# Create the Gradio interface
with gr.Blocks(css=css, title="TinyRP Chat Demo") as demo:
gr.HTML('<div class="title-text">π TinyRP Character Chat</div>')
gr.Markdown("""
### Welcome to TinyRP!
This is a demo of a small but capable roleplay model. Choose a character preset or create your own!
**Tips for better roleplay:**
- Be descriptive in your messages
- Stay in character
- Use ChatML format for best results
- Adjust temperature for creativity vs consistency
""")
with gr.Row():
with gr.Column(scale=2):
# Chat interface
chatbot = gr.Chatbot(
label="Chat",
height=500,
show_label=False,
avatar_images=("π§", "π")
)
with gr.Row():
msg = gr.Textbox(
label="Your message",
placeholder="Type your message here...",
lines=2,
scale=4
)
send_btn = gr.Button("Send", variant="primary", scale=1)
with gr.Column(scale=1):
# Character selection
with gr.Group():
gr.Markdown("### π Character Setup")
character_preset = gr.Dropdown(
choices=list(SAMPLE_CHARACTERS.keys()),
value="Custom Character",
label="Character Presets",
interactive=True
)
character_description = gr.Textbox(
label="Character Description",
placeholder="Describe your character's personality, background, and speaking style...",
lines=6,
value=""
)
load_preset_btn = gr.Button("Load Preset", variant="secondary")
# Generation parameters
with gr.Group():
gr.Markdown("### βοΈ Generation Settings")
use_chatml_format = gr.Checkbox(
label="Use ChatML Format",
value=True,
info="Recommended for better character consistency"
)
max_tokens = gr.Slider(
minimum=16,
maximum=512,
value=128,
step=16,
label="Max Response Length",
info="Longer = more detailed responses"
)
temperature = gr.Slider(
minimum=0.1,
maximum=2.0,
value=0.9,
step=0.1,
label="Temperature",
info="Higher = more creative/random"
)
top_p = gr.Slider(
minimum=0.1,
maximum=1.0,
value=0.85,
step=0.05,
label="Top-p",
info="Focus on top % of likely words"
)
repetition_penalty = gr.Slider(
minimum=1.0,
maximum=1.5,
value=1.1,
step=0.05,
label="Repetition Penalty",
info="Reduce repetitive text"
)
# Control buttons
with gr.Group():
clear_btn = gr.Button("ποΈ Clear Chat", variant="secondary")
# Sample character cards
with gr.Row():
gr.Markdown("### π Featured Characters")
with gr.Row():
for char_name, char_desc in list(SAMPLE_CHARACTERS.items())[1:4]: # Show first 3 non-custom
with gr.Column(scale=1):
gr.Markdown(f"""
<div class="character-card">
<h4>{char_name}</h4>
<p>{char_desc[:100]}...</p>
</div>
""")
# Event handlers
def respond_wrapper(message, history, char_desc, max_tok, temp, top_p, rep_pen, use_chatml):
if not message.strip():
return history, ""
try:
response = generate_response(
message, history, char_desc, max_tok, temp, top_p, rep_pen, use_chatml
)
history.append((message, response))
return history, ""
except Exception as e:
error_msg = f"Error generating response: {str(e)}"
history.append((message, error_msg))
return history, ""
# Connect events
send_btn.click(
respond_wrapper,
inputs=[msg, chatbot, character_description, max_tokens, temperature, top_p, repetition_penalty, use_chatml_format],
outputs=[chatbot, msg]
)
msg.submit(
respond_wrapper,
inputs=[msg, chatbot, character_description, max_tokens, temperature, top_p, repetition_penalty, use_chatml_format],
outputs=[chatbot, msg]
)
load_preset_btn.click(
load_character_preset,
inputs=[character_preset],
outputs=[character_description]
)
character_preset.change(
load_character_preset,
inputs=[character_preset],
outputs=[character_description]
)
clear_btn.click(lambda: ([], ""), outputs=[chatbot, msg])
if __name__ == "__main__":
demo.launch() |