switched back to NPC_brain with different prompt insertion
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import gradio as gr
|
|
| 2 |
import random
|
| 3 |
import spaces
|
| 4 |
import torch
|
| 5 |
-
from transformers import
|
| 6 |
import csv
|
| 7 |
import os
|
| 8 |
import uuid
|
|
@@ -13,27 +13,7 @@ os.makedirs(DATA_DIR, exist_ok=True)
|
|
| 13 |
csv_path = None
|
| 14 |
|
| 15 |
|
| 16 |
-
|
| 17 |
-
model = AutoModelForMultimodalLM.from_pretrained("openbmb/MiniCPM5-1B")
|
| 18 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 19 |
-
model.to(device)
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
def generate_from_messages(messages, max_new_tokens=256, enable_thinking=False):
|
| 23 |
-
generation_messages = list(messages)
|
| 24 |
-
if enable_thinking:
|
| 25 |
-
generation_messages.append(
|
| 26 |
-
{"role": "user", "content": "Please think through the answer before responding."}
|
| 27 |
-
)
|
| 28 |
-
inputs = tokenizer.apply_chat_template(
|
| 29 |
-
generation_messages,
|
| 30 |
-
add_generation_prompt=True,
|
| 31 |
-
tokenize=True,
|
| 32 |
-
return_dict=True,
|
| 33 |
-
return_tensors="pt",
|
| 34 |
-
).to(device)
|
| 35 |
-
output_ids = model.generate(**inputs, max_new_tokens=max_new_tokens)
|
| 36 |
-
return tokenizer.batch_decode(output_ids, skip_special_tokens=True)[0].strip()
|
| 37 |
|
| 38 |
DESC_SYSTEM_PROMPT = (
|
| 39 |
"Write a description of a fantasy character based on the user prompt"
|
|
@@ -265,37 +245,29 @@ def append_to_csv(csv_path, prompt, description, backstory, strength, dexterity,
|
|
| 265 |
])
|
| 266 |
|
| 267 |
@spaces.GPU
|
| 268 |
-
def char_description(prompt
|
| 269 |
|
| 270 |
-
prompt1 = [
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
output1_text = generate_from_messages(prompt1, max_new_tokens=256, enable_thinking=enable_thinking)
|
| 275 |
prompt2 = [
|
| 276 |
-
{"role": "
|
| 277 |
-
{"role": "user", "content": prompt},
|
| 278 |
-
{"role": "user", "content": output1_text},
|
| 279 |
-
{"role": "user", "content": "Backstory:"},
|
| 280 |
]
|
| 281 |
-
|
| 282 |
-
return output1_text,
|
| 283 |
|
| 284 |
@spaces.GPU
|
| 285 |
-
def rand_char_description(prompt
|
| 286 |
-
prompt1 = [
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
output1_text = generate_from_messages(prompt1, max_new_tokens=256, enable_thinking=enable_thinking)
|
| 291 |
prompt2 = [
|
| 292 |
-
{"role": "
|
| 293 |
-
{"role": "user", "content": prompt},
|
| 294 |
-
{"role": "user", "content": output1_text},
|
| 295 |
-
{"role": "user", "content": "Backstory:"},
|
| 296 |
]
|
| 297 |
-
|
| 298 |
-
return output1_text,
|
| 299 |
|
| 300 |
|
| 301 |
def char_description_csv(prompt, state):
|
|
|
|
| 2 |
import random
|
| 3 |
import spaces
|
| 4 |
import torch
|
| 5 |
+
from transformers import pipeline
|
| 6 |
import csv
|
| 7 |
import os
|
| 8 |
import uuid
|
|
|
|
| 13 |
csv_path = None
|
| 14 |
|
| 15 |
|
| 16 |
+
pipe = pipeline("text-generation", model="mecoffey/NPC_brain")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
DESC_SYSTEM_PROMPT = (
|
| 19 |
"Write a description of a fantasy character based on the user prompt"
|
|
|
|
| 245 |
])
|
| 246 |
|
| 247 |
@spaces.GPU
|
| 248 |
+
def char_description(prompt):
|
| 249 |
|
| 250 |
+
prompt1 = [{"role": "user", "content": prompt}]
|
| 251 |
+
output1 = pipe(prompt1, return_full_text=False, max_new_tokens=256)
|
| 252 |
+
output1_text = output1[0]["generated_text"].strip()
|
| 253 |
+
combine = str(prompt) + "\n" + str(output1_text) + "\nBackstory:"
|
|
|
|
| 254 |
prompt2 = [
|
| 255 |
+
{"role": "user", "content": combine},
|
|
|
|
|
|
|
|
|
|
| 256 |
]
|
| 257 |
+
output2 = pipe(prompt2, return_full_text=False, max_new_tokens=256)
|
| 258 |
+
return output1_text, output2[0]["generated_text"].strip()
|
| 259 |
|
| 260 |
@spaces.GPU
|
| 261 |
+
def rand_char_description(prompt):
|
| 262 |
+
prompt1 = [{"role": "user", "content": prompt}]
|
| 263 |
+
output1 = pipe(prompt1, return_full_text=False, max_new_tokens=256)
|
| 264 |
+
output1_text = output1[0]["generated_text"].strip()
|
| 265 |
+
combine = str(prompt) + "\n" + str(output1_text) + "\nBackstory:"
|
|
|
|
| 266 |
prompt2 = [
|
| 267 |
+
{"role": "user", "content": combine},
|
|
|
|
|
|
|
|
|
|
| 268 |
]
|
| 269 |
+
output2 = pipe(prompt2, return_full_text=False, max_new_tokens=256)
|
| 270 |
+
return output1_text, output2[0]["generated_text"].strip()
|
| 271 |
|
| 272 |
|
| 273 |
def char_description_csv(prompt, state):
|