Upload 4 files
Browse files- examples/YOUR_API_CALLER_GOES_HERE.py +84 -0
- examples/example_generation.py +56 -0
- examples/poet_list.json +1 -0
- examples/title_list.json +1 -0
examples/YOUR_API_CALLER_GOES_HERE.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import tiktoken
|
3 |
+
|
4 |
+
import re
|
5 |
+
|
6 |
+
def token_length(text):
|
7 |
+
"""
|
8 |
+
Count the number of words in a given string and multiply to approximate tokens
|
9 |
+
|
10 |
+
Args:
|
11 |
+
text (str): The input string.
|
12 |
+
|
13 |
+
Returns:
|
14 |
+
int: The approximate number of tokens in the input string.
|
15 |
+
"""
|
16 |
+
# Remove leading/trailing whitespace from the string
|
17 |
+
text = text.strip()
|
18 |
+
# Split the string by whitespace characters to get a list of words
|
19 |
+
words = re.split(r'\s+', text)
|
20 |
+
# Count the number of non-empty words
|
21 |
+
word_count = sum(1 for word in words if word)
|
22 |
+
return int(word_count*1.25)
|
23 |
+
|
24 |
+
def LLM_COMPLETION(prompt, context_size=None, preset='Divine Intellect', repetition_penalty=1.17, temperature = 1.31, top_p = 0.14, api_port=5000, guidance_scale = 1, negative_prompt = ""):
|
25 |
+
# Calls OPENAI-style completion endpoint, written for text-generation-webui
|
26 |
+
|
27 |
+
# For local streaming, the textgen websockets are hosted without ssl - http://
|
28 |
+
HOST = 'localhost:'+str(api_port)
|
29 |
+
URI = f'http://{HOST}/v1/completions'
|
30 |
+
|
31 |
+
if context_size==None:
|
32 |
+
context_max=4096
|
33 |
+
else:
|
34 |
+
context_max=context_size
|
35 |
+
|
36 |
+
max_new_tokens=context_max-token_length(prompt)
|
37 |
+
|
38 |
+
request = {
|
39 |
+
'prompt': prompt,
|
40 |
+
'max_tokens': max_new_tokens,
|
41 |
+
'auto_max_new_tokens': False,
|
42 |
+
'max_tokens_second': 0,
|
43 |
+
# Generation params. If 'preset' is set to different than 'None', the values
|
44 |
+
# in presets/preset-name.yaml are used instead of the individual numbers.
|
45 |
+
'preset': preset,
|
46 |
+
'do_sample': True,
|
47 |
+
'temperature': temperature,
|
48 |
+
'top_p': top_p,
|
49 |
+
'typical_p': 1,
|
50 |
+
'epsilon_cutoff': 0, # In units of 1e-4
|
51 |
+
'eta_cutoff': 0, # In units of 1e-4
|
52 |
+
'tfs': 1,
|
53 |
+
'top_a': 0,
|
54 |
+
'repetition_penalty': repetition_penalty,
|
55 |
+
'repetition_penalty_range': 0,
|
56 |
+
'top_k': 49,
|
57 |
+
'min_length': 0,
|
58 |
+
'no_repeat_ngram_size': 0,
|
59 |
+
'num_beams': 1,
|
60 |
+
'penalty_alpha': 0,
|
61 |
+
'length_penalty': 1,
|
62 |
+
'early_stopping': False,
|
63 |
+
'mirostat_mode': 0,
|
64 |
+
'mirostat_tau': 5,
|
65 |
+
'mirostat_eta': 0.1,
|
66 |
+
'grammar_string': '',
|
67 |
+
'guidance_scale': guidance_scale,
|
68 |
+
'negative_prompt': negative_prompt,
|
69 |
+
'seed': -1,
|
70 |
+
'add_bos_token': True,
|
71 |
+
'truncation_length': context_max,
|
72 |
+
'ban_eos_token': False,
|
73 |
+
'custom_token_bans': '',
|
74 |
+
'skip_special_tokens': True,
|
75 |
+
'stop': ["</s>"]
|
76 |
+
}
|
77 |
+
|
78 |
+
response = requests.post(URI, json=request)
|
79 |
+
|
80 |
+
if response.status_code == 200:
|
81 |
+
result = response.json()['choices'][0]['text']
|
82 |
+
return result
|
83 |
+
else:
|
84 |
+
print(response.status_code)
|
examples/example_generation.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import sys
|
2 |
+
import json
|
3 |
+
import random
|
4 |
+
from YOUR_API_CALLER_GOES_HERE import LLM_COMPLETION
|
5 |
+
|
6 |
+
def get_response(title, poet):
|
7 |
+
"""
|
8 |
+
Creates poem-generation prompt from title and poet, and puts it into
|
9 |
+
the expected penchant format
|
10 |
+
"""
|
11 |
+
|
12 |
+
penchant_prompt = f'Write an artistic poem. The title should be "{title}". Use vivid imagery and creative metaphors throughout your work. Draw inspiration from the works of {poet}. Pay close attention to the sounds that words make when read aloud, and use these sounds to create rhythm and musicality within your piece.'
|
13 |
+
|
14 |
+
penchant_template = f"<|im_start|>system\nYou are Dolphin, a helpful AI assistant.<|im_end|>\n<|im_start|>user\n{penchant_prompt}<|im_end|>\n<|im_start|>assistant\n"
|
15 |
+
|
16 |
+
poem = LLM_COMPLETION(penchant_template)
|
17 |
+
return poem
|
18 |
+
|
19 |
+
|
20 |
+
def process_files(titles_file, poets_file, output_file):
|
21 |
+
try:
|
22 |
+
# Read "titles" and "poets" from input files
|
23 |
+
with open(titles_file, 'r') as f:
|
24 |
+
titles_data = json.load(f)
|
25 |
+
titles = titles_data.get("titles", [])
|
26 |
+
|
27 |
+
with open(poets_file, 'r') as f:
|
28 |
+
poets_data = json.load(f)
|
29 |
+
poets = poets_data.get("poets", [])
|
30 |
+
|
31 |
+
# Write responses to output JSONL file
|
32 |
+
with open(output_file, 'w') as f:
|
33 |
+
poems_to_write = 100
|
34 |
+
count = 0
|
35 |
+
while count < poems_to_write:
|
36 |
+
title = random.choice(titles)
|
37 |
+
poet = random.choice(poets)
|
38 |
+
title = title.strip()
|
39 |
+
poet = poet.strip()
|
40 |
+
response = get_response(title, poet)
|
41 |
+
output_data = {"poet": poet, "title": title, "poem": response}
|
42 |
+
f.write(json.dumps(output_data) + "\n")
|
43 |
+
count += 1
|
44 |
+
except FileNotFoundError as e:
|
45 |
+
print(f"Error: {e.filename} not found.")
|
46 |
+
except Exception as e:
|
47 |
+
print(f"An error occurred: {e}")
|
48 |
+
|
49 |
+
if __name__ == "__main__":
|
50 |
+
if len(sys.argv) != 4:
|
51 |
+
print("Usage: python script.py titles_file.json poets_file.json output_file.jsonl")
|
52 |
+
else:
|
53 |
+
titles_file = sys.argv[1]
|
54 |
+
poets_file = sys.argv[2]
|
55 |
+
output_file = sys.argv[3]
|
56 |
+
process_files(titles_file, poets_file, output_file)
|
examples/poet_list.json
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{"poets": ["Walt Whitman \n", "Dylan Thomas \n", "Elizabeth Barrett Browning \n", "William Carlos Williams \n", "Samuel Taylor Coleridge \n", "Percy Bysshe Shelley \n", "Emily Dickinson \n", "Robert Browning \n", "William Butler Yeats \n", "Edna St. Vincent Millay \n", "William Blake \n", "William Wordsworth \n", "John Clare \n", "Robert Frost \n", "John Greenleaf Whittier \n", "John Keats \n", "Christina Georgina Rossetti \n", "George Gordon Byron \n", "John Milton\n", "John Donne \n", "Alfred Lord Tennyson \n"]}
|
examples/title_list.json
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{"titles": ["Mother Nature's First Love\n", "The Many Sides of My City\n", "She Gave Me Sour Grapes\n", "The Moonlight Poem\n", "The Bard's Mask\n", "Do you ever know what to say?\n", "Drawing the Line\n", "The Flowers and the Yew\n", "Poem From The Inside Out\n", "Love is a Sin\n", "Spitting On Butterflies\n", "You Can't Take Me Back\n", "How To Touch A Color\n", "Pale Green Doors\n", "The Sixteen Little Hats of Miss Muffet\n", "The Strolling Poets\n", "The Far Corners of the Summer\n", "Bones of the Forest\n", "The Art of Flirting\n", "Rain, Rain, Stand\n", "This Is Not Life\n", "The Fragment\n", "Dancing, with a Smile\n", "The Poet's Orchard\n", "Dinosaur Poem\n", "Lament\n", "Lyrical Mistakes\n", "I, the Brat\n", "The Sleepy Poet's Musings\n", "The Fog of Memory\n", "The Absence of the Ocean\n", "The story of my life as a flower\n", "A Discourse in White\n", "I Carry Your Picture In My Soul\n", "The Fall of Constantinople\n", "Angel in the Snow\n", "The Love That Is Always, Always Gone\n", "Dire\n", "Just like any other working day\n", "A Dream Song\n", "I Know I Shouldn't\n", "A Poem about Crows\n", "One Day in Hell\n", "She Feels What She Cannot Say\n", "The Mermaid and the Shark\n", "The Face-Shaped Hole\n", "The White Rickety Bridge\n", "The Rain that Fell on All the Children\n", "the moon poem\n", "The Princess and the Knight\n", "A love song for the steely heart of winter\n", "Poisoned Flowers\n", "Love in Flight\n", "The Day Life Broke the World\n", "What a Little Rain Must Have Done\n", "The Embers Had Burned Down Low\n", "Star Bright and Moon Marie\n", "A Complicated Descendant\n", "One of One\n", "Unusual Violin\n", "The Almond Tree\n", "The Monsters in My Mind\n", "Vultures Gaze into the Distance\n", "In Purgatory I Hid from You\n", "On a Boat\n", "Harsh Promises\n", "Anastasia: or, the\n", "The Novel in My Father's Hands\n", "The Cat and the Mistress\n", "The Lady and the Owl\n", "The Poetic Game\n", "You Can't Bomb the Sky\n", "Silent Night, Holy Night\n", "The Smallest Spider in the World\n", "Through the Window\n", "The Well-Tempered Heart\n", "The Poet and the Snake\n", "The Night of Wine\n", "Hearts in the Field\n", "Confessions of an Innocent Man\n", "Tree of Life\n", "I Remain\n", "All the Ways I Miss You\n", "A Bear in a Box\n", "The Souls of Fallen Stars\n", "Insatiable\n", "The Untouched Children\n", "The Steel Blue Fifties\n", "A Formal Introduction to the All-Knowing and All-Seeing Eye of God\n", "On the Edge of the Woods\n", "To Love a Stranger\n", "Diet of the Roving Reader\n", "Sick of all these ideas\n", "The City of Clockwork and Crystal\n", "Ode to My Ivories\n", "The Coolest Kind of Hate\n", "I Know You're My Lover\n", "Seeing Things\n", "Love Stomped On Out Of Town\n", "The Inevitable Accomplishment of a Wondrous Ambition\n", "The Rhapsody of the Ravenous\n", "No More Than Spiders\n", "Cheerio, Cheerio, we're all going to die\n", "Wayward and Wandering\n", "The Unspoken Word\n", "Lavender\n", "Where the Moon is Full\n", "I am the one you always want\n", "In the Shell\n", "The Missing Poem \n", "Little Drops of Love\n", "The Girl Who Waited\n", "Ode to a Brainless Idiot\n", "Snow White\n", "The End of the River\n", "Mad, Madder, Maddest\n", "Black Frost\n", "The Plums\n", "Narcissus and the Tortoise\n", "The Death of Juliet\n", "The Lord's Prayer\n", "An Old Man\u2019s Honest Confessions to his Soul\n", "Goethe's Sorrow\n", "An Example of the Loss of a Friend\n", "\n", "If You Want To be a Writer\n", "The Hero of the Streets\n", "The Sound of Silhouettes\n", "The Sheltering Stream\n", "Margaret\u2019s Song\n", "Pomegranate Fall\n", "The War that Never Ends\n", "The Birth of a Poem\n", "A love letter to a dead star\n", "I Lost My Way in the Woods\n", "The Poem With No Word\n", "In the City of Dreamers\n", "The Weight of It\n"]}
|