-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdeepproverbs.py
156 lines (129 loc) · 4.22 KB
/
deepproverbs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
from typing import List, Optional
import json
import os
import sys
if "linux" in sys.platform:
os.environ["LC_AL"] = "C.UTF-8"
os.environ["LANG"] = "C.UTF-8"
else:
os.environ["LC_AL"] = "en_US.utf-8"
os.environ["LANG"] = "en_US.utf-8"
import click
import gpt_2_simple as gpt2
import numpy as np
import tweepy
def generate_text(
checkpoint_dir: str,
length: int,
temperature: float,
destination_path: Optional[str],
prefix: Optional[str],
return_as_list: bool = False,
) -> List[str]:
sess = gpt2.start_tf_sess()
gpt2.load_gpt2(sess, checkpoint_dir=checkpoint_dir)
text = gpt2.generate(
sess,
checkpoint_dir=checkpoint_dir,
length=length,
temperature=temperature,
destination_path=destination_path,
prefix=prefix,
return_as_list=return_as_list,
)
return text
def tweet(
checkpoint_dir: str,
twitter_credential_path: str,
length: int = 1024,
temperature: float = 0.8,
prefix: Optional[str] = None,
delimiter: str = "\n==========\n",
):
print(os.getcwd())
# Parse the credentials for the twitter bot
with open(twitter_credential_path, "r") as json_file:
twitter_creds = json.load(json_file)
# Set the credentials based on the credentials file
CONSUMER_KEY = twitter_creds["consumer_key"]
CONSUMER_SECRET = twitter_creds["consumer_secret"]
ACCESS_KEY = twitter_creds["access_key"]
ACCESS_SECRET = twitter_creds["access_secret"]
# Authenticate with the Twitter API
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
# Generate some text
generated_text = generate_text(
checkpoint_dir, length, temperature, None, prefix, return_as_list=True
)
split_text = generated_text[0].split(delimiter)
# Filter out all examples which are longer than twitter's 280 character limit
valid_text = [x for x in split_text if len(x) <= 280]
# TWEET!!!
current_tweet = np.random.choice(valid_text, 1)
api.update_status(current_tweet[0])
print(current_tweet)
@click.group()
def main():
pass
@main.command("finetune")
@click.option(
"--model-name", type=str, default="124M", help="Can be 117M, 124M, or 355M"
)
@click.option("--text-path", type=str, default="./data/mtg_combined.txt")
@click.option("--checkpoint-dir", type=str, default="checkpoint")
@click.option("--num-steps", type=int, default=3000)
@click.option("--sample-length", type=int, default=1023)
@click.option("--save-every", type=int, default=None)
def finetune(
model_name: str,
text_path: str,
checkpoint_dir: str,
num_steps: int,
sample_length: int,
save_every: Optional[int],
) -> None:
# Download the model if it is not present
if not os.path.isdir(os.path.join("models", model_name)):
print(f"Downloading {model_name} model...")
gpt2.download_gpt2(model_name=model_name)
sess = gpt2.start_tf_sess()
if save_every is None:
save_every = int(num_steps / 4)
gpt2.finetune(
sess,
text_path,
model_name=model_name,
steps=num_steps,
sample_length=sample_length,
save_every=save_every,
checkpoint_dir=checkpoint_dir,
) # steps is max number of training steps
gpt2.generate(sess, checkpoint_dir=checkpoint_dir)
@main.command("generate")
@click.option("--checkpoint-dir", type=str, default="checkpoint")
@click.option("--length", type=int, default=1023)
@click.option(
"--temperature",
type=float,
default=1.0,
help="lower more consistent, higher more fun",
)
@click.option("--destination-path", type=str, default=None)
@click.option("--prefix", type=str, default=None)
def generate(
checkpoint_dir: str,
length: int,
temperature: float,
destination_path: str,
prefix: Optional[str],
) -> None:
generate_text(checkpoint_dir, length, temperature, destination_path, prefix)
@main.command("post-tweet")
@click.argument("checkpoint-dir", type=str)
@click.argument("twitter-credential-path", type=str)
def post_tweet(checkpoint_dir: str, twitter_credential_path: str):
tweet(checkpoint_dir, twitter_credential_path)
if __name__ == "__main__":
main()