Skip to content

Instantly share code, notes, and snippets.

@rahulvigneswaran
Created March 20, 2023 04:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rahulvigneswaran/eb2ac832c6646f1be37e6c373c569e8c to your computer and use it in GitHub Desktop.
Save rahulvigneswaran/eb2ac832c6646f1be37e6c373c569e8c to your computer and use it in GitHub Desktop.
Routine to generate sentence based on a word. Uses GPT-3. Requires OpenAI's API.
# ========================================================================================================================
import openai
openai.api_key =
# ========================================================================================================================
# >>> len(a[31:60]), len(a[60:])
# (29, 40)
import os
from tqdm import tqdm, trange
from data_utils import fine_labels
count = 1000
fine_labels = fine_labels()
fine_labels = fine_labels[34:70]
def generate(label_name):
response = openai.Completion.create(
engine="text-davinci-003",
prompt=f"Generate many sentences that describe a picture containing a {label_name}. Make sure every sentence contains the word {label_name}.",
temperature=0.71,
max_tokens=4000,
top_p=1,
frequency_penalty=0.36,
presence_penalty=0.75
)
return response.get("choices")[0]['text']
for i in trange(len(fine_labels)):
label = fine_labels[i]
print(f"Generating {label} - {i}")
gen_count = 0
while(gen_count<count):
a = generate(label)
b = a.split("\n")
b = [f"{i.split('.')[1][1:]}." for i in b[2:]]
if gen_count + len(b) > count:
extra = count - gen_count
b = b[:extra]
gen_count += len(b)
b = "\n".join(b) + "\n"
with open(os.path.join(f"{label}.txt"), "a") as f:
f.write(b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment