rf5860 commited on
Commit
60675e2
1 Parent(s): bd8cb64

Add backstory generator

Browse files
Files changed (1) hide show
  1. app.py +70 -17
app.py CHANGED
@@ -1,14 +1,27 @@
 
 
1
  import gradio as gr
2
  import random
3
 
 
 
 
 
 
4
  def generate_characters():
5
- class_list = ["Barbarian","Bard","Cleric","Druid","Fighter","Monk","Paladin","Ranger","Rogue","Sorcerer","Warlock","Wizard"]
6
- sub_class_list = ["Barbarian - Beserker","Barbarian - Wildheart","Barbarian - Wild Magic","Bard - College of Lore","Bard - College of Valour","Bard - College of Swords","Cleric - Life Domain","Cleric - Light Domain","Cleric - Trickery Domain","Cleric - Knowledge Domain","Cleric - Nature Domain","Cleric - Tempest Domain","Cleric - War Domain","Druid - Circle of the moon","Druid - Circle of the Land","Druid - Circle of the Spores","Fighter - Battle Master","Fighter - Eldritch Knight","Fighter - Champion","Monk - Way of the open hand","Monk - Way of shadow","Monk - Way of the four elements","Paladin - Oath of Devotion","Paladin - Oath of the ancients","Paladin - Oath of Vengence","Paladin - Oathbreaker","Ranger - Beat Master","Ranger - Hunter","Ranger - Gloom Stalker","Rogue - Thief","Rogue - Arcane Trickster","Rogue - Assassin","Sorcerer - Draconic Bloodline","Sorcerer - Wild Magic","Sorcerer - Storm Sorcery","Warlock - The Fiend","Warlock - the great old one","Warlock - Archfey","Wizard - Abjuration","Wizard - Conjuration","Wizard - Divination","Wizard - Enchantment","Wizard - Evocation","Wizard - Necromancer","Wizard - Illusion","Wizard - Transmutation"]
7
- race_list = ["Dragonborn","Drow","Dwarf","Elf","Githyanki","Gnome","Half-Elf","Half-Orc","Halfling","Human","Tiefling"]
8
- sub_race_list = ["Black Dragonborn","Blue Dragonborn","Brass Dragonborn","Bronze Dragonborn","Copper Dragonborn","Gold Dragonborn","Green Dragonborn","Red Dragonborn","Silver Dragonborn","White Dragonborn","Lolth-Sworn Drow","Seldarine Drow","Gold Dwarf","Shield Dwarf","Duegar (Dwarf)","High Elf - Elf","Wood Elf - Elf","Githyanki","Deep Gnome","Forest Gnome","Rock Gnome","High Half-Elf","Wood Half-Elf","Drow Half-Elf","Half-Orc","Lightfoot Halfling","Strongheart Halfling","Human","Asmodeus Tiefling","Mephistopheles Tiefling","Zariel Tiefling"]
9
- alignment_list = ["Chaotic Good","Lawful Good","Neutral Good","Chaotic Evil","Lawful Evil","Neutral Evil","True Chaotic","True Neutral"]
10
- background_list = ["Acolyte","Charlatan","Criminal","Entertainer","Folk Hero","Guild Artisan","Noble","Hermit","Outlander","Sage","Soldier"]
11
-
 
 
 
 
 
 
12
  def generate_character():
