added csv, changed smash to charm, edited system prompts
Browse files
app.py
CHANGED
|
@@ -3,6 +3,14 @@ import random
|
|
| 3 |
import spaces
|
| 4 |
import torch
|
| 5 |
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
pipe = pipeline("text-generation", model="limloop/MN-12B-Runeweaver-RP-RU")
|
| 8 |
|
|
@@ -30,7 +38,7 @@ wisdom = ''
|
|
| 30 |
charisma = ''
|
| 31 |
ac = ''
|
| 32 |
hp = ''
|
| 33 |
-
|
| 34 |
pref_traits = ''
|
| 35 |
ick_traits = ''
|
| 36 |
speed = ''
|
|
@@ -69,37 +77,90 @@ def traits_picker(filepath):
|
|
| 69 |
def rand_race_gen(filepath):
|
| 70 |
with open(filepath, 'r') as f:
|
| 71 |
items = [line.strip() for line in f if line.strip()]
|
| 72 |
-
|
| 73 |
-
return selected
|
| 74 |
|
| 75 |
def bard_stats_gen():
|
| 76 |
-
global
|
| 77 |
-
|
| 78 |
global pref_traits
|
| 79 |
pref_traits = traits_picker('traits.txt')
|
| 80 |
global ick_traits
|
| 81 |
ick_traits = traits_picker('traits.txt')
|
| 82 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
@spaces.GPU
|
| 85 |
def char_description(prompt):
|
| 86 |
|
| 87 |
prompt1 = f"{DESC_SYSTEM_PROMPT}\n\nCharacter Description: {prompt}\n\nDescribe:"
|
| 88 |
output1 = pipe(prompt1, return_full_text=False, max_new_tokens=256)
|
| 89 |
-
prompt2 = f"{BACK_SYSTEM_PROMPT}\n\
|
| 90 |
output2 = pipe(prompt2, return_full_text=False, max_new_tokens=256)
|
| 91 |
return output1[0]["generated_text"].strip(),output2[0]["generated_text"].strip()
|
| 92 |
|
| 93 |
@spaces.GPU
|
| 94 |
-
def rand_char_description():
|
| 95 |
-
|
| 96 |
-
prompt1 = f"{DESC_SYSTEM_PROMPT}\n\nCharacter Description: {rand_race}\n\nDescribe:"
|
| 97 |
output1 = pipe(prompt1, return_full_text=False, max_new_tokens=256)
|
| 98 |
-
prompt2 = f"{BACK_SYSTEM_PROMPT}\n\
|
| 99 |
output2 = pipe(prompt2, return_full_text=False, max_new_tokens=256)
|
| 100 |
return output1[0]["generated_text"].strip(),output2[0]["generated_text"].strip()
|
| 101 |
|
| 102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
|
| 105 |
##UI is fine as-is
|
|
@@ -130,14 +191,14 @@ with gr.Blocks() as demo:
|
|
| 130 |
with gr.Column(scale = 2):
|
| 131 |
text_out1 = gr.Textbox(label = "Description")
|
| 132 |
text_out2 = gr.Textbox(label = "Backstory")
|
| 133 |
-
btn.click(fn =
|
| 134 |
-
rand.click(fn =
|
| 135 |
|
| 136 |
|
| 137 |
with gr.Column(scale = 0):
|
| 138 |
ac_box = gr.Textbox(value = ac, label = "AC", interactive = False)
|
| 139 |
hp_box = gr.Textbox(value = hp, label = "HP", interactive = False)
|
| 140 |
-
speed_box = gr.Textbox(value =
|
| 141 |
btn.click(fn = stats_gen_2, outputs = [ac_box, hp_box, speed_box])
|
| 142 |
rand.click(fn = stats_gen_2, outputs = [ac_box, hp_box, speed_box])
|
| 143 |
|
|
@@ -146,14 +207,16 @@ with gr.Blocks() as demo:
|
|
| 146 |
|
| 147 |
with gr.Accordion("Bard Stats", open = False):
|
| 148 |
with gr.Row():
|
| 149 |
-
dcs = gr.Textbox(value =
|
| 150 |
prefs = gr.Textbox(value = pref_traits, label = "Advantage if:")
|
| 151 |
icks = gr.Textbox(value = ick_traits, label = "Disdvantage if:")
|
| 152 |
btn.click(fn = bard_stats_gen, outputs = [dcs, prefs, icks])
|
| 153 |
rand.click(fn = bard_stats_gen, outputs = [dcs, prefs, icks])
|
| 154 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
|
| 157 |
-
# Need to add export feature.
|
| 158 |
-
|
| 159 |
demo.launch(theme = gr.themes.Monochrome())
|
|
|
|
| 3 |
import spaces
|
| 4 |
import torch
|
| 5 |
from transformers import pipeline
|
| 6 |
+
import csv
|
| 7 |
+
import os
|
| 8 |
+
import uuid
|
| 9 |
+
import datetime
|
| 10 |
+
|
| 11 |
+
DATA_DIR = os.path.join(os.getcwd(), "session_data")
|
| 12 |
+
os.makedirs(DATA_DIR, exist_ok=True)
|
| 13 |
+
csv_path = None
|
| 14 |
|
| 15 |
pipe = pipeline("text-generation", model="limloop/MN-12B-Runeweaver-RP-RU")
|
| 16 |
|
|
|
|
| 38 |
charisma = ''
|
| 39 |
ac = ''
|
| 40 |
hp = ''
|
| 41 |
+
charm = ''
|
| 42 |
pref_traits = ''
|
| 43 |
ick_traits = ''
|
| 44 |
speed = ''
|
|
|
|
| 77 |
def rand_race_gen(filepath):
|
| 78 |
with open(filepath, 'r') as f:
|
| 79 |
items = [line.strip() for line in f if line.strip()]
|
| 80 |
+
return random.choice(items)
|
|
|
|
| 81 |
|
| 82 |
def bard_stats_gen():
|
| 83 |
+
global charm
|
| 84 |
+
charm = random.randint(1,30)
|
| 85 |
global pref_traits
|
| 86 |
pref_traits = traits_picker('traits.txt')
|
| 87 |
global ick_traits
|
| 88 |
ick_traits = traits_picker('traits.txt')
|
| 89 |
+
return charm, pref_traits, ick_traits
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def make_session_csv():
|
| 93 |
+
session_id = uuid.uuid4().hex
|
| 94 |
+
return os.path.join(DATA_DIR, f"story_session_{session_id}.csv")
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
def append_to_csv(csv_path, prompt, description, backstory):
|
| 98 |
+
is_new = not os.path.exists(csv_path)
|
| 99 |
+
with open(csv_path, "a", newline="", encoding="utf-8") as f:
|
| 100 |
+
writer = csv.writer(f)
|
| 101 |
+
if is_new:
|
| 102 |
+
writer.writerow(["timestamp", "prompt", "description", "backstory", "strength", "dexterity", "constitution", "intelligence", "wisdom", "charisma", "ac", "hp", "speed", "charm", "pref_traits", "ick_traits"])
|
| 103 |
+
writer.writerow([
|
| 104 |
+
datetime.datetime.utcnow().isoformat(),
|
| 105 |
+
prompt,
|
| 106 |
+
description,
|
| 107 |
+
backstory,
|
| 108 |
+
strength,
|
| 109 |
+
dexterity,
|
| 110 |
+
constitution,
|
| 111 |
+
intelligence,
|
| 112 |
+
wisdom,
|
| 113 |
+
charisma,
|
| 114 |
+
ac,
|
| 115 |
+
hp,
|
| 116 |
+
speed,
|
| 117 |
+
charm,
|
| 118 |
+
pref_traits,
|
| 119 |
+
ick_traits,
|
| 120 |
+
])
|
| 121 |
|
| 122 |
@spaces.GPU
|
| 123 |
def char_description(prompt):
|
| 124 |
|
| 125 |
prompt1 = f"{DESC_SYSTEM_PROMPT}\n\nCharacter Description: {prompt}\n\nDescribe:"
|
| 126 |
output1 = pipe(prompt1, return_full_text=False, max_new_tokens=256)
|
| 127 |
+
prompt2 = f"{BACK_SYSTEM_PROMPT}\n\nthey are described as{output1}\n\nThe user asked for: {prompt}\n\nBackstory:"
|
| 128 |
output2 = pipe(prompt2, return_full_text=False, max_new_tokens=256)
|
| 129 |
return output1[0]["generated_text"].strip(),output2[0]["generated_text"].strip()
|
| 130 |
|
| 131 |
@spaces.GPU
|
| 132 |
+
def rand_char_description(prompt):
|
| 133 |
+
prompt1 = f"{DESC_SYSTEM_PROMPT}\n\nCharacter Description: {prompt}\n\nDescribe:"
|
|
|
|
| 134 |
output1 = pipe(prompt1, return_full_text=False, max_new_tokens=256)
|
| 135 |
+
prompt2 = f"{BACK_SYSTEM_PROMPT}\n\nthey are described as{output1}\n\nThe user asked for: {prompt}\n\nBackstory:"
|
| 136 |
output2 = pipe(prompt2, return_full_text=False, max_new_tokens=256)
|
| 137 |
return output1[0]["generated_text"].strip(),output2[0]["generated_text"].strip()
|
| 138 |
|
| 139 |
|
| 140 |
+
def char_description_csv(prompt):
|
| 141 |
+
global csv_path
|
| 142 |
+
if csv_path is None:
|
| 143 |
+
csv_path = make_session_csv()
|
| 144 |
+
description, backstory = char_description(prompt)
|
| 145 |
+
append_to_csv(csv_path, prompt, description, backstory)
|
| 146 |
+
return description, backstory
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
def rand_char_description_csv():
|
| 150 |
+
global csv_path
|
| 151 |
+
if csv_path is None:
|
| 152 |
+
csv_path = make_session_csv()
|
| 153 |
+
rand_race = rand_race_gen('core_races.txt')
|
| 154 |
+
description, backstory = rand_char_description(rand_race)
|
| 155 |
+
append_to_csv(csv_path, rand_race, description, backstory)
|
| 156 |
+
return description, backstory
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
def get_csv_file():
|
| 160 |
+
global csv_path
|
| 161 |
+
if csv_path is None:
|
| 162 |
+
csv_path = make_session_csv()
|
| 163 |
+
return csv_path
|
| 164 |
|
| 165 |
|
| 166 |
##UI is fine as-is
|
|
|
|
| 191 |
with gr.Column(scale = 2):
|
| 192 |
text_out1 = gr.Textbox(label = "Description")
|
| 193 |
text_out2 = gr.Textbox(label = "Backstory")
|
| 194 |
+
btn.click(fn = char_description_csv,inputs = text_in, outputs = [text_out1, text_out2])
|
| 195 |
+
rand.click(fn = rand_char_description_csv, outputs = [text_out1, text_out2])
|
| 196 |
|
| 197 |
|
| 198 |
with gr.Column(scale = 0):
|
| 199 |
ac_box = gr.Textbox(value = ac, label = "AC", interactive = False)
|
| 200 |
hp_box = gr.Textbox(value = hp, label = "HP", interactive = False)
|
| 201 |
+
speed_box = gr.Textbox(value = speed, label = "Speed", interactive = False)
|
| 202 |
btn.click(fn = stats_gen_2, outputs = [ac_box, hp_box, speed_box])
|
| 203 |
rand.click(fn = stats_gen_2, outputs = [ac_box, hp_box, speed_box])
|
| 204 |
|
|
|
|
| 207 |
|
| 208 |
with gr.Accordion("Bard Stats", open = False):
|
| 209 |
with gr.Row():
|
| 210 |
+
dcs = gr.Textbox(value = charm, label = "DC to Charm")
|
| 211 |
prefs = gr.Textbox(value = pref_traits, label = "Advantage if:")
|
| 212 |
icks = gr.Textbox(value = ick_traits, label = "Disdvantage if:")
|
| 213 |
btn.click(fn = bard_stats_gen, outputs = [dcs, prefs, icks])
|
| 214 |
rand.click(fn = bard_stats_gen, outputs = [dcs, prefs, icks])
|
| 215 |
|
| 216 |
+
with gr.Row():
|
| 217 |
+
download_btn = gr.Button("Download CSV")
|
| 218 |
+
download_file = gr.File(label="Generated CSV", interactive=False)
|
| 219 |
+
download_btn.click(fn=get_csv_file, outputs=download_file)
|
| 220 |
|
| 221 |
|
|
|
|
|
|
|
| 222 |
demo.launch(theme = gr.themes.Monochrome())
|