DarwinAnim8or commited on
Commit
4aa4e53
Β·
verified Β·
1 Parent(s): 91315d8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -123
app.py CHANGED
@@ -5,82 +5,81 @@ import torch
5
  # Model configuration
6
  MODEL_NAME = "DarwinAnim8or/TinyRP"
7
 
8
- # Global variables for model
9
- tokenizer = None
10
- model = None
11
-
12
- def load_model():
13
- """Load model and tokenizer"""
14
- global tokenizer, model
15
- try:
16
- print("Loading model for CPU inference...")
17
- tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
18
- model = AutoModelForCausalLM.from_pretrained(
19
- MODEL_NAME,
20
- torch_dtype=torch.float32,
21
- device_map="cpu",
22
- trust_remote_code=True
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
- # Sample character presets
31
  CHARACTERS = {
32
- "Custom Character": "",
33
- "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.",
34
- "Mysterious Wizard": "You are Eldara, an ancient and wise wizard who speaks in riddles and knows secrets of the mystical arts. You are helpful but often cryptic.",
35
- "Friendly Tavern Keeper": "You are Bram, a cheerful tavern keeper who loves telling stories and meeting new travelers. Your tavern is a warm, welcoming place.",
36
- "Curious Scientist": "You are Dr. Maya Chen, a brilliant scientist fascinated by discovery. You explain complex concepts simply and love new experiments.",
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 chat_respond(message, history, character_desc, max_tokens, temperature, top_p, rep_penalty):
41
- """Main chat response function"""
42
 
43
  if not message.strip():
44
- return history
45
-
 
46
  if model is None:
47
- response = "❌ Model not loaded. Please check the model path."
48
- history.append([message, response])
49
- return history
50
 
51
  try:
52
  # Build ChatML conversation
53
  conversation = ""
54
 
55
  # Add character as system message
56
- if character_desc.strip():
57
- conversation += f"<|im_start|>system\n{character_desc}<|im_end|>\n"
58
 
59
- # Add history
60
- for user_msg, bot_msg in history:
61
  conversation += f"<|im_start|>user\n{user_msg}<|im_end|>\n"
62
- conversation += f"<|im_start|>assistant\n{bot_msg}<|im_end|>\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(conversation, return_tensors="pt", max_length=900, truncation=True)
 
 
 
 
 
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=rep_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 response
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 = "Sorry, couldn't generate a response."
92
 
93
- # Clean up response
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
- response = f"Error: {str(e)}"
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
- def clear_chat():
112
- """Clear chat history"""
113
- return []
114
-
115
- # Load model on startup
116
- model_loaded = load_model()
117
-
118
- # Create interface
119
- with gr.Blocks(title="TinyRP Chat") as demo:
120
-
121
- gr.Markdown("# 🎭 TinyRP Character Chat")
122
- gr.Markdown("Chat with AI characters using local CPU inference!")
123
-
124
- with gr.Row():
125
- with gr.Column(scale=3):
126
- chatbot = gr.Chatbot(height=500, label="Conversation")
127
- msg_box = gr.Textbox(label="Message", placeholder="Type here...")
128
-
129
- with gr.Column(scale=1):
130
- gr.Markdown("### Character")
131
- char_dropdown = gr.Dropdown(
132
- choices=list(CHARACTERS.keys()),
133
- value="Custom Character",
134
- label="Preset"
135
- )
136
- char_text = gr.Textbox(
137
- label="Description",
138
- lines=4,
139
- placeholder="Character description..."
140
- )
141
- load_btn = gr.Button("Load Character")
142
-
143
- gr.Markdown("### Settings")
144
- max_tokens = gr.Slider(16, 256, 80, label="Max tokens")
145
- temperature = gr.Slider(0.1, 2.0, 0.9, label="Temperature")
146
- top_p = gr.Slider(0.1, 1.0, 0.85, label="Top-p")
147
- rep_penalty = gr.Slider(1.0, 1.5, 1.1, label="Rep penalty")
148
-
149
- clear_btn = gr.Button("Clear Chat")
150
-
151
- # Character samples
152
- gr.Markdown("### Sample Characters")
153
- with gr.Row():
154
- for name in ["Adventurous Knight", "Mysterious Wizard", "Space Explorer"]:
155
- gr.Markdown(f"**{name}**: {CHARACTERS[name][:80]}...")
156
-
157
- # Event handlers - simplified
158
- msg_box.submit(
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()