13
  character = {
14
  'Class': random.choice(class_list),
@@ -22,7 +35,8 @@ def generate_characters():
22
 
23
  def generate_multiclass_character():
24
  character = generate_character()
25
- second_class = random.choice([cls for cls in class_list if cls != character['Class']])
 
26
  levels_class1 = random.randint(1, 11)
27
  levels_class2 = 12 - levels_class1
28
  character['Class'] += f' ({levels_class1} levels), {second_class} ({levels_class2} levels)'
@@ -31,17 +45,56 @@ def generate_characters():
31
  characters = [generate_character() for _ in range(2)]
32
  characters.append(generate_multiclass_character())
33
 
34
- output = ""
35
- for idx, char in enumerate(characters, 1):
36
- output += f"### Character {idx}:\n"
37
- for key, value in char.items():
38
  output += f"- **{key}:** {value}\n"
39
  output += "\n"
 
 
 
40
 
 
 
 
 
 
 
 
41
  return output
42
 
43
- iface = gr.Interface(fn=generate_characters,
44
- title="D&D Character Generator",
45
- description="Generates 3 random D&D characters.",
46
- inputs=[], outputs="markdown")
47
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from llama_cpp import Llama
2
+ from huggingface_hub import hf_hub_download
3
  import gradio as gr
4
  import random
5
 
6
+ repo_id = "TheBloke/Mistral-7B-Instruct-v0.1-GGUF"
7
+ filename = "mistral-7b-instruct-v0.1.Q4_K_M.gguf"
8
+ model_file_path = hf_hub_download(repo_id=repo_id, filename=filename)
9
+
10
+
11
  def generate_characters():
12
+ class_list = ["Barbarian", "Bard", "Cleric", "Druid", "Fighter",
13
+ "Monk", "Paladin", "Ranger", "Rogue", "Sorcerer", "Warlock", "Wizard"]
14
+ sub_class_list = ["Barbarian - Beserker", "Barbarian - Wildheart", "Barbarian - Wild Magic", "Bard - College of Lore", "Bard - College of Valour", "Bard - College of Swords", "Cleric - Life Domain", "Cleric - Light Domain", "Cleric - Trickery Domain", "Cleric - Knowledge Domain", "Cleric - Nature Domain", "Cleric - Tempest Domain", "Cleric - War Domain", "Druid - Circle of the moon", "Druid - Circle of the Land", "Druid - Circle of the Spores", "Fighter - Battle Master", "Fighter - Eldritch Knight", "Fighter - Champion", "Monk - Way of the open hand", "Monk - Way of shadow", "Monk - Way of the four elements",
15
+ "Paladin - Oath of Devotion", "Paladin - Oath of the ancients", "Paladin - Oath of Vengence", "Paladin - Oathbreaker", "Ranger - Beat Master", "Ranger - Hunter", "Ranger - Gloom Stalker", "Rogue - Thief", "Rogue - Arcane Trickster", "Rogue - Assassin", "Sorcerer - Draconic Bloodline", "Sorcerer - Wild Magic", "Sorcerer - Storm Sorcery", "Warlock - The Fiend", "Warlock - the great old one", "Warlock - Archfey", "Wizard - Abjuration", "Wizard - Conjuration", "Wizard - Divination", "Wizard - Enchantment", "Wizard - Evocation", "Wizard - Necromancer", "Wizard - Illusion", "Wizard - Transmutation"]
16
+ race_list = ["Dragonborn", "Drow", "Dwarf", "Elf", "Githyanki",
17
+ "Gnome", "Half-Elf", "Half-Orc", "Halfling", "Human", "Tiefling"]
18
+ sub_race_list = ["Black Dragonborn", "Blue Dragonborn", "Brass Dragonborn", "Bronze Dragonborn", "Copper Dragonborn", "Gold Dragonborn", "Green Dragonborn", "Red Dragonborn", "Silver Dragonborn", "White Dragonborn", "Lolth-Sworn Drow", "Seldarine Drow", "Gold Dwarf", "Shield Dwarf",
19
+ "Duegar (Dwarf)", "High Elf - Elf", "Wood Elf - Elf", "Githyanki", "Deep Gnome", "Forest Gnome", "Rock Gnome", "High Half-Elf", "Wood Half-Elf", "Drow Half-Elf", "Half-Orc", "Lightfoot Halfling", "Strongheart Halfling", "Human", "Asmodeus Tiefling", "Mephistopheles Tiefling", "Zariel Tiefling"]
20
+ alignment_list = ["Chaotic Good", "Lawful Good", "Neutral Good",
21
+ "Chaotic Evil", "Lawful Evil", "Neutral Evil", "True Chaotic", "True Neutral"]
22
+ background_list = ["Acolyte", "Charlatan", "Criminal", "Entertainer",
23
+ "Folk Hero", "Guild Artisan", "Noble", "Hermit", "Outlander", "Sage", "Soldier"]
24
+
25
  def generate_character():
26
  character = {
27
  'Class': random.choice(class_list),
 
35
 
36
  def generate_multiclass_character():
37
  character = generate_character()
38
+ second_class = random.choice(
39
+ [cls for cls in class_list if cls != character['Class']])
40
  levels_class1 = random.randint(1, 11)
41
  levels_class2 = 12 - levels_class1
42
  character['Class'] += f' ({levels_class1} levels), {second_class} ({levels_class2} levels)'
 
45
  characters = [generate_character() for _ in range(2)]
46
  characters.append(generate_multiclass_character())
47
 
48
+ def create_character_output(character):
49
+ output = ""
50
+ for key, value in character.items():
 
51
  output += f"- **{key}:** {value}\n"
52
  output += "\n"
53
+ backstory = generate_backstory(character)
54
+ # Remove everything up to and including `[/INST]\n`
55
+ output += backstory[backstory.find("[/INST]\n") + 8:]
56
 
57
+ return output
58
+
59
+ output = ""
60
+ for idx, char in enumerate(characters, 1):
61
+ output += f"### Character {idx}:\n"
62
+ output += create_character_output(char)
63
+
64
  return output
65
 
66
+
67
+ def generate_text_from_prompt(user_prompt,
68
+ max_tokens=4096,
69
+ temperature=1.0,
70
+ top_p=0.8,
71
+ echo=True,
72
+ stop=["<s>", "[/INST]", "Q"]):
73
+ mistral_model = Llama(model_path=model_file_path, n_ctx=4096)
74
+
75
+ # Define the parameters
76
+ model_output = mistral_model(
77
+ f"<s>[INST] {user_prompt} [/INST]",
78
+ max_tokens=max_tokens,
79
+ temperature=temperature,
80
+ top_p=top_p,
81
+ echo=echo,
82
+ stop=stop
83
+ )
84
+
85
+ return model_output
86
+
87
+
88
+ def generate_backstory(character):
89
+ return generate_text_from_prompt(
90
+ f"Please provide a name and creative, well-written backstory "
91
+ f" for the following D&D Character: \n```{character}```\n"
92
+ )
93
+
94
+
95
+ if __name__ == "__main__":
96
+ iface = gr.Interface(fn=generate_characters,
97
+ title="D&D Character Generator",
98
+ description="Generates 3 random D&D characters.",
99
+ inputs=[], outputs="markdown")
100
+ iface.launch()