2023-07-09 23:00:43 +00:00
|
|
|
import os
|
2023-07-17 01:42:20 +00:00
|
|
|
import random
|
2023-07-09 23:00:43 +00:00
|
|
|
|
2023-07-17 01:42:20 +00:00
|
|
|
import requests
|
2023-07-09 23:00:43 +00:00
|
|
|
from rich import traceback
|
|
|
|
|
|
|
|
import model
|
|
|
|
import utils
|
|
|
|
from client import DramaClient
|
2023-07-10 21:13:47 +00:00
|
|
|
from data import config, db
|
2023-07-09 23:00:43 +00:00
|
|
|
|
|
|
|
traceback.install()
|
|
|
|
|
|
|
|
|
|
|
|
class Bot:
|
|
|
|
def __init__(self):
|
|
|
|
print("Loading model...")
|
|
|
|
self.model = model.Model()
|
|
|
|
self.client = DramaClient()
|
|
|
|
print("Ready.")
|
|
|
|
|
|
|
|
def post_random_reply(self):
|
|
|
|
print("Looking for comments...")
|
|
|
|
|
|
|
|
comments = [
|
|
|
|
c
|
2023-07-10 21:13:47 +00:00
|
|
|
for c in self.client.fetch_new_comments()
|
2023-07-09 23:00:43 +00:00
|
|
|
if "author_name" in c
|
2023-07-10 00:21:01 +00:00
|
|
|
and not c["is_bot"]
|
|
|
|
and c["author_name"] != config["username"]
|
2023-07-09 23:00:43 +00:00
|
|
|
and c["post_id"] != 0
|
|
|
|
]
|
|
|
|
|
|
|
|
if len(comments) == 0:
|
|
|
|
print("No comments found.")
|
|
|
|
return
|
|
|
|
|
|
|
|
random.shuffle(comments)
|
|
|
|
comments = comments[: config["num_replies"]]
|
|
|
|
for comment in comments:
|
|
|
|
self.reply(comment)
|
|
|
|
|
|
|
|
def respond_to_replies(self):
|
|
|
|
replies = self.client.fetch_new_replies()
|
|
|
|
|
|
|
|
for reply in replies:
|
|
|
|
if "author_name" not in reply:
|
|
|
|
continue
|
|
|
|
|
2023-07-10 00:21:01 +00:00
|
|
|
if not reply["is_bot"]:
|
2023-07-09 23:00:43 +00:00
|
|
|
self.reply(reply)
|
|
|
|
|
|
|
|
def make_forced_replies(self):
|
|
|
|
file_path = f"{config['data_dir']}/forced.txt"
|
|
|
|
if not os.path.isfile(file_path):
|
|
|
|
return
|
|
|
|
|
|
|
|
with open(file_path, "r") as f:
|
|
|
|
lines = f.read().splitlines()
|
|
|
|
|
|
|
|
for comment_id in lines:
|
|
|
|
comment = self.client.get(f"/comment/{comment_id}")
|
|
|
|
self.reply(comment)
|
|
|
|
|
|
|
|
os.remove(file_path)
|
|
|
|
|
|
|
|
def reply(self, comment):
|
|
|
|
print(f"Generating reply for https://rdrama.net/comment/{comment['id']}")
|
|
|
|
|
|
|
|
post, thread_comments = self.client.fetch_context(comment)
|
|
|
|
|
|
|
|
if not post or not thread_comments:
|
|
|
|
print("Could not fetch context!")
|
2023-07-10 21:13:47 +00:00
|
|
|
return
|
2023-07-09 23:00:43 +00:00
|
|
|
|
|
|
|
prompt = utils.build_prompt(post, thread_comments)
|
|
|
|
|
|
|
|
candidates = []
|
2023-07-10 21:13:47 +00:00
|
|
|
rejects = []
|
|
|
|
while len(candidates) < config["num_candidates"]:
|
2023-07-09 23:00:43 +00:00
|
|
|
gen_text = self.model.generate(prompt)
|
|
|
|
reply = utils.extract_reply(gen_text)
|
|
|
|
print(f"Generated text: {gen_text}\nReply:\n{reply}")
|
|
|
|
reply = utils.format_reply(reply)
|
|
|
|
|
|
|
|
if len(reply) == 0:
|
|
|
|
print("Retrying: reply empty after processing.")
|
2023-07-10 21:13:47 +00:00
|
|
|
rejects.append(reply)
|
2023-07-09 23:00:43 +00:00
|
|
|
elif utils.is_low_quality(reply, post, thread_comments):
|
|
|
|
print("Retrying: low quality reply.")
|
2023-07-17 01:42:20 +00:00
|
|
|
rejects.append(reply)
|
2023-07-09 23:00:43 +00:00
|
|
|
else:
|
|
|
|
candidates.append(reply)
|
|
|
|
print("Accepting reply.")
|
|
|
|
|
2023-07-17 01:42:20 +00:00
|
|
|
if any(not utils.contains_url(c) for c in candidates):
|
|
|
|
for candidate in candidates:
|
|
|
|
if utils.contains_url(candidate):
|
|
|
|
rejects.append(candidate)
|
|
|
|
|
|
|
|
candidates = [c for c in candidates if not utils.contains_url(c)]
|
2023-07-10 21:13:47 +00:00
|
|
|
reply = utils.median_by_key(candidates, key=utils.reply_length)
|
|
|
|
|
2023-07-17 01:42:20 +00:00
|
|
|
json = self.client.reply(reply, comment)
|
|
|
|
|
|
|
|
if "id" not in json:
|
|
|
|
print("Error posting reply", json)
|
|
|
|
else:
|
|
|
|
data = {
|
|
|
|
"key": config["web_key"],
|
|
|
|
"comment_id": json["id"],
|
2023-07-10 21:13:47 +00:00
|
|
|
"prompt": prompt,
|
2023-07-17 01:42:20 +00:00
|
|
|
"parent_comment": comment["body"],
|
|
|
|
"candidates": candidates + rejects,
|
2023-07-10 21:13:47 +00:00
|
|
|
"selected": reply,
|
|
|
|
}
|
2023-07-17 01:42:20 +00:00
|
|
|
requests.post("https://rdra.ma/submit", json=data)
|
2023-07-09 23:00:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
bot = Bot()
|
|
|
|
bot.make_forced_replies()
|
|
|
|
bot.respond_to_replies()
|
|
|
|
bot.post_random_reply()
|
|
|
|
bot.respond_to_replies()
|