Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,82 +5,81 @@ import torch
|
|
5 |
# Model configuration
|
6 |
MODEL_NAME = "DarwinAnim8or/TinyRP"
|
7 |
|
8 |
-
#
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
)
|
24 |
-
print(f"β
Model loaded successfully: {MODEL_NAME}")
|
25 |
-
return True
|
26 |
-
except Exception as e:
|
27 |
-
print(f"β Error loading model: {e}")
|
28 |
-
return False
|
29 |
|
30 |
-
#
|
31 |
CHARACTERS = {
|
32 |
-
"
|
33 |
-
"
|
34 |
-
"
|
35 |
-
"
|
36 |
-
"
|
37 |
-
"Space Explorer": "You are Captain Nova, a fearless space explorer who has traveled to distant galaxies. You're brave, curious, and ready for adventure."
|
38 |
}
|
39 |
|
40 |
-
def
|
41 |
-
"""
|
42 |
|
43 |
if not message.strip():
|
44 |
-
|
45 |
-
|
|
|
46 |
if model is None:
|
47 |
-
|
48 |
-
|
49 |
-
return history
|
50 |
|
51 |
try:
|
52 |
# Build ChatML conversation
|
53 |
conversation = ""
|
54 |
|
55 |
# Add character as system message
|
56 |
-
if
|
57 |
-
conversation += f"<|im_start|>system\n{
|
58 |
|
59 |
-
# Add history
|
60 |
-
for user_msg,
|
61 |
conversation += f"<|im_start|>user\n{user_msg}<|im_end|>\n"
|
62 |
-
conversation += f"<|im_start|>assistant\n{
|
63 |
|
64 |
# Add current message
|
65 |
conversation += f"<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n"
|
66 |
|
67 |
# Tokenize
|
68 |
-
inputs = tokenizer.encode(
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
# Generate
|
|
|
71 |
with torch.no_grad():
|
72 |
outputs = model.generate(
|
73 |
inputs,
|
74 |
-
max_new_tokens=max_tokens,
|
75 |
-
temperature=temperature,
|
76 |
-
top_p=top_p,
|
77 |
-
repetition_penalty=
|
78 |
do_sample=True,
|
79 |
pad_token_id=tokenizer.eos_token_id,
|
80 |
eos_token_id=tokenizer.eos_token_id
|
81 |
)
|
82 |
|
83 |
-
# Decode
|
84 |
full_text = tokenizer.decode(outputs[0], skip_special_tokens=False)
|
85 |
|
86 |
# Extract assistant response
|
@@ -88,92 +87,68 @@ def chat_respond(message, history, character_desc, max_tokens, temperature, top_
|
|
88 |
response = full_text.split("<|im_start|>assistant\n")[-1]
|
89 |
response = response.replace("<|im_end|>", "").strip()
|
90 |
else:
|
91 |
-
response = "
|
92 |
|
93 |
-
# Clean
|
94 |
response = response.replace("<|im_start|>", "").replace("<|im_end|>", "")
|
95 |
response = response.strip()
|
96 |
|
97 |
if not response:
|
98 |
response = "No response generated."
|
99 |
|
|
|
|
|
100 |
except Exception as e:
|
101 |
-
|
102 |
-
|
103 |
-
# Add to history
|
104 |
-
history.append([message, response])
|
105 |
-
return history
|
106 |
-
|
107 |
-
def load_character(character_name):
|
108 |
-
"""Load character preset"""
|
109 |
-
return CHARACTERS.get(character_name, "")
|
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 |
-
fn=chat_respond,
|
160 |
-
inputs=[msg_box, chatbot, char_text, max_tokens, temperature, top_p, rep_penalty],
|
161 |
-
outputs=[chatbot]
|
162 |
-
).then(
|
163 |
-
fn=lambda: "",
|
164 |
-
outputs=[msg_box]
|
165 |
-
)
|
166 |
-
|
167 |
-
load_btn.click(
|
168 |
-
fn=load_character,
|
169 |
-
inputs=[char_dropdown],
|
170 |
-
outputs=[char_text]
|
171 |
-
)
|
172 |
-
|
173 |
-
clear_btn.click(
|
174 |
-
fn=clear_chat,
|
175 |
-
outputs=[chatbot]
|
176 |
-
)
|
177 |
|
178 |
if __name__ == "__main__":
|
179 |
demo.launch()
|
|
|
5 |
# Model configuration
|
6 |
MODEL_NAME = "DarwinAnim8or/TinyRP"
|
7 |
|
8 |
+
# Load model
|
9 |
+
print("Loading model...")
|
10 |
+
try:
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
12 |
+
model = AutoModelForCausalLM.from_pretrained(
|
13 |
+
MODEL_NAME,
|
14 |
+
torch_dtype=torch.float32,
|
15 |
+
device_map="cpu",
|
16 |
+
trust_remote_code=True
|
17 |
+
)
|
18 |
+
print("β
Model loaded successfully")
|
19 |
+
except Exception as e:
|
20 |
+
print(f"β Model loading failed: {e}")
|
21 |
+
tokenizer = None
|
22 |
+
model = None
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
+
# Character presets
|
25 |
CHARACTERS = {
|
26 |
+
"Knight": "You are Sir Gareth, a brave knight on a quest to save the kingdom. You speak with honor and courage.",
|
27 |
+
"Wizard": "You are Eldara, an ancient wizard who speaks in riddles and knows mystical secrets.",
|
28 |
+
"Tavern Keeper": "You are Bram, a cheerful tavern keeper who loves stories and meeting travelers.",
|
29 |
+
"Scientist": "You are Dr. Maya Chen, a brilliant scientist who loves discovery and explaining concepts simply.",
|
30 |
+
"Space Explorer": "You are Captain Nova, a fearless space explorer who has traveled distant galaxies."
|
|
|
31 |
}
|
32 |
|
33 |
+
def respond(message, history, character, max_tokens, temperature, top_p, repetition_penalty):
|
34 |
+
"""Generate response using ChatML format"""
|
35 |
|
36 |
if not message.strip():
|
37 |
+
yield "Please enter a message."
|
38 |
+
return
|
39 |
+
|
40 |
if model is None:
|
41 |
+
yield "β Model not loaded properly."
|
42 |
+
return
|
|
|
43 |
|
44 |
try:
|
45 |
# Build ChatML conversation
|
46 |
conversation = ""
|
47 |
|
48 |
# Add character as system message
|
49 |
+
if character != "None" and character in CHARACTERS:
|
50 |
+
conversation += f"<|im_start|>system\n{CHARACTERS[character]}<|im_end|>\n"
|
51 |
|
52 |
+
# Add conversation history
|
53 |
+
for user_msg, assistant_msg in history:
|
54 |
conversation += f"<|im_start|>user\n{user_msg}<|im_end|>\n"
|
55 |
+
conversation += f"<|im_start|>assistant\n{assistant_msg}<|im_end|>\n"
|
56 |
|
57 |
# Add current message
|
58 |
conversation += f"<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n"
|
59 |
|
60 |
# Tokenize
|
61 |
+
inputs = tokenizer.encode(
|
62 |
+
conversation,
|
63 |
+
return_tensors="pt",
|
64 |
+
max_length=900,
|
65 |
+
truncation=True
|
66 |
+
)
|
67 |
|
68 |
# Generate
|
69 |
+
response = ""
|
70 |
with torch.no_grad():
|
71 |
outputs = model.generate(
|
72 |
inputs,
|
73 |
+
max_new_tokens=int(max_tokens),
|
74 |
+
temperature=float(temperature),
|
75 |
+
top_p=float(top_p),
|
76 |
+
repetition_penalty=float(repetition_penalty),
|
77 |
do_sample=True,
|
78 |
pad_token_id=tokenizer.eos_token_id,
|
79 |
eos_token_id=tokenizer.eos_token_id
|
80 |
)
|
81 |
|
82 |
+
# Decode
|
83 |
full_text = tokenizer.decode(outputs[0], skip_special_tokens=False)
|
84 |
|
85 |
# Extract assistant response
|
|
|
87 |
response = full_text.split("<|im_start|>assistant\n")[-1]
|
88 |
response = response.replace("<|im_end|>", "").strip()
|
89 |
else:
|
90 |
+
response = "Could not generate response."
|
91 |
|
92 |
+
# Clean response
|
93 |
response = response.replace("<|im_start|>", "").replace("<|im_end|>", "")
|
94 |
response = response.strip()
|
95 |
|
96 |
if not response:
|
97 |
response = "No response generated."
|
98 |
|
99 |
+
yield response
|
100 |
+
|
101 |
except Exception as e:
|
102 |
+
yield f"Generation error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
|
104 |
+
# Create simple ChatInterface
|
105 |
+
demo = gr.ChatInterface(
|
106 |
+
fn=respond,
|
107 |
+
title="π TinyRP Character Chat",
|
108 |
+
description="Chat with AI characters using local CPU inference! Select a character and start chatting.",
|
109 |
+
additional_inputs=[
|
110 |
+
gr.Dropdown(
|
111 |
+
choices=["None"] + list(CHARACTERS.keys()),
|
112 |
+
value="Knight",
|
113 |
+
label="Character"
|
114 |
+
),
|
115 |
+
gr.Slider(
|
116 |
+
minimum=16,
|
117 |
+
maximum=256,
|
118 |
+
value=80,
|
119 |
+
step=16,
|
120 |
+
label="Max tokens"
|
121 |
+
),
|
122 |
+
gr.Slider(
|
123 |
+
minimum=0.1,
|
124 |
+
maximum=2.0,
|
125 |
+
value=0.9,
|
126 |
+
step=0.1,
|
127 |
+
label="Temperature"
|
128 |
+
),
|
129 |
+
gr.Slider(
|
130 |
+
minimum=0.1,
|
131 |
+
maximum=1.0,
|
132 |
+
value=0.85,
|
133 |
+
step=0.05,
|
134 |
+
label="Top-p"
|
135 |
+
),
|
136 |
+
gr.Slider(
|
137 |
+
minimum=1.0,
|
138 |
+
maximum=1.5,
|
139 |
+
value=1.1,
|
140 |
+
step=0.05,
|
141 |
+
label="Repetition penalty"
|
142 |
+
)
|
143 |
+
],
|
144 |
+
examples=[
|
145 |
+
["Hello! What's your name?"],
|
146 |
+
["Tell me about your adventures."],
|
147 |
+
["What's your favorite thing to do?"],
|
148 |
+
["Can you help me with something?"]
|
149 |
+
],
|
150 |
+
cache_examples=False
|
151 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
152 |
|
153 |
if __name__ == "__main__":
|
154 |
demo.launch()
|