Synced repo using 'sync_with_huggingface' Github Action
Browse files- Dockerfile +3 -0
- LICENSE +21 -0
- main.py +127 -68
- requirements.txt +4 -5
Dockerfile
CHANGED
@@ -27,6 +27,9 @@ RUN --mount=type=secret,id=access_token_secret,mode=0444,required=true \
|
|
27 |
RUN --mount=type=secret,id=bearer_token,mode=0444,required=true \
|
28 |
cat /run/secrets/bearer_token > /test
|
29 |
|
|
|
|
|
|
|
30 |
# Set up a new user named "user" with user ID 1000
|
31 |
RUN useradd -m -u 1000 user
|
32 |
|
|
|
27 |
RUN --mount=type=secret,id=bearer_token,mode=0444,required=true \
|
28 |
cat /run/secrets/bearer_token > /test
|
29 |
|
30 |
+
RUN --mount=type=secret,id=hf_token,mode=0444,required=true \
|
31 |
+
cat /run/secrets/hf_token > /test
|
32 |
+
|
33 |
# Set up a new user named "user" with user ID 1000
|
34 |
RUN useradd -m -u 1000 user
|
35 |
|
LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
MIT License
|
2 |
+
|
3 |
+
Copyright (c) 2023 Nathan Raw
|
4 |
+
|
5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6 |
+
of this software and associated documentation files (the "Software"), to deal
|
7 |
+
in the Software without restriction, including without limitation the rights
|
8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9 |
+
copies of the Software, and to permit persons to whom the Software is
|
10 |
+
furnished to do so, subject to the following conditions:
|
11 |
+
|
12 |
+
The above copyright notice and this permission notice shall be included in all
|
13 |
+
copies or substantial portions of the Software.
|
14 |
+
|
15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21 |
+
SOFTWARE.
|
main.py
CHANGED
@@ -1,25 +1,40 @@
|
|
1 |
import logging
|
2 |
import os
|
|
|
3 |
import time
|
4 |
-
|
5 |
|
6 |
import openai
|
|
|
|
|
7 |
import tweepy
|
|
|
8 |
|
9 |
logger = logging.getLogger()
|
10 |
logging.basicConfig(level=logging.INFO)
|
11 |
logger.setLevel(logging.INFO)
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
openai_key = os.environ.get("openai_api_key")
|
14 |
consumer_key = os.environ.get("consumer_key")
|
15 |
consumer_secret_key = os.environ.get("consumer_secret_key")
|
16 |
access_token = os.environ.get("access_token")
|
17 |
access_token_secret = os.environ.get("access_token_secret")
|
18 |
bearer_token = os.environ.get("bearer_token")
|
19 |
-
|
20 |
-
home_path = os.environ.get("HOME")
|
21 |
-
last_id_filepath = home_path + "/app/last_id.txt"
|
22 |
-
|
23 |
|
24 |
client = tweepy.Client(
|
25 |
bearer_token=bearer_token,
|
@@ -31,25 +46,6 @@ client = tweepy.Client(
|
|
31 |
)
|
32 |
|
33 |
|
34 |
-
def get_last_tweet(file_path: str):
|
35 |
-
path = Path(file_path)
|
36 |
-
if not path.exists():
|
37 |
-
mentions = client.get_users_mentions(
|
38 |
-
id=1612106815096999936,
|
39 |
-
expansions=["author_id", "in_reply_to_user_id", "referenced_tweets.id"],
|
40 |
-
)
|
41 |
-
since_id = mentions.data[0].id
|
42 |
-
put_last_tweet(file_path, since_id)
|
43 |
-
return since_id
|
44 |
-
return int(path.read_text().strip())
|
45 |
-
|
46 |
-
|
47 |
-
def put_last_tweet(file_path: str, id: str):
|
48 |
-
Path(file_path).write_text(str(id))
|
49 |
-
logger.info("Updated the file with the latest tweet Id")
|
50 |
-
return
|
51 |
-
|
52 |
-
|
53 |
def response(text):
|
54 |
openai.api_key = openai_key
|
55 |
response = openai.Completion.create(
|
@@ -78,71 +74,134 @@ def toast(text):
|
|
78 |
|
79 |
|
80 |
def reply_to_mentions():
|
81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
mentions = client.get_users_mentions(
|
83 |
-
id=
|
84 |
-
since_id=since_id,
|
85 |
expansions=["author_id", "in_reply_to_user_id", "referenced_tweets.id"],
|
|
|
|
|
86 |
)
|
|
|
|
|
87 |
if mentions.data is None:
|
|
|
88 |
logger.info("No new mentions found")
|
89 |
return
|
90 |
|
|
|
|
|
|
|
91 |
for mention in reversed(mentions.data):
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
if mention.referenced_tweets is None:
|
97 |
-
logger.info(f"Skipping {mention.id} as it is not a reply")
|
98 |
-
tweet_to_roast_id = mention.referenced_tweets[0].id
|
99 |
-
tweet_to_roast = client.get_tweet(id=tweet_to_roast_id)
|
100 |
-
text_to_roast = tweet_to_roast.data.text
|
101 |
-
|
102 |
-
text_out = None
|
103 |
-
text_in = mention.text.lower()
|
104 |
-
text_in = text_in.replace('@roastortoastgpt', '')
|
105 |
-
text_in = text_in.replace('roastortoastgpt','')
|
106 |
-
|
107 |
-
logger.info(f"In Text: {text_in}")
|
108 |
-
if "roast" in text_in:
|
109 |
-
logger.info(f"Roasting!")
|
110 |
-
text_out = roast(text_to_roast)
|
111 |
-
elif "toast" in text_in:
|
112 |
-
logger.info("Toasting!")
|
113 |
-
text_out = toast(text_to_roast)
|
114 |
-
|
115 |
-
if text_out is None:
|
116 |
-
continue
|
117 |
-
|
118 |
-
logger.info(f"Out Text: {text_out}")
|
119 |
-
except Exception as e:
|
120 |
-
logger.error(e)
|
121 |
continue
|
122 |
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
continue
|
129 |
|
130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
|
132 |
|
133 |
def main():
|
|
|
|
|
134 |
while True:
|
135 |
try:
|
|
|
|
|
136 |
reply_to_mentions()
|
137 |
except Exception as e:
|
138 |
logger.error(e)
|
139 |
-
# Print more helpful error messages with line number
|
140 |
-
import traceback
|
141 |
-
|
142 |
traceback.print_exc()
|
143 |
|
144 |
-
|
|
|
145 |
|
146 |
|
147 |
-
|
148 |
-
|
|
|
1 |
import logging
|
2 |
import os
|
3 |
+
import re
|
4 |
import time
|
5 |
+
import traceback
|
6 |
|
7 |
import openai
|
8 |
+
import pandas as pd
|
9 |
+
import requests
|
10 |
import tweepy
|
11 |
+
from huggingface_hub import upload_file
|
12 |
|
13 |
logger = logging.getLogger()
|
14 |
logging.basicConfig(level=logging.INFO)
|
15 |
logger.setLevel(logging.INFO)
|
16 |
|
17 |
+
bot_user_id = 1612106815096999936
|
18 |
+
bot_user_name = "RoastOrToastGPT"
|
19 |
+
|
20 |
+
persistent_storage_repo_id = "team6/roast-history"
|
21 |
+
persistent_storage_file_name = "history.csv"
|
22 |
+
persistent_storage_file_url = (
|
23 |
+
f"https://huggingface.co/datasets/{persistent_storage_repo_id}/resolve/main/{persistent_storage_file_name}"
|
24 |
+
)
|
25 |
+
|
26 |
+
# will be used to remove the @roastortoastgpt from the tweet text (case insensitive)
|
27 |
+
pattern_at_mention = re.compile(re.escape("@roastortoastgpt"), re.IGNORECASE)
|
28 |
+
|
29 |
+
print(persistent_storage_file_url)
|
30 |
+
|
31 |
openai_key = os.environ.get("openai_api_key")
|
32 |
consumer_key = os.environ.get("consumer_key")
|
33 |
consumer_secret_key = os.environ.get("consumer_secret_key")
|
34 |
access_token = os.environ.get("access_token")
|
35 |
access_token_secret = os.environ.get("access_token_secret")
|
36 |
bearer_token = os.environ.get("bearer_token")
|
37 |
+
hf_token = os.environ.get("hf_token")
|
|
|
|
|
|
|
38 |
|
39 |
client = tweepy.Client(
|
40 |
bearer_token=bearer_token,
|
|
|
46 |
)
|
47 |
|
48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
def response(text):
|
50 |
openai.api_key = openai_key
|
51 |
response = openai.Completion.create(
|
|
|
74 |
|
75 |
|
76 |
def reply_to_mentions():
|
77 |
+
df = pd.read_csv(persistent_storage_file_url)
|
78 |
+
last_tweet_id = df.iloc[-1]["id"]
|
79 |
+
|
80 |
+
# List of unique conversation ids that we've already responded to.
|
81 |
+
# This is to prevent us from responding to the same conversation twice.
|
82 |
+
all_convo_ids = df["conversation_id"].unique().tolist()
|
83 |
+
|
84 |
+
# get the mentions. These are both direct mentions and replies to our tweets
|
85 |
mentions = client.get_users_mentions(
|
86 |
+
id=bot_user_id,
|
|
|
87 |
expansions=["author_id", "in_reply_to_user_id", "referenced_tweets.id"],
|
88 |
+
tweet_fields=["conversation_id"],
|
89 |
+
since_id=last_tweet_id,
|
90 |
)
|
91 |
+
|
92 |
+
# if there are no new mentions, return
|
93 |
if mentions.data is None:
|
94 |
+
# log it
|
95 |
logger.info("No new mentions found")
|
96 |
return
|
97 |
|
98 |
+
data_to_add = {"id": [], "conversation_id": []}
|
99 |
+
# otherwise, iterate through the mentions and respond to them
|
100 |
+
# we iterate through the mentions in reverse order so that we respond to the oldest mentions first
|
101 |
for mention in reversed(mentions.data):
|
102 |
+
|
103 |
+
if mention.author_id == bot_user_id:
|
104 |
+
# don't respond to our own tweets
|
105 |
+
logger.info(f"Skipping {mention.id} as it is from the bot")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
continue
|
107 |
|
108 |
+
if mention.in_reply_to_user_id == bot_user_id:
|
109 |
+
# don't respond to our own tweets
|
110 |
+
logger.info(f"Skipping {mention.id} as the tweet to roast is from the bot")
|
111 |
+
continue
|
112 |
+
|
113 |
+
if not mention.referenced_tweets:
|
114 |
+
logger.info(f"Skipping {mention.id} as it is not a reply")
|
115 |
+
continue
|
116 |
+
|
117 |
+
# if we've already responded to this conversation, skip it
|
118 |
+
# also should catch the case where we've already responded to this tweet (though that shouldn't happen)
|
119 |
+
if mention.conversation_id in all_convo_ids:
|
120 |
+
logger.info(f"Skipping {mention.id} as we've already responded to this conversation")
|
121 |
+
continue
|
122 |
+
|
123 |
+
logger.info(f"Responding to {mention.id}, which said {mention.text}")
|
124 |
+
|
125 |
+
tweet_to_roast_id = mention.referenced_tweets[0].id
|
126 |
+
tweet_to_roast = client.get_tweet(tweet_to_roast_id)
|
127 |
+
text_to_roast = tweet_to_roast.data.text
|
128 |
+
|
129 |
+
mention_text = mention.text
|
130 |
+
mention_text = pattern_at_mention.sub("", mention_text)
|
131 |
+
logger.info(f"Mention Text: {mention_text}")
|
132 |
+
|
133 |
+
if "roast" in mention_text.lower():
|
134 |
+
logger.info(f"Roasting {mention.id}")
|
135 |
+
text_out = roast(text_to_roast)
|
136 |
+
elif "toast" in mention_text.lower():
|
137 |
+
logger.info(f"Toasting {mention.id}")
|
138 |
+
text_out = toast(text_to_roast)
|
139 |
+
else:
|
140 |
+
logger.info(f"Skipping {mention.id} as it is not a roast or toast")
|
141 |
continue
|
142 |
|
143 |
+
# Quote tweet the tweet to roast
|
144 |
+
logger.info(f"Quote tweeting {tweet_to_roast_id} with response: {text_out}")
|
145 |
+
quote_tweet_response = client.create_tweet(
|
146 |
+
text=text_out,
|
147 |
+
quote_tweet_id=tweet_to_roast_id,
|
148 |
+
)
|
149 |
+
print("QUOTE TWEET RESPONSE", quote_tweet_response.data)
|
150 |
+
response_quote_tweet_id = quote_tweet_response.data.get("id")
|
151 |
+
logger.info(f"Response Quote Tweet ID: {response_quote_tweet_id}")
|
152 |
+
response_quote_tweet_url = f"https://twitter.com/{bot_user_name}/status/{response_quote_tweet_id}"
|
153 |
+
logger.info(f"Response Quote Tweet URL: {response_quote_tweet_url}")
|
154 |
+
|
155 |
+
# reply to the mention with the link to the response tweet
|
156 |
+
logger.info(f"Responding to: {mention.id}")
|
157 |
+
response_reply = client.create_tweet(
|
158 |
+
text=f"Here's my response: {response_quote_tweet_url}",
|
159 |
+
in_reply_to_tweet_id=mention.id,
|
160 |
+
)
|
161 |
+
response_reply_id = response_reply.data.get("id")
|
162 |
+
logger.info(f"Response Reply ID: {response_reply_id}")
|
163 |
+
|
164 |
+
# add the mention to the history
|
165 |
+
data_to_add["id"].append(mention.id)
|
166 |
+
data_to_add["conversation_id"].append(mention.conversation_id)
|
167 |
+
|
168 |
+
# add a line break to the log
|
169 |
+
logger.info("-" * 100)
|
170 |
+
|
171 |
+
# update the history df and upload it to the persistent storage repo
|
172 |
+
if len(data_to_add["id"]) == 0:
|
173 |
+
logger.info("No new mentions to add to the history")
|
174 |
+
return
|
175 |
+
|
176 |
+
logger.info(f"Adding {len(data_to_add['id'])} new mentions to the history")
|
177 |
+
|
178 |
+
df_to_add = pd.DataFrame(data_to_add)
|
179 |
+
df = pd.concat([df, df_to_add], ignore_index=True)
|
180 |
+
df.to_csv(persistent_storage_file_name, index=False)
|
181 |
+
upload_file(
|
182 |
+
repo_id=persistent_storage_repo_id,
|
183 |
+
path_or_fileobj=persistent_storage_file_name,
|
184 |
+
path_in_repo=persistent_storage_file_name,
|
185 |
+
repo_type="dataset",
|
186 |
+
token=hf_token,
|
187 |
+
)
|
188 |
|
189 |
|
190 |
def main():
|
191 |
+
logger.info("Starting up...")
|
192 |
+
|
193 |
while True:
|
194 |
try:
|
195 |
+
# Dummy request to keep the Hugging Face Space awake
|
196 |
+
requests.get("https://team6-roast.hf.space/")
|
197 |
reply_to_mentions()
|
198 |
except Exception as e:
|
199 |
logger.error(e)
|
|
|
|
|
|
|
200 |
traceback.print_exc()
|
201 |
|
202 |
+
logger.info("Sleeping for 30 seconds...")
|
203 |
+
time.sleep(30)
|
204 |
|
205 |
|
206 |
+
if __name__ == "__main__":
|
207 |
+
main()
|
requirements.txt
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
-
fastapi==0.74.*
|
2 |
-
requests==2.27.*
|
3 |
-
sentencepiece==0.1.*
|
4 |
-
uvicorn[standard]==0.17.*
|
5 |
openai
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
openai
|
2 |
+
pandas
|
3 |
+
requests
|
4 |
+
tweepy
|
5 |
+
huggingface_hub
|